4 "github.com/anacrolix/missinggo/pubsub"
5 "github.com/anacrolix/torrent/metainfo"
6 "github.com/anacrolix/torrent/tracker"
9 // This file contains Torrent, until I decide where the private, lower-case
10 // "torrent" type belongs. That type is currently mostly in torrent.go.
12 // The public interface to a live torrent within a Client.
13 type Torrent interface {
15 GotInfo() <-chan struct{}
17 NewReader() (ret *Reader)
18 PieceStateRuns() []PieceStateRun
21 BytesCompleted() int64
22 SubscribePieceStateChanges() *pubsub.Subscription
24 SetDisplayName(dn string)
26 AddPeers(pp []Peer) error
28 Trackers() [][]tracker.Client
30 Peers() map[PeersKey]Peer
34 type clientTorrent struct {
39 // The torrent's infohash. This is fixed and cannot change. It uniquely
40 // identifies a torrent.
41 func (t clientTorrent) InfoHash() InfoHash {
42 return t.torrent.InfoHash
45 // Closed when the info (.Info()) for the torrent has become available. Using
46 // features of Torrent that require the info before it is available will have
47 // undefined behaviour.
48 func (t clientTorrent) GotInfo() <-chan struct{} {
49 return t.torrent.gotMetainfo
52 // Returns the metainfo, or nil if it's not yet available.
53 func (t clientTorrent) Info() *metainfo.Info {
57 // Returns a Reader bound to the torrent's data. All read calls block until
58 // the data requested is actually available. Priorities are set to ensure the
59 // data requested will be downloaded as soon as possible.
60 func (t clientTorrent) NewReader() (ret *Reader) {
63 readahead: 5 * 1024 * 1024,
68 // Returns the state of pieces of the torrent. They are grouped into runs of
69 // same state. The sum of the state run lengths is the number of pieces
71 func (t clientTorrent) PieceStateRuns() []PieceStateRun {
73 defer t.stateMu.Unlock()
74 return t.torrent.pieceStateRuns()
77 func (t clientTorrent) NumPieces() int {
81 // Drop the torrent from the client, and close it.
82 func (t clientTorrent) Drop() {
84 t.cl.dropTorrent(t.torrent.InfoHash)
88 // Number of bytes of the entire torrent we have completed.
89 func (t clientTorrent) BytesCompleted() int64 {
91 defer t.cl.mu.RUnlock()
92 return t.bytesCompleted()
95 // The subscription emits as (int) the index of pieces as their state changes.
96 // A state change is when the PieceState for a piece alters in value.
97 func (t clientTorrent) SubscribePieceStateChanges() *pubsub.Subscription {
98 return t.torrent.pieceStateChanges.Subscribe()
101 // Returns true if the torrent is currently being seeded. This occurs when the
102 // client is willing to upload without wanting anything in return.
103 func (t clientTorrent) Seeding() bool {
105 defer t.cl.mu.Unlock()
106 return t.cl.seeding(t.torrent)
109 // Clobbers the torrent display name. The display name is used as the torrent
110 // name if the metainfo is not available.
111 func (t clientTorrent) SetDisplayName(dn string) {
113 defer t.cl.mu.Unlock()
114 t.torrent.setDisplayName(dn)
117 // Client returns Torrent's client instance
118 func (t clientTorrent) Client() *Client {
122 // Trackers returns torrent's trackers
123 func (t clientTorrent) Trackers() [][]tracker.Client {
124 return t.torrent.Trackers
127 // Peers returns torrent's peers
128 func (t clientTorrent) Peers() map[PeersKey]Peer {
129 return t.torrent.Peers