Unverified Commit 985120b6 authored by Raúl Kripalani's avatar Raúl Kripalani Committed by GitHub

upgrade swarm; add ID() on mock conns and streams. (#970)

parent 14f4ff5e
This diff is collapsed.
......@@ -2,7 +2,9 @@ package mocknet
import (
"container/list"
"strconv"
"sync"
"sync/atomic"
process "github.com/jbenet/goprocess"
ic "github.com/libp2p/go-libp2p-core/crypto"
......@@ -12,12 +14,16 @@ import (
manet "github.com/multiformats/go-multiaddr-net"
)
var connCounter int64
// conn represents one side's perspective of a
// live connection between two peers.
// it goes over a particular link.
type conn struct {
notifLk sync.Mutex
id int64
local peer.ID
remote peer.ID
......@@ -43,6 +49,7 @@ func newConn(p process.Process, ln, rn *peernet, l *link, dir network.Direction)
c.local = ln.peer
c.remote = rn.peer
c.stat = network.Stat{Direction: dir}
c.id = atomic.AddInt64(&connCounter, 1)
c.localAddr = ln.ps.Addrs(ln.peer)[0]
for _, a := range rn.ps.Addrs(rn.peer) {
......@@ -61,6 +68,10 @@ func newConn(p process.Process, ln, rn *peernet, l *link, dir network.Direction)
return c
}
func (c *conn) ID() string {
return strconv.FormatInt(c.id, 10)
}
func (c *conn) Close() error {
return c.pairProc.Close()
}
......
......@@ -5,6 +5,7 @@ import (
"errors"
"io"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
......@@ -14,12 +15,15 @@ import (
protocol "github.com/libp2p/go-libp2p-core/protocol"
)
var streamCounter int64
// stream implements network.Stream
type stream struct {
notifLk sync.Mutex
rstream *stream
conn *conn
id int64
write *io.PipeWriter
read *io.PipeReader
......@@ -57,6 +61,7 @@ func newStream(w *io.PipeWriter, r *io.PipeReader, dir network.Direction) *strea
s := &stream{
read: r,
write: w,
id: atomic.AddInt64(&streamCounter, 1),
reset: make(chan struct{}, 1),
close: make(chan struct{}, 1),
closed: make(chan struct{}),
......@@ -86,6 +91,10 @@ func (s *stream) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (s *stream) ID() string {
return strconv.FormatInt(s.id, 10)
}
func (s *stream) Protocol() protocol.ID {
// Ignore type error. It means that the protocol is unset.
p, _ := s.protocol.Load().(protocol.ID)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment