]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Readers obtained from File.NewReader should not readahead into other Files
[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         t.pendPieceRange(begin, end)
153 }
154
155 func (t *Torrent) CancelPieces(begin, end int) {
156         t.cl.mu.Lock()
157         defer t.cl.mu.Unlock()
158         t.unpendPieceRange(begin, end)
159 }
160
161 // Returns handles to the files in the torrent. This requires the metainfo is
162 // available first.
163 func (t *Torrent) Files() (ret []File) {
164         info := t.Info()
165         if info == nil {
166                 return
167         }
168         var offset int64
169         for _, fi := range info.UpvertedFiles() {
170                 ret = append(ret, File{
171                         t,
172                         strings.Join(append([]string{info.Name}, fi.Path...), "/"),
173                         offset,
174                         fi.Length,
175                         fi,
176                 })
177                 offset += fi.Length
178         }
179         return
180 }
181
182 func (t *Torrent) AddPeers(pp []Peer) {
183         cl := t.cl
184         cl.mu.Lock()
185         defer cl.mu.Unlock()
186         t.addPeers(pp)
187 }
188
189 // Marks the entire torrent for download. Requires the info first, see
190 // GotInfo.
191 func (t *Torrent) DownloadAll() {
192         t.cl.mu.Lock()
193         defer t.cl.mu.Unlock()
194         t.pendPieceRange(0, t.numPieces())
195 }
196
197 func (t *Torrent) String() string {
198         s := t.name()
199         if s == "" {
200                 s = t.infoHash.HexString()
201         }
202         return s
203 }
204
205 func (t *Torrent) AddTrackers(announceList [][]string) {
206         t.cl.mu.Lock()
207         defer t.cl.mu.Unlock()
208         t.addTrackers(announceList)
209 }
210
211 func (t *Torrent) Piece(i int) *Piece {
212         t.cl.mu.Lock()
213         defer t.cl.mu.Unlock()
214         return &t.pieces[i]
215 }