1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00

Add logging around tracker and number of peers

This commit is contained in:
Jesse
2019-12-29 21:59:33 -05:00
parent 67066ad77c
commit 23f3a144af
2 changed files with 9 additions and 1 deletions

View File

@@ -31,6 +31,7 @@ type Torrent struct {
InfoHash [20]byte
PieceHashes [][20]byte
Length int
Name string
}
type pieceWork struct {
@@ -198,6 +199,7 @@ func calculateBoundsForPiece(index, numPieces, length int) (begin int, end int)
// Download downloads the torrent
func (t *Torrent) Download() ([]byte, error) {
log.Println("Starting download for", t.Name)
// Init queues for workers to retrieve work and send results
workQueue := make(chan *pieceWork, len(t.PieceHashes))
results := make(chan *pieceResult, len(t.PieceHashes))
@@ -222,7 +224,7 @@ func (t *Torrent) Download() ([]byte, error) {
percent := float64(donePieces) / float64(len(t.PieceHashes)) * 100
numWorkers := runtime.NumGoroutine() - 1 // subtract 1 for main thread
log.Printf("(%0.2f%%) Downloaded piece #%d with %d workers\n", percent, res.index, numWorkers)
log.Printf("(%0.2f%%) Downloaded piece #%d from %d peers\n", percent, res.index, numWorkers)
}
close(workQueue)

View File

@@ -6,6 +6,7 @@ import (
"crypto/sha1"
"fmt"
"io"
"log"
"github.com/jackpal/bencode-go"
"github.com/veggiedefender/torrent-client/p2p"
@@ -44,13 +45,18 @@ func (t *TorrentFile) Download() ([]byte, error) {
return nil, err
}
log.Println("Connecting with tracker", t.Announce)
peers, err := t.getPeers(peerID, Port)
log.Printf("Found %d peers", len(peers))
torrent := p2p.Torrent{
Peers: peers,
PeerID: peerID,
InfoHash: t.InfoHash,
PieceHashes: t.PieceHashes,
Length: t.Length,
Name: t.Name,
}
buf, err := torrent.Download()
if err != nil {