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

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

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

	identity  peer.ID
	peerHost  p2phost.Host
	peerstore pstore.Peerstore
17 18
}

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// 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,

		Listeners: ListenerRegistry{
			Listeners: map[string]Listener{},
		},
		Streams: StreamRegistry{
			Streams: map[uint64]*Stream{},
		},
	}
}

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

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