]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Reintroduce connection piece inclinations, and begin caching piece priorities
[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.
38 func (t Torrent) NewReader() (ret *Reader) {
39         ret = &Reader{
40                 t:         &t,
41                 readahead: 5 * 1024 * 1024,
42         }
43         t.addReader(ret)
44         return
45 }
46
47 // Returns the state of pieces of the torrent. They are grouped into runs of
48 // same state. The sum of the state run lengths is the number of pieces
49 // in the torrent.
50 func (t Torrent) PieceStateRuns() []PieceStateRun {
51         t.torrent.stateMu.Lock()
52         defer t.torrent.stateMu.Unlock()
53         return t.torrent.pieceStateRuns()
54 }
55
56 // The number of pieces in the torrent. This requires that the info has been
57 // obtained first.
58 func (t Torrent) NumPieces() int {
59         return t.torrent.numPieces()
60 }
61
62 // Drop the torrent from the client, and close it.
63 func (t Torrent) Drop() {
64         t.cl.mu.Lock()
65         t.cl.dropTorrent(t.torrent.InfoHash)
66         t.cl.mu.Unlock()
67 }
68
69 // Number of bytes of the entire torrent we have completed.
70 func (t Torrent) BytesCompleted() int64 {
71         t.cl.mu.RLock()
72         defer t.cl.mu.RUnlock()
73         return t.torrent.bytesCompleted()
74 }
75
76 // The subscription emits as (int) the index of pieces as their state changes.
77 // A state change is when the PieceState for a piece alters in value.
78 func (t Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
79         return t.torrent.pieceStateChanges.Subscribe()
80 }
81
82 // Returns true if the torrent is currently being seeded. This occurs when the
83 // client is willing to upload without wanting anything in return.
84 func (t Torrent) Seeding() bool {
85         t.cl.mu.Lock()
86         defer t.cl.mu.Unlock()
87         return t.cl.seeding(t.torrent)
88 }
89
90 // Clobbers the torrent display name. The display name is used as the torrent
91 // name if the metainfo is not available.
92 func (t Torrent) SetDisplayName(dn string) {
93         t.cl.mu.Lock()
94         defer t.cl.mu.Unlock()
95         t.torrent.setDisplayName(dn)
96 }
97
98 // The current working name for the torrent. Either the name in the info dict,
99 // or a display name given such as by the dn value in a magnet link, or "".
100 func (t Torrent) Name() string {
101         t.cl.mu.Lock()
102         defer t.cl.mu.Unlock()
103         return t.torrent.Name()
104 }
105
106 func (t Torrent) Length() int64 {
107         select {
108         case <-t.GotInfo():
109                 return t.torrent.length
110         default:
111                 return -1
112         }
113 }
114
115 // Returns a run-time generated metainfo for the torrent that includes the
116 // info bytes and announce-list as currently known to the client.
117 func (t Torrent) MetaInfo() *metainfo.MetaInfo {
118         t.cl.mu.Lock()
119         defer t.cl.mu.Unlock()
120         return t.torrent.MetaInfo()
121 }
122
123 func (t Torrent) addReader(r *Reader) {
124         t.cl.mu.Lock()
125         defer t.cl.mu.Unlock()
126         if t.torrent.readers == nil {
127                 t.torrent.readers = make(map[*Reader]struct{})
128         }
129         t.torrent.readers[r] = struct{}{}
130         t.torrent.readersChanged()
131 }
132
133 func (t Torrent) deleteReader(r *Reader) {
134         t.cl.mu.Lock()
135         defer t.cl.mu.Unlock()
136         delete(t.torrent.readers, r)
137         t.torrent.readersChanged()
138 }
139
140 func (t Torrent) DownloadPieces(begin, end int) {
141         t.cl.mu.Lock()
142         defer t.cl.mu.Unlock()
143         for i := begin; i < end; i++ {
144                 t.torrent.pendPiece(i, t.cl)
145         }
146 }