Hide some methods that shouldn't be public

This commit is contained in:
Matt Joiner 2015-02-10 00:12:29 +11:00
parent c3a592ac39
commit 029d6939f8
4 changed files with 28 additions and 30 deletions

View File

@ -111,7 +111,7 @@ func (me *Client) PrioritizeDataRegion(ih InfoHash, off, len_ int64) error {
firstIndex := int(off / int64(t.UsualPieceSize())) firstIndex := int(off / int64(t.UsualPieceSize()))
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
index := firstIndex + i index := firstIndex + i
if index >= t.NumPieces() { if index >= t.numPieces() {
continue continue
} }
me.queueFirstHash(t, index) me.queueFirstHash(t, index)
@ -283,13 +283,13 @@ func (cl *Client) readRaisePiecePriorities(t *torrent, off, _len int64) {
index := int(off / int64(t.UsualPieceSize())) index := int(off / int64(t.UsualPieceSize()))
cl.raisePiecePriority(t, index, piecePriorityNow) cl.raisePiecePriority(t, index, piecePriorityNow)
index++ index++
if index >= t.NumPieces() { if index >= t.numPieces() {
return return
} }
cl.raisePiecePriority(t, index, piecePriorityNext) cl.raisePiecePriority(t, index, piecePriorityNext)
for i := 0; i < t.numConnsUnchoked()-2; i++ { for i := 0; i < t.numConnsUnchoked()-2; i++ {
index++ index++
if index >= t.NumPieces() { if index >= t.numPieces() {
break break
} }
cl.raisePiecePriority(t, index, piecePriorityReadahead) cl.raisePiecePriority(t, index, piecePriorityReadahead)
@ -523,7 +523,7 @@ func (me *Client) Stop() {
} }
me.event.Broadcast() me.event.Broadcast()
for _, t := range me.torrents { for _, t := range me.torrents {
t.Close() t.close()
} }
me.mu.Unlock() me.mu.Unlock()
} }
@ -957,9 +957,9 @@ func (t *torrent) initRequestOrdering(c *connection) {
if c.pieceRequestOrder != nil || c.piecePriorities != nil { if c.pieceRequestOrder != nil || c.piecePriorities != nil {
panic("double init of request ordering") panic("double init of request ordering")
} }
c.piecePriorities = mathRand.Perm(t.NumPieces()) c.piecePriorities = mathRand.Perm(t.numPieces())
c.pieceRequestOrder = pieceordering.New() c.pieceRequestOrder = pieceordering.New()
for i := 0; i < t.NumPieces(); i++ { for i := 0; i < t.numPieces(); i++ {
if !c.PeerHasPiece(pp.Integer(i)) { if !c.PeerHasPiece(pp.Integer(i)) {
continue continue
} }
@ -973,7 +973,7 @@ func (t *torrent) initRequestOrdering(c *connection) {
func (me *Client) peerGotPiece(t *torrent, c *connection, piece int) { func (me *Client) peerGotPiece(t *torrent, c *connection, piece int) {
if t.haveInfo() { if t.haveInfo() {
if c.PeerPieces == nil { if c.PeerPieces == nil {
c.PeerPieces = make([]bool, t.NumPieces()) c.PeerPieces = make([]bool, t.numPieces())
} }
} else { } else {
for piece >= len(c.PeerPieces) { for piece >= len(c.PeerPieces) {
@ -1206,11 +1206,11 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error {
break break
} }
if t.haveInfo() { if t.haveInfo() {
if len(msg.Bitfield) < t.NumPieces() { if len(msg.Bitfield) < t.numPieces() {
err = errors.New("received invalid bitfield") err = errors.New("received invalid bitfield")
break break
} }
msg.Bitfield = msg.Bitfield[:t.NumPieces()] msg.Bitfield = msg.Bitfield[:t.numPieces()]
} }
c.PeerPieces = msg.Bitfield c.PeerPieces = msg.Bitfield
for index, has := range c.PeerPieces { for index, has := range c.PeerPieces {
@ -1632,6 +1632,10 @@ type Torrent struct {
*torrent *torrent
} }
func (t Torrent) NumPieces() int {
return t.numPieces()
}
func (t Torrent) Drop() { func (t Torrent) Drop() {
t.cl.dropTorrent(t.InfoHash) t.cl.dropTorrent(t.InfoHash)
} }
@ -1693,14 +1697,14 @@ func (t Torrent) AddPeers(pp []Peer) error {
func (t Torrent) DownloadAll() { func (t Torrent) DownloadAll() {
t.cl.mu.Lock() 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? // TODO: Leave higher priorities as they were?
t.cl.prioritizePiece(t.torrent, i, piecePriorityNormal) t.cl.prioritizePiece(t.torrent, i, piecePriorityNormal)
} }
// Nice to have the first and last pieces soon for various interactive // Nice to have the first and last pieces soon for various interactive
// purposes. // purposes.
t.cl.prioritizePiece(t.torrent, 0, piecePriorityReadahead) 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() t.cl.mu.Unlock()
} }
@ -1795,7 +1799,7 @@ func (me *Client) dropTorrent(infoHash InfoHash) (err error) {
err = fmt.Errorf("no such torrent") err = fmt.Errorf("no such torrent")
return return
} }
err = t.Close() err = t.close()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -2039,7 +2043,7 @@ func (cl *Client) allTorrentsCompleted() bool {
if !t.haveInfo() { if !t.haveInfo() {
return false return false
} }
if t.NumPiecesCompleted() != t.NumPieces() { if t.NumPiecesCompleted() != t.numPieces() {
return false return false
} }
} }
@ -2206,7 +2210,7 @@ func (me *Client) pieceHashed(t *torrent, piece pp.Integer, correct bool) {
} }
} }
if t.haveAllPieces() && me.noUpload { if t.haveAllPieces() && me.noUpload {
t.CeaseNetworking() t.ceaseNetworking()
} }
me.event.Broadcast() me.event.Broadcast()
} }

View File

@ -29,7 +29,7 @@ func main() {
defer wg.Done() defer wg.Done()
<-t.GotMetainfo <-t.GotMetainfo
mi := t.MetaInfo() mi := t.MetaInfo()
t.Close() t.Drop()
f, err := os.Create(mi.Info.Name + ".torrent") f, err := os.Create(mi.Info.Name + ".torrent")
if err != nil { if err != nil {
log.Fatalf("error creating torrent metainfo file: %s", err) log.Fatalf("error creating torrent metainfo file: %s", err)

View File

@ -107,7 +107,7 @@ func (t *torrent) worstConnsHeap() (wcs *worstConns) {
return return
} }
func (t *torrent) CeaseNetworking() { func (t *torrent) ceaseNetworking() {
t.stateMu.Lock() t.stateMu.Lock()
defer t.stateMu.Unlock() defer t.stateMu.Unlock()
select { select {
@ -191,7 +191,7 @@ func (t *torrent) setMetadata(md metainfo.Info, dataDir string, infoBytes []byte
} }
for _, conn := range t.Conns { for _, conn := range t.Conns {
t.initRequestOrdering(conn) 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) log.Printf("closing connection: %s", err)
conn.Close() conn.Close()
} }
@ -410,7 +410,7 @@ 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 := pp.Integer(0); i < pp.Integer(t.numPieces()); i++ {
left += int64(t.PieceNumPendingBytes(i)) left += int64(t.PieceNumPendingBytes(i))
} }
return return
@ -424,21 +424,15 @@ func NumChunksForPiece(chunkSize int, pieceSize int) int {
return (pieceSize + chunkSize - 1) / chunkSize 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 { func (t *torrent) UsualPieceSize() int {
return int(t.Info.PieceLength) return int(t.Info.PieceLength)
} }
func (t *torrent) LastPieceSize() int { 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 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() { if t.isClosed() {
return return
} }
t.CeaseNetworking() t.ceaseNetworking()
close(t.closing) close(t.closing)
t.dataLock.Lock() t.dataLock.Lock()
if t.Data != nil { if t.Data != nil {
@ -565,7 +559,7 @@ type Peer struct {
} }
func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) { 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) len_ = pp.Integer(t.Length() % t.Info.PieceLength)
} }
if len_ == 0 { if len_ == 0 {

View File

@ -52,7 +52,7 @@ func TestTorrentDoubleClose(t *testing.T) {
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
wg.Add(1) wg.Add(1)
go func() { go func() {
tt.Close() tt.close()
wg.Done() wg.Done()
}() }()
} }