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

3
import (
Łukasz Magiera's avatar
Łukasz Magiera committed
4
	logging "gx/ipfs/QmcVVHfdyv15GVPk7NrxdWjh2hLVccXnoD8j2tyQShiXJb/go-log"
5 6 7
	pstore "gx/ipfs/QmZR2XWVVBCtbgBWnQhWk2xcQfaR3W8faQPriAiaaj7rsr/go-libp2p-peerstore"
	p2phost "gx/ipfs/Qmb8T6YBBsjYsVGfrihQLfCJveczZnneSBqBKkYEBWDjge/go-libp2p-host"
	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
		},
32
		Streams: &StreamRegistry{
33 34 35 36 37
			Streams: map[uint64]*Stream{},
		},
	}
}

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

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