+++ /dev/null
-.idea/
\ No newline at end of file
if !ok {
return
}
- T = clientTorrent{cl, t}
+ T = Torrent{cl, t}
return
}
return
}
var (
- k PeersKey
+ k peersKey
p Peer
)
for k, p = range t.Peers {
t = &torrent{
InfoHash: ih,
chunkSize: defaultChunkSize,
- Peers: make(map[PeersKey]Peer),
+ Peers: make(map[peersKey]Peer),
closing: make(chan struct{}),
ceasingNetworking: make(chan struct{}),
// 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()
}
// 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())
}
}
-func (t clientTorrent) AddPeers(pp []Peer) error {
+func (t Torrent) AddPeers(pp []Peer) error {
cl := t.cl
cl.mu.Lock()
defer cl.mu.Unlock()
// 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()) {
// 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()
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
// 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)
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{}
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)
}
}
})
bar.PrependFunc(func(*uiprogress.Bar) string {
- return t.Info().Name
+ return t.Name()
})
go func() {
<-t.GotInfo()
// 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
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{
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
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,
}
}
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
// Accesses torrent data via a client.
type Reader struct {
- t *clientTorrent
+ t *Torrent
pos int64
responsive bool
readahead int64
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,
// 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()
// 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)
// 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
-}
return
}
-type PeersKey struct {
+type peersKey struct {
IPBytes string
Port int
}
// 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
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
}