reflector.go/reflector/client.go
Mark Beamer Jr 35f98ce162 code cleanup
-Added travis support
-updated travis to analyze code beneath the root.
-refactored upload.go to fix travis errors.
-gocyclo should ignore test files. $GOFILES needed to be adjusted.
-fix rows.Close() ignoring error. Created func to handle so defer can be used when needed also.
-fixed ignored errors.
-fixed unit test that was not passing correctly to anonymous function.
-fixed govet error for passing param inside go func.
-removed returned error, in favor of logging instead.
-added error logging for ignored error.
-fixed potential race conditions.
-removed unused append
-fixed time usage to align with go standards.
-removed unused variables
-made changes for code review.
-code comments for exported functions.
-Documented bitmap.go and insert into contact list.
-Documented dht, message, bootstrap
-Fixed comment typos
-Documented message,node, routing_table, testing in DHT package.
-Documented server, client, prism, server and shared in peer and reflector packages.
-Documented the stores in Store package.
-made defer adjustments inline and deleted the separate function.
-adjusted method in upload to take the only parameter it requires.
2018-06-13 09:29:13 -04:00

114 lines
2.1 KiB
Go

package reflector
import (
"encoding/json"
"net"
"strconv"
"github.com/lbryio/lbry.go/errors"
log "github.com/sirupsen/logrus"
)
// Client is an instance of a client connected to a server.
type Client struct {
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("tcp", address)
if err != nil {
return err
}
c.connected = true
return c.doHandshake(protocolVersion1)
}
// Close closes the connection with the client.
func (c *Client) Close() error {
c.connected = false
return c.conn.Close()
}
// SendBlob sends a send blob request to the client.
func (c *Client) SendBlob(blob []byte) error {
if !c.connected {
return errors.Err("not connected")
}
if len(blob) != maxBlobSize {
return errors.Err("blob must be exactly " + strconv.Itoa(maxBlobSize) + " bytes")
}
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 {
return errors.Err("server did not received blob")
}
return nil
}
func (c *Client) doHandshake(version int) error {
if !c.connected {
return errors.Err("not connected")
}
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 {
return errors.Err("handshake version mismatch")
}
return nil
}