mock_conn.go 3.31 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
4
5
6
package mocknet

import (
	"container/list"
	"sync"

Jeromy's avatar
Jeromy committed
7
	process "github.com/jbenet/goprocess"
Jeromy's avatar
Jeromy committed
8
	ic "github.com/libp2p/go-libp2p-crypto"
Jeromy's avatar
Jeromy committed
9
	inet "github.com/libp2p/go-libp2p-net"
Jeromy's avatar
Jeromy committed
10
11
	peer "github.com/libp2p/go-libp2p-peer"
	ma "github.com/multiformats/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
)

// conn represents one side's perspective of a
// live connection between two peers.
// it goes over a particular link.
type conn struct {
	local  peer.ID
	remote peer.ID

	localAddr  ma.Multiaddr
	remoteAddr ma.Multiaddr

	localPrivKey ic.PrivKey
	remotePubKey ic.PubKey

	net     *peernet
	link    *link
	rconn   *conn // counterpart
	streams list.List
Jeromy's avatar
Jeromy committed
31
	proc    process.Process
32
	stat    inet.Stat
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
34
35
36

	sync.RWMutex
}

37
func newConn(ln, rn *peernet, l *link, dir inet.Direction) *conn {
Jeromy's avatar
Jeromy committed
38
39
40
	c := &conn{net: ln, link: l}
	c.local = ln.peer
	c.remote = rn.peer
41
	c.stat = inet.Stat{Direction: dir}
Jeromy's avatar
Jeromy committed
42
43
44
45
46
47
48
49
50
51
52

	c.localAddr = ln.ps.Addrs(ln.peer)[0]
	c.remoteAddr = rn.ps.Addrs(rn.peer)[0]

	c.localPrivKey = ln.ps.PrivKey(ln.peer)
	c.remotePubKey = rn.ps.PubKey(rn.peer)

	c.proc = process.WithTeardown(c.teardown)
	return c
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
func (c *conn) Close() error {
Jeromy's avatar
Jeromy committed
54
55
56
57
	return c.proc.Close()
}

func (c *conn) teardown() error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
	for _, s := range c.allStreams() {
Steven Allen's avatar
Steven Allen committed
59
		s.Reset()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
61
	}
	c.net.removeConn(c)
62
63
64
	c.net.notifyAll(func(n inet.Notifiee) {
		n.Disconnected(c.net, c)
	})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
	return nil
}

func (c *conn) addStream(s *stream) {
	c.Lock()
	s.conn = c
	c.streams.PushBack(s)
	c.Unlock()
}

func (c *conn) removeStream(s *stream) {
	c.Lock()
	defer c.Unlock()
	for e := c.streams.Front(); e != nil; e = e.Next() {
		if s == e.Value {
			c.streams.Remove(e)
			return
		}
	}
}

func (c *conn) allStreams() []inet.Stream {
	c.RLock()
	defer c.RUnlock()

	strs := make([]inet.Stream, 0, c.streams.Len())
	for e := c.streams.Front(); e != nil; e = e.Next() {
		s := e.Value.(*stream)
		strs = append(strs, s)
	}
	return strs
}

func (c *conn) remoteOpenedStream(s *stream) {
	c.addStream(s)
	c.net.handleNewStream(s)
101
102
103
	c.net.notifyAll(func(n inet.Notifiee) {
		n.OpenedStream(c.net, s)
	})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104
105
106
107
108
}

func (c *conn) openStream() *stream {
	sl, sr := c.link.newStreamPair()
	c.addStream(sl)
109
110
111
	c.net.notifyAll(func(n inet.Notifiee) {
		n.OpenedStream(c.net, sl)
	})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
112
113
114
115
	c.rconn.remoteOpenedStream(sr)
	return sl
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116
func (c *conn) NewStream() (inet.Stream, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117
118
119
120
121
122
	log.Debugf("Conn.NewStreamWithProtocol: %s --> %s", c.local, c.remote)

	s := c.openStream()
	return s, nil
}

Steven Allen's avatar
Steven Allen committed
123
func (c *conn) GetStreams() []inet.Stream {
124
125
126
127
	var out []inet.Stream
	for e := c.streams.Front(); e != nil; e = e.Next() {
		out = append(out, e.Value.(*stream))
	}
Steven Allen's avatar
Steven Allen committed
128
	return out
129
130
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// LocalMultiaddr is the Multiaddr on this side
func (c *conn) LocalMultiaddr() ma.Multiaddr {
	return c.localAddr
}

// LocalPeer is the Peer on our side of the connection
func (c *conn) LocalPeer() peer.ID {
	return c.local
}

// LocalPrivateKey is the private key of the peer on our side.
func (c *conn) LocalPrivateKey() ic.PrivKey {
	return c.localPrivKey
}

// RemoteMultiaddr is the Multiaddr on the remote side
func (c *conn) RemoteMultiaddr() ma.Multiaddr {
	return c.remoteAddr
}

// RemotePeer is the Peer on the remote side
func (c *conn) RemotePeer() peer.ID {
	return c.remote
}

// RemotePublicKey is the private key of the peer on our side.
func (c *conn) RemotePublicKey() ic.PubKey {
	return c.remotePubKey
}
160
161
162
163
164

// Stat returns metadata about the connection
func (c *conn) Stat() inet.Stat {
	return c.stat
}