From 8b8a600a817ca18d0f57bbec5f72ed1be3e56bd4 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Fri, 13 Feb 2015 07:39:13 +0000 Subject: [PATCH] add dial and listen methods to the core for super awesomeness --- core/net.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 core/net.go diff --git a/core/net.go b/core/net.go new file mode 100644 index 000000000..49ec47c6c --- /dev/null +++ b/core/net.go @@ -0,0 +1,56 @@ +package core + +import ( + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" + net "github.com/jbenet/go-ipfs/p2p/net" + peer "github.com/jbenet/go-ipfs/p2p/peer" + pro "github.com/jbenet/go-ipfs/p2p/protocol" +) + +type ipfsListener struct { + nd *IpfsNode + conCh chan net.Stream + proto pro.ID + ctx context.Context + cancel func() +} + +func (il *ipfsListener) Accept() (net.Stream, error) { + select { + case c := <-il.conCh: + return c, nil + case <-il.ctx.Done(): + return nil, il.ctx.Err() + } +} + +func (il *ipfsListener) Close() error { + il.cancel() + // TODO: unregister handler from peerhost + return nil +} + +func (nd *IpfsNode) Listen(protocol string) (*ipfsListener, error) { + ctx, cancel := context.WithCancel(nd.Context()) + + list := &ipfsListener{ + proto: pro.ID(protocol), + conCh: make(chan net.Stream), + ctx: ctx, + cancel: cancel, + } + + nd.PeerHost.SetStreamHandler(list.proto, func(s net.Stream) { + select { + case list.conCh <- s: + case <-ctx.Done(): + s.Close() + } + }) + + return list, nil +} + +func (nd *IpfsNode) Dial(protocol string, p peer.ID) (net.Stream, error) { + return nd.PeerHost.NewStream(pro.ID(protocol), p) +}