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