-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.go
97 lines (72 loc) · 3.61 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package host
import (
"context"
"github.com/rambollwong/rainbowbee/core"
"github.com/rambollwong/rainbowbee/core/blacklist"
"github.com/rambollwong/rainbowbee/core/handler"
"github.com/rambollwong/rainbowbee/core/manager"
"github.com/rambollwong/rainbowbee/core/network"
"github.com/rambollwong/rainbowbee/core/peer"
"github.com/rambollwong/rainbowbee/core/protocol"
"github.com/rambollwong/rainbowbee/core/store"
"github.com/rambollwong/rainbowlog"
ma "github.com/multiformats/go-multiaddr"
)
// PeerProtocols stores the information about a peer's ID and the list of protocol IDs supported by the peer.
type PeerProtocols struct {
PID peer.ID // Peer ID
Protocols []protocol.ID // List of supported protocol IDs
}
// ConnectionHandler is a function for handling connections.
type ConnectionHandler func(conn network.Connection) (bool, error)
// Host provides network capabilities.
type Host interface {
core.Switcher
// Context returns the context of the host instance.
Context() context.Context
// ID returns the local peer ID.
ID() peer.ID
// RegisterMsgPayloadHandler registers a handler.MsgPayloadHandler for handling
// messages received with the specified protocolID.
RegisterMsgPayloadHandler(protocolID protocol.ID, handler handler.MsgPayloadHandler) error
// UnregisterMsgPayloadHandler unregisters the handler.MsgPayloadHandler for
// the specified protocolID.
UnregisterMsgPayloadHandler(protocolID protocol.ID) error
// SendMsg sends a message with the specified protocolID to the receiver
// identified by receiverPID.
SendMsg(protocolID protocol.ID, receiverPID peer.ID, msgPayload []byte) error
// Dial attempts to establish a connection with a peer at the given remote address.
Dial(remoteAddr ma.Multiaddr) (network.Connection, error)
// PeerStore returns the store.PeerStore instance associated with the host.
PeerStore() store.PeerStore
// ConnectionManager returns the manager.ConnectionManager instance associated with the host.
ConnectionManager() manager.ConnectionManager
// ProtocolManager returns the manager.ProtocolManager instance associated with the host.
ProtocolManager() manager.ProtocolManager
// PeerBlackList returns the blacklist.PeerBlackList instance associated with the host.
PeerBlackList() blacklist.PeerBlackList
// PeerProtocols returns the list of connected peers and their supported protocol IDs.
// If protocolIDs is nil, it returns information about all connected peers.
// Otherwise, it returns information about connected peers that support the specified protocol IDs.
PeerProtocols(protocolIDs []protocol.ID) ([]*PeerProtocols, error)
// PeerSupportProtocol checks if the peer identified by pid supports the specified protocolID.
// It returns true if the peer supports the protocol, otherwise it returns false.
PeerSupportProtocol(pid peer.ID, protocolID protocol.ID) bool
// Notify registers a Notifiee to receive notifications from the host.
Notify(notifiee Notifiee)
// AddDirectPeer appends a direct peer to the host.
AddDirectPeer(dp ma.Multiaddr) error
// ClearDirectPeers removes all direct peers from the host.
ClearDirectPeers()
// LocalAddresses returns a list of network addresses that the host is listening on.
LocalAddresses() []ma.Multiaddr
// Network returns the network.Network instance associated with the host.
Network() network.Network
// Logger returns the logger instance of the host.
Logger() *rainbowlog.Logger
}
// Components is an interface that represents a collection of components that can be attached to a host.
type Components interface {
// AttachHost attaches the given host to the components.
AttachHost(host Host)
}