]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Merge Torrent and torrent types
[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.InfoHash {
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.
60 func (t *Torrent) Drop() {
61         t.cl.mu.Lock()
62         t.cl.dropTorrent(t.infoHash)
63         t.cl.mu.Unlock()
64 }
65
66 // Number of bytes of the entire torrent we have completed.
67 func (t *Torrent) BytesCompleted() int64 {
68         t.cl.mu.RLock()
69         defer t.cl.mu.RUnlock()
70         return t.bytesCompleted()
71 }
72
73 // The subscription emits as (int) the index of pieces as their state changes.
74 // A state change is when the PieceState for a piece alters in value.
75 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
76         return t.pieceStateChanges.Subscribe()
77 }
78
79 // Returns true if the torrent is currently being seeded. This occurs when the
80 // client is willing to upload without wanting anything in return.
81 func (t *Torrent) Seeding() bool {
82         t.cl.mu.Lock()
83         defer t.cl.mu.Unlock()
84         return t.cl.seeding(t)
85 }
86
87 // Clobbers the torrent display name. The display name is used as the torrent
88 // name if the metainfo is not available.
89 func (t *Torrent) SetDisplayName(dn string) {
90         t.cl.mu.Lock()
91         defer t.cl.mu.Unlock()
92         t.setDisplayName(dn)
93 }
94
95 // The current working name for the torrent. Either the name in the info dict,
96 // or a display name given such as by the dn value in a magnet link, or "".
97 func (t *Torrent) Name() string {
98         t.cl.mu.Lock()
99         defer t.cl.mu.Unlock()
100         return t.name()
101 }
102
103 func (t *Torrent) Length() int64 {
104         select {
105         case <-t.GotInfo():
106                 return t.length
107         default:
108                 return -1
109         }
110 }
111
112 // Returns a run-time generated metainfo for the torrent that includes the
113 // info bytes and announce-list as currently known to the client.
114 func (t *Torrent) Metainfo() *metainfo.MetaInfo {
115         t.cl.mu.Lock()
116         defer t.cl.mu.Unlock()
117         return t.metainfo()
118 }
119
120 func (t *Torrent) addReader(r *Reader) {
121         t.cl.mu.Lock()
122         defer t.cl.mu.Unlock()
123         if t.readers == nil {
124                 t.readers = make(map[*Reader]struct{})
125         }
126         t.readers[r] = struct{}{}
127         t.readersChanged()
128 }
129
130 func (t *Torrent) deleteReader(r *Reader) {
131         t.cl.mu.Lock()
132         defer t.cl.mu.Unlock()
133         delete(t.readers, r)
134         t.readersChanged()
135 }
136
137 func (t *Torrent) DownloadPieces(begin, end int) {
138         t.cl.mu.Lock()
139         defer t.cl.mu.Unlock()
140         t.pendPieceRange(begin, end)
141 }
142
143 func (t *Torrent) CancelPieces(begin, end int) {
144         t.cl.mu.Lock()
145         defer t.cl.mu.Unlock()
146         t.unpendPieceRange(begin, end)
147 }