client.go | 26 +++++++++++++------------- cmd/torrent-pick/main.go | 2 +- cmd/torrent/main.go | 4 ++-- download.go | 27 --------------------------- file.go | 4 ++-- fs/torrentfs.go | 6 +++--- issue35_test.go | 22 +++++++++++----------- reader.go | 2 +- t.go | 57 ++++++++++++++++++++++++++++++++++++----------------- diff --git a/client.go b/client.go index d5414d340f102991a463647fde9be744ed6227af..05e0351a5588327f98cda3de9b15b974447e0944 100644 --- a/client.go +++ b/client.go @@ -671,14 +671,14 @@ } } // Returns a handle to the given torrent, if it's present in the client. -func (cl *Client) Torrent(ih InfoHash) (T Download, ok bool) { +func (cl *Client) Torrent(ih InfoHash) (T Torrent, ok bool) { cl.mu.Lock() defer cl.mu.Unlock() t, ok := cl.torrents[ih] if !ok { return } - T = Torrent{cl, t} + T = clientTorrent{cl, t} return } @@ -2030,7 +2030,7 @@ } // Returns handles to the files in the torrent. This requires the metainfo is // available first. -func (t Torrent) Files() (ret []File) { +func (t clientTorrent) Files() (ret []File) { t.cl.mu.Lock() info := t.Info() t.cl.mu.Unlock() @@ -2052,7 +2052,7 @@ return } // Marks the pieces in the given region for download. -func (t Torrent) SetRegionPriority(off, len int64) { +func (t clientTorrent) SetRegionPriority(off, len int64) { t.cl.mu.Lock() defer t.cl.mu.Unlock() pieceSize := int64(t.usualPieceSize()) @@ -2061,7 +2061,7 @@ t.cl.raisePiecePriority(t.torrent, int(i), PiecePriorityNormal) } } -func (t Torrent) AddPeers(pp []Peer) error { +func (t clientTorrent) AddPeers(pp []Peer) error { cl := t.cl cl.mu.Lock() defer cl.mu.Unlock() @@ -2071,7 +2071,7 @@ } // Marks the entire torrent for download. Requires the info first, see // GotInfo. -func (t Torrent) DownloadAll() { +func (t clientTorrent) DownloadAll() { t.cl.mu.Lock() defer t.cl.mu.Unlock() for i := range iter.N(t.numPieces()) { @@ -2157,8 +2157,8 @@ // Add or merge a torrent spec. If the torrent is already present, the // trackers will be merged with the existing ones. If the Info isn't yet // known, it will be set. The display name is replaced if the new spec // provides one. Returns new if the torrent wasn't already in the client. -func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (D Download, new bool, err error) { - T := Torrent{} +func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (D Torrent, new bool, err error) { + T := clientTorrent{} T.cl = cl D = &T cl.mu.Lock() @@ -2722,16 +2722,16 @@ cl.pieceHashed(t, piece, sum == p.Hash) } // Returns handles to all the torrents loaded in the Client. -func (me *Client) Torrents() (ret []Download) { +func (me *Client) Torrents() (ret []Torrent) { me.mu.Lock() for _, t := range me.torrents { - ret = append(ret, Torrent{me, t}) + ret = append(ret, clientTorrent{me, t}) } me.mu.Unlock() return } -func (me *Client) AddMagnet(uri string) (T Download, err error) { +func (me *Client) AddMagnet(uri string) (T Torrent, err error) { spec, err := TorrentSpecFromMagnetURI(uri) if err != nil { return @@ -2740,12 +2740,12 @@ T, _, err = me.AddTorrentSpec(spec) return } -func (me *Client) AddTorrent(mi *metainfo.MetaInfo) (T Download, err error) { +func (me *Client) AddTorrent(mi *metainfo.MetaInfo) (T Torrent, err error) { T, _, err = me.AddTorrentSpec(TorrentSpecFromMetaInfo(mi)) return } -func (me *Client) AddTorrentFromFile(filename string) (T Download, err error) { +func (me *Client) AddTorrentFromFile(filename string) (T Torrent, err error) { mi, err := metainfo.LoadFromFile(filename) if err != nil { return diff --git a/cmd/torrent-pick/main.go b/cmd/torrent-pick/main.go index f02ea8e7cc69656b9e422c9da4b28e10a8a671d1..928f11f4df00d16b0f7832c1e8ae7ac69e2ead54 100644 --- a/cmd/torrent-pick/main.go +++ b/cmd/torrent-pick/main.go @@ -134,7 +134,7 @@ dstWriter := bufio.NewWriter(f) done := make(chan struct{}) for _, arg := range posArgs { - t := func() torrent.Download { + t := func() torrent.Torrent { if strings.HasPrefix(arg, "magnet:") { t, err := client.AddMagnet(arg) if err != nil { diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 43b75b5dbcfe72c76838839c10d3aca3e26c1072..66871794859ba59589ad30d393206282af7e6ed2 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -36,7 +36,7 @@ } return } -func torrentBar(t torrent.Download) { +func torrentBar(t torrent.Torrent) { bar := uiprogress.AddBar(1) bar.AppendCompleted() bar.AppendFunc(func(*uiprogress.Bar) (ret string) { @@ -69,7 +69,7 @@ } func addTorrents(client *torrent.Client) { for _, arg := range opts.Torrent { - t := func() torrent.Download { + t := func() torrent.Torrent { if strings.HasPrefix(arg, "magnet:") { t, err := client.AddMagnet(arg) if err != nil { diff --git a/download.go b/download.go deleted file mode 100644 index 0a69c6a64f36cd0032c4194243c2e5166c6d7cfc..0000000000000000000000000000000000000000 --- a/download.go +++ /dev/null @@ -1,27 +0,0 @@ -package torrent - -import( - "github.com/anacrolix/torrent/metainfo" - "github.com/anacrolix/missinggo/pubsub" - "github.com/anacrolix/torrent/tracker" -) - -type Download interface { - InfoHash() InfoHash - GotInfo() <-chan struct{} - Info() *metainfo.Info - NewReader() (ret *Reader) - PieceStateRuns() []PieceStateRun - NumPieces() int - Drop() - BytesCompleted() int64 - SubscribePieceStateChanges() *pubsub.Subscription - Seeding() bool - SetDisplayName(dn string) - Client() *Client - AddPeers(pp []Peer) error - DownloadAll() - Trackers() [][]tracker.Client - Files() (ret []File) - Peers() map[PeersKey]Peer -} \ No newline at end of file diff --git a/file.go b/file.go index 0d7f8730a4d34167e7d0750f717908194c9aa771..2395d266de24242700c913dffe87e54ef1cc24e0 100644 --- a/file.go +++ b/file.go @@ -8,14 +8,14 @@ ) // Provides access to regions of torrent data that correspond to its files. type File struct { - t Torrent + t clientTorrent path string offset int64 length int64 fi metainfo.FileInfo } -func (f *File) Torrent() Download { +func (f *File) Torrent() Torrent { return f.t } diff --git a/fs/torrentfs.go b/fs/torrentfs.go index b9cb3ad70e8acacaace2930720ca612acd0deff4..c4cacd4aed7bfff7a915c52d412a98813f435f7e 100644 --- a/fs/torrentfs.go +++ b/fs/torrentfs.go @@ -50,7 +50,7 @@ type node struct { path string metadata *metainfo.Info FS *TorrentFS - t torrent.Download + t torrent.Torrent } type fileNode struct { @@ -69,7 +69,7 @@ func (n *node) fsPath() string { return "/" + n.metadata.Name + "/" + n.path } -func blockingRead(ctx context.Context, fs *TorrentFS, t torrent.Download, off int64, p []byte) (n int, err error) { +func blockingRead(ctx context.Context, fs *TorrentFS, t torrent.Torrent, off int64, p []byte) (n int, err error) { fs.mu.Lock() fs.blockedReads++ fs.event.Broadcast() @@ -101,7 +101,7 @@ fs.mu.Unlock() return } -func readFull(ctx context.Context, fs *TorrentFS, t torrent.Download, off int64, p []byte) (n int, err error) { +func readFull(ctx context.Context, fs *TorrentFS, t torrent.Torrent, off int64, p []byte) (n int, err error) { for len(p) != 0 { var nn int nn, err = blockingRead(ctx, fs, t, off, p) diff --git a/issue35_test.go b/issue35_test.go index 59dbe38fbd83db62991badafd2dff30129e94d7e..1d8f113d686e1d8b3beaa3bed937f808f25deb4e 100644 --- a/issue35_test.go +++ b/issue35_test.go @@ -1,15 +1,15 @@ package torrent import ( + "errors" + "fmt" + "github.com/anacrolix/torrent/dht" "github.com/anacrolix/torrent/metainfo" - "testing" + "io" + "os" "path/filepath" - "os" - "github.com/anacrolix/torrent/dht" - "io" - "errors" - "fmt" "runtime" + "testing" ) var numclients int = 0 @@ -42,12 +42,12 @@ NoUpload: false, Seed: true, DataDir: filepath.Join(os.TempDir(), "torrent-test/data"), ConfigDir: filepath.Join(os.TempDir(), "torrent-test/config"), - DHTConfig: &dht.ServerConfig{ - Passive: false, + DHTConfig: &dht.ServerConfig{ + Passive: false, BootstrapNodes: []string{}, - NoSecurity: false, + NoSecurity: false, }, - Debug: true, + Debug: true, } } @@ -74,7 +74,7 @@ } return nil } -func TestInfohash(t *testing.T){ +func TestInfohash(t *testing.T) { os.RemoveAll(filepath.Join(os.TempDir(), "torrent-test")) os.MkdirAll(filepath.Join(os.TempDir(), "torrent-test"), 0700) var cl_one *Client diff --git a/reader.go b/reader.go index e26dfe1d7cf0c4d150a7d3749903daa11e7c3482..86d2e07999e4adc5563f9d5a184e04d98fc977bb 100644 --- a/reader.go +++ b/reader.go @@ -8,7 +8,7 @@ ) // Accesses torrent data via a client. type Reader struct { - t *Torrent + t *clientTorrent pos int64 responsive bool readahead int64 diff --git a/t.go b/t.go index 5f58703eb5eb88d043067f7f74db9b6bad95f4aa..173cdd31dd1caa7e9f39a063c71b44ccb9fb5240 100644 --- a/t.go +++ b/t.go @@ -2,41 +2,62 @@ package torrent import ( "github.com/anacrolix/missinggo/pubsub" - "github.com/anacrolix/torrent/tracker" "github.com/anacrolix/torrent/metainfo" + "github.com/anacrolix/torrent/tracker" ) // This file contains Torrent, until I decide where the private, lower-case // "torrent" type belongs. That type is currently mostly in torrent.go. -// The public handle to a live torrent within a Client. -type Torrent struct { +// The public interface to a live torrent within a Client. +type Torrent interface { + InfoHash() InfoHash + GotInfo() <-chan struct{} + Info() *metainfo.Info + NewReader() (ret *Reader) + PieceStateRuns() []PieceStateRun + NumPieces() int + Drop() + BytesCompleted() int64 + SubscribePieceStateChanges() *pubsub.Subscription + Seeding() bool + SetDisplayName(dn string) + Client() *Client + AddPeers(pp []Peer) error + DownloadAll() + Trackers() [][]tracker.Client + Files() (ret []File) + Peers() map[PeersKey]Peer +} + + +type clientTorrent struct { cl *Client *torrent } // The torrent's infohash. This is fixed and cannot change. It uniquely // identifies a torrent. -func (t Torrent) InfoHash() InfoHash { +func (t clientTorrent) InfoHash() InfoHash { return t.torrent.InfoHash } // 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. -func (t Torrent) GotInfo() <-chan struct{} { +func (t clientTorrent) GotInfo() <-chan struct{} { return t.torrent.gotMetainfo } // Returns the metainfo, or nil if it's not yet available. -func (t Torrent) Info() *metainfo.Info { +func (t clientTorrent) Info() *metainfo.Info { return t.torrent.Info } // Returns a Reader bound to the torrent's data. All read calls block until // the data requested is actually available. Priorities are set to ensure the // data requested will be downloaded as soon as possible. -func (t Torrent) NewReader() (ret *Reader) { +func (t clientTorrent) NewReader() (ret *Reader) { ret = &Reader{ t: &t, readahead: 5 * 1024 * 1024, @@ -47,25 +68,25 @@ // 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. -func (t Torrent) PieceStateRuns() []PieceStateRun { +func (t clientTorrent) PieceStateRuns() []PieceStateRun { t.stateMu.Lock() defer t.stateMu.Unlock() return t.torrent.pieceStateRuns() } -func (t Torrent) NumPieces() int { +func (t clientTorrent) NumPieces() int { return t.numPieces() } // Drop the torrent from the client, and close it. -func (t Torrent) Drop() { +func (t clientTorrent) Drop() { t.cl.mu.Lock() t.cl.dropTorrent(t.torrent.InfoHash) t.cl.mu.Unlock() } // Number of bytes of the entire torrent we have completed. -func (t Torrent) BytesCompleted() int64 { +func (t clientTorrent) BytesCompleted() int64 { t.cl.mu.RLock() defer t.cl.mu.RUnlock() return t.bytesCompleted() @@ -73,13 +94,13 @@ } // 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. -func (t Torrent) SubscribePieceStateChanges() *pubsub.Subscription { +func (t clientTorrent) SubscribePieceStateChanges() *pubsub.Subscription { return t.torrent.pieceStateChanges.Subscribe() } // Returns true if the torrent is currently being seeded. This occurs when the // client is willing to upload without wanting anything in return. -func (t Torrent) Seeding() bool { +func (t clientTorrent) Seeding() bool { t.cl.mu.Lock() defer t.cl.mu.Unlock() return t.cl.seeding(t.torrent) @@ -87,23 +108,23 @@ } // Clobbers the torrent display name. The display name is used as the torrent // name if the metainfo is not available. -func (t Torrent) SetDisplayName(dn string) { +func (t clientTorrent) SetDisplayName(dn string) { t.cl.mu.Lock() defer t.cl.mu.Unlock() t.torrent.setDisplayName(dn) } // Client returns Torrent's client instance -func (t Torrent) Client() *Client { +func (t clientTorrent) Client() *Client { return t.cl } // Trackers returns torrent's trackers -func (t Torrent) Trackers() [][]tracker.Client { +func (t clientTorrent) Trackers() [][]tracker.Client { return t.torrent.Trackers } // Peers returns torrent's peers -func (t Torrent) Peers() map[PeersKey]Peer { +func (t clientTorrent) Peers() map[PeersKey]Peer { return t.torrent.Peers -} \ No newline at end of file +}