]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Add separate piece priorities, and cache pending pieces
[btrtrc.git] / t.go
1 package torrent
2
3 import (
4         "strings"
5
6         "github.com/anacrolix/missinggo/pubsub"
7
8         "github.com/anacrolix/torrent/metainfo"
9 )
10
11 // The torrent's infohash. This is fixed and cannot change. It uniquely
12 // identifies a torrent.
13 func (t *Torrent) InfoHash() metainfo.Hash {
14         return t.infoHash
15 }
16
17 // Returns a channel that is closed when the info (.Info()) for the torrent
18 // has become available.
19 func (t *Torrent) GotInfo() <-chan struct{} {
20         t.cl.mu.Lock()
21         defer t.cl.mu.Unlock()
22         return t.gotMetainfo.C()
23 }
24
25 // Returns the metainfo info dictionary, or nil if it's not yet available.
26 func (t *Torrent) Info() *metainfo.Info {
27         t.cl.mu.Lock()
28         defer t.cl.mu.Unlock()
29         return t.info
30 }
31
32 // Returns a Reader bound to the torrent's data. All read calls block until
33 // the data requested is actually available.
34 func (t *Torrent) NewReader() Reader {
35         r := reader{
36                 mu:        &t.cl.mu,
37                 t:         t,
38                 readahead: 5 * 1024 * 1024,
39                 length:    *t.length,
40         }
41         t.addReader(&r)
42         return &r
43 }
44
45 // Returns the state of pieces of the torrent. They are grouped into runs of
46 // same state. The sum of the state run lengths is the number of pieces
47 // in the torrent.
48 func (t *Torrent) PieceStateRuns() []PieceStateRun {
49         t.cl.mu.Lock()
50         defer t.cl.mu.Unlock()
51         return t.pieceStateRuns()
52 }
53
54 func (t *Torrent) PieceState(piece int) PieceState {
55         t.cl.mu.Lock()
56         defer t.cl.mu.Unlock()
57         return t.pieceState(piece)
58 }
59
60 // The number of pieces in the torrent. This requires that the info has been
61 // obtained first.
62 func (t *Torrent) NumPieces() int {
63         return t.numPieces()
64 }
65
66 // Get missing bytes count for specific piece.
67 func (t *Torrent) PieceBytesMissing(piece int) int64 {
68         t.cl.mu.Lock()
69         defer t.cl.mu.Unlock()
70
71         return int64(t.pieces[piece].bytesLeft())
72 }
73
74 // Drop the torrent from the client, and close it. It's always safe to do
75 // this. No data corruption can, or should occur to either the torrent's data,
76 // or connected peers.
77 func (t *Torrent) Drop() {
78         t.cl.mu.Lock()
79         t.cl.dropTorrent(t.infoHash)
80         t.cl.mu.Unlock()
81 }
82
83 // Number of bytes of the entire torrent we have completed.
84 func (t *Torrent) BytesCompleted() int64 {
85         t.cl.mu.RLock()
86         defer t.cl.mu.RUnlock()
87         return t.bytesCompleted()
88 }
89
90 // The subscription emits as (int) the index of pieces as their state changes.
91 // A state change is when the PieceState for a piece alters in value.
92 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
93         return t.pieceStateChanges.Subscribe()
94 }
95
96 // Returns true if the torrent is currently being seeded. This occurs when the
97 // client is willing to upload without wanting anything in return.
98 func (t *Torrent) Seeding() bool {
99         t.cl.mu.Lock()
100         defer t.cl.mu.Unlock()
101         return t.seeding()
102 }
103
104 // Clobbers the torrent display name. The display name is used as the torrent
105 // name if the metainfo is not available.
106 func (t *Torrent) SetDisplayName(dn string) {
107         t.cl.mu.Lock()
108         defer t.cl.mu.Unlock()
109         t.setDisplayName(dn)
110 }
111
112 // The current working name for the torrent. Either the name in the info dict,
113 // or a display name given such as by the dn value in a magnet link, or "".
114 func (t *Torrent) Name() string {
115         t.cl.mu.Lock()
116         defer t.cl.mu.Unlock()
117         return t.name()
118 }
119
120 // The completed length of all the torrent data, in all its files. This is
121 // derived from the torrent info, when it is available.
122 func (t *Torrent) Length() int64 {
123         return *t.length
124 }
125
126 // Returns a run-time generated metainfo for the torrent that includes the
127 // info bytes and announce-list as currently known to the client.
128 func (t *Torrent) Metainfo() metainfo.MetaInfo {
129         t.cl.mu.Lock()
130         defer t.cl.mu.Unlock()
131         return t.newMetaInfo()
132 }
133
134 func (t *Torrent) addReader(r *reader) {
135         t.cl.mu.Lock()
136         defer t.cl.mu.Unlock()
137         if t.readers == nil {
138                 t.readers = make(map[*reader]struct{})
139         }
140         t.readers[r] = struct{}{}
141         r.posChanged()
142 }
143
144 func (t *Torrent) deleteReader(r *reader) {
145         delete(t.readers, r)
146         t.readersChanged()
147 }
148
149 func (t *Torrent) DownloadPieces(begin, end int) {
150         t.cl.mu.Lock()
151         defer t.cl.mu.Unlock()
152         for i := begin; i < end; i++ {
153                 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
154                         t.updatePiecePriority(i)
155                 }
156         }
157 }
158
159 func (t *Torrent) CancelPieces(begin, end int) {
160         t.cl.mu.Lock()
161         defer t.cl.mu.Unlock()
162         for i := begin; i < end; i++ {
163                 p := &t.pieces[i]
164                 if p.priority == PiecePriorityNone {
165                         continue
166                 }
167                 p.priority = PiecePriorityNone
168                 t.updatePiecePriority(i)
169         }
170 }
171
172 func (t *Torrent) initFiles() {
173         var offset int64
174         t.files = new([]*File)
175         for _, fi := range t.info.UpvertedFiles() {
176                 *t.files = append(*t.files, &File{
177                         t,
178                         strings.Join(append([]string{t.info.Name}, fi.Path...), "/"),
179                         offset,
180                         fi.Length,
181                         fi,
182                         PiecePriorityNone,
183                 })
184                 offset += fi.Length
185         }
186
187 }
188
189 // Returns handles to the files in the torrent. This requires that the Info is
190 // available first.
191 func (t *Torrent) Files() []*File {
192         return *t.files
193 }
194
195 func (t *Torrent) AddPeers(pp []Peer) {
196         cl := t.cl
197         cl.mu.Lock()
198         defer cl.mu.Unlock()
199         t.addPeers(pp)
200 }
201
202 // Marks the entire torrent for download. Requires the info first, see
203 // GotInfo. Sets piece priorities for historical reasons.
204 func (t *Torrent) DownloadAll() {
205         t.DownloadPieces(0, t.numPieces())
206 }
207
208 func (t *Torrent) String() string {
209         s := t.name()
210         if s == "" {
211                 s = t.infoHash.HexString()
212         }
213         return s
214 }
215
216 func (t *Torrent) AddTrackers(announceList [][]string) {
217         t.cl.mu.Lock()
218         defer t.cl.mu.Unlock()
219         t.addTrackers(announceList)
220 }
221
222 func (t *Torrent) Piece(i int) *Piece {
223         t.cl.mu.Lock()
224         defer t.cl.mu.Unlock()
225         return &t.pieces[i]
226 }