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