]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Change pieceIndex to peer_protocol.Integer
[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 pieceIndex) 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() pieceIndex {
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. This is the sum of
84 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
85 // for download rate, as it can go down when pieces are lost or fail checks.
86 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
87 func (t *Torrent) BytesCompleted() int64 {
88         t.cl.mu.RLock()
89         defer t.cl.mu.RUnlock()
90         return t.bytesCompleted()
91 }
92
93 // The subscription emits as (int) the index of pieces as their state changes.
94 // A state change is when the PieceState for a piece alters in value.
95 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
96         return t.pieceStateChanges.Subscribe()
97 }
98
99 // Returns true if the torrent is currently being seeded. This occurs when the
100 // client is willing to upload without wanting anything in return.
101 func (t *Torrent) Seeding() bool {
102         t.cl.mu.Lock()
103         defer t.cl.mu.Unlock()
104         return t.seeding()
105 }
106
107 // Clobbers the torrent display name. The display name is used as the torrent
108 // name if the metainfo is not available.
109 func (t *Torrent) SetDisplayName(dn string) {
110         t.cl.mu.Lock()
111         defer t.cl.mu.Unlock()
112         t.setDisplayName(dn)
113 }
114
115 // The current working name for the torrent. Either the name in the info dict,
116 // or a display name given such as by the dn value in a magnet link, or "".
117 func (t *Torrent) Name() string {
118         t.cl.mu.Lock()
119         defer t.cl.mu.Unlock()
120         return t.name()
121 }
122
123 // The completed length of all the torrent data, in all its files. This is
124 // derived from the torrent info, when it is available.
125 func (t *Torrent) Length() int64 {
126         return *t.length
127 }
128
129 // Returns a run-time generated metainfo for the torrent that includes the
130 // info bytes and announce-list as currently known to the client.
131 func (t *Torrent) Metainfo() metainfo.MetaInfo {
132         t.cl.mu.Lock()
133         defer t.cl.mu.Unlock()
134         return t.newMetaInfo()
135 }
136
137 func (t *Torrent) addReader(r *reader) {
138         t.cl.mu.Lock()
139         defer t.cl.mu.Unlock()
140         if t.readers == nil {
141                 t.readers = make(map[*reader]struct{})
142         }
143         t.readers[r] = struct{}{}
144         r.posChanged()
145 }
146
147 func (t *Torrent) deleteReader(r *reader) {
148         delete(t.readers, r)
149         t.readersChanged()
150 }
151
152 // Raise the priorities of pieces in the range [begin, end) to at least Normal
153 // priority. Piece indexes are not the same as bytes. Requires that the info
154 // has been obtained, see Torrent.Info and Torrent.GotInfo.
155 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
156         t.cl.mu.Lock()
157         defer t.cl.mu.Unlock()
158         t.downloadPiecesLocked(begin, end)
159 }
160
161 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
162         for i := begin; i < end; i++ {
163                 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
164                         t.updatePiecePriority(i)
165                 }
166         }
167 }
168
169 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
170         t.cl.mu.Lock()
171         defer t.cl.mu.Unlock()
172         t.cancelPiecesLocked(begin, end)
173 }
174
175 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
176         for i := begin; i < end; i++ {
177                 p := &t.pieces[i]
178                 if p.priority == PiecePriorityNone {
179                         continue
180                 }
181                 p.priority = PiecePriorityNone
182                 t.updatePiecePriority(i)
183         }
184 }
185
186 func (t *Torrent) initFiles() {
187         var offset int64
188         t.files = new([]*File)
189         for _, fi := range t.info.UpvertedFiles() {
190                 *t.files = append(*t.files, &File{
191                         t,
192                         strings.Join(append([]string{t.info.Name}, fi.Path...), "/"),
193                         offset,
194                         fi.Length,
195                         fi,
196                         PiecePriorityNone,
197                 })
198                 offset += fi.Length
199         }
200
201 }
202
203 // Returns handles to the files in the torrent. This requires that the Info is
204 // available first.
205 func (t *Torrent) Files() []*File {
206         return *t.files
207 }
208
209 func (t *Torrent) AddPeers(pp []Peer) {
210         cl := t.cl
211         cl.mu.Lock()
212         defer cl.mu.Unlock()
213         t.addPeers(pp)
214 }
215
216 // Marks the entire torrent for download. Requires the info first, see
217 // GotInfo. Sets piece priorities for historical reasons.
218 func (t *Torrent) DownloadAll() {
219         t.DownloadPieces(0, t.numPieces())
220 }
221
222 func (t *Torrent) String() string {
223         s := t.name()
224         if s == "" {
225                 s = t.infoHash.HexString()
226         }
227         return s
228 }
229
230 func (t *Torrent) AddTrackers(announceList [][]string) {
231         t.cl.mu.Lock()
232         defer t.cl.mu.Unlock()
233         t.addTrackers(announceList)
234 }
235
236 func (t *Torrent) Piece(i pieceIndex) *Piece {
237         t.cl.mu.Lock()
238         defer t.cl.mu.Unlock()
239         return &t.pieces[i]
240 }