2015-04-14 13:59:41 +00:00
|
|
|
package torrent
|
|
|
|
|
2015-04-28 05:24:17 +00:00
|
|
|
import (
|
2015-09-06 02:33:22 +00:00
|
|
|
"github.com/anacrolix/missinggo/pubsub"
|
2016-01-06 01:19:49 +00:00
|
|
|
|
2015-04-28 05:24:17 +00:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
2015-09-06 02:31:23 +00:00
|
|
|
// This file contains Torrent, until I decide where the private, lower-case
|
|
|
|
// "torrent" type belongs. That type is currently mostly in torrent.go.
|
2015-04-14 13:59:41 +00:00
|
|
|
|
2016-01-06 01:19:49 +00:00
|
|
|
// The public handle to a live torrent within a Client.
|
|
|
|
type Torrent struct {
|
2016-01-16 13:14:15 +00:00
|
|
|
cl *Client
|
|
|
|
torrent *torrent
|
2015-04-29 14:30:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 02:31:23 +00:00
|
|
|
// The torrent's infohash. This is fixed and cannot change. It uniquely
|
|
|
|
// identifies a torrent.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) InfoHash() InfoHash {
|
2015-08-01 17:55:48 +00:00
|
|
|
return t.torrent.InfoHash
|
|
|
|
}
|
|
|
|
|
2015-04-29 14:30:19 +00:00
|
|
|
// Closed when the info (.Info()) for the torrent has become available. Using
|
|
|
|
// features of Torrent that require the info before it is available will have
|
|
|
|
// undefined behaviour.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) GotInfo() <-chan struct{} {
|
2015-04-29 14:30:19 +00:00
|
|
|
return t.torrent.gotMetainfo
|
2015-04-28 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 02:31:23 +00:00
|
|
|
// Returns the metainfo, or nil if it's not yet available.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) Info() *metainfo.Info {
|
2015-04-28 05:24:17 +00:00
|
|
|
return t.torrent.Info
|
2015-04-14 13:59:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:30:55 +00:00
|
|
|
// Returns a Reader bound to the torrent's data. All read calls block until
|
|
|
|
// the data requested is actually available. Priorities are set to ensure the
|
|
|
|
// data requested will be downloaded as soon as possible.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) NewReader() (ret *Reader) {
|
2015-04-14 13:59:41 +00:00
|
|
|
ret = &Reader{
|
2015-12-12 03:03:25 +00:00
|
|
|
t: &t,
|
2015-04-14 13:59:41 +00:00
|
|
|
readahead: 5 * 1024 * 1024,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2015-06-01 08:22:12 +00:00
|
|
|
|
|
|
|
// Returns the state of pieces of the torrent. They are grouped into runs of
|
|
|
|
// same state. The sum of the state run lengths is the number of pieces
|
|
|
|
// in the torrent.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) PieceStateRuns() []PieceStateRun {
|
2016-01-16 13:14:15 +00:00
|
|
|
t.torrent.stateMu.Lock()
|
|
|
|
defer t.torrent.stateMu.Unlock()
|
2015-06-01 08:22:12 +00:00
|
|
|
return t.torrent.pieceStateRuns()
|
|
|
|
}
|
2015-06-22 16:02:22 +00:00
|
|
|
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) NumPieces() int {
|
2016-01-16 13:14:15 +00:00
|
|
|
return t.torrent.numPieces()
|
2015-06-22 16:02:22 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 02:31:23 +00:00
|
|
|
// Drop the torrent from the client, and close it.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) Drop() {
|
2015-06-22 16:02:22 +00:00
|
|
|
t.cl.mu.Lock()
|
2015-08-01 17:55:48 +00:00
|
|
|
t.cl.dropTorrent(t.torrent.InfoHash)
|
2015-06-22 16:02:22 +00:00
|
|
|
t.cl.mu.Unlock()
|
|
|
|
}
|
2015-07-21 12:54:02 +00:00
|
|
|
|
2015-09-06 02:31:23 +00:00
|
|
|
// Number of bytes of the entire torrent we have completed.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) BytesCompleted() int64 {
|
2015-07-21 12:54:02 +00:00
|
|
|
t.cl.mu.RLock()
|
|
|
|
defer t.cl.mu.RUnlock()
|
2016-01-16 13:14:15 +00:00
|
|
|
return t.torrent.bytesCompleted()
|
2015-07-21 12:54:02 +00:00
|
|
|
}
|
2015-09-06 02:33:22 +00:00
|
|
|
|
2015-12-12 03:00:07 +00:00
|
|
|
// The subscription emits as (int) the index of pieces as their state changes.
|
|
|
|
// A state change is when the PieceState for a piece alters in value.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
|
2015-09-06 02:33:22 +00:00
|
|
|
return t.torrent.pieceStateChanges.Subscribe()
|
|
|
|
}
|
2015-11-22 07:44:33 +00:00
|
|
|
|
2015-12-12 03:00:07 +00:00
|
|
|
// Returns true if the torrent is currently being seeded. This occurs when the
|
|
|
|
// client is willing to upload without wanting anything in return.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) Seeding() bool {
|
2015-11-22 07:44:33 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
|
|
|
return t.cl.seeding(t.torrent)
|
|
|
|
}
|
2015-12-12 03:03:04 +00:00
|
|
|
|
|
|
|
// Clobbers the torrent display name. The display name is used as the torrent
|
|
|
|
// name if the metainfo is not available.
|
2016-01-06 01:19:49 +00:00
|
|
|
func (t Torrent) SetDisplayName(dn string) {
|
2015-12-12 03:03:04 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
|
|
|
t.torrent.setDisplayName(dn)
|
|
|
|
}
|
2016-01-16 13:14:15 +00:00
|
|
|
|
|
|
|
func (t Torrent) Name() string {
|
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
|
|
|
return t.torrent.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Torrent) Length() int64 {
|
|
|
|
select {
|
|
|
|
case <-t.GotInfo():
|
|
|
|
return t.torrent.length
|
|
|
|
default:
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 14:49:04 +00:00
|
|
|
|
|
|
|
// Returns a run-time generated metainfo for the torrent that includes the
|
|
|
|
// info bytes and announce-list as currently known to the client.
|
|
|
|
func (t Torrent) MetaInfo() *metainfo.MetaInfo {
|
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
|
|
|
return t.torrent.MetaInfo()
|
|
|
|
}
|