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

3
import (
4 5
	pstore "gx/ipfs/QmQAGG1zxfePqj2t7bLxyN8AFccZ889DDR9Gn8kVLDrGZo/go-libp2p-peerstore"
	p2phost "gx/ipfs/QmahxMNoNuSsgQefo9rkpcfRFmQrMN6Q99aztKXf63K7YJ/go-libp2p-host"
Steven Allen's avatar
Steven Allen committed
6 7
	peer "gx/ipfs/QmcqU6QUDSXprb1518vYDGczrTJTyGwLG9eUa5iNX4xUtS/go-libp2p-peer"
	logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
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

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

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

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

33
		Streams: &StreamRegistry{
Łukasz Magiera's avatar
Łukasz Magiera committed
34
			Streams:     map[uint64]*Stream{},
35
			ConnManager: peerHost.ConnManager(),
Łukasz Magiera's avatar
Łukasz Magiera committed
36
			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
func (p2p *P2P) CheckProtoExists(proto string) bool {
44
	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
}