]> Sergey Matveev's repositories - btrtrc.git/commitdiff
added the initial version of the (t *Torrent) KnownSwarm() function
authorBora M. Alper <bora@boramalper.org>
Tue, 12 Sep 2017 14:22:53 +0000 (15:22 +0100)
committerBora M. Alper <bora@boramalper.org>
Tue, 12 Sep 2017 14:22:53 +0000 (15:22 +0100)
torrent.go

index 7f04e5f3aae1c62568fef25a2cefc1e4124517f8..aba33139a9154348f10d8e8ca91fc49bb0f133c8 100644 (file)
@@ -11,6 +11,7 @@ import (
        "math/rand"
        "net"
        "os"
+       "strconv"
        "sync"
        "text/tabwriter"
        "time"
@@ -122,6 +123,44 @@ func (t *Torrent) Closed() <-chan struct{} {
        return t.closed.LockedChan(&t.cl.mu)
 }
 
+// KnownSwarm returns the known subset of the peers in the Torrent's swarm, including active,
+// pending, and half-open peers.
+func (t *Torrent) KnownSwarm() (ks []Peer) {
+       // Add pending peers to the list
+       for _, peer := range t.peers {
+               ks = append(ks, peer)
+       }
+
+       // Add active peers to the list
+       for conn := range t.conns {
+               host, portString, err := net.SplitHostPort(conn.remoteAddr().String())
+               if err != nil {
+                       panic(err)
+               }
+
+               ip := net.ParseIP(host)
+               port, err := strconv.Atoi(portString)
+               if err != nil {
+                       panic(err)
+               }
+
+               ks = append(ks, Peer{
+                       Id: conn.PeerID,
+                       IP: ip,
+                       Port: port,
+                       Source: conn.Discovery,
+                       // TODO: the connection can be unencrypted due to our (or the peer's) preference,
+                       //       but the remote peer might support the encryption. Find a better way to query
+                       //       that information, if possible.
+                       SupportsEncryption: conn.encrypted,
+               })
+       }
+
+       // TODO: how can we add half-open peers?
+
+       return
+}
+
 func (t *Torrent) setChunkSize(size pp.Integer) {
        t.chunkSize = size
        t.chunkPool = &sync.Pool{