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