1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 01:52:26 +08:00

added debug printing to peerstream

This commit is contained in:
Juan Batiz-Benet
2015-01-12 10:53:44 -08:00
parent a768e841bc
commit ab824a51ff
5 changed files with 69 additions and 1 deletions

2
Godeps/Godeps.json generated
View File

@ -144,7 +144,7 @@
},
{
"ImportPath": "github.com/jbenet/go-peerstream",
"Rev": "cddf450fca891e45aa471d882dae2c28ac642fb4"
"Rev": "ccc044c2a5999f36743881ff73568660a581f2f2"
},
{
"ImportPath": "github.com/jbenet/go-random",

View File

@ -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

View File

@ -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

View File

@ -1,6 +1,8 @@
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

View File

@ -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