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