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

3
import (
Łukasz Magiera's avatar
Łukasz Magiera committed
4 5 6 7
	peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
	logging "gx/ipfs/QmRREK2CAZ5Re2Bd9zZFG6FeYDppUWt5cMgsoUEp3ktgSr/go-log"
	pstore "gx/ipfs/QmeKD8YT7887Xu6Z86iZmpYNxrLogJexqxEugSmaf14k64/go-libp2p-peerstore"
	p2phost "gx/ipfs/QmfH9FKYv3Jp1xiyL8sPchGBUBg6JA6XviwajAo3qgnT3B/go-libp2p-host"
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 {
Łukasz Magiera's avatar
Łukasz Magiera committed
14 15 16
	ListenersLocal *ListenersLocal
	ListenersP2P   *ListenersP2P
	Streams        *StreamRegistry
17 18 19 20

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

23 24 25 26 27 28 29
// 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,

Łukasz Magiera's avatar
Łukasz Magiera committed
30 31 32
		ListenersLocal: newListenerRegistry(identity, peerHost),
		ListenersP2P:   newListenerP2PRegistry(identity, peerHost),

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
}