]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Change a bunch of Stringer and status stuff
authorMatt Joiner <anacrolix@gmail.com>
Mon, 5 Feb 2018 06:47:47 +0000 (17:47 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 5 Feb 2018 06:47:47 +0000 (17:47 +1100)
Cumulative interest time, download rate per connection, request low and high waters, peer extension bytes

connection.go
handshake.go
peerid.go
peerid_test.go

index 947adc5ffbbbb37a4dbdb96a77157e25cf2ca0d3..20a4f90164209c867e82f62edc0678a75be7e01b 100644 (file)
@@ -59,7 +59,10 @@ type connection struct {
        lastChunkSent           time.Time
 
        // Stuff controlled by the local peer.
-       Interested       bool
+       Interested           bool
+       lastBecameInterested time.Time
+       priorInterest        time.Duration
+
        Choked           bool
        requests         map[request]struct{}
        requestsLowWater int
@@ -99,6 +102,14 @@ type connection struct {
        writerCond  sync.Cond
 }
 
+func (cn *connection) cumInterest() time.Duration {
+       ret := cn.priorInterest
+       if cn.Interested {
+               ret += time.Since(cn.lastBecameInterested)
+       }
+       return ret
+}
+
 func (cn *connection) peerHasAllPieces() (all bool, known bool) {
        if cn.peerSentHaveAll {
                return true, true
@@ -197,30 +208,38 @@ func (cn *connection) statusFlags() (ret string) {
        return
 }
 
-func (cn *connection) String() string {
-       var buf bytes.Buffer
-       cn.WriteStatus(&buf, nil)
-       return buf.String()
+// func (cn *connection) String() string {
+//     var buf bytes.Buffer
+//     cn.WriteStatus(&buf, nil)
+//     return buf.String()
+// }
+
+func (cn *connection) downloadRate() float64 {
+       return float64(cn.stats.BytesReadUsefulData) / cn.cumInterest().Seconds()
 }
 
 func (cn *connection) WriteStatus(w io.Writer, t *Torrent) {
        // \t isn't preserved in <pre> blocks?
-       fmt.Fprintf(w, "%-40s: %s-%s\n", cn.PeerID, cn.localAddr(), cn.remoteAddr())
-       fmt.Fprintf(w, "    last msg: %s, connected: %s, last helpful: %s\n",
+       fmt.Fprintf(w, "%+-55q %s %s-%s\n", cn.PeerID, cn.PeerExtensionBytes, cn.localAddr(), cn.remoteAddr())
+       fmt.Fprintf(w, "    last msg: %s, connected: %s, last helpful: %s, itime: %s\n",
                eventAgeString(cn.lastMessageReceived),
                eventAgeString(cn.completedHandshake),
-               eventAgeString(cn.lastHelpful()))
+               eventAgeString(cn.lastHelpful()),
+               cn.cumInterest(),
+       )
        fmt.Fprintf(w,
-               "    %s completed, %d pieces touched, good chunks: %d/%d-%d reqq: %d-%d, flags: %s\n",
+               "    %s completed, %d pieces touched, good chunks: %d/%d-%d reqq: (%d,%d,%d]-%d, flags: %s, dr: %.1f KiB/s\n",
                cn.completedString(),
                len(cn.peerTouchedPieces),
                cn.stats.ChunksReadUseful,
-               // TODO: Use ChunksRead? Verify that value is the same as this sum?
-               cn.stats.ChunksReadUnwanted+cn.stats.ChunksReadUseful,
+               cn.stats.ChunksRead,
                cn.stats.ChunksWritten,
+               cn.requestsLowWater,
                cn.numLocalRequests(),
+               cn.nominalMaxRequests(),
                len(cn.PeerRequests),
                cn.statusFlags(),
+               cn.downloadRate()/(1<<10),
        )
        roi := cn.pieceRequestOrderIter()
        fmt.Fprintf(w, "    next pieces: %v%s\n",
@@ -350,6 +369,11 @@ func (cn *connection) SetInterested(interested bool, msg func(pp.Message) bool)
                return true
        }
        cn.Interested = interested
+       if interested {
+               cn.lastBecameInterested = time.Now()
+       } else if !cn.lastBecameInterested.IsZero() {
+               cn.priorInterest += time.Since(cn.lastBecameInterested)
+       }
        // log.Printf("%p: setting interest: %v", cn, interested)
        return msg(pp.Message{
                Type: func() pp.MessageType {
index f15f5bbf6377221fd95f22a3b9e08ef1d9027d80..6627b60839c6067fe1ca735033dbc08074e12d94 100644 (file)
@@ -38,6 +38,10 @@ type (
        peerExtensionBytes [8]byte
 )
 
+func (me peerExtensionBytes) String() string {
+       return hex.EncodeToString(me[:])
+}
+
 func newPeerExtensionBytes(bits ...ExtensionBit) (ret peerExtensionBytes) {
        for _, b := range bits {
                ret.SetBit(b)
@@ -124,7 +128,7 @@ func handshake(sock io.ReadWriter, ih *metainfo.Hash, peerID [20]byte, extension
        missinggo.CopyExact(&res.peerExtensionBytes, b[20:28])
        missinggo.CopyExact(&res.Hash, b[28:48])
        missinggo.CopyExact(&res.PeerID, b[48:68])
-       peerExtensions.Add(hex.EncodeToString(res.peerExtensionBytes[:]), 1)
+       peerExtensions.Add(res.peerExtensionBytes.String(), 1)
 
        // TODO: Maybe we can just drop peers here if we're not interested. This
        // could prevent them trying to reconnect, falsely believing there was
index 1514b79c02ccf0b4fdacb867a2839f331a0bc6ed..9bc947544bb8d8a399c2800e58b2fbe9b8360ce3 100644 (file)
--- a/peerid.go
+++ b/peerid.go
@@ -1,17 +1,14 @@
 package torrent
 
-import (
-       "encoding/hex"
-)
-
 // Peer client ID.
 type PeerID [20]byte
 
-// Pretty prints the ID as hex, except parts that adher to the Peer ID
-// Conventions of BEP 20.
-func (me PeerID) String() string {
-       if me[0] == '-' && me[7] == '-' {
-               return string(me[:8]) + hex.EncodeToString(me[8:])
-       }
-       return hex.EncodeToString(me[:])
-}
+// // Pretty prints the ID as hex, except parts that adher to the Peer ID
+// // Conventions of BEP 20.
+// func (me PeerID) String() string {
+//     // if me[0] == '-' && me[7] == '-' {
+//     //      return string(me[:8]) + hex.EncodeToString(me[8:])
+//     // }
+//     // return hex.EncodeToString(me[:])
+//     return fmt.Sprintf("%+q", me[:])
+// }
index 965a28abacac3a62b38229a7cf60869685cf87d2..bcf09998b1df2ca1e04e917c5db6d03904ce6ac7 100644 (file)
@@ -1,24 +1,16 @@
 package torrent
 
-import (
-       "fmt"
-       "testing"
-
-       "github.com/anacrolix/missinggo"
-       "github.com/stretchr/testify/assert"
-)
-
-func TestPeerIdString(t *testing.T) {
-       for _, _case := range []struct {
-               id string
-               s  string
-       }{
-               {"\x1cNJ}\x9c\xc7\xc4o\x94<\x9b\x8c\xc2!I\x1c\a\xec\x98n", "1c4e4a7d9cc7c46f943c9b8cc221491c07ec986e"},
-               {"-FD51W\xe4-LaZMk0N8ZLA7", "-FD51W\xe4-4c615a4d6b304e385a4c4137"},
-       } {
-               var pi PeerID
-               missinggo.CopyExact(&pi, _case.id)
-               assert.EqualValues(t, _case.s, pi.String())
-               assert.EqualValues(t, fmt.Sprintf("%q", _case.s), fmt.Sprintf("%q", pi))
-       }
-}
+// func TestPeerIdString(t *testing.T) {
+//     for _, _case := range []struct {
+//             id string
+//             s  string
+//     }{
+//             {"\x1cNJ}\x9c\xc7\xc4o\x94<\x9b\x8c\xc2!I\x1c\a\xec\x98n", "\"\x1cNJ}\x9c\xc7\xc4o\x94<\x9b\x8c\xc2!I\x1c\a\xec\x98n\""},
+//             {"-FD51W\xe4-LaZMk0N8ZLA7", "-FD51W\xe4-LaZMk0N8ZLA7"},
+//     } {
+//             var pi PeerID
+//             missinggo.CopyExact(&pi, _case.id)
+//             assert.EqualValues(t, _case.s, pi.String())
+//             assert.EqualValues(t, fmt.Sprintf("%q", _case.s), fmt.Sprintf("%q", pi))
+//     }
+// }