Commit 180b8bb8 authored by hannahhoward's avatar hannahhoward

feat(network): add ConnectTo method

Add a ConnectTo method to the network layer to create connections
parent ad6cda91
docs/GraphSync.png

260 KB | W: | H:

docs/GraphSync.png

264 KB | W: | H:

docs/GraphSync.png
docs/GraphSync.png
docs/GraphSync.png
docs/GraphSync.png
  • 2-up
  • Swipe
  • Onion skin
This diff is collapsed.
......@@ -62,6 +62,7 @@ package "go-graphsync" {
interface GraphSyncNetwork {
SendMessage(ctx context.Context, receiver peer.Id, m GraphSyncMessage)
SetDelegate(receiver Receiver)
ConnectTo(ctx context.Context, peer.ID) error
NewMessageSender(context.Context, peer.ID) (MessageSender, error)
}
......
......@@ -27,6 +27,9 @@ type GraphSyncNetwork interface {
// network.
SetDelegate(Receiver)
// ConnectTo establishes a connection to the given peer
ConnectTo(context.Context, peer.ID) error
NewMessageSender(context.Context, peer.ID) (MessageSender, error)
}
......
......@@ -14,6 +14,7 @@ import (
host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
)
var log = logging.Logger("graphsync_network")
......@@ -130,6 +131,10 @@ func (gsnet *libp2pGraphSyncNetwork) SetDelegate(r Receiver) {
gsnet.receiver = r
}
func (gsnet *libp2pGraphSyncNetwork) ConnectTo(ctx context.Context, p peer.ID) error {
return gsnet.host.Connect(ctx, pstore.PeerInfo{ID: p})
}
// handleNewStream receives a new stream from the network.
func (gsnet *libp2pGraphSyncNetwork) handleNewStream(s inet.Stream) {
defer s.Close()
......
......@@ -54,10 +54,6 @@ func TestMessageSendAndReceive(t *testing.T) {
if err != nil {
t.Fatal("error linking hosts")
}
_, err = mn.ConnectPeers(host1.ID(), host2.ID())
if err != nil {
t.Fatal("error linking peers")
}
gsnet1 := NewFromLibp2pHost(host1,
testselector.MockDecodeSelectorFunc,
testselector.MockDecodeSelectionResponseFunc)
......@@ -81,6 +77,11 @@ func TestMessageSendAndReceive(t *testing.T) {
sent.AddRequest(id, selector, root, priority)
sent.AddResponse(id, status, selectionResponse)
err = gsnet1.ConnectTo(ctx, host2.ID())
if err != nil {
t.Fatal("Unable to connect peers")
}
gsnet1.SendMessage(ctx, host2.ID(), sent)
select {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment