@cryptotaxi247 / kubo / commits / bba2d05ca

p2p: cleanup after listener iface split

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Sep 5, 2018 at 23:13 UTC bba2d05ca951357d9c34bf225419ff15515c0563
4 files changed +9 -38
p2p/local_listener.go
+1 -31
@@ -4,11 +4,9 @@ import (
4 "errors"
5 "sync"
6
7 - net "gx/ipfs/QmQSbtGXCyNrj34LWL8EgXyNNYDZ8r3SwQcpW5pPxVhLnM/go-libp2p-net"
7 peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
8 ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
9 "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
11 - p2phost "gx/ipfs/QmfH9FKYv3Jp1xiyL8sPchGBUBg6JA6XviwajAo3qgnT3B/go-libp2p-host"
10 )
11
12 // ListenerLocal listens for connections and proxies them to a target
@@ -31,40 +29,12 @@ type ListenersLocal struct {
29 starting map[string]struct{}
30 }
31
34 -func newListenerRegistry(id peer.ID, host p2phost.Host) *ListenersLocal {
32 +func newListenerRegistry(id peer.ID) *ListenersLocal {
33 reg := &ListenersLocal{
34 Listeners: map[string]ListenerLocal{},
35 starting: map[string]struct{}{},
36 }
37
40 - addr, err := ma.NewMultiaddr(maPrefix + id.Pretty())
41 - if err != nil {
42 - panic(err)
43 - }
44 -
45 - host.SetStreamHandlerMatch("/x/", func(p string) bool {
46 - reg.RLock()
47 - defer reg.RUnlock()
48 -
49 - for _, l := range reg.Listeners {
50 - if l.ListenAddress().Equal(addr) && string(l.Protocol()) == p {
51 - return true
52 - }
53 - }
54 -
55 - return false
56 - }, func(stream net.Stream) {
57 - reg.RLock()
58 - defer reg.RUnlock()
59 -
60 - for _, l := range reg.Listeners {
61 - if l.ListenAddress().Equal(addr) && l.Protocol() == stream.Protocol() {
62 - go l.(*remoteListener).handleStream(stream)
63 - return
64 - }
65 - }
66 - })
67 -
38 return reg
39 }
40
p2p/p2p.go
+1 -1
@@ -27,7 +27,7 @@ func NewP2P(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore)
27 peerHost: peerHost,
28 peerstore: peerstore,
29
30 - ListenersLocal: newListenerRegistry(identity, peerHost),
30 + ListenersLocal: newListenerRegistry(identity),
31 ListenersP2P: newListenerP2PRegistry(identity, peerHost),
32
33 Streams: &StreamRegistry{
p2p/p2p_listener.go
+6 -5
@@ -12,12 +12,13 @@ import (
12 )
13
14 // Listener listens for connections and proxies them to a target
15 -type P2PListener interface {
15 +type ListenerP2P interface {
16 Protocol() protocol.ID
17 ListenAddress() ma.Multiaddr
18 TargetAddress() ma.Multiaddr
19
20 start() error
21 + handleStream(remote net.Stream)
22
23 // Close closes the listener. Does not affect child streams
24 Close() error
@@ -27,13 +28,13 @@ type P2PListener interface {
28 type ListenersP2P struct {
29 sync.RWMutex
30
30 - Listeners map[protocol.ID]ListenerLocal
31 + Listeners map[protocol.ID]ListenerP2P
32 starting map[protocol.ID]struct{}
33 }
34
35 func newListenerP2PRegistry(id peer.ID, host p2phost.Host) *ListenersP2P {
36 reg := &ListenersP2P{
36 - Listeners: map[protocol.ID]ListenerLocal{},
37 + Listeners: map[protocol.ID]ListenerP2P{},
38 starting: map[protocol.ID]struct{}{},
39 }
40
@@ -59,7 +60,7 @@ func newListenerP2PRegistry(id peer.ID, host p2phost.Host) *ListenersP2P {
60
61 for _, l := range reg.Listeners {
62 if l.ListenAddress().Equal(addr) && l.Protocol() == stream.Protocol() {
62 - go l.(*remoteListener).handleStream(stream)
63 + go l.handleStream(stream)
64 return
65 }
66 }
@@ -69,7 +70,7 @@ func newListenerP2PRegistry(id peer.ID, host p2phost.Host) *ListenersP2P {
70 }
71
72 // Register registers listenerInfo into this registry and starts it
72 -func (r *ListenersP2P) Register(l ListenerLocal) error {
73 +func (r *ListenersP2P) Register(l ListenerP2P) error {
74 r.Lock()
75
76 k := l.Protocol()
p2p/remote.go
+1 -1
@@ -23,7 +23,7 @@ type remoteListener struct {
23 }
24
25 // ForwardRemote creates new p2p listener
26 -func (p2p *P2P) ForwardRemote(ctx context.Context, proto protocol.ID, addr ma.Multiaddr) (P2PListener, error) {
26 +func (p2p *P2P) ForwardRemote(ctx context.Context, proto protocol.ID, addr ma.Multiaddr) (ListenerP2P, error) {
27 listener := &remoteListener{
28 p2p: p2p,
29