forked from spacemeshos/go-spacemesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
128 lines (101 loc) · 3.41 KB
/
peer.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package p2p
import (
"github.com/spacemeshos/go-spacemesh/crypto"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p/net"
"github.com/spacemeshos/go-spacemesh/p2p/node"
)
// Peer is a remote network node.
// At minimum local node knows its id (public key) and announced tcp address/port.
// Peers are maintained by the swarm and are not visible to higher-level types on the network stack.
// All Peer methods are NOT thread-safe - they are designed to be used only from a singleton Swarm type.
// Peer handles swarm sessions and net connections with a remote node.
type Peer interface {
ID() []byte // node id is public key bytes
String() string // node public key string
Pretty() string
TCPAddress() string // tcp address advertised by node e.g. 127.0.0.1:3058
PublicKey() crypto.PublicKey
GetConnections() map[string]net.Connection
DeleteAllConnections()
GetSessions() map[string]NetworkSession
// GetAuthenticatedSession returns an authenticated session with
// the node if one exists.
GetAuthenticatedSession() NetworkSession
// GetActiveConnection returns an active connection with the node if we have one.
GetActiveConnection() net.Connection
// GetRemoteNodeData returns RemoteNodeData for this peer.
GetRemoteNodeData() node.RemoteNodeData
}
type peerImpl struct {
publicKey crypto.PublicKey
tcpAddress string
connections map[string]net.Connection
sessions map[string]NetworkSession
}
// NewRemoteNode creates a new remote node using provided id and tcp address.
func NewRemoteNode(id string, tcpAddress string) (Peer, error) {
key, err := crypto.NewPublicKeyFromString(id)
if err != nil {
log.Error("invalid node id format", err)
return nil, err
}
n := &peerImpl{
publicKey: key,
tcpAddress: tcpAddress,
connections: make(map[string]net.Connection),
sessions: make(map[string]NetworkSession),
}
return n, nil
}
func (n *peerImpl) GetAuthenticatedSession() NetworkSession {
for _, v := range n.sessions {
if v.IsAuthenticated() {
return v
}
}
return nil
}
func (n *peerImpl) GetRemoteNodeData() node.RemoteNodeData {
return node.NewRemoteNodeData(n.String(), n.TCPAddress())
}
func (n *peerImpl) GetActiveConnection() net.Connection {
// todo: sort by last data transfer time to pick the best connection
// just return a random connection for now
for _, v := range n.connections {
return v
}
return nil
}
// GetConnections gets all connections with this peer.
func (n *peerImpl) GetConnections() map[string]net.Connection {
return n.connections
}
// DeleteAllConnections delete all connections with this peer.
func (n *peerImpl) DeleteAllConnections() {
n.connections = make(map[string]net.Connection)
}
// GetSession returns all the network sessions with this peer.
func (n *peerImpl) GetSessions() map[string]NetworkSession {
return n.sessions
}
// String returns a string identifier for this peer.
func (n *peerImpl) String() string {
return n.publicKey.String()
}
// ID returns the binary identifier for this peer.
func (n *peerImpl) ID() []byte {
return n.publicKey.Bytes()
}
// Pretty returns a readable identifier for this peer.
func (n *peerImpl) Pretty() string {
return n.publicKey.Pretty()
}
// PublicKey returns the peer's public key.
func (n *peerImpl) PublicKey() crypto.PublicKey {
return n.publicKey
}
// TCPAddress returns the public TCP address of this peer.
func (n *peerImpl) TCPAddress() string {
return n.tcpAddress
}