]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Reexport Torrent.MetaInfo
[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, 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         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 func (t Torrent) NumPieces() int {
57         return t.torrent.numPieces()
58 }
59
60 // Drop the torrent from the client, and close it.
61 func (t Torrent) Drop() {
62         t.cl.mu.Lock()
63         t.cl.dropTorrent(t.torrent.InfoHash)
64         t.cl.mu.Unlock()
65 }
66
67 // Number of bytes of the entire torrent we have completed.
68 func (t Torrent) BytesCompleted() int64 {
69         t.cl.mu.RLock()
70         defer t.cl.mu.RUnlock()
71         return t.torrent.bytesCompleted()
72 }
73
74 // The subscription emits as (int) the index of pieces as their state changes.
75 // A state change is when the PieceState for a piece alters in value.
76 func (t Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
77         return t.torrent.pieceStateChanges.Subscribe()
78 }
79
80 // Returns true if the torrent is currently being seeded. This occurs when the
81 // client is willing to upload without wanting anything in return.
82 func (t Torrent) Seeding() bool {
83         t.cl.mu.Lock()
84         defer t.cl.mu.Unlock()
85         return t.cl.seeding(t.torrent)
86 }
87
88 // Clobbers the torrent display name. The display name is used as the torrent
89 // name if the metainfo is not available.
90 func (t Torrent) SetDisplayName(dn string) {
91         t.cl.mu.Lock()
92         defer t.cl.mu.Unlock()
93         t.torrent.setDisplayName(dn)
94 }
95
96 func (t Torrent) Name() string {
97         t.cl.mu.Lock()
98         defer t.cl.mu.Unlock()
99         return t.torrent.Name()
100 }
101
102 func (t Torrent) Length() int64 {
103         select {
104         case <-t.GotInfo():
105                 return t.torrent.length
106         default:
107                 return -1
108         }
109 }
110
111 // Returns a run-time generated metainfo for the torrent that includes the
112 // info bytes and announce-list as currently known to the client.
113 func (t Torrent) MetaInfo() *metainfo.MetaInfo {
114         t.cl.mu.Lock()
115         defer t.cl.mu.Unlock()
116         return t.torrent.MetaInfo()
117 }