p2p.go 1.18 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1
package p2p
2

3
import (
tavit ohanian's avatar
tavit ohanian committed
4 5 6 7
	logging "gitlab.dms3.io/dms3/go-log"
	p2phost "gitlab.dms3.io/p2p/go-p2p-core/host"
	peer "gitlab.dms3.io/p2p/go-p2p-core/peer"
	pstore "gitlab.dms3.io/p2p/go-p2p-core/peerstore"
8 9
)

Łukasz Magiera's avatar
Łukasz Magiera committed
10 11
var log = logging.Logger("p2p-mount")

12
// P2P structure holds information on currently running streams/Listeners
Łukasz Magiera's avatar
Łukasz Magiera committed
13
type P2P struct {
14 15
	ListenersLocal *Listeners
	ListenersP2P   *Listeners
Łukasz Magiera's avatar
Łukasz Magiera committed
16
	Streams        *StreamRegistry
17 18 19 20

	identity  peer.ID
	peerHost  p2phost.Host
	peerstore pstore.Peerstore
21 22
}

23 24
// New creates new P2P struct
func New(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore) *P2P {
25 26 27 28 29
	return &P2P{
		identity:  identity,
		peerHost:  peerHost,
		peerstore: peerstore,

30 31
		ListenersLocal: newListenersLocal(),
		ListenersP2P:   newListenersP2P(peerHost),
Łukasz Magiera's avatar
Łukasz Magiera committed
32

33
		Streams: &StreamRegistry{
Łukasz Magiera's avatar
Łukasz Magiera committed
34 35 36
			Streams:     map[uint64]*Stream{},
			ConnManager: peerHost.ConnManager(),
			conns:       map[peer.ID]int{},
37 38 39 40
		},
	}
}

41
// CheckProtoExists checks whether a proto handler is registered to
42
// mux handler
Łukasz Magiera's avatar
Łukasz Magiera committed
43 44
func (p2p *P2P) CheckProtoExists(proto string) bool {
	protos := p2p.peerHost.Mux().Protocols()
45 46 47 48 49 50 51 52

	for _, p := range protos {
		if p != proto {
			continue
		}
		return true
	}
	return false
53
}