From 029d6939f8ef8ff94ff1c7dcc1f8336ac0dbe591 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 10 Feb 2015 00:12:29 +1100 Subject: [PATCH] Hide some methods that shouldn't be public --- client.go | 32 ++++++++++++++++++-------------- cmd/magnet-metainfo/main.go | 2 +- torrent.go | 22 ++++++++-------------- torrent_test.go | 2 +- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/client.go b/client.go index e4549eea..7e5f2b45 100644 --- a/client.go +++ b/client.go @@ -111,7 +111,7 @@ func (me *Client) PrioritizeDataRegion(ih InfoHash, off, len_ int64) error { firstIndex := int(off / int64(t.UsualPieceSize())) for i := 0; i < 5; i++ { index := firstIndex + i - if index >= t.NumPieces() { + if index >= t.numPieces() { continue } me.queueFirstHash(t, index) @@ -283,13 +283,13 @@ func (cl *Client) readRaisePiecePriorities(t *torrent, off, _len int64) { index := int(off / int64(t.UsualPieceSize())) cl.raisePiecePriority(t, index, piecePriorityNow) index++ - if index >= t.NumPieces() { + if index >= t.numPieces() { return } cl.raisePiecePriority(t, index, piecePriorityNext) for i := 0; i < t.numConnsUnchoked()-2; i++ { index++ - if index >= t.NumPieces() { + if index >= t.numPieces() { break } cl.raisePiecePriority(t, index, piecePriorityReadahead) @@ -523,7 +523,7 @@ func (me *Client) Stop() { } me.event.Broadcast() for _, t := range me.torrents { - t.Close() + t.close() } me.mu.Unlock() } @@ -957,9 +957,9 @@ func (t *torrent) initRequestOrdering(c *connection) { if c.pieceRequestOrder != nil || c.piecePriorities != nil { panic("double init of request ordering") } - c.piecePriorities = mathRand.Perm(t.NumPieces()) + c.piecePriorities = mathRand.Perm(t.numPieces()) c.pieceRequestOrder = pieceordering.New() - for i := 0; i < t.NumPieces(); i++ { + for i := 0; i < t.numPieces(); i++ { if !c.PeerHasPiece(pp.Integer(i)) { continue } @@ -973,7 +973,7 @@ func (t *torrent) initRequestOrdering(c *connection) { func (me *Client) peerGotPiece(t *torrent, c *connection, piece int) { if t.haveInfo() { if c.PeerPieces == nil { - c.PeerPieces = make([]bool, t.NumPieces()) + c.PeerPieces = make([]bool, t.numPieces()) } } else { for piece >= len(c.PeerPieces) { @@ -1206,11 +1206,11 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error { break } if t.haveInfo() { - if len(msg.Bitfield) < t.NumPieces() { + if len(msg.Bitfield) < t.numPieces() { err = errors.New("received invalid bitfield") break } - msg.Bitfield = msg.Bitfield[:t.NumPieces()] + msg.Bitfield = msg.Bitfield[:t.numPieces()] } c.PeerPieces = msg.Bitfield for index, has := range c.PeerPieces { @@ -1632,6 +1632,10 @@ type Torrent struct { *torrent } +func (t Torrent) NumPieces() int { + return t.numPieces() +} + func (t Torrent) Drop() { t.cl.dropTorrent(t.InfoHash) } @@ -1693,14 +1697,14 @@ func (t Torrent) AddPeers(pp []Peer) error { func (t Torrent) DownloadAll() { t.cl.mu.Lock() - for i := 0; i < t.NumPieces(); i++ { + for i := 0; i < t.numPieces(); i++ { // TODO: Leave higher priorities as they were? t.cl.prioritizePiece(t.torrent, i, piecePriorityNormal) } // Nice to have the first and last pieces soon for various interactive // purposes. t.cl.prioritizePiece(t.torrent, 0, piecePriorityReadahead) - t.cl.prioritizePiece(t.torrent, t.NumPieces()-1, piecePriorityReadahead) + t.cl.prioritizePiece(t.torrent, t.numPieces()-1, piecePriorityReadahead) t.cl.mu.Unlock() } @@ -1795,7 +1799,7 @@ func (me *Client) dropTorrent(infoHash InfoHash) (err error) { err = fmt.Errorf("no such torrent") return } - err = t.Close() + err = t.close() if err != nil { panic(err) } @@ -2039,7 +2043,7 @@ func (cl *Client) allTorrentsCompleted() bool { if !t.haveInfo() { return false } - if t.NumPiecesCompleted() != t.NumPieces() { + if t.NumPiecesCompleted() != t.numPieces() { return false } } @@ -2206,7 +2210,7 @@ func (me *Client) pieceHashed(t *torrent, piece pp.Integer, correct bool) { } } if t.haveAllPieces() && me.noUpload { - t.CeaseNetworking() + t.ceaseNetworking() } me.event.Broadcast() } diff --git a/cmd/magnet-metainfo/main.go b/cmd/magnet-metainfo/main.go index a356e802..fb2a5cec 100644 --- a/cmd/magnet-metainfo/main.go +++ b/cmd/magnet-metainfo/main.go @@ -29,7 +29,7 @@ func main() { defer wg.Done() <-t.GotMetainfo mi := t.MetaInfo() - t.Close() + t.Drop() f, err := os.Create(mi.Info.Name + ".torrent") if err != nil { log.Fatalf("error creating torrent metainfo file: %s", err) diff --git a/torrent.go b/torrent.go index 41b97368..a2901ba5 100644 --- a/torrent.go +++ b/torrent.go @@ -107,7 +107,7 @@ func (t *torrent) worstConnsHeap() (wcs *worstConns) { return } -func (t *torrent) CeaseNetworking() { +func (t *torrent) ceaseNetworking() { t.stateMu.Lock() defer t.stateMu.Unlock() select { @@ -191,7 +191,7 @@ func (t *torrent) setMetadata(md metainfo.Info, dataDir string, infoBytes []byte } for _, conn := range t.Conns { t.initRequestOrdering(conn) - if err := conn.setNumPieces(t.NumPieces()); err != nil { + if err := conn.setNumPieces(t.numPieces()); err != nil { log.Printf("closing connection: %s", err) conn.Close() } @@ -410,7 +410,7 @@ func (t *torrent) BytesLeft() (left int64) { if !t.haveInfo() { return -1 } - for i := pp.Integer(0); i < pp.Integer(t.NumPieces()); i++ { + for i := pp.Integer(0); i < pp.Integer(t.numPieces()); i++ { left += int64(t.PieceNumPendingBytes(i)) } return @@ -424,21 +424,15 @@ func NumChunksForPiece(chunkSize int, pieceSize int) int { return (pieceSize + chunkSize - 1) / chunkSize } -func (t *torrent) ChunkCount() (num int) { - num += (t.NumPieces() - 1) * NumChunksForPiece(chunkSize, int(t.PieceLength(0))) - num += NumChunksForPiece(chunkSize, int(t.PieceLength(pp.Integer(t.NumPieces()-1)))) - return -} - func (t *torrent) UsualPieceSize() int { return int(t.Info.PieceLength) } func (t *torrent) LastPieceSize() int { - return int(t.PieceLength(pp.Integer(t.NumPieces() - 1))) + return int(t.PieceLength(pp.Integer(t.numPieces() - 1))) } -func (t *torrent) NumPieces() int { +func (t *torrent) numPieces() int { return len(t.Info.Pieces) / 20 } @@ -464,11 +458,11 @@ func (t *torrent) isClosed() bool { } } -func (t *torrent) Close() (err error) { +func (t *torrent) close() (err error) { if t.isClosed() { return } - t.CeaseNetworking() + t.ceaseNetworking() close(t.closing) t.dataLock.Lock() if t.Data != nil { @@ -565,7 +559,7 @@ type Peer struct { } func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) { - if int(piece) == t.NumPieces()-1 { + if int(piece) == t.numPieces()-1 { len_ = pp.Integer(t.Length() % t.Info.PieceLength) } if len_ == 0 { diff --git a/torrent_test.go b/torrent_test.go index ffe43ca7..ab92429b 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -52,7 +52,7 @@ func TestTorrentDoubleClose(t *testing.T) { for i := 0; i < 2; i++ { wg.Add(1) go func() { - tt.Close() + tt.close() wg.Done() }() }