]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Rename connection->PeerConn and fix exports
[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
32 // the data requested is actually available.
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 // Returns the state of pieces of the torrent. They are grouped into runs of
45 // same state. The sum of the state run lengths is the number of pieces
46 // in the torrent.
47 func (t *Torrent) PieceStateRuns() []PieceStateRun {
48         t.cl.rLock()
49         defer t.cl.rUnlock()
50         return t.pieceStateRuns()
51 }
52
53 func (t *Torrent) PieceState(piece pieceIndex) PieceState {
54         t.cl.rLock()
55         defer t.cl.rUnlock()
56         return t.pieceState(piece)
57 }
58
59 // The number of pieces in the torrent. This requires that the info has been
60 // obtained first.
61 func (t *Torrent) NumPieces() pieceIndex {
62         return t.numPieces()
63 }
64
65 // Get missing bytes count for specific piece.
66 func (t *Torrent) PieceBytesMissing(piece int) int64 {
67         t.cl.lock()
68         defer t.cl.unlock()
69
70         return int64(t.pieces[piece].bytesLeft())
71 }
72
73 // Drop the torrent from the client, and close it. It's always safe to do
74 // this. No data corruption can, or should occur to either the torrent's data,
75 // or connected peers.
76 func (t *Torrent) Drop() {
77         t.cl.lock()
78         t.cl.dropTorrent(t.infoHash)
79         t.cl.unlock()
80 }
81
82 // Number of bytes of the entire torrent we have completed. This is the sum of
83 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
84 // for download rate, as it can go down when pieces are lost or fail checks.
85 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
86 func (t *Torrent) BytesCompleted() int64 {
87         t.cl.rLock()
88         defer t.cl.rUnlock()
89         return t.bytesCompleted()
90 }
91
92 // The subscription emits as (int) the index of pieces as their state changes.
93 // A state change is when the PieceState for a piece alters in value.
94 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
95         return t.pieceStateChanges.Subscribe()
96 }
97
98 // Returns true if the torrent is currently being seeded. This occurs when the
99 // client is willing to upload without wanting anything in return.
100 func (t *Torrent) Seeding() bool {
101         t.cl.lock()
102         defer t.cl.unlock()
103         return t.seeding()
104 }
105
106 // Clobbers the torrent display name. The display name is used as the torrent
107 // name if the metainfo is not available.
108 func (t *Torrent) SetDisplayName(dn string) {
109         t.nameMu.Lock()
110         defer t.nameMu.Unlock()
111         if t.haveInfo() {
112                 return
113         }
114         t.displayName = dn
115 }
116
117 // The current working name for the torrent. Either the name in the info dict,
118 // or a display name given such as by the dn value in a magnet link, or "".
119 func (t *Torrent) Name() string {
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.lock()
133         defer t.cl.unlock()
134         return t.newMetaInfo()
135 }
136
137 func (t *Torrent) addReader(r *reader) {
138         t.cl.lock()
139         defer t.cl.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.lock()
157         defer t.cl.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.lock()
171         defer t.cl.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                 var path []string
191                 if len(fi.PathUTF8) != 0 {
192                         path = fi.PathUTF8
193                 } else {
194                         path = fi.Path
195                 }
196                 *t.files = append(*t.files, &File{
197                         t,
198                         strings.Join(append([]string{t.info.Name}, path...), "/"),
199                         offset,
200                         fi.Length,
201                         fi,
202                         PiecePriorityNone,
203                 })
204                 offset += fi.Length
205         }
206
207 }
208
209 // Returns handles to the files in the torrent. This requires that the Info is
210 // available first.
211 func (t *Torrent) Files() []*File {
212         return *t.files
213 }
214
215 func (t *Torrent) AddPeers(pp []Peer) {
216         cl := t.cl
217         cl.lock()
218         defer cl.unlock()
219         t.addPeers(pp)
220 }
221
222 // Marks the entire torrent for download. Requires the info first, see
223 // GotInfo. Sets piece priorities for historical reasons.
224 func (t *Torrent) DownloadAll() {
225         t.DownloadPieces(0, t.numPieces())
226 }
227
228 func (t *Torrent) String() string {
229         s := t.name()
230         if s == "" {
231                 return t.infoHash.HexString()
232         } else {
233                 return strconv.Quote(s)
234         }
235 }
236
237 func (t *Torrent) AddTrackers(announceList [][]string) {
238         t.cl.lock()
239         defer t.cl.unlock()
240         t.addTrackers(announceList)
241 }
242
243 func (t *Torrent) Piece(i pieceIndex) *Piece {
244         return t.piece(i)
245 }
246
247 func (t *Torrent) PeerConns() []*PeerConn {
248         ret := make([]*PeerConn, 0, len(t.conns))
249         for c := range t.conns {
250                 ret = append(ret, c)
251         }
252         return ret
253 }