go-libp2p-daemon/p2pclient/p2pclient.go

132 lines
3.2 KiB
Go
Raw Normal View History

2018-09-27 17:38:24 +00:00
package p2pclient
import (
"errors"
"sync"
ggio "github.com/gogo/protobuf/io"
logging "github.com/ipfs/go-log"
pb "github.com/libp2p/go-libp2p-daemon/pb"
2018-12-11 19:35:02 +00:00
peer "github.com/libp2p/go-libp2p-peer"
multiaddr "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
2018-09-27 17:38:24 +00:00
)
var log = logging.Logger("p2pclient")
// MessageSizeMax is cribbed from github.com/libp2p/go-libp2p-net
const MessageSizeMax = 1 << 22 // 4 MB
// Client is the struct that manages a connection to a libp2p daemon.
type Client struct {
controlMaddr multiaddr.Multiaddr
listenMaddr multiaddr.Multiaddr
listener manet.Listener
2018-09-27 17:38:24 +00:00
mhandlers sync.Mutex
handlers map[string]StreamHandlerFunc
2018-09-27 17:38:24 +00:00
}
// NewClient creates a new libp2p daemon client, connecting to a daemon
// listening on a multi-addr at controlMaddr, and establishing an inbound
// listening multi-address at listenMaddr
func NewClient(controlMaddr, listenMaddr multiaddr.Multiaddr) (*Client, error) {
client := &Client{
controlMaddr: controlMaddr,
listenMaddr: listenMaddr,
handlers: make(map[string]StreamHandlerFunc),
2018-09-27 17:38:24 +00:00
}
2018-09-27 17:45:20 +00:00
if err := client.listen(); err != nil {
return nil, err
2018-09-27 17:45:20 +00:00
}
return client, nil
}
2018-09-27 17:45:20 +00:00
func (c *Client) newControlConn() (manet.Conn, error) {
return manet.Dial(c.controlMaddr)
2018-09-27 17:45:20 +00:00
}
2018-09-27 17:38:24 +00:00
// Identify queries the daemon for its peer ID and listen addresses.
2018-10-01 20:40:50 +00:00
func (c *Client) Identify() (peer.ID, []multiaddr.Multiaddr, error) {
control, err := c.newControlConn()
if err != nil {
2018-10-01 20:40:50 +00:00
return peer.ID(""), nil, err
}
defer control.Close()
r := ggio.NewDelimitedReader(control, MessageSizeMax)
w := ggio.NewDelimitedWriter(control)
2018-09-27 17:38:24 +00:00
req := &pb.Request{Type: pb.Request_IDENTIFY.Enum()}
2018-10-01 20:40:50 +00:00
if err = w.WriteMsg(req); err != nil {
return peer.ID(""), nil, err
2018-09-27 17:38:24 +00:00
}
res := &pb.Response{}
2018-10-01 20:40:50 +00:00
if err = r.ReadMsg(res); err != nil {
return peer.ID(""), nil, err
2018-09-27 17:38:24 +00:00
}
2018-10-01 20:40:50 +00:00
if reserr := res.GetError(); reserr != nil {
return peer.ID(""), nil, errors.New(reserr.GetMsg())
2018-09-27 17:38:24 +00:00
}
idres := res.GetIdentify()
2018-10-01 20:40:50 +00:00
id, err := peer.IDFromBytes(idres.Id)
if err != nil {
return peer.ID(""), nil, err
}
2018-09-27 17:38:24 +00:00
addrs := make([]multiaddr.Multiaddr, 0, len(idres.Addrs))
for i, addrbytes := range idres.Addrs {
addr, err := multiaddr.NewMultiaddrBytes(addrbytes)
if err != nil {
log.Errorf("failed to parse multiaddr in position %d in response to identify request", i)
continue
}
addrs = append(addrs, addr)
}
2018-10-01 20:40:50 +00:00
return id, addrs, nil
2018-09-27 17:38:24 +00:00
}
// Connect establishes a connection to a peer after populating the Peerstore
// entry for said peer with a list of addresses.
2018-10-01 20:40:50 +00:00
func (c *Client) Connect(p peer.ID, addrs []multiaddr.Multiaddr) error {
control, err := c.newControlConn()
if err != nil {
return err
}
defer control.Close()
r := ggio.NewDelimitedReader(control, MessageSizeMax)
w := ggio.NewDelimitedWriter(control)
2018-09-27 17:38:24 +00:00
addrbytes := make([][]byte, len(addrs))
for i, addr := range addrs {
addrbytes[i] = addr.Bytes()
}
req := &pb.Request{
Type: pb.Request_CONNECT.Enum(),
Connect: &pb.ConnectRequest{
2018-10-01 20:40:50 +00:00
Peer: []byte(p),
2018-09-27 17:38:24 +00:00
Addrs: addrbytes,
},
}
if err := w.WriteMsg(req); err != nil {
return err
}
res := &pb.Response{}
if err := r.ReadMsg(res); err != nil {
return err
}
if err := res.GetError(); err != nil {
return errors.New(err.GetMsg())
}
return nil
}