2
0
mirror of synced 2025-02-24 06:38:14 +00:00

Refactorings

This commit is contained in:
Gleb Sinyavsky 2016-01-05 11:42:26 +03:00
parent 8637086ef9
commit 48c3df2d43
9 changed files with 72 additions and 78 deletions

View File

@ -671,14 +671,14 @@ func (cl *Client) incomingConnection(nc net.Conn, utp bool) {
} }
// Returns a handle to the given torrent, if it's present in the client. // Returns a handle to the given torrent, if it's present in the client.
func (cl *Client) Torrent(ih InfoHash) (T Download, ok bool) { func (cl *Client) Torrent(ih InfoHash) (T Torrent, ok bool) {
cl.mu.Lock() cl.mu.Lock()
defer cl.mu.Unlock() defer cl.mu.Unlock()
t, ok := cl.torrents[ih] t, ok := cl.torrents[ih]
if !ok { if !ok {
return return
} }
T = Torrent{cl, t} T = clientTorrent{cl, t}
return return
} }
@ -2030,7 +2030,7 @@ type Handle interface {
// Returns handles to the files in the torrent. This requires the metainfo is // Returns handles to the files in the torrent. This requires the metainfo is
// available first. // available first.
func (t Torrent) Files() (ret []File) { func (t clientTorrent) Files() (ret []File) {
t.cl.mu.Lock() t.cl.mu.Lock()
info := t.Info() info := t.Info()
t.cl.mu.Unlock() t.cl.mu.Unlock()
@ -2052,7 +2052,7 @@ func (t Torrent) Files() (ret []File) {
} }
// Marks the pieces in the given region for download. // Marks the pieces in the given region for download.
func (t Torrent) SetRegionPriority(off, len int64) { func (t clientTorrent) SetRegionPriority(off, len int64) {
t.cl.mu.Lock() t.cl.mu.Lock()
defer t.cl.mu.Unlock() defer t.cl.mu.Unlock()
pieceSize := int64(t.usualPieceSize()) pieceSize := int64(t.usualPieceSize())
@ -2061,7 +2061,7 @@ func (t Torrent) SetRegionPriority(off, len int64) {
} }
} }
func (t Torrent) AddPeers(pp []Peer) error { func (t clientTorrent) AddPeers(pp []Peer) error {
cl := t.cl cl := t.cl
cl.mu.Lock() cl.mu.Lock()
defer cl.mu.Unlock() defer cl.mu.Unlock()
@ -2071,7 +2071,7 @@ func (t Torrent) AddPeers(pp []Peer) error {
// Marks the entire torrent for download. Requires the info first, see // Marks the entire torrent for download. Requires the info first, see
// GotInfo. // GotInfo.
func (t Torrent) DownloadAll() { func (t clientTorrent) DownloadAll() {
t.cl.mu.Lock() t.cl.mu.Lock()
defer t.cl.mu.Unlock() defer t.cl.mu.Unlock()
for i := range iter.N(t.numPieces()) { for i := range iter.N(t.numPieces()) {
@ -2157,8 +2157,8 @@ func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) (spec *TorrentSpec) {
// trackers will be merged with the existing ones. If the Info isn't yet // 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 // 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. // provides one. Returns new if the torrent wasn't already in the client.
func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (D Download, new bool, err error) { func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (D Torrent, new bool, err error) {
T := Torrent{} T := clientTorrent{}
T.cl = cl T.cl = cl
D = &T D = &T
cl.mu.Lock() cl.mu.Lock()
@ -2722,16 +2722,16 @@ func (cl *Client) verifyPiece(t *torrent, piece int) {
} }
// Returns handles to all the torrents loaded in the Client. // Returns handles to all the torrents loaded in the Client.
func (me *Client) Torrents() (ret []Download) { func (me *Client) Torrents() (ret []Torrent) {
me.mu.Lock() me.mu.Lock()
for _, t := range me.torrents { for _, t := range me.torrents {
ret = append(ret, Torrent{me, t}) ret = append(ret, clientTorrent{me, t})
} }
me.mu.Unlock() me.mu.Unlock()
return return
} }
func (me *Client) AddMagnet(uri string) (T Download, err error) { func (me *Client) AddMagnet(uri string) (T Torrent, err error) {
spec, err := TorrentSpecFromMagnetURI(uri) spec, err := TorrentSpecFromMagnetURI(uri)
if err != nil { if err != nil {
return return
@ -2740,12 +2740,12 @@ func (me *Client) AddMagnet(uri string) (T Download, err error) {
return return
} }
func (me *Client) AddTorrent(mi *metainfo.MetaInfo) (T Download, err error) { func (me *Client) AddTorrent(mi *metainfo.MetaInfo) (T Torrent, err error) {
T, _, err = me.AddTorrentSpec(TorrentSpecFromMetaInfo(mi)) T, _, err = me.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
return return
} }
func (me *Client) AddTorrentFromFile(filename string) (T Download, err error) { func (me *Client) AddTorrentFromFile(filename string) (T Torrent, err error) {
mi, err := metainfo.LoadFromFile(filename) mi, err := metainfo.LoadFromFile(filename)
if err != nil { if err != nil {
return return

View File

@ -134,7 +134,7 @@ func main() {
done := make(chan struct{}) done := make(chan struct{})
for _, arg := range posArgs { for _, arg := range posArgs {
t := func() torrent.Download { t := func() torrent.Torrent {
if strings.HasPrefix(arg, "magnet:") { if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg) t, err := client.AddMagnet(arg)
if err != nil { if err != nil {

View File

@ -36,7 +36,7 @@ func resolvedPeerAddrs(ss []string) (ret []torrent.Peer, err error) {
return return
} }
func torrentBar(t torrent.Download) { func torrentBar(t torrent.Torrent) {
bar := uiprogress.AddBar(1) bar := uiprogress.AddBar(1)
bar.AppendCompleted() bar.AppendCompleted()
bar.AppendFunc(func(*uiprogress.Bar) (ret string) { bar.AppendFunc(func(*uiprogress.Bar) (ret string) {
@ -69,7 +69,7 @@ func torrentBar(t torrent.Download) {
func addTorrents(client *torrent.Client) { func addTorrents(client *torrent.Client) {
for _, arg := range opts.Torrent { for _, arg := range opts.Torrent {
t := func() torrent.Download { t := func() torrent.Torrent {
if strings.HasPrefix(arg, "magnet:") { if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg) t, err := client.AddMagnet(arg)
if err != nil { if err != nil {

View File

@ -1,27 +0,0 @@
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
Files() (ret []File)
Peers() map[PeersKey]Peer
}

View File

@ -8,14 +8,14 @@ import (
// Provides access to regions of torrent data that correspond to its files. // Provides access to regions of torrent data that correspond to its files.
type File struct { type File struct {
t Torrent t clientTorrent
path string path string
offset int64 offset int64
length int64 length int64
fi metainfo.FileInfo fi metainfo.FileInfo
} }
func (f *File) Torrent() Download { func (f *File) Torrent() Torrent {
return f.t return f.t
} }

View File

@ -50,7 +50,7 @@ type node struct {
path string path string
metadata *metainfo.Info metadata *metainfo.Info
FS *TorrentFS FS *TorrentFS
t torrent.Download t torrent.Torrent
} }
type fileNode struct { type fileNode struct {
@ -69,7 +69,7 @@ func (n *node) fsPath() string {
return "/" + n.metadata.Name + "/" + n.path return "/" + n.metadata.Name + "/" + n.path
} }
func blockingRead(ctx context.Context, fs *TorrentFS, t torrent.Download, off int64, p []byte) (n int, err error) { func blockingRead(ctx context.Context, fs *TorrentFS, t torrent.Torrent, off int64, p []byte) (n int, err error) {
fs.mu.Lock() fs.mu.Lock()
fs.blockedReads++ fs.blockedReads++
fs.event.Broadcast() fs.event.Broadcast()
@ -101,7 +101,7 @@ func blockingRead(ctx context.Context, fs *TorrentFS, t torrent.Download, off in
return return
} }
func readFull(ctx context.Context, fs *TorrentFS, t torrent.Download, off int64, p []byte) (n int, err error) { func readFull(ctx context.Context, fs *TorrentFS, t torrent.Torrent, off int64, p []byte) (n int, err error) {
for len(p) != 0 { for len(p) != 0 {
var nn int var nn int
nn, err = blockingRead(ctx, fs, t, off, p) nn, err = blockingRead(ctx, fs, t, off, p)

View File

@ -1,15 +1,15 @@
package torrent package torrent
import ( import (
"github.com/anacrolix/torrent/metainfo"
"testing"
"path/filepath"
"os"
"github.com/anacrolix/torrent/dht"
"io"
"errors" "errors"
"fmt" "fmt"
"github.com/anacrolix/torrent/dht"
"github.com/anacrolix/torrent/metainfo"
"io"
"os"
"path/filepath"
"runtime" "runtime"
"testing"
) )
var numclients int = 0 var numclients int = 0

View File

@ -8,7 +8,7 @@ import (
// Accesses torrent data via a client. // Accesses torrent data via a client.
type Reader struct { type Reader struct {
t *Torrent t *clientTorrent
pos int64 pos int64
responsive bool responsive bool
readahead int64 readahead int64

55
t.go
View File

@ -2,41 +2,62 @@ package torrent
import ( import (
"github.com/anacrolix/missinggo/pubsub" "github.com/anacrolix/missinggo/pubsub"
"github.com/anacrolix/torrent/tracker"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/tracker"
) )
// This file contains Torrent, until I decide where the private, lower-case // This file contains Torrent, until I decide where the private, lower-case
// "torrent" type belongs. That type is currently mostly in torrent.go. // "torrent" type belongs. That type is currently mostly in torrent.go.
// The public handle to a live torrent within a Client. // The public interface to a live torrent within a Client.
type Torrent struct { 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 {
cl *Client cl *Client
*torrent *torrent
} }
// The torrent's infohash. This is fixed and cannot change. It uniquely // The torrent's infohash. This is fixed and cannot change. It uniquely
// identifies a torrent. // identifies a torrent.
func (t Torrent) InfoHash() InfoHash { func (t clientTorrent) InfoHash() InfoHash {
return t.torrent.InfoHash return t.torrent.InfoHash
} }
// Closed when the info (.Info()) for the torrent has become available. Using // 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 // features of Torrent that require the info before it is available will have
// undefined behaviour. // undefined behaviour.
func (t Torrent) GotInfo() <-chan struct{} { func (t clientTorrent) GotInfo() <-chan struct{} {
return t.torrent.gotMetainfo return t.torrent.gotMetainfo
} }
// Returns the metainfo, or nil if it's not yet available. // Returns the metainfo, or nil if it's not yet available.
func (t Torrent) Info() *metainfo.Info { func (t clientTorrent) Info() *metainfo.Info {
return t.torrent.Info return t.torrent.Info
} }
// Returns a Reader bound to the torrent's data. All read calls block until // 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 // the data requested is actually available. Priorities are set to ensure the
// data requested will be downloaded as soon as possible. // data requested will be downloaded as soon as possible.
func (t Torrent) NewReader() (ret *Reader) { func (t clientTorrent) NewReader() (ret *Reader) {
ret = &Reader{ ret = &Reader{
t: &t, t: &t,
readahead: 5 * 1024 * 1024, readahead: 5 * 1024 * 1024,
@ -47,25 +68,25 @@ func (t Torrent) NewReader() (ret *Reader) {
// Returns the state of pieces of the torrent. They are grouped into runs of // 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 // same state. The sum of the state run lengths is the number of pieces
// in the torrent. // in the torrent.
func (t Torrent) PieceStateRuns() []PieceStateRun { func (t clientTorrent) PieceStateRuns() []PieceStateRun {
t.stateMu.Lock() t.stateMu.Lock()
defer t.stateMu.Unlock() defer t.stateMu.Unlock()
return t.torrent.pieceStateRuns() return t.torrent.pieceStateRuns()
} }
func (t Torrent) NumPieces() int { func (t clientTorrent) NumPieces() int {
return t.numPieces() return t.numPieces()
} }
// Drop the torrent from the client, and close it. // Drop the torrent from the client, and close it.
func (t Torrent) Drop() { func (t clientTorrent) Drop() {
t.cl.mu.Lock() t.cl.mu.Lock()
t.cl.dropTorrent(t.torrent.InfoHash) t.cl.dropTorrent(t.torrent.InfoHash)
t.cl.mu.Unlock() t.cl.mu.Unlock()
} }
// Number of bytes of the entire torrent we have completed. // Number of bytes of the entire torrent we have completed.
func (t Torrent) BytesCompleted() int64 { func (t clientTorrent) BytesCompleted() int64 {
t.cl.mu.RLock() t.cl.mu.RLock()
defer t.cl.mu.RUnlock() defer t.cl.mu.RUnlock()
return t.bytesCompleted() return t.bytesCompleted()
@ -73,13 +94,13 @@ func (t Torrent) BytesCompleted() int64 {
// The subscription emits as (int) the index of pieces as their state changes. // 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. // A state change is when the PieceState for a piece alters in value.
func (t Torrent) SubscribePieceStateChanges() *pubsub.Subscription { func (t clientTorrent) SubscribePieceStateChanges() *pubsub.Subscription {
return t.torrent.pieceStateChanges.Subscribe() return t.torrent.pieceStateChanges.Subscribe()
} }
// Returns true if the torrent is currently being seeded. This occurs when the // Returns true if the torrent is currently being seeded. This occurs when the
// client is willing to upload without wanting anything in return. // client is willing to upload without wanting anything in return.
func (t Torrent) Seeding() bool { func (t clientTorrent) Seeding() bool {
t.cl.mu.Lock() t.cl.mu.Lock()
defer t.cl.mu.Unlock() defer t.cl.mu.Unlock()
return t.cl.seeding(t.torrent) return t.cl.seeding(t.torrent)
@ -87,23 +108,23 @@ func (t Torrent) Seeding() bool {
// Clobbers the torrent display name. The display name is used as the torrent // Clobbers the torrent display name. The display name is used as the torrent
// name if the metainfo is not available. // name if the metainfo is not available.
func (t Torrent) SetDisplayName(dn string) { func (t clientTorrent) SetDisplayName(dn string) {
t.cl.mu.Lock() t.cl.mu.Lock()
defer t.cl.mu.Unlock() defer t.cl.mu.Unlock()
t.torrent.setDisplayName(dn) t.torrent.setDisplayName(dn)
} }
// Client returns Torrent's client instance // Client returns Torrent's client instance
func (t Torrent) Client() *Client { func (t clientTorrent) Client() *Client {
return t.cl return t.cl
} }
// Trackers returns torrent's trackers // Trackers returns torrent's trackers
func (t Torrent) Trackers() [][]tracker.Client { func (t clientTorrent) Trackers() [][]tracker.Client {
return t.torrent.Trackers return t.torrent.Trackers
} }
// Peers returns torrent's peers // Peers returns torrent's peers
func (t Torrent) Peers() map[PeersKey]Peer { func (t clientTorrent) Peers() map[PeersKey]Peer {
return t.torrent.Peers return t.torrent.Peers
} }