]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Fix handling of infohash as hex in Torrent.String
[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         return t.info
28 }
29
30 // Returns a Reader bound to the torrent's data. All read calls block until
31 // the data requested is actually available.
32 func (t *Torrent) NewReader() (ret *Reader) {
33         ret = &Reader{
34                 mu:        &t.cl.mu,
35                 t:         t,
36                 readahead: 5 * 1024 * 1024,
37         }
38         t.addReader(ret)
39         return
40 }
41
42 // Returns the state of pieces of the torrent. They are grouped into runs of
43 // same state. The sum of the state run lengths is the number of pieces
44 // in the torrent.
45 func (t *Torrent) PieceStateRuns() []PieceStateRun {
46         t.cl.mu.Lock()
47         defer t.cl.mu.Unlock()
48         return t.pieceStateRuns()
49 }
50
51 func (t *Torrent) PieceState(piece int) PieceState {
52         t.cl.mu.Lock()
53         defer t.cl.mu.Unlock()
54         return t.pieceState(piece)
55 }
56
57 // The number of pieces in the torrent. This requires that the info has been
58 // obtained first.
59 func (t *Torrent) NumPieces() int {
60         return t.numPieces()
61 }
62
63 // Get missing bytes count for specific piece.
64 func (t *Torrent) PieceBytesMissing(piece int) int64 {
65         t.cl.mu.Lock()
66         defer t.cl.mu.Unlock()
67
68         return int64(t.pieces[piece].bytesLeft())
69 }
70
71 // Drop the torrent from the client, and close it. It's always safe to do
72 // this. No data corruption can, or should occur to either the torrent's data,
73 // or connected peers.
74 func (t *Torrent) Drop() {
75         t.cl.mu.Lock()
76         t.cl.dropTorrent(t.infoHash)
77         t.cl.mu.Unlock()
78 }
79
80 // Number of bytes of the entire torrent we have completed.
81 func (t *Torrent) BytesCompleted() int64 {
82         t.cl.mu.RLock()
83         defer t.cl.mu.RUnlock()
84         return t.bytesCompleted()
85 }
86
87 // The subscription emits as (int) the index of pieces as their state changes.
88 // A state change is when the PieceState for a piece alters in value.
89 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
90         return t.pieceStateChanges.Subscribe()
91 }
92
93 // Returns true if the torrent is currently being seeded. This occurs when the
94 // client is willing to upload without wanting anything in return.
95 func (t *Torrent) Seeding() bool {
96         t.cl.mu.Lock()
97         defer t.cl.mu.Unlock()
98         return t.seeding()
99 }
100
101 // Clobbers the torrent display name. The display name is used as the torrent
102 // name if the metainfo is not available.
103 func (t *Torrent) SetDisplayName(dn string) {
104         t.cl.mu.Lock()
105         defer t.cl.mu.Unlock()
106         t.setDisplayName(dn)
107 }
108
109 // The current working name for the torrent. Either the name in the info dict,
110 // or a display name given such as by the dn value in a magnet link, or "".
111 func (t *Torrent) Name() string {
112         t.cl.mu.Lock()
113         defer t.cl.mu.Unlock()
114         return t.name()
115 }
116
117 // The completed length of all the torrent data, in all its files. This is
118 // derived from the torrent info, when it is available.
119 func (t *Torrent) Length() int64 {
120         if t.info == nil {
121                 panic("not valid until info obtained")
122         }
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         t.cl.mu.Lock()
165         info := t.Info()
166         t.cl.mu.Unlock()
167         if info == nil {
168                 return
169         }
170         var offset int64
171         for _, fi := range info.UpvertedFiles() {
172                 ret = append(ret, File{
173                         t,
174                         strings.Join(append([]string{info.Name}, fi.Path...), "/"),
175                         offset,
176                         fi.Length,
177                         fi,
178                 })
179                 offset += fi.Length
180         }
181         return
182 }
183
184 func (t *Torrent) AddPeers(pp []Peer) {
185         cl := t.cl
186         cl.mu.Lock()
187         defer cl.mu.Unlock()
188         t.addPeers(pp)
189 }
190
191 // Marks the entire torrent for download. Requires the info first, see
192 // GotInfo.
193 func (t *Torrent) DownloadAll() {
194         t.cl.mu.Lock()
195         defer t.cl.mu.Unlock()
196         t.pendPieceRange(0, t.numPieces())
197 }
198
199 func (t *Torrent) String() string {
200         s := t.name()
201         if s == "" {
202                 s = t.infoHash.HexString()
203         }
204         return s
205 }
206
207 func (t *Torrent) AddTrackers(announceList [][]string) {
208         t.cl.mu.Lock()
209         defer t.cl.mu.Unlock()
210         t.addTrackers(announceList)
211 }