Torrent structs replaced with Download interface in exported Client methods

This commit is contained in:
Gleb Sinyavsky 2015-12-27 14:49:15 +03:00
parent a0f374ce8d
commit 5b790bf874
6 changed files with 49 additions and 11 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

View File

@ -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

View 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{}

25
download.go Normal file
View File

@ -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
}

View File

@ -15,7 +15,7 @@ type File struct {
fi metainfo.FileInfo
}
func (f *File) Torrent() Torrent {
func (f *File) Torrent() Download {
return f.t
}

12
t.go
View File

@ -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
}