master
go 54 lines 1.23 KB
Raw
1 package p2p
2
3 import (
4 logging "github.com/ipfs/go-log/v2"
5 p2phost "github.com/libp2p/go-libp2p/core/host"
6 "github.com/libp2p/go-libp2p/core/peer"
7 pstore "github.com/libp2p/go-libp2p/core/peerstore"
8 "github.com/libp2p/go-libp2p/core/protocol"
9 )
10
11 var log = logging.Logger("p2p-mount")
12
13 // P2P structure holds information on currently running streams/Listeners.
14 type P2P struct {
15 ListenersLocal *Listeners
16 ListenersP2P *Listeners
17 Streams *StreamRegistry
18
19 identity peer.ID
20 peerHost p2phost.Host
21 peerstore pstore.Peerstore
22 }
23
24 // New creates new P2P struct.
25 func New(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore) *P2P {
26 return &P2P{
27 identity: identity,
28 peerHost: peerHost,
29 peerstore: peerstore,
30
31 ListenersLocal: newListenersLocal(),
32 ListenersP2P: newListenersP2P(peerHost),
33
34 Streams: &StreamRegistry{
35 Streams: map[uint64]*Stream{},
36 ConnManager: peerHost.ConnManager(),
37 conns: map[peer.ID]int{},
38 },
39 }
40 }
41
42 // CheckProtoExists checks whether a proto handler is registered to
43 // mux handler.
44 func (p2p *P2P) CheckProtoExists(proto protocol.ID) bool {
45 protos := p2p.peerHost.Mux().Protocols()
46
47 for _, p := range protos {
48 if p != proto {
49 continue
50 }
51 return true
52 }
53 return false
54 }