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