.gitignore | 1 + client.go | 14 ++++++++------ client_test.go | 6 +++--- download.go | 25 +++++++++++++++++++++++++ file.go | 2 +- t.go | 12 +++++++++++- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..62c893550adb53d3a8fc29a1584ff831cb829062 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/client.go b/client.go index ba2f03cec7e7abb1c43dace4e288b74da89eea9f..5e542e8e16e7d66ebc3b3ed32b0288fbda7ac200 100644 --- a/client.go +++ b/client.go @@ -671,7 +671,7 @@ } } // Returns a handle to the given torrent, if it's present in the client. -func (cl *Client) Torrent(ih InfoHash) (T Torrent, ok bool) { +func (cl *Client) Torrent(ih InfoHash) (T Download, ok bool) { cl.mu.Lock() defer cl.mu.Unlock() t, ok := cl.torrents[ih] @@ -2160,8 +2160,10 @@ // 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) (T Torrent, new bool, err error) { +func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (D Download, new bool, err error) { + T := Torrent{} T.cl = cl + D = &T cl.mu.Lock() defer cl.mu.Unlock() @@ -2729,7 +2731,7 @@ cl.pieceHashed(t, index, sum == p.Hash) } // Returns handles to all the torrents loaded in the Client. -func (me *Client) Torrents() (ret []Torrent) { +func (me *Client) Torrents() (ret []Download) { me.mu.Lock() for _, t := range me.torrents { ret = append(ret, Torrent{me, t}) @@ -2738,7 +2740,7 @@ me.mu.Unlock() return } -func (me *Client) AddMagnet(uri string) (T Torrent, err error) { +func (me *Client) AddMagnet(uri string) (T Download, err error) { spec, err := TorrentSpecFromMagnetURI(uri) if err != nil { return @@ -2747,12 +2749,12 @@ T, _, err = me.AddTorrentSpec(spec) return } -func (me *Client) AddTorrent(mi *metainfo.MetaInfo) (T Torrent, err error) { +func (me *Client) AddTorrent(mi *metainfo.MetaInfo) (T Download, err error) { T, _, err = me.AddTorrentSpec(TorrentSpecFromMetaInfo(mi)) return } -func (me *Client) AddTorrentFromFile(filename string) (T Torrent, err error) { +func (me *Client) AddTorrentFromFile(filename string) (T Download, err error) { mi, err := metainfo.LoadFromFile(filename) if err != nil { return diff --git a/client_test.go b/client_test.go index 80d1535c18e69becb7129568f99cbc74565f4abe..88088bec45d95894eb0bbe17dc57031435396d21 100644 --- a/client_test.go +++ b/client_test.go @@ -290,7 +290,7 @@ }()) // TODO: The piece state publishing is kinda jammed in here until I have a // more thorough test. go func() { - s := leecherGreeting.pieceStateChanges.Subscribe() + s := leecherGreeting.SubscribePieceStateChanges() defer s.Close() for i := range s.Values { log.Print(i) @@ -410,8 +410,8 @@ _, new, _ = cl.AddTorrentSpec(&spec) if new { t.FailNow() } - assert.EqualValues(t, T.Trackers[0][0].URL(), "http://a") - assert.EqualValues(t, T.Trackers[1][0].URL(), "udp://b") + assert.EqualValues(t, T.Trackers()[0][0].URL(), "http://a") + assert.EqualValues(t, T.Trackers()[1][0].URL(), "udp://b") } type badData struct{} diff --git a/download.go b/download.go new file mode 100644 index 0000000000000000000000000000000000000000..b7e6095eaf4031dcf2f7c8a6731ea8d0c0eacb37 --- /dev/null +++ b/download.go @@ -0,0 +1,25 @@ +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 +} \ No newline at end of file diff --git a/file.go b/file.go index cdeb3c70425be9ae4cbfd93005f4f87377e0c5ca..0d7f8730a4d34167e7d0750f717908194c9aa771 100644 --- a/file.go +++ b/file.go @@ -15,7 +15,7 @@ length int64 fi metainfo.FileInfo } -func (f *File) Torrent() Torrent { +func (f *File) Torrent() Download { return f.t } diff --git a/t.go b/t.go index 65709037ab88a21b62ea9357ccc7d3e1e6ce9d86..05917e29f4bd6bc27c7e2b82e2bca9e2488b7c61 100644 --- a/t.go +++ b/t.go @@ -2,7 +2,7 @@ package torrent import ( "github.com/anacrolix/missinggo/pubsub" - + "github.com/anacrolix/torrent/tracker" "github.com/anacrolix/torrent/metainfo" ) @@ -92,3 +92,13 @@ t.cl.mu.Lock() defer t.cl.mu.Unlock() t.torrent.setDisplayName(dn) } + +// Client returns Torrent's client instance +func (t Torrent) Client() *Client { + return t.cl +} + +// Trackers returns torrent's trackers +func (t Torrent) Trackers() [][]tracker.Client { + return t.torrent.Trackers +} \ No newline at end of file