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

import (
4 5
	"io"

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

// 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 {
21
	case <-dht.Process().Closing():
22
		return
23
	default:
24
	}
25 26 27 28 29 30 31 32 33 34 35 36 37

	// Note: We *could* just check the peerstore to see if the remote side supports the dht
	// protocol, but its not clear that that information will make it into the peerstore
	// by the time this notification is sent. So just to be very careful, we brute force this
	// and open a new stream
	s, err := dht.host.NewStream(dht.Context(), v.RemotePeer(), ProtocolDHT, ProtocolDHTOld)
	switch err {
	case nil:
		s.Close()
		// connected fine? full dht node
		dht.Update(dht.Context(), v.RemotePeer())
	case mstream.ErrNotSupported:
		// Client mode only, don't bother adding them to our routing table
38 39 40
	case io.EOF:
		// This is kindof an error, but it happens someone often so make it a warning
		log.Warningf("checking dht client type: %s", err)
41 42
	default:
		// real error? thats odd
43
		log.Errorf("checking dht client type: %s", err)
44
	}
45 46 47 48 49
}

func (nn *netNotifiee) Disconnected(n inet.Network, v inet.Conn) {
	dht := nn.DHT()
	select {
50
	case <-dht.Process().Closing():
51
		return
52
	default:
53 54 55 56 57 58
	}
	dht.routingTable.Remove(v.RemotePeer())
}

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