1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-28 00:39:31 +08:00

p2p: fix some stuff after refactor

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2018-05-22 14:35:41 +02:00
parent 132d6fae4a
commit cf6ddcbf70
4 changed files with 24 additions and 18 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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,

View File

@ -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 {