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