From: Matt Joiner Date: Fri, 27 Mar 2015 04:12:15 +0000 (+1100) Subject: Merge all the tracker packages, why would anyone want them separately? X-Git-Tag: v1.0.0~1239 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=5f7ad4c7693e589f89efbf15d114a9999f1f619c;p=btrtrc.git Merge all the tracker packages, why would anyone want them separately? --- diff --git a/client.go b/client.go index 5c49565c..0436dc3c 100644 --- a/client.go +++ b/client.go @@ -53,7 +53,6 @@ import ( "github.com/anacrolix/torrent/mse" pp "github.com/anacrolix/torrent/peer_protocol" "github.com/anacrolix/torrent/tracker" - _ "github.com/anacrolix/torrent/tracker/udp" . "github.com/anacrolix/torrent/util" ) diff --git a/cmd/tracker-announce/main.go b/cmd/tracker-announce/main.go index 499b180a..eee47201 100644 --- a/cmd/tracker-announce/main.go +++ b/cmd/tracker-announce/main.go @@ -9,8 +9,6 @@ import ( "github.com/anacrolix/torrent" "github.com/anacrolix/torrent/tracker" - _ "github.com/anacrolix/torrent/tracker/http" - _ "github.com/anacrolix/torrent/tracker/udp" ) func argSpec(arg string) (ts *torrent.TorrentSpec, err error) { diff --git a/tracker/http/httptracker.go b/tracker/http.go similarity index 82% rename from tracker/http/httptracker.go rename to tracker/http.go index 432d00cf..8c2682a8 100644 --- a/tracker/http/httptracker.go +++ b/tracker/http.go @@ -1,4 +1,4 @@ -package http +package tracker import ( "bytes" @@ -11,22 +11,20 @@ import ( "net/url" "strconv" - "github.com/anacrolix/torrent/util" - "github.com/anacrolix/libtorgo/bencode" - "github.com/anacrolix/torrent/tracker" + "github.com/anacrolix/torrent/util" ) func init() { - tracker.RegisterClientScheme("http", NewClient) + RegisterClientScheme("http", NewClient) } type client struct { url url.URL } -func NewClient(url *url.URL) tracker.Client { +func NewClient(url *url.URL) Client { return &client{ url: *url, } @@ -41,7 +39,7 @@ type response struct { Peers interface{} `bencode:"peers"` } -func (r *response) UnmarshalPeers() (ret []tracker.Peer, err error) { +func (r *response) UnmarshalPeers() (ret []Peer, err error) { s, ok := r.Peers.(string) if !ok { err = fmt.Errorf("unsupported peers value type: %T", r.Peers) @@ -52,14 +50,14 @@ func (r *response) UnmarshalPeers() (ret []tracker.Peer, err error) { if err != nil { return } - ret = make([]tracker.Peer, 0, len(cp)) + ret = make([]Peer, 0, len(cp)) for _, p := range cp { - ret = append(ret, tracker.Peer{net.IP(p.IP[:]), int(p.Port)}) + ret = append(ret, Peer{net.IP(p.IP[:]), int(p.Port)}) } return } -func (me *client) Announce(ar *tracker.AnnounceRequest) (ret tracker.AnnounceResponse, err error) { +func (me *client) Announce(ar *AnnounceRequest) (ret AnnounceResponse, err error) { q := make(url.Values) q.Set("info_hash", string(ar.InfoHash[:])) q.Set("peer_id", string(ar.PeerId[:])) @@ -67,7 +65,7 @@ func (me *client) Announce(ar *tracker.AnnounceRequest) (ret tracker.AnnounceRes q.Set("uploaded", strconv.FormatInt(ar.Uploaded, 10)) q.Set("downloaded", strconv.FormatInt(ar.Downloaded, 10)) q.Set("left", strconv.FormatInt(ar.Left, 10)) - if ar.Event != tracker.None { + if ar.Event != None { q.Set("event", ar.Event.String()) } // http://stackoverflow.com/questions/17418004/why-does-tracker-server-not-understand-my-request-bittorrent-protocol diff --git a/tracker/udp/udp_tracker.go b/tracker/udp.go similarity index 84% rename from tracker/udp/udp_tracker.go rename to tracker/udp.go index 857436b3..4c795c52 100644 --- a/tracker/udp/udp_tracker.go +++ b/tracker/udp.go @@ -1,4 +1,4 @@ -package udp_tracker +package tracker import ( "bytes" @@ -10,8 +10,6 @@ import ( "net" "net/url" "time" - - "github.com/anacrolix/torrent/tracker" ) type Action int32 @@ -55,18 +53,13 @@ type AnnounceResponseHeader struct { Seeders int32 } -type Peer struct { - IP [4]byte - Port uint16 -} - func init() { - tracker.RegisterClientScheme("udp", newClient) + RegisterClientScheme("udp", newClient) } -func newClient(url *url.URL) tracker.Client { - return &client{ - url: url, +func newClient(url *url.URL) Client { + return &udpClient{ + url: *url, } } @@ -85,25 +78,25 @@ func timeout(contiguousTimeouts int) (d time.Duration) { return } -type client struct { +type udpClient struct { contiguousTimeouts int connectionIdReceived time.Time connectionId int64 socket net.Conn - url *url.URL + url url.URL } -func (c *client) URL() string { +func (c *udpClient) URL() string { return c.url.String() } -func (c *client) String() string { +func (c *udpClient) String() string { return c.URL() } -func (c *client) Announce(req *tracker.AnnounceRequest) (res tracker.AnnounceResponse, err error) { +func (c *udpClient) Announce(req *AnnounceRequest) (res AnnounceResponse, err error) { if !c.connected() { - err = tracker.ErrNotConnected + err = ErrNotConnected return } reqURI := c.url.RequestURI() @@ -137,7 +130,7 @@ func (c *client) Announce(req *tracker.AnnounceRequest) (res tracker.AnnounceRes default: return } - res.Peers = append(res.Peers, tracker.Peer{ + res.Peers = append(res.Peers, Peer{ IP: p.IP[:], Port: int(p.Port), }) @@ -146,7 +139,7 @@ func (c *client) Announce(req *tracker.AnnounceRequest) (res tracker.AnnounceRes // body is the binary serializable request body. trailer is optional data // following it, such as for BEP 41. -func (c *client) write(h *RequestHeader, body interface{}, trailer []byte) (err error) { +func (c *udpClient) write(h *RequestHeader, body interface{}, trailer []byte) (err error) { buf := &bytes.Buffer{} err = binary.Write(buf, binary.BigEndian, h) if err != nil { @@ -182,7 +175,7 @@ func write(w io.Writer, data interface{}) error { // args is the binary serializable request body. trailer is optional data // following it, such as for BEP 41. -func (c *client) request(action Action, args interface{}, options []byte) (responseBody *bytes.Reader, err error) { +func (c *udpClient) request(action Action, args interface{}, options []byte) (responseBody *bytes.Reader, err error) { tid := newTransactionId() err = c.write(&RequestHeader{ ConnectionId: c.connectionId, @@ -238,11 +231,11 @@ func readBody(r *bytes.Reader, data ...interface{}) (err error) { return } -func (c *client) connected() bool { +func (c *udpClient) connected() bool { return !c.connectionIdReceived.IsZero() && time.Now().Before(c.connectionIdReceived.Add(time.Minute)) } -func (c *client) Connect() (err error) { +func (c *udpClient) Connect() (err error) { if c.connected() { return nil } diff --git a/tracker/udp/udp_tracker_test.go b/tracker/udp_test.go similarity index 88% rename from tracker/udp/udp_tracker_test.go rename to tracker/udp_test.go index 216cff83..6266fa17 100644 --- a/tracker/udp/udp_tracker_test.go +++ b/tracker/udp_test.go @@ -1,4 +1,4 @@ -package udp_tracker +package tracker import ( "bytes" @@ -13,7 +13,7 @@ import ( "syscall" "testing" - "github.com/anacrolix/torrent/tracker" + "github.com/anacrolix/torrent/util" ) // Ensure net.IPs are stored big-endian, to match the way they're read from @@ -29,8 +29,10 @@ func TestNetIPv4Bytes(t *testing.T) { } func TestMarshalAnnounceResponse(t *testing.T) { - w := bytes.NewBuffer(nil) - if err := binary.Write(w, binary.BigEndian, []Peer{{[4]byte{127, 0, 0, 1}, 2}, {[4]byte{255, 0, 0, 3}, 4}}); err != nil { + w := bytes.Buffer{} + peers := util.CompactPeers{{[4]byte{127, 0, 0, 1}, 2}, {[4]byte{255, 0, 0, 3}, 4}} + err := peers.WriteBinary(&w) + if err != nil { t.Fatalf("error writing udp announce response addrs: %s", err) } if w.String() != "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04" { @@ -87,16 +89,16 @@ func TestUDPTracker(t *testing.T) { if testing.Short() { t.SkipNow() } - tr, err := tracker.New("udp://tracker.openbittorrent.com:80/announce") + tr, err := New("udp://tracker.openbittorrent.com:80/announce") if err != nil { t.Skip(err) } if err := tr.Connect(); err != nil { t.Skip(err) } - req := tracker.AnnounceRequest{ + req := AnnounceRequest{ NumWant: -1, - Event: tracker.Started, + Event: Started, } rand.Read(req.PeerId[:]) copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}) @@ -111,8 +113,8 @@ func TestAnnounceRandomInfoHash(t *testing.T) { if testing.Short() { t.SkipNow() } - req := tracker.AnnounceRequest{ - Event: tracker.Stopped, + req := AnnounceRequest{ + Event: Stopped, } rand.Read(req.PeerId[:]) rand.Read(req.InfoHash[:]) @@ -126,7 +128,7 @@ func TestAnnounceRandomInfoHash(t *testing.T) { } { go func(url string) { defer wg.Done() - tr, err := tracker.New(url) + tr, err := New(url) if err != nil { t.Fatal(err) } @@ -165,7 +167,7 @@ func TestURLPathOption(t *testing.T) { t.Fatal(err) } log.Print("connected") - _, err = cl.Announce(&tracker.AnnounceRequest{}) + _, err = cl.Announce(&AnnounceRequest{}) if err != nil { t.Fatal(err) } @@ -184,7 +186,7 @@ func TestURLPathOption(t *testing.T) { n, _, _ := conn.ReadFrom(b[:]) r = bytes.NewReader(b[:n]) read(r, &h) - read(r, &tracker.AnnounceRequest{}) + read(r, &AnnounceRequest{}) all, _ := ioutil.ReadAll(r) if string(all) != "\x02\x09/announce" { t.FailNow() diff --git a/util/types.go b/util/types.go index b3c876a5..cbf2872b 100644 --- a/util/types.go +++ b/util/types.go @@ -5,6 +5,7 @@ import ( "encoding" "encoding/binary" "fmt" + "io" "github.com/anacrolix/libtorgo/bencode" ) @@ -33,6 +34,10 @@ func (me *CompactPeers) UnmarshalBinary(b []byte) (err error) { return } +func (me CompactPeers) WriteBinary(w io.Writer) error { + return binary.Write(w, binary.BigEndian, me) +} + type CompactPeer struct { IP [4]byte Port uint16