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
type Listener interface {
	Protocol() string
	Address() string

	// Close closes the listener. Does not affect child streams
	Close() error
}

Łukasz Magiera's avatar
Łukasz Magiera committed
27 28 29
// NewP2P creates new P2P struct
func NewP2P(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore) *P2P {
	return &P2P{
30 31 32 33 34 35
		identity:  identity,
		peerHost:  peerHost,
		peerstore: peerstore,
	}
}

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

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