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

3
import (
4 5
	pstore "gx/ipfs/QmZR2XWVVBCtbgBWnQhWk2xcQfaR3W8faQPriAiaaj7rsr/go-libp2p-peerstore"
	p2phost "gx/ipfs/Qmb8T6YBBsjYsVGfrihQLfCJveczZnneSBqBKkYEBWDjge/go-libp2p-host"
6
	logging "gx/ipfs/QmcVVHfdyv15GVPk7NrxdWjh2hLVccXnoD8j2tyQShiXJb/go-log"
7
	peer "gx/ipfs/QmdVrMn1LhB4ybb8hMVaMLXnA8XRSewMnK6YqXKXoTcRvN/go-libp2p-peer"
8 9
)

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

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

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

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

29 30
		Listeners: &ListenerRegistry{
			Listeners: map[listenerKey]Listener{},
31
			starting:  map[listenerKey]struct{}{},
32
		},
33
		Streams: &StreamRegistry{
34 35 36 37 38
			Streams: map[uint64]*Stream{},
		},
	}
}

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

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