notif.go 3 KB
Newer Older
1 2 3
package dht

import (
4 5
	inet "github.com/libp2p/go-libp2p-net"
	ma "github.com/multiformats/go-multiaddr"
Jeromy's avatar
Jeromy committed
6
	mstream "github.com/multiformats/go-multistream"
7 8 9 10 11 12 13 14 15 16 17 18
)

// netNotifiee defines methods to be used with the IpfsDHT
type netNotifiee IpfsDHT

func (nn *netNotifiee) DHT() *IpfsDHT {
	return (*IpfsDHT)(nn)
}

func (nn *netNotifiee) Connected(n inet.Network, v inet.Conn) {
	dht := nn.DHT()
	select {
19
	case <-dht.Process().Closing():
20
		return
21
	default:
22
	}
23

Steven Allen's avatar
Steven Allen committed
24
	p := v.RemotePeer()
25
	protos, err := dht.peerstore.SupportsProtocols(p, dht.protocolStrs()...)
Steven Allen's avatar
Steven Allen committed
26
	if err == nil && len(protos) != 0 {
27 28 29
		// We lock here for consistency with the lock in testConnection.
		// This probably isn't necessary because (dis)connect
		// notifications are serialized but it's nice to be consistent.
Steven Allen's avatar
Steven Allen committed
30 31 32 33 34
		dht.plk.Lock()
		defer dht.plk.Unlock()
		if dht.host.Network().Connectedness(p) == inet.Connected {
			dht.Update(dht.Context(), p)
		}
35 36 37
		return
	}

38
	// Note: Unfortunately, the peerstore may not yet know that this peer is
Steven Allen's avatar
Steven Allen committed
39 40 41
	// a DHT server. So, if it didn't return a positive response above, test
	// manually.
	go nn.testConnection(v)
Steven Allen's avatar
Steven Allen committed
42 43
}

Steven Allen's avatar
Steven Allen committed
44
func (nn *netNotifiee) testConnection(v inet.Conn) {
Steven Allen's avatar
Steven Allen committed
45
	dht := nn.DHT()
Steven Allen's avatar
Steven Allen committed
46 47 48 49 50 51 52 53
	p := v.RemotePeer()

	// Forcibly use *this* connection. Otherwise, if we have two connections, we could:
	// 1. Test it twice.
	// 2. Have it closed from under us leaving the second (open) connection untested.
	s, err := v.NewStream()
	if err != nil {
		// Connection error
Steven Allen's avatar
Steven Allen committed
54 55
		return
	}
Steven Allen's avatar
Steven Allen committed
56 57
	defer s.Close()

58
	selected, err := mstream.SelectOneOf(dht.protocolStrs(), s)
Steven Allen's avatar
Steven Allen committed
59 60 61 62 63 64 65
	if err != nil {
		// Doesn't support the protocol
		return
	}
	// Remember this choice (makes subsequent negotiations faster)
	dht.peerstore.AddProtocols(p, selected)

66 67 68
	// We lock here as we race with disconnect. If we didn't lock, we could
	// finish processing a connect after handling the associated disconnect
	// event and add the peer to the routing table after removing it.
Steven Allen's avatar
Steven Allen committed
69 70 71 72 73
	dht.plk.Lock()
	defer dht.plk.Unlock()
	if dht.host.Network().Connectedness(p) == inet.Connected {
		dht.Update(dht.Context(), p)
	}
74 75 76 77 78
}

func (nn *netNotifiee) Disconnected(n inet.Network, v inet.Conn) {
	dht := nn.DHT()
	select {
79
	case <-dht.Process().Closing():
80
		return
81
	default:
82
	}
83

84
	p := v.RemotePeer()
85

86 87
	// Lock and check to see if we're still connected. We lock to make sure
	// we don't concurrently process a connect event.
88 89 90 91 92 93
	dht.plk.Lock()
	defer dht.plk.Unlock()
	if dht.host.Network().Connectedness(p) == inet.Connected {
		// We're still connected.
		return
	}
94

95
	dht.routingTable.Remove(p)
96 97 98 99

	dht.smlk.Lock()
	defer dht.smlk.Unlock()
	ms, ok := dht.strmap[p]
100 101 102
	if !ok {
		return
	}
103 104 105 106 107 108 109 110
	delete(dht.strmap, p)

	// Do this asynchronously as ms.lk can block for a while.
	go func() {
		ms.lk.Lock()
		defer ms.lk.Unlock()
		ms.invalidate()
	}()
111 112 113 114
}

func (nn *netNotifiee) OpenedStream(n inet.Network, v inet.Stream) {}
func (nn *netNotifiee) ClosedStream(n inet.Network, v inet.Stream) {}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
115 116
func (nn *netNotifiee) Listen(n inet.Network, a ma.Multiaddr)      {}
func (nn *netNotifiee) ListenClose(n inet.Network, a ma.Multiaddr) {}