Revert "Merge pull request #54 from zhulik/master"
This reverts commit5cf75b869c
, reversing changes made to09050ff2c5
.
This commit is contained in:
parent
5dabd3ed03
commit
153c13db43
|
@ -1 +0,0 @@
|
|||
.idea/
|
20
client.go
20
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
|
||||
|
|
|
@ -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{}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
2
file.go
2
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
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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 @@ 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
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
// Accesses torrent data via a client.
|
||||
type Reader struct {
|
||||
t *clientTorrent
|
||||
t *Torrent
|
||||
pos int64
|
||||
responsive bool
|
||||
readahead int64
|
||||
|
|
64
t.go
64
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue