]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Change the behaviour of Torrent.Length with when info isn't available
[btrtrc.git] / t.go
1 package torrent
2
3 import (
4         "github.com/anacrolix/missinggo/pubsub"
5
6         "github.com/anacrolix/torrent/metainfo"
7 )
8
9 // The torrent's infohash. This is fixed and cannot change. It uniquely
10 // identifies a torrent.
11 func (t *Torrent) InfoHash() metainfo.Hash {
12         return t.infoHash
13 }
14
15 // Returns a channel that is closed when the info (.Info()) for the torrent
16 // has become available.
17 func (t *Torrent) GotInfo() <-chan struct{} {
18         return t.gotMetainfo
19 }
20
21 // Returns the metainfo info dictionary, or nil if it's not yet available.
22 func (t *Torrent) Info() *metainfo.InfoEx {
23         return t.info
24 }
25
26 // Returns a Reader bound to the torrent's data. All read calls block until
27 // the data requested is actually available.
28 func (t *Torrent) NewReader() (ret *Reader) {
29         ret = &Reader{
30                 t:         t,
31                 readahead: 5 * 1024 * 1024,
32         }
33         t.addReader(ret)
34         return
35 }
36
37 // Returns the state of pieces of the torrent. They are grouped into runs of
38 // same state. The sum of the state run lengths is the number of pieces
39 // in the torrent.
40 func (t *Torrent) PieceStateRuns() []PieceStateRun {
41         t.cl.mu.Lock()
42         defer t.cl.mu.Unlock()
43         return t.pieceStateRuns()
44 }
45
46 func (t *Torrent) PieceState(piece int) PieceState {
47         t.cl.mu.Lock()
48         defer t.cl.mu.Unlock()
49         return t.pieceState(piece)
50 }
51
52 // The number of pieces in the torrent. This requires that the info has been
53 // obtained first.
54 func (t *Torrent) NumPieces() int {
55         return t.numPieces()
56 }
57
58 // Drop the torrent from the client, and close it. It's always safe to do
59 // this. No data corruption can, or should occur to either the torrent's data,
60 // or connected peers.
61 func (t *Torrent) Drop() {
62         t.cl.mu.Lock()
63         t.cl.dropTorrent(t.infoHash)
64         t.cl.mu.Unlock()
65 }
66
67 // Number of bytes of the entire torrent we have completed.
68 func (t *Torrent) BytesCompleted() int64 {
69         t.cl.mu.RLock()
70         defer t.cl.mu.RUnlock()
71         return t.bytesCompleted()
72 }
73
74 // The subscription emits as (int) the index of pieces as their state changes.
75 // A state change is when the PieceState for a piece alters in value.
76 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
77         return t.pieceStateChanges.Subscribe()
78 }
79
80 // Returns true if the torrent is currently being seeded. This occurs when the
81 // client is willing to upload without wanting anything in return.
82 func (t *Torrent) Seeding() bool {
83         t.cl.mu.Lock()
84         defer t.cl.mu.Unlock()
85         return t.cl.seeding(t)
86 }
87
88 // Clobbers the torrent display name. The display name is used as the torrent
89 // name if the metainfo is not available.
90 func (t *Torrent) SetDisplayName(dn string) {
91         t.cl.mu.Lock()
92         defer t.cl.mu.Unlock()
93         t.setDisplayName(dn)
94 }
95
96 // The current working name for the torrent. Either the name in the info dict,
97 // or a display name given such as by the dn value in a magnet link, or "".
98 func (t *Torrent) Name() string {
99         t.cl.mu.Lock()
100         defer t.cl.mu.Unlock()
101         return t.name()
102 }
103
104 // The completed length of all the torrent data, in all its files. This is
105 // derived from the torrent info, when it is available.
106 func (t *Torrent) Length() int64 {
107         if t.info == nil {
108                 panic("not valid until info obtained")
109         }
110         return t.length
111 }
112
113 // Returns a run-time generated metainfo for the torrent that includes the
114 // info bytes and announce-list as currently known to the client.
115 func (t *Torrent) Metainfo() *metainfo.MetaInfo {
116         t.cl.mu.Lock()
117         defer t.cl.mu.Unlock()
118         return t.metainfo()
119 }
120
121 func (t *Torrent) addReader(r *Reader) {
122         t.cl.mu.Lock()
123         defer t.cl.mu.Unlock()
124         if t.readers == nil {
125                 t.readers = make(map[*Reader]struct{})
126         }
127         t.readers[r] = struct{}{}
128         t.readersChanged()
129 }
130
131 func (t *Torrent) deleteReader(r *Reader) {
132         t.cl.mu.Lock()
133         defer t.cl.mu.Unlock()
134         delete(t.readers, r)
135         t.readersChanged()
136 }
137
138 func (t *Torrent) DownloadPieces(begin, end int) {
139         t.cl.mu.Lock()
140         defer t.cl.mu.Unlock()
141         t.pendPieceRange(begin, end)
142 }
143
144 func (t *Torrent) CancelPieces(begin, end int) {
145         t.cl.mu.Lock()
146         defer t.cl.mu.Unlock()
147         t.unpendPieceRange(begin, end)
148 }