.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 +++--- diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 62c893550adb53d3a8fc29a1584ff831cb829062..0000000000000000000000000000000000000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.idea/ \ No newline at end of file diff --git a/client.go b/client.go index 05e0351a5588327f98cda3de9b15b974447e0944..322c7e704b138531fab1188465086016247b4254 100644 --- a/client.go +++ b/client.go @@ -678,7 +678,7 @@ t, ok := cl.torrents[ih] if !ok { return } - T = clientTorrent{cl, t} + T = Torrent{cl, t} return } @@ -1828,7 +1828,7 @@ if len(t.HalfOpen) >= me.halfOpenLimit { 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 @@ } // 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 @@ return } // 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 @@ t.cl.raisePiecePriority(t.torrent, int(i), PiecePriorityNormal) } } -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 @@ } // 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 @@ // 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) (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 @@ // Returns handles to all the torrents loaded in the Client. 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 88088bec45d95894eb0bbe17dc57031435396d21..80d1535c18e69becb7129568f99cbc74565f4abe 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.SubscribePieceStateChanges() + s := leecherGreeting.pieceStateChanges.Subscribe() 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/cmd/magnet-metainfo/main.go b/cmd/magnet-metainfo/main.go index d3a070c0120b5cb515d2d7a24590a005a478587f..878fc7db9404c81445c57325b9a2ce16bde88982 100644 --- a/cmd/magnet-metainfo/main.go +++ b/cmd/magnet-metainfo/main.go @@ -27,9 +27,9 @@ wg.Add(1) 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 66871794859ba59589ad30d393206282af7e6ed2..4adda93e07cf71678e8a8a8d6764b0bc5af20448 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -54,7 +54,7 @@ return fmt.Sprintf("downloading (%s/%s)", humanize.Bytes(uint64(t.BytesCompleted())), humanize.Bytes(uint64(t.Info().TotalLength()))) } }) 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 2395d266de24242700c913dffe87e54ef1cc24e0..cdeb3c70425be9ae4cbfd93005f4f87377e0c5ca 100644 --- a/file.go +++ b/file.go @@ -8,7 +8,7 @@ ) // 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 c4cacd4aed7bfff7a915c52d412a98813f435f7e..9b3f3958768532a1497d9befbd450a4ba2d64928 100644 --- a/fs/torrentfs.go +++ b/fs/torrentfs.go @@ -225,7 +225,7 @@ 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 1d8f113d686e1d8b3beaa3bed937f808f25deb4e..59dbe38fbd83db62991badafd2dff30129e94d7e 100644 --- a/issue35_test.go +++ b/issue35_test.go @@ -1,15 +1,15 @@ package torrent import ( + "github.com/anacrolix/torrent/metainfo" + "testing" + "path/filepath" + "os" + "github.com/anacrolix/torrent/dht" + "io" "errors" "fmt" - "github.com/anacrolix/torrent/dht" - "github.com/anacrolix/torrent/metainfo" - "io" - "os" - "path/filepath" "runtime" - "testing" ) var numclients int = 0 @@ -42,12 +42,12 @@ NoUpload: false, 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 @@ } 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 86d2e07999e4adc5563f9d5a184e04d98fc977bb..e26dfe1d7cf0c4d150a7d3749903daa11e7c3482 100644 --- a/reader.go +++ b/reader.go @@ -8,7 +8,7 @@ ) // 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 173cdd31dd1caa7e9f39a063c71b44ccb9fb5240..65709037ab88a21b62ea9357ccc7d3e1e6ce9d86 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 @@ // 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 @@ } // 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 @@ } // 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 db554e679d5433f6d91b8fc41e2a908e9da6b8b5..b2478656cc72d5fe7f4cf4d19dc662dc5956bf6b 100644 --- a/torrent.go +++ b/torrent.go @@ -39,7 +39,7 @@ } return } -type PeersKey struct { +type peersKey struct { IPBytes string Port int } @@ -78,7 +78,7 @@ // 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 @@ cl.openNewConns(t) 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 }