2015-04-14 21:59:41 +08:00
|
|
|
package torrent
|
|
|
|
|
2015-04-28 13:24:17 +08:00
|
|
|
import (
|
2015-09-06 10:33:22 +08:00
|
|
|
"github.com/anacrolix/missinggo/pubsub"
|
2015-04-28 13:24:17 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2016-01-05 16:42:26 +08:00
|
|
|
"github.com/anacrolix/torrent/tracker"
|
2015-04-28 13:24:17 +08:00
|
|
|
)
|
|
|
|
|
2015-09-06 10:31:23 +08: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 21:59:41 +08:00
|
|
|
|
2016-01-05 16:42:26 +08:00
|
|
|
// The public interface to a live torrent within a Client.
|
|
|
|
type Torrent interface {
|
|
|
|
InfoHash() InfoHash
|
|
|
|
GotInfo() <-chan struct{}
|
|
|
|
Info() *metainfo.Info
|
|
|
|
NewReader() (ret *Reader)
|
|
|
|
PieceStateRuns() []PieceStateRun
|
|
|
|
NumPieces() int
|
|
|
|
Drop()
|
|
|
|
BytesCompleted() int64
|
|
|
|
SubscribePieceStateChanges() *pubsub.Subscription
|
|
|
|
Seeding() bool
|
|
|
|
SetDisplayName(dn string)
|
|
|
|
Client() *Client
|
|
|
|
AddPeers(pp []Peer) error
|
|
|
|
DownloadAll()
|
|
|
|
Trackers() [][]tracker.Client
|
|
|
|
Files() (ret []File)
|
|
|
|
Peers() map[PeersKey]Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type clientTorrent struct {
|
2015-04-14 21:59:41 +08:00
|
|
|
cl *Client
|
|
|
|
*torrent
|
2015-04-29 22:30:19 +08:00
|
|
|
}
|
|
|
|
|
2015-09-06 10:31:23 +08:00
|
|
|
// The torrent's infohash. This is fixed and cannot change. It uniquely
|
|
|
|
// identifies a torrent.
|
2016-01-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) InfoHash() InfoHash {
|
2015-08-02 01:55:48 +08:00
|
|
|
return t.torrent.InfoHash
|
|
|
|
}
|
|
|
|
|
2015-04-29 22:30:19 +08: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-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) GotInfo() <-chan struct{} {
|
2015-04-29 22:30:19 +08:00
|
|
|
return t.torrent.gotMetainfo
|
2015-04-28 13:24:17 +08:00
|
|
|
}
|
|
|
|
|
2015-09-06 10:31:23 +08:00
|
|
|
// Returns the metainfo, or nil if it's not yet available.
|
2016-01-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) Info() *metainfo.Info {
|
2015-04-28 13:24:17 +08:00
|
|
|
return t.torrent.Info
|
2015-04-14 21:59:41 +08:00
|
|
|
}
|
|
|
|
|
2015-06-03 11:30:55 +08: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-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) NewReader() (ret *Reader) {
|
2015-04-14 21:59:41 +08:00
|
|
|
ret = &Reader{
|
2015-12-12 11:03:25 +08:00
|
|
|
t: &t,
|
2015-04-14 21:59:41 +08:00
|
|
|
readahead: 5 * 1024 * 1024,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2015-06-01 16:22:12 +08: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-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) PieceStateRuns() []PieceStateRun {
|
2015-06-01 16:22:12 +08:00
|
|
|
t.stateMu.Lock()
|
|
|
|
defer t.stateMu.Unlock()
|
|
|
|
return t.torrent.pieceStateRuns()
|
|
|
|
}
|
2015-06-23 00:02:22 +08:00
|
|
|
|
2016-01-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) NumPieces() int {
|
2015-06-23 00:02:22 +08:00
|
|
|
return t.numPieces()
|
|
|
|
}
|
|
|
|
|
2015-09-06 10:31:23 +08:00
|
|
|
// Drop the torrent from the client, and close it.
|
2016-01-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) Drop() {
|
2015-06-23 00:02:22 +08:00
|
|
|
t.cl.mu.Lock()
|
2015-08-02 01:55:48 +08:00
|
|
|
t.cl.dropTorrent(t.torrent.InfoHash)
|
2015-06-23 00:02:22 +08:00
|
|
|
t.cl.mu.Unlock()
|
|
|
|
}
|
2015-07-21 20:54:02 +08:00
|
|
|
|
2015-09-06 10:31:23 +08:00
|
|
|
// Number of bytes of the entire torrent we have completed.
|
2016-01-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) BytesCompleted() int64 {
|
2015-07-21 20:54:02 +08:00
|
|
|
t.cl.mu.RLock()
|
|
|
|
defer t.cl.mu.RUnlock()
|
|
|
|
return t.bytesCompleted()
|
|
|
|
}
|
2015-09-06 10:33:22 +08:00
|
|
|
|
2015-12-12 11:00:07 +08: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-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) SubscribePieceStateChanges() *pubsub.Subscription {
|
2015-09-06 10:33:22 +08:00
|
|
|
return t.torrent.pieceStateChanges.Subscribe()
|
|
|
|
}
|
2015-11-22 15:44:33 +08:00
|
|
|
|
2015-12-12 11:00:07 +08: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-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) Seeding() bool {
|
2015-11-22 15:44:33 +08:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
|
|
|
return t.cl.seeding(t.torrent)
|
|
|
|
}
|
2015-12-12 11:03:04 +08:00
|
|
|
|
|
|
|
// Clobbers the torrent display name. The display name is used as the torrent
|
|
|
|
// name if the metainfo is not available.
|
2016-01-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) SetDisplayName(dn string) {
|
2015-12-12 11:03:04 +08:00
|
|
|
t.cl.mu.Lock()
|
|
|
|
defer t.cl.mu.Unlock()
|
|
|
|
t.torrent.setDisplayName(dn)
|
|
|
|
}
|
2015-12-27 19:49:15 +08:00
|
|
|
|
|
|
|
// Client returns Torrent's client instance
|
2016-01-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) Client() *Client {
|
2015-12-27 19:49:15 +08:00
|
|
|
return t.cl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trackers returns torrent's trackers
|
2016-01-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) Trackers() [][]tracker.Client {
|
2015-12-27 19:49:15 +08:00
|
|
|
return t.torrent.Trackers
|
2015-12-27 20:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Peers returns torrent's peers
|
2016-01-05 16:42:26 +08:00
|
|
|
func (t clientTorrent) Peers() map[PeersKey]Peer {
|
2015-12-27 20:19:39 +08:00
|
|
|
return t.torrent.Peers
|
2016-01-05 16:42:26 +08:00
|
|
|
}
|