]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Explicitly import Torrent.InfoHash
[btrtrc.git] / t.go
1 package torrent
2
3 import (
4         "github.com/anacrolix/torrent/metainfo"
5 )
6
7 // The public interface for a torrent within a Client.
8
9 // A handle to a live torrent within a Client.
10 type Torrent struct {
11         cl *Client
12         *torrent
13 }
14
15 func (t Torrent) InfoHash() InfoHash {
16         return t.torrent.InfoHash
17 }
18
19 // Closed when the info (.Info()) for the torrent has become available. Using
20 // features of Torrent that require the info before it is available will have
21 // undefined behaviour.
22 func (t *Torrent) GotInfo() <-chan struct{} {
23         return t.torrent.gotMetainfo
24 }
25
26 func (t *Torrent) Info() *metainfo.Info {
27         return t.torrent.Info
28 }
29
30 // Returns a Reader bound to the torrent's data. All read calls block until
31 // the data requested is actually available. Priorities are set to ensure the
32 // data requested will be downloaded as soon as possible.
33 func (t *Torrent) NewReader() (ret *Reader) {
34         ret = &Reader{
35                 t:         t,
36                 readahead: 5 * 1024 * 1024,
37         }
38         return
39 }
40
41 // Returns the state of pieces of the torrent. They are grouped into runs of
42 // same state. The sum of the state run lengths is the number of pieces
43 // in the torrent.
44 func (t *Torrent) PieceStateRuns() []PieceStateRun {
45         t.stateMu.Lock()
46         defer t.stateMu.Unlock()
47         return t.torrent.pieceStateRuns()
48 }
49
50 func (t Torrent) NumPieces() int {
51         return t.numPieces()
52 }
53
54 func (t Torrent) Drop() {
55         t.cl.mu.Lock()
56         t.cl.dropTorrent(t.torrent.InfoHash)
57         t.cl.mu.Unlock()
58 }
59
60 func (t Torrent) BytesCompleted() int64 {
61         t.cl.mu.RLock()
62         defer t.cl.mu.RUnlock()
63         return t.bytesCompleted()
64 }