added to net/conn interface
Juan Batiz-Benet committed
Oct 18, 2014 at 05:56 UTC
0078264a5ba16b1726443261d1917814119ad682
3 files changed
+50
net/conn/conn.go
+25
@@ -88,6 +88,21 @@ func (c *singleConn) close() error {
88
return err
89
}
90
91
+// ID is an identifier unique to this connection.
92
+func (c *singleConn) ID() string {
93
+ return ID(c)
94
+}
95
+
96
+// LocalMultiaddr is the Multiaddr on this side
97
+func (c *singleConn) LocalMultiaddr() ma.Multiaddr {
98
+ return c.maconn.LocalMultiaddr()
99
+}
100
+
101
+// RemoteMultiaddr is the Multiaddr on the remote side
102
+func (c *singleConn) RemoteMultiaddr() ma.Multiaddr {
103
+ return c.maconn.RemoteMultiaddr()
104
+}
105
+
106
// LocalPeer is the Peer on this side
107
func (c *singleConn) LocalPeer() *peer.Peer {
108
return c.local
@@ -108,6 +123,16 @@ func (c *singleConn) Out() chan<- []byte {
123
return c.msgio.outgoing.MsgChan
124
}
125
126
+// ID returns the
127
+func ID(c Conn) string {
128
+ l := fmt.Sprintf("%s/%s", c.LocalMultiaddr(), c.LocalPeer().ID)
129
+ r := fmt.Sprintf("%s/%s", c.RemoteMultiaddr(), c.RemotePeer().ID)
130
+ lh := u.Hash([]byte(l))
131
+ rh := u.Hash([]byte(r))
132
+ ch := u.XOR(lh, rh)
133
+ return u.Key(ch).Pretty()
134
+}
135
+
136
// Dialer is an object that can open connections. We could have a "convenience"
137
// Dial function as before, but it would have many arguments, as dialing is
138
// no longer simple (need a peerstore, a local peer, a context, a network, etc)
net/conn/interface.go
+9
@@ -15,9 +15,18 @@ type Conn interface {
15
// implement ContextCloser too!
16
ContextCloser
17
18
+ // ID is an identifier unique to this connection.
19
+ ID() string
20
+
21
+ // LocalMultiaddr is the Multiaddr on this side
22
+ LocalMultiaddr() ma.Multiaddr
23
+
24
// LocalPeer is the Peer on this side
25
LocalPeer() *peer.Peer
26
27
+ // RemoteMultiaddr is the Multiaddr on the remote side
28
+ RemoteMultiaddr() ma.Multiaddr
29
+
30
// RemotePeer is the Peer on the remote side
31
RemotePeer() *peer.Peer
32
net/conn/secure_conn.go
+16
@@ -4,6 +4,7 @@ import (
4
"errors"
5
6
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
+ ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
8
9
spipe "github.com/jbenet/go-ipfs/crypto/spipe"
10
peer "github.com/jbenet/go-ipfs/peer"
@@ -92,6 +93,21 @@ func (c *secureConn) close() error {
93
return err
94
}
95
96
+// ID is an identifier unique to this connection.
97
+func (c *secureConn) ID() string {
98
+ return ID(c)
99
+}
100
+
101
+// LocalMultiaddr is the Multiaddr on this side
102
+func (c *secureConn) LocalMultiaddr() ma.Multiaddr {
103
+ return c.insecure.LocalMultiaddr()
104
+}
105
+
106
+// RemoteMultiaddr is the Multiaddr on the remote side
107
+func (c *secureConn) RemoteMultiaddr() ma.Multiaddr {
108
+ return c.insecure.RemoteMultiaddr()
109
+}
110
+
111
// LocalPeer is the Peer on this side
112
func (c *secureConn) LocalPeer() *peer.Peer {
113
return c.insecure.LocalPeer()