From 153c13db4374722e7665311e0355fdb4c3820363 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 6 Jan 2016 12:19:49 +1100 Subject: [PATCH] Revert "Merge pull request #54 from zhulik/master" This reverts commit 5cf75b869c469196a8473c4962b99a450daaeeec, reversing changes made to 09050ff2c5607f3991e6ec6422ed57ba0dee0bad. --- .gitignore | 1 - client.go | 20 ++++++------ client_test.go | 6 ++-- cmd/magnet-metainfo/main.go | 4 +-- cmd/torrent/main.go | 2 +- file.go | 2 +- fs/torrentfs.go | 2 +- issue35_test.go | 22 ++++++------- reader.go | 2 +- t.go | 64 ++++++++----------------------------- torrent.go | 6 ++-- 11 files changed, 46 insertions(+), 85 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 62c89355..00000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.idea/ \ No newline at end of file diff --git a/client.go b/client.go index 05e0351a..322c7e70 100644 --- a/client.go +++ b/client.go @@ -678,7 +678,7 @@ func (cl *Client) Torrent(ih InfoHash) (T Torrent, ok bool) { if !ok { return } - T = clientTorrent{cl, t} + T = Torrent{cl, t} return } @@ -1828,7 +1828,7 @@ func (me *Client) openNewConns(t *torrent) { return } var ( - k PeersKey + k peersKey p Peer ) for k, p = range t.Peers { @@ -1945,7 +1945,7 @@ func newTorrent(ih InfoHash) (t *torrent, err error) { t = &torrent{ InfoHash: ih, chunkSize: defaultChunkSize, - Peers: make(map[PeersKey]Peer), + Peers: make(map[peersKey]Peer), closing: make(chan struct{}), ceasingNetworking: make(chan struct{}), @@ -2030,7 +2030,7 @@ type Handle interface { // Returns handles to the files in the torrent. This requires the metainfo is // available first. -func (t clientTorrent) Files() (ret []File) { +func (t Torrent) Files() (ret []File) { t.cl.mu.Lock() info := t.Info() t.cl.mu.Unlock() @@ -2052,7 +2052,7 @@ func (t clientTorrent) Files() (ret []File) { } // Marks the pieces in the given region for download. -func (t clientTorrent) SetRegionPriority(off, len int64) { +func (t Torrent) SetRegionPriority(off, len int64) { t.cl.mu.Lock() defer t.cl.mu.Unlock() pieceSize := int64(t.usualPieceSize()) @@ -2061,7 +2061,7 @@ func (t clientTorrent) SetRegionPriority(off, len int64) { } } -func (t clientTorrent) AddPeers(pp []Peer) error { +func (t Torrent) AddPeers(pp []Peer) error { cl := t.cl cl.mu.Lock() defer cl.mu.Unlock() @@ -2071,7 +2071,7 @@ func (t clientTorrent) AddPeers(pp []Peer) error { // Marks the entire torrent for download. Requires the info first, see // GotInfo. -func (t clientTorrent) DownloadAll() { +func (t Torrent) DownloadAll() { t.cl.mu.Lock() defer t.cl.mu.Unlock() for i := range iter.N(t.numPieces()) { @@ -2157,10 +2157,8 @@ 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) (D Torrent, new bool, err error) { - T := clientTorrent{} +func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (T Torrent, new bool, err error) { T.cl = cl - D = &T cl.mu.Lock() defer cl.mu.Unlock() @@ -2725,7 +2723,7 @@ func (cl *Client) verifyPiece(t *torrent, piece int) { func (me *Client) Torrents() (ret []Torrent) { me.mu.Lock() for _, t := range me.torrents { - ret = append(ret, clientTorrent{me, t}) + ret = append(ret, Torrent{me, t}) } me.mu.Unlock() return diff --git a/client_test.go b/client_test.go index 88088bec..80d1535c 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.SubscribePieceStateChanges() + s := leecherGreeting.pieceStateChanges.Subscribe() 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/cmd/magnet-metainfo/main.go b/cmd/magnet-metainfo/main.go index d3a070c0..878fc7db 100644 --- a/cmd/magnet-metainfo/main.go +++ b/cmd/magnet-metainfo/main.go @@ -27,9 +27,9 @@ func main() { go func() { defer wg.Done() <-t.GotInfo() - mi := t.Info() + mi := t.MetaInfo() t.Drop() - f, err := os.Create(mi.Name + ".torrent") + f, err := os.Create(mi.Info.Name + ".torrent") if err != nil { log.Fatalf("error creating torrent metainfo file: %s", err) } diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 66871794..4adda93e 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -54,7 +54,7 @@ func torrentBar(t torrent.Torrent) { } }) bar.PrependFunc(func(*uiprogress.Bar) string { - return t.Info().Name + return t.Name() }) go func() { <-t.GotInfo() diff --git a/file.go b/file.go index 2395d266..cdeb3c70 100644 --- a/file.go +++ b/file.go @@ -8,7 +8,7 @@ import ( // Provides access to regions of torrent data that correspond to its files. type File struct { - t clientTorrent + t Torrent path string offset int64 length int64 diff --git a/fs/torrentfs.go b/fs/torrentfs.go index c4cacd4a..9b3f3958 100644 --- a/fs/torrentfs.go +++ b/fs/torrentfs.go @@ -225,7 +225,7 @@ func (dn dirNode) Attr(ctx context.Context, attr *fuse.Attr) error { func (me rootNode) Lookup(ctx context.Context, name string) (_node fusefs.Node, err error) { for _, t := range me.fs.Client.Torrents() { info := t.Info() - if t.Info().Name != name || info == nil { + if t.Name() != name || info == nil { continue } __node := node{ diff --git a/issue35_test.go b/issue35_test.go index 1d8f113d..59dbe38f 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" - "io" - "os" + "testing" "path/filepath" + "os" + "github.com/anacrolix/torrent/dht" + "io" + "errors" + "fmt" "runtime" - "testing" ) var numclients int = 0 @@ -42,12 +42,12 @@ func testingConfig() *Config { 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 @@ func writeranddata(path string) error { 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 86d2e079..e26dfe1d 100644 --- a/reader.go +++ b/reader.go @@ -8,7 +8,7 @@ import ( // Accesses torrent data via a client. type Reader struct { - t *clientTorrent + t *Torrent pos int64 responsive bool readahead int64 diff --git a/t.go b/t.go index 173cdd31..65709037 100644 --- a/t.go +++ b/t.go @@ -2,62 +2,41 @@ package torrent import ( "github.com/anacrolix/missinggo/pubsub" + "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 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 { +// The public handle to a live torrent within a Client. +type Torrent struct { cl *Client *torrent } // The torrent's infohash. This is fixed and cannot change. It uniquely // identifies a torrent. -func (t clientTorrent) InfoHash() InfoHash { +func (t Torrent) 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 clientTorrent) GotInfo() <-chan struct{} { +func (t Torrent) GotInfo() <-chan struct{} { return t.torrent.gotMetainfo } // Returns the metainfo, or nil if it's not yet available. -func (t clientTorrent) Info() *metainfo.Info { +func (t Torrent) 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 clientTorrent) NewReader() (ret *Reader) { +func (t Torrent) NewReader() (ret *Reader) { ret = &Reader{ t: &t, readahead: 5 * 1024 * 1024, @@ -68,25 +47,25 @@ func (t clientTorrent) NewReader() (ret *Reader) { // 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 clientTorrent) PieceStateRuns() []PieceStateRun { +func (t Torrent) PieceStateRuns() []PieceStateRun { t.stateMu.Lock() defer t.stateMu.Unlock() return t.torrent.pieceStateRuns() } -func (t clientTorrent) NumPieces() int { +func (t Torrent) NumPieces() int { return t.numPieces() } // Drop the torrent from the client, and close it. -func (t clientTorrent) Drop() { +func (t Torrent) 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 clientTorrent) BytesCompleted() int64 { +func (t Torrent) BytesCompleted() int64 { t.cl.mu.RLock() defer t.cl.mu.RUnlock() return t.bytesCompleted() @@ -94,13 +73,13 @@ func (t clientTorrent) BytesCompleted() int64 { // 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 clientTorrent) SubscribePieceStateChanges() *pubsub.Subscription { +func (t Torrent) 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 clientTorrent) Seeding() bool { +func (t Torrent) Seeding() bool { t.cl.mu.Lock() defer t.cl.mu.Unlock() return t.cl.seeding(t.torrent) @@ -108,23 +87,8 @@ func (t clientTorrent) Seeding() bool { // Clobbers the torrent display name. The display name is used as the torrent // name if the metainfo is not available. -func (t clientTorrent) SetDisplayName(dn string) { +func (t Torrent) SetDisplayName(dn string) { t.cl.mu.Lock() defer t.cl.mu.Unlock() t.torrent.setDisplayName(dn) } - -// Client returns Torrent's client instance -func (t clientTorrent) Client() *Client { - return t.cl -} - -// Trackers returns torrent's trackers -func (t clientTorrent) Trackers() [][]tracker.Client { - return t.torrent.Trackers -} - -// Peers returns torrent's peers -func (t clientTorrent) Peers() map[PeersKey]Peer { - return t.torrent.Peers -} diff --git a/torrent.go b/torrent.go index db554e67..b2478656 100644 --- a/torrent.go +++ b/torrent.go @@ -39,7 +39,7 @@ func (t *torrent) pieceNumPendingBytes(index int) (count pp.Integer) { return } -type PeersKey struct { +type peersKey struct { IPBytes string Port int } @@ -78,7 +78,7 @@ type torrent struct { // Reserve of peers to connect to. A peer can be both here and in the // active connections if were told about the peer after connecting with // them. That encourages us to reconnect to peers that are well known. - Peers map[PeersKey]Peer + Peers map[peersKey]Peer wantPeers sync.Cond // BEP 12 Multitracker Metadata Extension. The tracker.Client instances @@ -180,7 +180,7 @@ func (t *torrent) addPeer(p Peer, cl *Client) { if len(t.Peers) >= torrentPeersHighWater { return } - key := PeersKey{string(p.IP), p.Port} + key := peersKey{string(p.IP), p.Port} if _, ok := t.Peers[key]; ok { return } -- 2.44.0