client.go | 5 +++++ connection.go | 11 ++++++++++- diff --git a/client.go b/client.go index bd49d9782fc030cf17a7693dbeb5c29acdf948fc..920875478f83f9ca5d4677e8f5e4af0d0cc758f3 100644 --- a/client.go +++ b/client.go @@ -1255,8 +1255,10 @@ c.SetInterested(false) } } +// Handle a received chunk from a peer. func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) error { chunksDownloadedCount.Add(1) + c.ChunksReceived++ req := newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece))) @@ -1270,6 +1272,9 @@ if _, ok := t.Pieces[req.Index].PendingChunkSpecs[req.chunkSpec]; !ok { unusedDownloadedChunksCount.Add(1) return nil } + + c.UsefulChunksReceived++ + c.lastUsefulChunkReceived = time.Now() // Write the chunk out. err := t.WriteChunk(int(msg.Index), int64(msg.Begin), msg.Piece) diff --git a/connection.go b/connection.go index 323f88edb131360926e490bedc3e3064ce3b84eb..6f84c854c26fd47f030c5f1ff530993166a1bfe5 100644 --- a/connection.go +++ b/connection.go @@ -33,6 +33,13 @@ mu sync.Mutex // Only for closing. post chan pp.Message writeCh chan []byte + ChunksReceived int + UsefulChunksReceived int + + lastMessageReceived time.Time + completedHandshake time.Time + lastUsefulChunkReceived time.Time + // Stuff controlled by the local peer. Interested bool Choked bool @@ -64,6 +71,8 @@ closing: make(chan struct{}), writeCh: make(chan []byte), post: make(chan pp.Message), + + completedHandshake: time.Now(), } go c.writer() go c.writeOptimizer(time.Minute) @@ -119,7 +128,7 @@ return nil } func (cn *connection) WriteStatus(w io.Writer) { - fmt.Fprintf(w, "%q: %s-%s: %s completed, reqs: %d-%d, flags: ", cn.PeerID, cn.Socket.LocalAddr(), cn.Socket.RemoteAddr(), cn.completedString(), len(cn.Requests), len(cn.PeerRequests)) + fmt.Fprintf(w, "%-90s: %s completed, good chunks: %d/%d reqs: %d-%d, last msg: %.0fs ago age: %.1fmin last useful chunk: %s ago flags: ", fmt.Sprintf("%q: %s-%s", cn.PeerID, cn.Socket.LocalAddr(), cn.Socket.RemoteAddr()), cn.completedString(), cn.UsefulChunksReceived, cn.ChunksReceived, len(cn.Requests), len(cn.PeerRequests), time.Now().Sub(cn.lastMessageReceived).Seconds(), time.Now().Sub(cn.completedHandshake).Minutes(), time.Now().Sub(cn.lastUsefulChunkReceived)) c := func(b byte) { fmt.Fprintf(w, "%c", b) }