]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/tracker.go
f2cea82e9d852a980f62a6873c6b6205f5484b0a
[btrtrc.git] / tracker / tracker.go
1 package tracker
2
3 import (
4         "errors"
5         "net/http"
6         "net/url"
7 )
8
9 // Marshalled as binary by the UDP client, so be careful making changes.
10 type AnnounceRequest struct {
11         InfoHash   [20]byte
12         PeerId     [20]byte
13         Downloaded int64
14         Left       uint64
15         Uploaded   int64
16         // Apparently this is optional. None can be used for announces done at
17         // regular intervals.
18         Event     AnnounceEvent
19         IPAddress int32
20         Key       int32
21         NumWant   int32 // How many peer addresses are desired. -1 for default.
22         Port      uint16
23 } // 82 bytes
24
25 type AnnounceResponse struct {
26         Interval int32 // Minimum seconds the local peer should wait before next announce.
27         Leechers int32
28         Seeders  int32
29         Peers    []Peer
30 }
31
32 type AnnounceEvent int32
33
34 func (e AnnounceEvent) String() string {
35         // See BEP 3, "event".
36         return []string{"empty", "completed", "started", "stopped"}[e]
37 }
38
39 const (
40         None      AnnounceEvent = iota
41         Completed               // The local peer just completed the torrent.
42         Started                 // The local peer has just resumed this torrent.
43         Stopped                 // The local peer is leaving the swarm.
44 )
45
46 var (
47         ErrBadScheme = errors.New("unknown scheme")
48 )
49
50 // TODO: Just split udp/http announcing completely, to support various different options they have.
51
52 func Announce(cl *http.Client, userAgent string, urlStr string, req *AnnounceRequest) (res AnnounceResponse, err error) {
53         return AnnounceHost(cl, userAgent, urlStr, req, "")
54 }
55
56 func AnnounceHost(cl *http.Client, userAgent string, urlStr string, req *AnnounceRequest, host string) (res AnnounceResponse, err error) {
57         _url, err := url.Parse(urlStr)
58         if err != nil {
59                 return
60         }
61         switch _url.Scheme {
62         case "http", "https":
63                 return announceHTTP(cl, userAgent, req, _url, host)
64         case "udp":
65                 return announceUDP(req, _url)
66         default:
67                 err = ErrBadScheme
68                 return
69         }
70 }