]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Merge pull request #54 from zhulik/master
[btrtrc.git] / t.go
1 package torrent
2
3 import (
4         "github.com/anacrolix/missinggo/pubsub"
5         "github.com/anacrolix/torrent/metainfo"
6         "github.com/anacrolix/torrent/tracker"
7 )
8
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.
11
12 // The public interface to a live torrent within a Client.
13 type Torrent interface {
14         InfoHash() InfoHash
15         GotInfo() <-chan struct{}
16         Info() *metainfo.Info
17         NewReader() (ret *Reader)
18         PieceStateRuns() []PieceStateRun
19         NumPieces() int
20         Drop()
21         BytesCompleted() int64
22         SubscribePieceStateChanges() *pubsub.Subscription
23         Seeding() bool
24         SetDisplayName(dn string)
25         Client() *Client
26         AddPeers(pp []Peer) error
27         DownloadAll()
28         Trackers() [][]tracker.Client
29         Files() (ret []File)
30         Peers() map[PeersKey]Peer
31 }
32
33
34 type clientTorrent struct {
35         cl *Client
36         *torrent
37 }
38
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
43 }
44
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
50 }
51
52 // Returns the metainfo, or nil if it's not yet available.
53 func (t clientTorrent) Info() *metainfo.Info {
54         return t.torrent.Info
55 }
56
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) {
61         ret = &Reader{
62                 t:         &t,
63                 readahead: 5 * 1024 * 1024,
64         }
65         return
66 }
67
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
70 // in the torrent.
71 func (t clientTorrent) PieceStateRuns() []PieceStateRun {
72         t.stateMu.Lock()
73         defer t.stateMu.Unlock()
74         return t.torrent.pieceStateRuns()
75 }
76
77 func (t clientTorrent) NumPieces() int {
78         return t.numPieces()
79 }
80
81 // Drop the torrent from the client, and close it.
82 func (t clientTorrent) Drop() {
83         t.cl.mu.Lock()
84         t.cl.dropTorrent(t.torrent.InfoHash)
85         t.cl.mu.Unlock()
86 }
87
88 // Number of bytes of the entire torrent we have completed.
89 func (t clientTorrent) BytesCompleted() int64 {
90         t.cl.mu.RLock()
91         defer t.cl.mu.RUnlock()
92         return t.bytesCompleted()
93 }
94
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()
99 }
100
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 {
104         t.cl.mu.Lock()
105         defer t.cl.mu.Unlock()
106         return t.cl.seeding(t.torrent)
107 }
108
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) {
112         t.cl.mu.Lock()
113         defer t.cl.mu.Unlock()
114         t.torrent.setDisplayName(dn)
115 }
116
117 // Client returns Torrent's client instance
118 func (t clientTorrent) Client() *Client {
119         return t.cl
120 }
121
122 // Trackers returns torrent's trackers
123 func (t clientTorrent) Trackers() [][]tracker.Client {
124         return t.torrent.Trackers
125 }
126
127 // Peers returns torrent's peers
128 func (t clientTorrent) Peers() map[PeersKey]Peer {
129         return t.torrent.Peers
130 }