提交 ab824a51 作者: Juan Batiz-Benet

added debug printing to peerstream

上级 a768e841
......@@ -144,7 +144,7 @@
},
{
"ImportPath": "github.com/jbenet/go-peerstream",
"Rev": "cddf450fca891e45aa471d882dae2c28ac642fb4"
"Rev": "ccc044c2a5999f36743881ff73568660a581f2f2"
},
{
"ImportPath": "github.com/jbenet/go-random",
......
......@@ -2,6 +2,7 @@ package peerstream
import (
"errors"
"fmt"
"net"
"sync"
......@@ -55,6 +56,15 @@ func newConn(nconn net.Conn, tconn pst.Conn, s *Swarm) *Conn {
}
}
// String returns a string representation of the Conn
func (c *Conn) String() string {
c.streamLock.RLock()
ls := len(c.streams)
c.streamLock.RUnlock()
f := "<peerstream.Conn %d streams %s <--> %s>"
return fmt.Sprintf(f, ls, c.netConn.LocalAddr(), c.netConn.RemoteAddr())
}
// Swarm returns the Swarm associated with this Conn
func (c *Conn) Swarm() *Swarm {
return c.swarm
......
......@@ -24,6 +24,12 @@ func newListener(nl net.Listener, s *Swarm) *Listener {
}
}
// String returns a string representation of the Listener
func (l *Listener) String() string {
f := "<peerstream.Listener %s>"
return fmt.Sprintf(f, l.netList.Addr())
}
// NetListener is the underlying net.Listener
func (l *Listener) NetListener() net.Listener {
return l.netList
......
package peerstream
import (
"fmt"
pst "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport"
)
......@@ -31,6 +33,12 @@ func newStream(ss pst.Stream, c *Conn) *Stream {
return s
}
// String returns a string representation of the Stream
func (s *Stream) String() string {
f := "<peerstream.Stream %s <--> %s>"
return fmt.Sprintf(f, s.conn.NetConn().LocalAddr(), s.conn.NetConn().RemoteAddr())
}
// SPDYStream returns the underlying *spdystream.Stream
func (s *Stream) Stream() pst.Stream {
return s.pstStream
......
......@@ -2,6 +2,7 @@ package peerstream
import (
"errors"
"fmt"
"net"
"sync"
"time"
......@@ -56,6 +57,49 @@ func NewSwarm(t pst.Transport) *Swarm {
return s
}
// String returns a string with various internal stats
func (s *Swarm) String() string {
s.listenerLock.Lock()
ls := len(s.listeners)
s.listenerLock.Unlock()
s.connLock.Lock()
cs := len(s.conns)
s.connLock.Unlock()
s.streamLock.Lock()
ss := len(s.streams)
s.streamLock.Unlock()
str := "<peerstream.Swarm %d listeners %d conns %d streams>"
return fmt.Sprintf(str, ls, cs, ss)
}
// Dump returns a string with all the internal state
func (s *Swarm) Dump() string {
str := s.String() + "\n"
s.listenerLock.Lock()
for l, _ := range s.listeners {
str += fmt.Sprintf("\t%s %v\n", l, l.Groups())
}
s.listenerLock.Unlock()
s.connLock.Lock()
for c, _ := range s.conns {
str += fmt.Sprintf("\t%s %v\n", c, c.Groups())
}
s.connLock.Unlock()
s.streamLock.Lock()
for ss, _ := range s.streams {
str += fmt.Sprintf("\t%s %v\n", ss, ss.Groups())
}
s.streamLock.Unlock()
return str
}
// SetStreamHandler assigns the stream handler in the swarm.
// The handler assumes responsibility for closing the stream.
// This need not happen at the end of the handler, leaving the
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论