Harmless improvements

This commit is contained in:
Matt Joiner 2015-03-11 02:39:01 +11:00
parent a583c4a914
commit 914bc12bb6
2 changed files with 16 additions and 30 deletions

View File

@ -202,7 +202,7 @@ func (cl *Client) WriteStatus(_w io.Writer) {
if addr := cl.ListenAddr(); addr != nil { if addr := cl.ListenAddr(); addr != nil {
fmt.Fprintf(w, "Listening on %s\n", cl.ListenAddr()) fmt.Fprintf(w, "Listening on %s\n", cl.ListenAddr())
} else { } else {
fmt.Println(w, "Not listening!") fmt.Fprintln(w, "Not listening!")
} }
fmt.Fprintf(w, "Peer ID: %q\n", cl.peerID) fmt.Fprintf(w, "Peer ID: %q\n", cl.peerID)
fmt.Fprintf(w, "Handshaking: %d\n", cl.handshaking) fmt.Fprintf(w, "Handshaking: %d\n", cl.handshaking)
@ -214,7 +214,6 @@ func (cl *Client) WriteStatus(_w io.Writer) {
fmt.Fprintf(w, "DHT announces: %d\n", cl.dHT.NumConfirmedAnnounces) fmt.Fprintf(w, "DHT announces: %d\n", cl.dHT.NumConfirmedAnnounces)
fmt.Fprintf(w, "Outstanding transactions: %d\n", dhtStats.NumOutstandingTransactions) fmt.Fprintf(w, "Outstanding transactions: %d\n", dhtStats.NumOutstandingTransactions)
} }
cl.downloadStrategy.WriteStatus(w)
fmt.Fprintln(w) fmt.Fprintln(w)
for _, t := range cl.sortedTorrents() { for _, t := range cl.sortedTorrents() {
if t.Name() == "" { if t.Name() == "" {
@ -249,7 +248,7 @@ func (cl *Client) torrentReadAt(t *torrent, off int64, p []byte) (n int, err err
return return
} }
pieceOff := pp.Integer(off % int64(t.usualPieceSize())) pieceOff := pp.Integer(off % int64(t.usualPieceSize()))
pieceLeft := int(t.PieceLength(pp.Integer(index)) - pieceOff) pieceLeft := int(t.PieceLength(index) - pieceOff)
if pieceLeft <= 0 { if pieceLeft <= 0 {
err = io.EOF err = io.EOF
return return
@ -491,10 +490,6 @@ func NewClient(cfg *Config) (cl *Client, err error) {
} }
} }
if cl.downloadStrategy == nil {
cl.downloadStrategy = &defaultDownloadStrategy{}
}
// Returns the laddr string to listen on for the next Listen call. // Returns the laddr string to listen on for the next Listen call.
listenAddr := func() string { listenAddr := func() string {
if addr := cl.ListenAddr(); addr != nil { if addr := cl.ListenAddr(); addr != nil {
@ -1043,7 +1038,6 @@ func (cl *Client) connCancel(t *torrent, cn *connection, r request) (ok bool) {
ok = cn.Cancel(r) ok = cn.Cancel(r)
if ok { if ok {
postedCancels.Add(1) postedCancels.Add(1)
cl.downloadStrategy.DeleteRequest(t, r)
} }
return return
} }
@ -1052,7 +1046,6 @@ func (cl *Client) connDeleteRequest(t *torrent, cn *connection, r request) {
if !cn.RequestPending(r) { if !cn.RequestPending(r) {
return return
} }
cl.downloadStrategy.DeleteRequest(t, r)
delete(cn.Requests, r) delete(cn.Requests, r)
} }
@ -1567,7 +1560,6 @@ func (cl *Client) startTorrent(t *torrent) {
} }
}() }()
} }
cl.downloadStrategy.TorrentStarted(t)
} }
// Storage cannot be changed once it's set. // Storage cannot be changed once it's set.
@ -1975,7 +1967,6 @@ func (me *Client) dropTorrent(infoHash InfoHash) (err error) {
panic(err) panic(err)
} }
delete(me.torrents, infoHash) delete(me.torrents, infoHash)
me.downloadStrategy.TorrentStopped(t)
return return
} }
@ -2106,7 +2097,6 @@ func (cl *Client) announceTorrentDHT(t *torrent, impliedPort bool) {
} }
ps.Close() ps.Close()
log.Printf("finished DHT peer scrape for %s: %d peers", t, len(allAddrs)) log.Printf("finished DHT peer scrape for %s: %d peers", t, len(allAddrs))
} }
} }
@ -2255,6 +2245,8 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er
defer me.replenishConnRequests(t, c) defer me.replenishConnRequests(t, c)
piece := t.Pieces[req.Index]
// Do we actually want this chunk? // Do we actually want this chunk?
if _, ok := t.Pieces[req.Index].PendingChunkSpecs[req.chunkSpec]; !ok { if _, ok := t.Pieces[req.Index].PendingChunkSpecs[req.chunkSpec]; !ok {
unusedDownloadedChunksCount.Add(1) unusedDownloadedChunksCount.Add(1)
@ -2272,17 +2264,14 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er
} }
// Record that we have the chunk. // Record that we have the chunk.
delete(t.Pieces[req.Index].PendingChunkSpecs, req.chunkSpec) delete(piece.PendingChunkSpecs, req.chunkSpec)
if len(t.Pieces[req.Index].PendingChunkSpecs) == 0 { if len(piece.PendingChunkSpecs) == 0 {
for _, c := range t.Conns { for _, c := range t.Conns {
c.pieceRequestOrder.DeletePiece(int(req.Index)) c.pieceRequestOrder.DeletePiece(int(req.Index))
} }
me.queuePieceCheck(t, req.Index) me.queuePieceCheck(t, req.Index)
} }
// Unprioritize the chunk.
me.downloadStrategy.TorrentGotChunk(t, req)
// Cancel pending requests for this chunk. // Cancel pending requests for this chunk.
for _, c := range t.Conns { for _, c := range t.Conns {
if me.connCancel(t, c, req) { if me.connCancel(t, c, req) {
@ -2290,8 +2279,6 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er
} }
} }
me.downloadStrategy.AssertNotRequested(t, req)
return nil return nil
} }
@ -2316,10 +2303,9 @@ func (me *Client) pieceHashed(t *torrent, piece pp.Integer, correct bool) {
p.PendingChunkSpecs = nil p.PendingChunkSpecs = nil
p.complete = true p.complete = true
p.Event.Broadcast() p.Event.Broadcast()
me.downloadStrategy.TorrentGotPiece(t, int(piece))
} else { } else {
if len(p.PendingChunkSpecs) == 0 { if len(p.PendingChunkSpecs) == 0 {
t.pendAllChunkSpecs(piece) t.pendAllChunkSpecs(int(piece))
} }
if p.Priority != piecePriorityNone { if p.Priority != piecePriorityNone {
me.openNewConns(t) me.openNewConns(t)
@ -2333,13 +2319,13 @@ func (me *Client) pieceHashed(t *torrent, piece pp.Integer, correct bool) {
}) })
// TODO: Cancel requests for this piece. // TODO: Cancel requests for this piece.
for r := range conn.Requests { for r := range conn.Requests {
if r.Index == piece { if int(r.Index) == piece {
panic("wat") panic("wat")
} }
} }
conn.pieceRequestOrder.DeletePiece(int(piece)) conn.pieceRequestOrder.DeletePiece(int(piece))
} }
if t.wantPiece(int(piece)) && conn.PeerHasPiece(piece) { if t.wantPiece(int(piece)) && conn.PeerHasPiece(pp.Integer(piece)) {
t.connPendPiece(conn, int(piece)) t.connPendPiece(conn, int(piece))
me.replenishConnRequests(t, conn) me.replenishConnRequests(t, conn)
} }

