2015-04-14 13:59:41 +00:00
|
|
|
package torrent
|
|
|
|
|
2015-04-28 05:24:17 +00:00
|
|
|
import (
|
2016-05-03 06:47:11 +00:00
|
|
|
"strings"
|
|
|
|
|
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
|
|
|
// The torrent's infohash. This is fixed and cannot change. It uniquely
|
|
|
|
// identifies a torrent.
|
2016-04-04 03:01:31 +00:00
|
|
|
func (t *Torrent) InfoHash() metainfo.Hash {
|
2016-04-03 08:40:43 +00:00
|
|
|
return t.infoHash
|
2015-08-01 17:55:48 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:10:10 +00:00
|
|
|
// Returns a channel that is closed when the info (.Info()) for the torrent
|
|
|
|
// has become available.
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) GotInfo() <-chan struct{} {
|
2016-05-09 06:44:06 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-05-09 05:47:39 +00:00
|
|
|
return t.gotMetainfo.C()
|
2015-04-28 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 14:49:34 +00:00
|
|
|
// Returns the metainfo info dictionary, or nil if it's not yet available.
|
2016-08-26 10:29:05 +00:00
|
|
|
func (t *Torrent) Info() *metainfo.Info {
|
2017-08-29 05:16:53 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-04-03 08:40:43 +00:00
|
|
|
return t.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
|
2016-01-28 06:22:10 +00:00
|
|
|
// the data requested is actually available.
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) NewReader() (ret *Reader) {
|
2015-04-14 13:59:41 +00:00
|
|
|
ret = &Reader{
|
2016-08-30 05:41:26 +00:00
|
|
|
mu: &t.cl.mu,
|
2016-04-03 08:40:43 +00:00
|
|
|
t: t,
|
2015-04-14 13:59:41 +00:00
|
|
|
readahead: 5 * 1024 * 1024,
|
|
|
|
}
|
2016-01-18 07:35:14 +00:00
|
|
|
t.addReader(ret)
|
2015-04-14 13:59:41 +00:00
|
|
|
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-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) PieceStateRuns() []PieceStateRun {
|
2016-02-20 16:31:50 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-04-03 08:40:43 +00:00
|
|
|
return t.pieceStateRuns()
|
2015-06-01 08:22:12 +00:00
|
|
|
}
|
2015-06-22 16:02:22 +00:00
|
|
|
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) PieceState(piece int) PieceState {
|
2016-02-20 16:31:50 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-04-03 08:40:43 +00:00
|
|
|
return t.pieceState(piece)
|
2016-02-07 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 14:49:34 +00:00
|
|
|
// The number of pieces in the torrent. This requires that the info has been
|
|
|
|
// obtained first.
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) NumPieces() int {
|
|
|
|
return t.numPieces()
|
2015-06-22 16:02:22 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 04:46:28 +00:00
|
|
|
// Get missing bytes count for specific piece.
|
|
|
|
func (t *Torrent) PieceBytesMissing(piece int) int64 {
|
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
|
|
|
|
|
|
|
return int64(t.pieces[piece].bytesLeft())
|
|
|
|
}
|
|
|
|
|
2016-04-18 11:52:30 +00:00
|
|
|
// Drop the torrent from the client, and close it. It's always safe to do
|
|
|
|
// this. No data corruption can, or should occur to either the torrent's data,
|
|
|
|
// or connected peers.
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) Drop() {
|
2015-06-22 16:02:22 +00:00
|
|
|
t.cl.mu.Lock()
|
2016-04-03 08:40:43 +00:00
|
|
|
t.cl.dropTorrent(t.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-04-03 08:40:43 +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-04-03 08:40:43 +00:00
|
|
|
return t.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-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
|
|
|
|
return t.pieceStateChanges.Subscribe()
|
2015-09-06 02:33:22 +00:00
|
|
|
}
|
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-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) Seeding() bool {
|
2015-11-22 07:44:33 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-05-11 11:44:55 +00:00
|
|
|
return t.seeding()
|
2015-11-22 07:44:33 +00:00
|
|
|
}
|
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-04-03 08:40:43 +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()
|
2016-04-03 08:40:43 +00:00
|
|
|
t.setDisplayName(dn)
|
2015-12-12 03:03:04 +00:00
|
|
|
}
|
2016-01-16 13:14:15 +00:00
|
|
|
|
2016-01-16 14:49:34 +00:00
|
|
|
// The current working name for the torrent. Either the name in the info dict,
|
|
|
|
// or a display name given such as by the dn value in a magnet link, or "".
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) Name() string {
|
2016-01-16 13:14:15 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-04-03 08:40:43 +00:00
|
|
|
return t.name()
|
2016-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:10:10 +00:00
|
|
|
// The completed length of all the torrent data, in all its files. This is
|
|
|
|
// derived from the torrent info, when it is available.
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) Length() int64 {
|
2016-04-20 10:10:39 +00:00
|
|
|
if t.info == nil {
|
|
|
|
panic("not valid until info obtained")
|
2016-01-16 13:14:15 +00:00
|
|
|
}
|
2016-04-20 10:10:39 +00:00
|
|
|
return t.length
|
2016-01-16 13:14:15 +00:00
|
|
|
}
|
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.
|
2016-08-26 10:29:05 +00:00
|
|
|
func (t *Torrent) Metainfo() metainfo.MetaInfo {
|
2016-01-16 14:49:04 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-05-22 12:45:08 +00:00
|
|
|
return t.newMetaInfo()
|
2016-01-16 14:49:04 +00:00
|
|
|
}
|
2016-01-18 07:35:14 +00:00
|
|
|
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) addReader(r *Reader) {
|
2016-01-18 07:35:14 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-04-03 08:40:43 +00:00
|
|
|
if t.readers == nil {
|
|
|
|
t.readers = make(map[*Reader]struct{})
|
2016-01-18 07:35:14 +00:00
|
|
|
}
|
2016-04-03 08:40:43 +00:00
|
|
|
t.readers[r] = struct{}{}
|
2016-10-31 08:00:08 +00:00
|
|
|
r.posChanged()
|
2016-01-18 07:35:14 +00:00
|
|
|
}
|
|
|
|
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) deleteReader(r *Reader) {
|
|
|
|
delete(t.readers, r)
|
|
|
|
t.readersChanged()
|
2016-01-18 07:35:14 +00:00
|
|
|
}
|
2016-01-18 14:28:56 +00:00
|
|
|
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) DownloadPieces(begin, end int) {
|
2016-01-18 14:28:56 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-04-03 08:40:43 +00:00
|
|
|
t.pendPieceRange(begin, end)
|
2016-02-04 14:18:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) CancelPieces(begin, end int) {
|
2016-02-04 14:18:54 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2016-04-03 08:40:43 +00:00
|
|
|
t.unpendPieceRange(begin, end)
|
2016-02-20 03:39:56 +00:00
|
|
|
}
|
2016-05-03 06:47:11 +00:00
|
|
|
|
|
|
|
// Returns handles to the files in the torrent. This requires the metainfo is
|
|
|
|
// available first.
|
|
|
|
func (t *Torrent) Files() (ret []File) {
|
|
|
|
info := t.Info()
|
|
|
|
if info == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var offset int64
|
|
|
|
for _, fi := range info.UpvertedFiles() {
|
|
|
|
ret = append(ret, File{
|
|
|
|
t,
|
|
|
|
strings.Join(append([]string{info.Name}, fi.Path...), "/"),
|
|
|
|
offset,
|
|
|
|
fi.Length,
|
|
|
|
fi,
|
|
|
|
})
|
|
|
|
offset += fi.Length
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-12 02:26:09 +00:00
|
|
|
func (t *Torrent) AddPeers(pp []Peer) {
|
2016-05-03 06:47:11 +00:00
|
|
|
cl := t.cl
|
|
|
|
cl.mu.Lock()
|
|
|
|
defer cl.mu.Unlock()
|
2016-06-15 05:29:47 +00:00
|
|
|
t.addPeers(pp)
|
2016-05-03 06:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marks the entire torrent for download. Requires the info first, see
|
|
|
|
// GotInfo.
|
|
|
|
func (t *Torrent) DownloadAll() {
|
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
|
|
|
t.pendPieceRange(0, t.numPieces())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Torrent) String() string {
|
|
|
|
s := t.name()
|
|
|
|
if s == "" {
|
2017-07-01 06:02:06 +00:00
|
|
|
s = t.infoHash.HexString()
|
2016-05-03 06:47:11 +00:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2016-05-23 00:18:58 +00:00
|
|
|
|
2017-02-15 07:40:30 +00:00
|
|
|
func (t *Torrent) AddTrackers(announceList [][]string) {
|
2016-05-23 00:18:58 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
2017-02-15 07:40:30 +00:00
|
|
|
t.addTrackers(announceList)
|
2016-05-23 00:18:58 +00:00
|
|
|
}
|
2017-09-15 09:22:32 +00:00
|
|
|
|
2017-09-15 09:35:16 +00:00
|
|
|
func (t *Torrent) Piece(i int) *Piece {
|
2017-09-15 09:22:32 +00:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
|
|
|
return &t.pieces[i]
|
|
|
|
}
|