]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use peerID type more, and make its Stringer output nicer
authorMatt Joiner <anacrolix@gmail.com>
Sat, 6 Jan 2018 01:34:31 +0000 (12:34 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 6 Jan 2018 01:34:31 +0000 (12:34 +1100)
client.go
connection.go
handshake.go
peerid.go [new file with mode: 0644]
peerid_test.go [new file with mode: 0644]

index 2a7f9333b7935c5120d95c02c7a7bea200ff66cf..d6da9ed1a4f4f084bb320e753f43893aa24146ae 100644 (file)
--- a/client.go
+++ b/client.go
@@ -43,7 +43,7 @@ type Client struct {
        config Config
 
        halfOpenLimit  int
-       peerID         [20]byte
+       peerID         peerID
        defaultStorage *storage.Client
        onClose        []func()
        tcpListener    net.Listener
index 92c72f5733d63ea59109e61736a58f9080eca64f..1c2a24fef680c3b4af76c6648087830adb99ae06 100644 (file)
@@ -73,7 +73,7 @@ type connection struct {
        sentHaves        []bool
 
        // Stuff controlled by the remote peer.
-       PeerID             [20]byte
+       PeerID             peerID
        PeerInterested     bool
        PeerChoked         bool
        PeerRequests       map[request]struct{}
@@ -194,7 +194,7 @@ func (cn *connection) String() string {
 
 func (cn *connection) WriteStatus(w io.Writer, t *Torrent) {
        // \t isn't preserved in <pre> blocks?
-       fmt.Fprintf(w, "%+q: %s-%s\n", cn.PeerID, cn.localAddr(), cn.remoteAddr())
+       fmt.Fprintf(w, "%-40s: %s-%s\n", cn.PeerID, cn.localAddr(), cn.remoteAddr())
        fmt.Fprintf(w, "    last msg: %s, connected: %s, last useful chunk: %s\n",
                eventAgeString(cn.lastMessageReceived),
                eventAgeString(cn.completedHandshake),
index 0ffea147afdea506a9f9ae17466215eafb44ab57..7658151731778c60041d2a971abd9e85b770775a 100644 (file)
@@ -36,7 +36,6 @@ func handshakeWriter(w io.Writer, bb <-chan []byte, done chan<- error) {
 
 type (
        peerExtensionBytes [8]byte
-       peerID             [20]byte
 )
 
 func (pex peerExtensionBytes) SupportsExtended() bool {
diff --git a/peerid.go b/peerid.go
new file mode 100644 (file)
index 0000000..9d8bf1c
--- /dev/null
+++ b/peerid.go
@@ -0,0 +1,14 @@
+package torrent
+
+import (
+       "encoding/hex"
+)
+
+type peerID [20]byte
+
+func (me peerID) String() string {
+       if me[0] == '-' && me[7] == '-' {
+               return string(me[:8]) + hex.EncodeToString(me[8:])
+       }
+       return hex.EncodeToString(me[:])
+}
diff --git a/peerid_test.go b/peerid_test.go
new file mode 100644 (file)
index 0000000..3d89ef4
--- /dev/null
@@ -0,0 +1,22 @@
+package torrent
+
+import (
+       "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())
+       }
+}