]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Torrent structs replaced with Download interface in exported Client methods
authorGleb Sinyavsky <zhulik.gleb@gmail.com>
Sun, 27 Dec 2015 11:49:15 +0000 (14:49 +0300)
committerGleb Sinyavsky <zhulik.gleb@gmail.com>
Sun, 27 Dec 2015 11:49:15 +0000 (14:49 +0300)
.gitignore [new file with mode: 0644]
client.go
client_test.go
download.go [new file with mode: 0644]
file.go
t.go

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..62c8935
--- /dev/null
@@ -0,0 +1 @@
+.idea/
\ No newline at end of file
index ba2f03cec7e7abb1c43dace4e288b74da89eea9f..5e542e8e16e7d66ebc3b3ed32b0288fbda7ac200 100644 (file)
--- 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
index 80d1535c18e69becb7129568f99cbc74565f4abe..88088bec45d95894eb0bbe17dc57031435396d21 100644 (file)
@@ -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 (file)
index 0000000..b7e6095
--- /dev/null
@@ -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 (file)
--- 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 65709037ab88a21b62ea9357ccc7d3e1e6ce9d86..05917e29f4bd6bc27c7e2b82e2bca9e2488b7c61 100644 (file)
--- 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