From 95d5d4a30c461351ccc0e1990c5eb0eb52982bf5 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 22 Aug 2014 17:33:17 +1000 Subject: [PATCH] Improve status and logging --- client.go | 3 +++ connection.go | 21 +++++++-------------- download_strategies.go | 19 +++++++++++++++++-- torrent.go | 1 + 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index fc5f92fd..023e4707 100644 --- a/client.go +++ b/client.go @@ -47,6 +47,7 @@ var ( chunksDownloadedCount = expvar.NewInt("chunksDownloadedCount") peersFoundByDHT = expvar.NewInt("peersFoundByDHT") peersFoundByPEX = expvar.NewInt("peersFoundByPEX") + uploadChunksPosted = expvar.NewInt("uploadChunksPosted") ) const extensionBytes = "\x00\x00\x00\x00\x00\x10\x00\x00" @@ -122,6 +123,7 @@ func (cl *Client) WriteStatus(w io.Writer) { fmt.Fprintf(w, "DHT nodes: %d\n", cl.dHT.NumNodes()) fmt.Fprintf(w, "DHT Server ID: %x\n", cl.dHT.IDString()) } + cl.downloadStrategy.WriteStatus(w) fmt.Fprintln(w) for _, t := range cl.torrents { fmt.Fprintf(w, "%s: %f%%\n", t.Name(), func() float32 { @@ -714,6 +716,7 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error { Begin: msg.Begin, Piece: p, }) + uploadChunksPosted.Add(1) case pp.Cancel: req := newRequest(msg.Index, msg.Begin, msg.Length) if !c.PeerCancel(req) { diff --git a/connection.go b/connection.go index e79b5d66..00c92da6 100644 --- a/connection.go +++ b/connection.go @@ -121,26 +121,19 @@ func (cn *connection) WriteStatus(w io.Writer) { c := func(b byte) { fmt.Fprintf(w, "%c", b) } - // https://trac.transmissionbt.com/wiki/PeerStatusText - if cn.PeerInterested && !cn.Choked { - c('O') - } + // Inspired by https://trac.transmissionbt.com/wiki/PeerStatusText if len(cn.Requests) != 0 { c('D') } if cn.PeerChoked && cn.Interested { c('d') } - if !cn.Choked && cn.PeerInterested { - c('U') - } else { - c('u') - } - if !cn.PeerChoked && !cn.Interested { - c('K') - } - if !cn.Choked && !cn.PeerInterested { - c('?') + if !cn.Choked { + if cn.PeerInterested { + c('U') + } else { + c('u') + } } if cn.Discovery != 0 { c(byte(cn.Discovery)) diff --git a/download_strategies.go b/download_strategies.go index 8ffa61fd..d4c91a00 100644 --- a/download_strategies.go +++ b/download_strategies.go @@ -1,7 +1,8 @@ package torrent import ( - "container/list" + "fmt" + "io" pp "bitbucket.org/anacrolix/go.torrent/peer_protocol" ) @@ -14,12 +15,15 @@ type DownloadStrategy interface { TorrentPrioritize(t *torrent, off, _len int64) TorrentGotChunk(t *torrent, r request) TorrentGotPiece(t *torrent, piece int) + WriteStatus(w io.Writer) } type DefaultDownloadStrategy struct { heat map[*torrent]map[request]int } +func (me *DefaultDownloadStrategy) WriteStatus(w io.Writer) {} + func (s *DefaultDownloadStrategy) FillRequests(t *torrent, c *connection) { if c.Interested { if c.PeerChoked { @@ -115,7 +119,18 @@ type responsiveDownloadStrategy struct { // the last piece read for a given torrent. Readahead int lastReadPiece map[*torrent]int - priorities map[*torrent]*list.List + priorities map[*torrent]map[request]struct{} +} + +func (me *responsiveDownloadStrategy) WriteStatus(w io.Writer) { + fmt.Fprintf(w, "Priorities:\n") + for t, pp := range me.priorities { + fmt.Fprintf(w, "\t%s:", t.Name()) + for r := range pp { + fmt.Fprintf(w, "%v ", r) + } + fmt.Fprintln(w) + } } func (me *responsiveDownloadStrategy) TorrentStarted(t *torrent) { diff --git a/torrent.go b/torrent.go index 9fdb79d5..c046897c 100644 --- a/torrent.go +++ b/torrent.go @@ -233,6 +233,7 @@ func (t *torrent) WriteStatus(w io.Writer) { // } // } fmt.Fprintf(w, "Pending peers: %d\n", len(t.Peers)) + fmt.Fprintf(w, "Active peers: %d\n", len(t.Conns)) for _, c := range t.Conns { c.WriteStatus(w) } -- 2.48.1