2017-08-11 00:25:42 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2018-01-24 17:45:18 +01:00
|
|
|
|
|
|
|
"github.com/lbryio/errors.go"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-11 00:25:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2017-08-15 22:02:18 +02:00
|
|
|
conn net.Conn
|
|
|
|
connected bool
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Connect(address string) error {
|
|
|
|
var err error
|
|
|
|
c.conn, err = net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-15 22:02:18 +02:00
|
|
|
c.connected = true
|
2017-08-11 00:25:42 +02:00
|
|
|
return c.doHandshake(protocolVersion1)
|
|
|
|
}
|
|
|
|
func (c *Client) Close() error {
|
2017-08-15 22:02:18 +02:00
|
|
|
c.connected = false
|
2017-08-11 00:25:42 +02:00
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SendBlob(blob []byte) error {
|
2017-08-15 22:02:18 +02:00
|
|
|
if !c.connected {
|
2018-01-24 17:45:18 +01:00
|
|
|
return errors.Err("not connected")
|
2017-08-15 22:02:18 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 00:25:42 +02:00
|
|
|
if len(blob) != BlobSize {
|
2018-01-24 17:45:18 +01:00
|
|
|
return errors.Err("blob must be exactly " + strconv.Itoa(BlobSize) + " bytes")
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
blobHash := getBlobHash(blob)
|
|
|
|
sendRequest, err := json.Marshal(sendBlobRequest{
|
|
|
|
BlobSize: len(blob),
|
|
|
|
BlobHash: blobHash,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = c.conn.Write(sendRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dec := json.NewDecoder(c.conn)
|
|
|
|
|
|
|
|
var sendResp sendBlobResponse
|
|
|
|
err = dec.Decode(&sendResp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sendResp.SendBlob {
|
|
|
|
return ErrBlobExists
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Sending blob " + blobHash[:8])
|
|
|
|
|
|
|
|
_, err = c.conn.Write(blob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var transferResp blobTransferResponse
|
|
|
|
err = dec.Decode(&transferResp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !transferResp.ReceivedBlob {
|
2018-01-24 17:45:18 +01:00
|
|
|
return errors.Err("server did not received blob")
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) doHandshake(version int) error {
|
2017-08-15 22:02:18 +02:00
|
|
|
if !c.connected {
|
2018-01-24 17:45:18 +01:00
|
|
|
return errors.Err("not connected")
|
2017-08-15 22:02:18 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 00:25:42 +02:00
|
|
|
handshake, err := json.Marshal(handshakeRequestResponse{Version: version})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.conn.Write(handshake)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp handshakeRequestResponse
|
|
|
|
dec := json.NewDecoder(c.conn)
|
|
|
|
err = dec.Decode(&resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if resp.Version != version {
|
2018-01-24 17:45:18 +01:00
|
|
|
return errors.Err("handshake version mismatch")
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|