From 5b790bf87424c27e3ad88b51025b0a4fd160cef6 Mon Sep 17 00:00:00 2001 From: Gleb Sinyavsky Date: Sun, 27 Dec 2015 14:49:15 +0300 Subject: [PATCH] Torrent structs replaced with Download interface in exported Client methods --- .gitignore | 1 + client.go | 14 ++++++++------ client_test.go | 6 +++--- download.go | 25 +++++++++++++++++++++++++ file.go | 2 +- t.go | 12 +++++++++++- 6 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 .gitignore create mode 100644 download.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..62c89355 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/client.go b/client.go index ba2f03ce..5e542e8e 100644 --- a/client.go +++ b/client.go @@ -671,7 +671,7 @@ func (cl *Client) incomingConnection(nc net.Conn, utp bool) { } // 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 @@ func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) (spec *TorrentSpec) { // 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 @@ func (cl *Client) verifyPiece(t *torrent, index pp.Integer) { } // 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 @@ func (me *Client) Torrents() (ret []Torrent) { 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 @@ func (me *Client) AddMagnet(uri string) (T Torrent, err error) { 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 80d1535c..88088bec 100644 --- a/client_test.go +++ b/client_test.go @@ -290,7 +290,7 @@ func TestClientTransfer(t *testing.T) { // 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 @@ func TestMergingTrackersByAddingSpecs(t *testing.T) { 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 00000000..b7e6095e --- /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 cdeb3c70..0d7f8730 100644 --- a/file.go +++ b/file.go @@ -15,7 +15,7 @@ type File struct { 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 65709037..05917e29 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 @@ func (t Torrent) SetDisplayName(dn string) { 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 -- 2.48.1