reflector.go/reflector/client.go

121 lines
2.4 KiB
Go
Raw Permalink Normal View History

2018-01-29 20:37:26 +01:00
package reflector
import (
"encoding/json"
"net"
2019-01-09 23:52:30 +01:00
"github.com/lbryio/lbry.go/extras/errors"
2019-01-10 14:02:43 +01:00
"github.com/lbryio/lbry.go/stream"
log "github.com/sirupsen/logrus"
)
// ErrBlobExists is a default error for when a blob already exists on the reflector server.
var ErrBlobExists = errors.Base("blob exists on server")
// Client is an instance of a client connected to a server.
type Client struct {
2017-08-15 22:02:18 +02:00
conn net.Conn
connected bool
}
// Connect connects to a specific clients and errors if it cannot be contacted.
func (c *Client) Connect(address string) error {
var err error
c.conn, err = net.Dial(network, address)
if err != nil {
return err
}
2017-08-15 22:02:18 +02:00
c.connected = true
return c.doHandshake(protocolVersion1)
}
// Close closes the connection with the client.
func (c *Client) Close() error {
2017-08-15 22:02:18 +02:00
c.connected = false
return c.conn.Close()
}
// SendBlob sends a send blob request to the client.
func (c *Client) SendBlob(blob stream.Blob) error {
2017-08-15 22:02:18 +02:00
if !c.connected {
return errors.Err("not connected")
2017-08-15 22:02:18 +02:00
}
if err := blob.ValidForSend(); err != nil {
return errors.Err(err)
}
blobHash := blob.HashHex()
sendRequest, err := json.Marshal(sendBlobRequest{
BlobSize: blob.Size(),
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 {
2019-06-05 17:03:55 +02:00
return errors.Prefix(blobHash[:8], 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 {
return errors.Err("server did not received blob")
}
return nil
}
func (c *Client) doHandshake(version int) error {
2017-08-15 22:02:18 +02:00
if !c.connected {
return errors.Err("not connected")
2017-08-15 22:02:18 +02:00
}
2019-06-05 17:03:55 +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
err = json.NewDecoder(c.conn).Decode(&resp)
if err != nil {
return err
2019-06-05 17:03:55 +02:00
} else if resp.Version == nil {
return errors.Err("invalid handshake")
} else if *resp.Version != version {
return errors.Err("handshake version mismatch")
}
return nil
}