7 "github.com/anacrolix/missinggo/pubsub"
9 "github.com/anacrolix/torrent/metainfo"
12 // The torrent's infohash. This is fixed and cannot change. It uniquely
13 // identifies a torrent.
14 func (t *Torrent) InfoHash() metainfo.Hash {
18 // Returns a channel that is closed when the info (.Info()) for the torrent
19 // has become available.
20 func (t *Torrent) GotInfo() <-chan struct{} {
22 defer t.cl.mu.Unlock()
23 return t.gotMetainfo.C()
26 // Returns the metainfo info dictionary, or nil if it's not yet available.
27 func (t *Torrent) Info() *metainfo.InfoEx {
31 // Returns a Reader bound to the torrent's data. All read calls block until
32 // the data requested is actually available.
33 func (t *Torrent) NewReader() (ret *Reader) {
36 readahead: 5 * 1024 * 1024,
42 // Returns the state of pieces of the torrent. They are grouped into runs of
43 // same state. The sum of the state run lengths is the number of pieces
45 func (t *Torrent) PieceStateRuns() []PieceStateRun {
47 defer t.cl.mu.Unlock()
48 return t.pieceStateRuns()
51 func (t *Torrent) PieceState(piece int) PieceState {
53 defer t.cl.mu.Unlock()
54 return t.pieceState(piece)
57 // The number of pieces in the torrent. This requires that the info has been
59 func (t *Torrent) NumPieces() int {
63 // Drop the torrent from the client, and close it. It's always safe to do
64 // this. No data corruption can, or should occur to either the torrent's data,
65 // or connected peers.
66 func (t *Torrent) Drop() {
68 t.cl.dropTorrent(t.infoHash)
72 // Number of bytes of the entire torrent we have completed.
73 func (t *Torrent) BytesCompleted() int64 {
75 defer t.cl.mu.RUnlock()
76 return t.bytesCompleted()
79 // The subscription emits as (int) the index of pieces as their state changes.
80 // A state change is when the PieceState for a piece alters in value.
81 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
82 return t.pieceStateChanges.Subscribe()
85 // Returns true if the torrent is currently being seeded. This occurs when the
86 // client is willing to upload without wanting anything in return.
87 func (t *Torrent) Seeding() bool {
89 defer t.cl.mu.Unlock()
93 // Clobbers the torrent display name. The display name is used as the torrent
94 // name if the metainfo is not available.
95 func (t *Torrent) SetDisplayName(dn string) {
97 defer t.cl.mu.Unlock()
101 // The current working name for the torrent. Either the name in the info dict,
102 // or a display name given such as by the dn value in a magnet link, or "".
103 func (t *Torrent) Name() string {
105 defer t.cl.mu.Unlock()
109 // The completed length of all the torrent data, in all its files. This is
110 // derived from the torrent info, when it is available.
111 func (t *Torrent) Length() int64 {
113 panic("not valid until info obtained")
118 // Returns a run-time generated metainfo for the torrent that includes the
119 // info bytes and announce-list as currently known to the client.
120 func (t *Torrent) Metainfo() *metainfo.MetaInfo {
122 defer t.cl.mu.Unlock()
123 return t.newMetaInfo()
126 func (t *Torrent) addReader(r *Reader) {
128 defer t.cl.mu.Unlock()
129 if t.readers == nil {
130 t.readers = make(map[*Reader]struct{})
132 t.readers[r] = struct{}{}
136 func (t *Torrent) deleteReader(r *Reader) {
138 defer t.cl.mu.Unlock()
143 func (t *Torrent) DownloadPieces(begin, end int) {
145 defer t.cl.mu.Unlock()
146 t.pendPieceRange(begin, end)
149 func (t *Torrent) CancelPieces(begin, end int) {
151 defer t.cl.mu.Unlock()
152 t.unpendPieceRange(begin, end)
155 // Returns handles to the files in the torrent. This requires the metainfo is
157 func (t *Torrent) Files() (ret []File) {
165 for _, fi := range info.UpvertedFiles() {
166 ret = append(ret, File{
168 strings.Join(append([]string{info.Name}, fi.Path...), "/"),
178 func (t *Torrent) AddPeers(pp []Peer) {
185 // Marks the entire torrent for download. Requires the info first, see
187 func (t *Torrent) DownloadAll() {
189 defer t.cl.mu.Unlock()
190 t.pendPieceRange(0, t.numPieces())
193 func (t *Torrent) String() string {
196 s = fmt.Sprintf("%x", t.infoHash)
201 func (t *Torrent) AddTrackers(announceList [][]string) {
203 defer t.cl.mu.Unlock()
204 t.addTrackers(announceList)