github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/p2p/net/mock/mock_conn.go (about) 1 package mocknet 2 3 import ( 4 "container/list" 5 "sync" 6 7 ic "github.com/ipfs/go-ipfs/p2p/crypto" 8 inet "github.com/ipfs/go-ipfs/p2p/net" 9 peer "github.com/ipfs/go-ipfs/p2p/peer" 10 11 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" 12 ) 13 14 // conn represents one side's perspective of a 15 // live connection between two peers. 16 // it goes over a particular link. 17 type conn struct { 18 local peer.ID 19 remote peer.ID 20 21 localAddr ma.Multiaddr 22 remoteAddr ma.Multiaddr 23 24 localPrivKey ic.PrivKey 25 remotePubKey ic.PubKey 26 27 net *peernet 28 link *link 29 rconn *conn // counterpart 30 streams list.List 31 32 sync.RWMutex 33 } 34 35 func (c *conn) Close() error { 36 for _, s := range c.allStreams() { 37 s.Close() 38 } 39 c.net.removeConn(c) 40 c.net.notifyAll(func(n inet.Notifiee) { 41 n.Disconnected(c.net, c) 42 }) 43 return nil 44 } 45 46 func (c *conn) addStream(s *stream) { 47 c.Lock() 48 s.conn = c 49 c.streams.PushBack(s) 50 c.Unlock() 51 } 52 53 func (c *conn) removeStream(s *stream) { 54 c.Lock() 55 defer c.Unlock() 56 for e := c.streams.Front(); e != nil; e = e.Next() { 57 if s == e.Value { 58 c.streams.Remove(e) 59 return 60 } 61 } 62 } 63 64 func (c *conn) allStreams() []inet.Stream { 65 c.RLock() 66 defer c.RUnlock() 67 68 strs := make([]inet.Stream, 0, c.streams.Len()) 69 for e := c.streams.Front(); e != nil; e = e.Next() { 70 s := e.Value.(*stream) 71 strs = append(strs, s) 72 } 73 return strs 74 } 75 76 func (c *conn) remoteOpenedStream(s *stream) { 77 c.addStream(s) 78 c.net.handleNewStream(s) 79 c.net.notifyAll(func(n inet.Notifiee) { 80 n.OpenedStream(c.net, s) 81 }) 82 } 83 84 func (c *conn) openStream() *stream { 85 sl, sr := c.link.newStreamPair() 86 c.addStream(sl) 87 c.net.notifyAll(func(n inet.Notifiee) { 88 n.OpenedStream(c.net, sl) 89 }) 90 c.rconn.remoteOpenedStream(sr) 91 return sl 92 } 93 94 func (c *conn) NewStream() (inet.Stream, error) { 95 log.Debugf("Conn.NewStreamWithProtocol: %s --> %s", c.local, c.remote) 96 97 s := c.openStream() 98 return s, nil 99 } 100 101 // LocalMultiaddr is the Multiaddr on this side 102 func (c *conn) LocalMultiaddr() ma.Multiaddr { 103 return c.localAddr 104 } 105 106 // LocalPeer is the Peer on our side of the connection 107 func (c *conn) LocalPeer() peer.ID { 108 return c.local 109 } 110 111 // LocalPrivateKey is the private key of the peer on our side. 112 func (c *conn) LocalPrivateKey() ic.PrivKey { 113 return c.localPrivKey 114 } 115 116 // RemoteMultiaddr is the Multiaddr on the remote side 117 func (c *conn) RemoteMultiaddr() ma.Multiaddr { 118 return c.remoteAddr 119 } 120 121 // RemotePeer is the Peer on the remote side 122 func (c *conn) RemotePeer() peer.ID { 123 return c.remote 124 } 125 126 // RemotePublicKey is the private key of the peer on our side. 127 func (c *conn) RemotePublicKey() ic.PubKey { 128 return c.remotePubKey 129 }