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

3
import (
4 5
	"sync"

6 7 8
	pstore "gx/ipfs/QmZR2XWVVBCtbgBWnQhWk2xcQfaR3W8faQPriAiaaj7rsr/go-libp2p-peerstore"
	p2phost "gx/ipfs/Qmb8T6YBBsjYsVGfrihQLfCJveczZnneSBqBKkYEBWDjge/go-libp2p-host"
	peer "gx/ipfs/QmdVrMn1LhB4ybb8hMVaMLXnA8XRSewMnK6YqXKXoTcRvN/go-libp2p-peer"
9 10
)

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

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

21 22 23 24 25 26 27
// 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,

28 29
		Listeners: &ListenerRegistry{
			Listeners: map[listenerKey]Listener{},
Łukasz Magiera's avatar
Łukasz Magiera committed
30
			lk:        sync.Mutex{},
31
		},
32
		Streams: &StreamRegistry{
33
			Streams: map[uint64]*Stream{},
Łukasz Magiera's avatar
Łukasz Magiera committed
34
			lk:      sync.Mutex{},
35 36 37 38
		},
	}
}

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
}