View File

@ -523,14 +523,14 @@ func (t *torrent) bytesLeft() (left int64) {
if !t.haveInfo() { if !t.haveInfo() {
return -1 return -1
} }
for i := pp.Integer(0); i < pp.Integer(t.numPieces()); i++ { for i := 0; i < t.numPieces(); i++ {
left += int64(t.PieceNumPendingBytes(i)) left += int64(t.PieceNumPendingBytes(i))
} }
return return
} }
func (t *torrent) piecePartiallyDownloaded(index int) bool { func (t *torrent) piecePartiallyDownloaded(index int) bool {
return t.PieceNumPendingBytes(pp.Integer(index)) != t.PieceLength(pp.Integer(index)) return t.PieceNumPendingBytes(index) != t.PieceLength(index)
} }
func numChunksForPiece(chunkSize int, pieceSize int) int { func numChunksForPiece(chunkSize int, pieceSize int) int {
@ -542,7 +542,7 @@ func (t *torrent) usualPieceSize() int {
} }
func (t *torrent) lastPieceSize() int { func (t *torrent) lastPieceSize() int {
return int(t.PieceLength(pp.Integer(t.numPieces() - 1))) return int(t.PieceLength(t.numPieces() - 1))
} }
func (t *torrent) numPieces() int { func (t *torrent) numPieces() int {
@ -634,9 +634,9 @@ func (t *torrent) bitfield() (bf []bool) {
} }
func (t *torrent) pieceChunks(piece int) (css []chunkSpec) { func (t *torrent) pieceChunks(piece int) (css []chunkSpec) {
css = make([]chunkSpec, 0, (t.PieceLength(pp.Integer(piece))+chunkSize-1)/chunkSize) css = make([]chunkSpec, 0, (t.PieceLength(piece)+chunkSize-1)/chunkSize)
var cs chunkSpec var cs chunkSpec
for left := t.PieceLength(pp.Integer(piece)); left != 0; left -= cs.Length { for left := t.PieceLength(piece); left != 0; left -= cs.Length {
cs.Length = left cs.Length = left
if cs.Length > chunkSize { if cs.Length > chunkSize {
cs.Length = chunkSize cs.Length = chunkSize
@ -647,7 +647,7 @@ func (t *torrent) pieceChunks(piece int) (css []chunkSpec) {
return return
} }
func (t *torrent) pendAllChunkSpecs(index pp.Integer) { func (t *torrent) pendAllChunkSpecs(index int) {
piece := t.Pieces[index] piece := t.Pieces[index]
if piece.PendingChunkSpecs == nil { if piece.PendingChunkSpecs == nil {
piece.PendingChunkSpecs = make( piece.PendingChunkSpecs = make(
@ -668,7 +668,7 @@ type Peer struct {
Source peerSource Source peerSource
} }
func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) { func (t *torrent) PieceLength(piece int) (len_ pp.Integer) {
if int(piece) == t.numPieces()-1 { if int(piece) == t.numPieces()-1 {
len_ = pp.Integer(t.Length() % t.Info.PieceLength) len_ = pp.Integer(t.Length() % t.Info.PieceLength)
} }