FedP2P/t.go

150 lines
3.8 KiB
Go
Raw Normal View History

package torrent
2015-04-28 13:24:17 +08:00
import (
"github.com/anacrolix/missinggo/pubsub"
2015-04-28 13:24:17 +08:00
"github.com/anacrolix/torrent/metainfo"
)
2015-09-06 10:31:23 +08:00
// The torrent's infohash. This is fixed and cannot change. It uniquely
// identifies a torrent.
func (t *Torrent) InfoHash() metainfo.Hash {
2016-04-03 16:40:43 +08:00
return t.infoHash
2015-08-02 01:55:48 +08:00
}
// Closed when the info (.Info()) for the torrent has become available. Using
// features of Torrent that require the info before it is available will have
// undefined behaviour.
2016-04-03 16:40:43 +08:00
func (t *Torrent) GotInfo() <-chan struct{} {
return t.gotMetainfo
2015-04-28 13:24:17 +08:00
}
// Returns the metainfo info dictionary, or nil if it's not yet available.
2016-04-03 16:40:43 +08:00
func (t *Torrent) Info() *metainfo.InfoEx {
return t.info
}
2015-06-03 11:30:55 +08:00
// Returns a Reader bound to the torrent's data. All read calls block until
2016-01-28 14:22:10 +08:00
// the data requested is actually available.
2016-04-03 16:40:43 +08:00
func (t *Torrent) NewReader() (ret *Reader) {
ret = &Reader{
2016-04-03 16:40:43 +08:00
t: t,
readahead: 5 * 1024 * 1024,
}
t.addReader(ret)
return
}
// Returns the state of pieces of the torrent. They are grouped into runs of
// same state. The sum of the state run lengths is the number of pieces
// in the torrent.
2016-04-03 16:40:43 +08:00
func (t *Torrent) PieceStateRuns() []PieceStateRun {
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
return t.pieceStateRuns()
}
2015-06-23 00:02:22 +08:00
2016-04-03 16:40:43 +08:00
func (t *Torrent) PieceState(piece int) PieceState {
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
return t.pieceState(piece)
2016-02-07 18:57:57 +08:00
}
// The number of pieces in the torrent. This requires that the info has been
// obtained first.
2016-04-03 16:40:43 +08:00
func (t *Torrent) NumPieces() int {
return t.numPieces()
2015-06-23 00:02:22 +08:00
}
// Drop the torrent from the client, and close it. It's always safe to do
// this. No data corruption can, or should occur to either the torrent's data,
// or connected peers.
2016-04-03 16:40:43 +08:00
func (t *Torrent) Drop() {
2015-06-23 00:02:22 +08:00
t.cl.mu.Lock()
2016-04-03 16:40:43 +08:00
t.cl.dropTorrent(t.infoHash)
2015-06-23 00:02:22 +08:00
t.cl.mu.Unlock()
}
2015-07-21 20:54:02 +08:00
2015-09-06 10:31:23 +08:00
// Number of bytes of the entire torrent we have completed.
2016-04-03 16:40:43 +08:00
func (t *Torrent) BytesCompleted() int64 {
2015-07-21 20:54:02 +08:00
t.cl.mu.RLock()
defer t.cl.mu.RUnlock()
2016-04-03 16:40:43 +08:00
return t.bytesCompleted()
2015-07-21 20:54:02 +08:00
}
2015-12-12 11:00:07 +08:00
// The subscription emits as (int) the index of pieces as their state changes.
// A state change is when the PieceState for a piece alters in value.
2016-04-03 16:40:43 +08:00
func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
return t.pieceStateChanges.Subscribe()
}
2015-11-22 15:44:33 +08:00
2015-12-12 11:00:07 +08:00
// Returns true if the torrent is currently being seeded. This occurs when the
// client is willing to upload without wanting anything in return.
2016-04-03 16:40:43 +08:00
func (t *Torrent) Seeding() bool {
2015-11-22 15:44:33 +08:00
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
return t.cl.seeding(t)
2015-11-22 15:44:33 +08:00
}
2015-12-12 11:03:04 +08:00
// Clobbers the torrent display name. The display name is used as the torrent
// name if the metainfo is not available.
2016-04-03 16:40:43 +08:00
func (t *Torrent) SetDisplayName(dn string) {
2015-12-12 11:03:04 +08:00
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
t.setDisplayName(dn)
2015-12-12 11:03:04 +08:00
}
// The current working name for the torrent. Either the name in the info dict,
// or a display name given such as by the dn value in a magnet link, or "".
2016-04-03 16:40:43 +08:00
func (t *Torrent) Name() string {
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
return t.name()
}
2016-04-03 16:40:43 +08:00
func (t *Torrent) Length() int64 {
select {
case <-t.GotInfo():
2016-04-03 16:40:43 +08:00
return t.length
default:
return -1
}
}
2016-01-16 22:49:04 +08:00
// Returns a run-time generated metainfo for the torrent that includes the
// info bytes and announce-list as currently known to the client.
2016-04-03 16:40:43 +08:00
func (t *Torrent) Metainfo() *metainfo.MetaInfo {
2016-01-16 22:49:04 +08:00
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
return t.metainfo()
2016-01-16 22:49:04 +08:00
}
2016-04-03 16:40:43 +08:00
func (t *Torrent) addReader(r *Reader) {
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
if t.readers == nil {
t.readers = make(map[*Reader]struct{})
}
2016-04-03 16:40:43 +08:00
t.readers[r] = struct{}{}
t.readersChanged()
}
2016-04-03 16:40:43 +08:00
func (t *Torrent) deleteReader(r *Reader) {
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
delete(t.readers, r)
t.readersChanged()
}
2016-04-03 16:40:43 +08:00
func (t *Torrent) DownloadPieces(begin, end int) {
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
t.pendPieceRange(begin, end)
2016-02-04 22:18:54 +08:00
}
2016-04-03 16:40:43 +08:00
func (t *Torrent) CancelPieces(begin, end int) {
2016-02-04 22:18:54 +08:00
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
2016-04-03 16:40:43 +08:00
t.unpendPieceRange(begin, end)
2016-02-20 11:39:56 +08:00
}