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