diff --git a/p2p/inbound.go b/p2p/inbound.go index ad5497764..03304a373 100644 --- a/p2p/inbound.go +++ b/p2p/inbound.go @@ -16,13 +16,17 @@ type inboundListener struct { // Application proto identifier. proto string + // Address to proxy the incoming connections to addr ma.Multiaddr } // NewListener creates new p2p listener func (p2p *P2P) NewListener(ctx context.Context, proto string, addr ma.Multiaddr) (Listener, error) { listenerInfo := &inboundListener{ + p2p: p2p, + proto: proto, + addr: addr, } p2p.peerHost.SetStreamHandler(protocol.ID(proto), func(remote net.Stream) { diff --git a/p2p/listener.go b/p2p/listener.go index 881771c59..dc1fc3009 100644 --- a/p2p/listener.go +++ b/p2p/listener.go @@ -1,11 +1,5 @@ package p2p -import ( - pstore "gx/ipfs/QmZb7hAgQEhW9dBbzBudU39gCeD4zbe6xafD52LUuF4cUN/go-libp2p-peerstore" - peer "gx/ipfs/QmcJukH2sAFjY3HdBKq35WDzWoL3UUu2gt9wdfqZTUyM74/go-libp2p-peer" - p2phost "gx/ipfs/QmdHyfNVTZ5VtUx4Xz23z8wtnioSrFQ28XSfpVkdhQBkGA/go-libp2p-host" -) - type Listener interface { Protocol() string Address() string @@ -14,15 +8,6 @@ type Listener interface { Close() error } -// NewP2P creates new P2P struct -func NewP2P(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore) *P2P { - return &P2P{ - identity: identity, - peerHost: peerHost, - peerstore: peerstore, - } -} - // ListenerRegistry is a collection of local application proto listeners. type ListenerRegistry struct { Listeners map[string]Listener diff --git a/p2p/outbound.go b/p2p/outbound.go index 1d4da9e53..b0a5541f2 100644 --- a/p2p/outbound.go +++ b/p2p/outbound.go @@ -12,10 +12,9 @@ import ( peer "gx/ipfs/QmcJukH2sAFjY3HdBKq35WDzWoL3UUu2gt9wdfqZTUyM74/go-libp2p-peer" ) -// inboundListener accepts libp2p streams and proxies them to a manet host +// outboundListener accepts libp2p streams and proxies them to a manet host type outboundListener struct { - ctx context.Context - cancel context.CancelFunc + ctx context.Context p2p *P2P id peer.ID @@ -34,6 +33,8 @@ func (p2p *P2P) Dial(ctx context.Context, peer peer.ID, proto string, bindAddr m } listener := &outboundListener{ + ctx: ctx, + p2p: p2p, id: p2p.identity, diff --git a/p2p/p2p.go b/p2p/p2p.go index 72cc3d925..3998dd3b6 100644 --- a/p2p/p2p.go +++ b/p2p/p2p.go @@ -16,6 +16,22 @@ type P2P struct { peerstore pstore.Peerstore } +// NewP2P creates new P2P struct +func NewP2P(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore) *P2P { + return &P2P{ + identity: identity, + peerHost: peerHost, + peerstore: peerstore, + + Listeners: ListenerRegistry{ + Listeners: map[string]Listener{}, + }, + Streams: StreamRegistry{ + Streams: map[uint64]*Stream{}, + }, + } +} + // CheckProtoExists checks whether a proto handler is registered to // mux handler func (p2p *P2P) CheckProtoExists(proto string) bool {