]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Merge all the tracker packages, why would anyone want them separately?
authorMatt Joiner <anacrolix@gmail.com>
Fri, 27 Mar 2015 04:12:15 +0000 (15:12 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 27 Mar 2015 04:12:15 +0000 (15:12 +1100)
client.go
cmd/tracker-announce/main.go
tracker/http.go [moved from tracker/http/httptracker.go with 82% similarity]
tracker/udp.go [moved from tracker/udp/udp_tracker.go with 84% similarity]
tracker/udp_test.go [moved from tracker/udp/udp_tracker_test.go with 88% similarity]
util/types.go

index 5c49565cadfc47ab7f4c4adf3ca5d479971a83a1..0436dc3c1368dc963a48a989a36e46d69ea3c4d1 100644 (file)
--- 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"
 )
 
index 499b180a0cf3825c7e7cc5e7c86da7b946d1ad18..eee4720119ff6659fb665474ff461b4b7b71f1ce 100644 (file)
@@ -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) {
similarity index 82%
rename from tracker/http/httptracker.go
rename to tracker/http.go
index 432d00cf62a4d2f2fb01374d78dc658b803ab494..8c2682a89d18359498d99045f0c8c49bbad177b3 100644 (file)
@@ -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
similarity index 84%
rename from tracker/udp/udp_tracker.go
rename to tracker/udp.go
index 857436b38d7169a9947925a8f6c65d787001cbab..4c795c5279f60e51a7d525d2d391311ab8690d0c 100644 (file)
@@ -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
        }
similarity index 88%
rename from tracker/udp/udp_tracker_test.go
rename to tracker/udp_test.go
index 216cff833f7aadfe3de56e096f0987770b2afd3f..6266fa1718d5eb4f68002b52648dd1a924290003 100644 (file)
@@ -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()
index b3c876a50a0fdb07d33bfe9205e6da77c53dd014..cbf2872b7c48deaf14b4dc263f28ddd2ef650458 100644 (file)
@@ -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