notif.go 2.6 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
)

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

Steven Allen's avatar
Steven Allen committed
12 13
var dhtProtocols = []string{string(ProtocolDHT), string(ProtocolDHTOld)}

14 15 16 17 18 19 20
func (nn *netNotifiee) DHT() *IpfsDHT {
	return (*IpfsDHT)(nn)
}

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

Steven Allen's avatar
Steven Allen committed
26 27 28 29 30 31 32 33
	p := v.RemotePeer()
	protos, err := dht.peerstore.SupportsProtocols(p, dhtProtocols...)
	if err == nil && len(protos) != 0 {
		dht.plk.Lock()
		defer dht.plk.Unlock()
		if dht.host.Network().Connectedness(p) == inet.Connected {
			dht.Update(dht.Context(), p)
		}
34 35 36
		return
	}

Steven Allen's avatar
Steven Allen committed
37 38 39 40
	// Note: Unfortunately, the peerstore may not yet now that this peer is
	// 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
41 42
}

Steven Allen's avatar
Steven Allen committed
43
func (nn *netNotifiee) testConnection(v inet.Conn) {
Steven Allen's avatar
Steven Allen committed
44
	dht := nn.DHT()
Steven Allen's avatar
Steven Allen committed
45 46 47 48 49 50 51 52
	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
53 54
		return
	}
Steven Allen's avatar
Steven Allen committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	defer s.Close()

	selected, err := mstream.SelectOneOf(dhtProtocols, s)
	if err != nil {
		// Doesn't support the protocol
		return
	}
	// Remember this choice (makes subsequent negotiations faster)
	dht.peerstore.AddProtocols(p, selected)

	dht.plk.Lock()
	defer dht.plk.Unlock()
	// Make sure we're still connected under the lock (race with disconnect)
	if dht.host.Network().Connectedness(p) == inet.Connected {
		dht.Update(dht.Context(), p)
	}
71 72 73 74 75
}

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

81
	p := v.RemotePeer()
82

83 84 85 86
	func() {
		dht.plk.Lock()
		defer dht.plk.Unlock()

Steven Allen's avatar
Steven Allen committed
87
		if dht.host.Network().Connectedness(p) != inet.Connected {
88 89 90 91 92 93 94
			dht.routingTable.Remove(p)
		}
	}()

	dht.smlk.Lock()
	defer dht.smlk.Unlock()
	ms, ok := dht.strmap[p]
95 96 97
	if !ok {
		return
	}
98 99 100 101 102 103 104 105
	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()
	}()
106 107 108 109
}

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
110 111
func (nn *netNotifiee) Listen(n inet.Network, a ma.Multiaddr)      {}
func (nn *netNotifiee) ListenClose(n inet.Network, a ma.Multiaddr) {}