]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/tracker.go
Move some tracker.CompactPeer into util
[btrtrc.git] / tracker / tracker.go
1 package tracker
2
3 import (
4         "errors"
5         "net"
6         "net/url"
7 )
8
9 type AnnounceRequest struct {
10         InfoHash   [20]byte
11         PeerId     [20]byte
12         Downloaded int64
13         Left       int64
14         Uploaded   int64
15         Event      AnnounceEvent
16         IPAddress  int32
17         Key        int32
18         NumWant    int32 // How many peer addresses are desired. -1 for default.
19         Port       int16
20 }
21
22 type AnnounceResponse struct {
23         Interval int32 // Minimum seconds the local peer should wait before next announce.
24         Leechers int32
25         Seeders  int32
26         Peers    []Peer
27 }
28
29 type AnnounceEvent int32
30
31 type Peer struct {
32         IP   net.IP
33         Port int
34 }
35
36 const (
37         None      AnnounceEvent = iota
38         Completed               // The local peer just completed the torrent.
39         Started                 // The local peer has just resumed this torrent.
40         Stopped                 // The local peer is leaving the swarm.
41 )
42
43 type Client interface {
44         // Returns ErrNotConnected if Connect needs to be called.
45         Announce(*AnnounceRequest) (AnnounceResponse, error)
46         Connect() error
47         String() string
48 }
49
50 var (
51         ErrNotConnected = errors.New("not connected")
52         ErrBadScheme    = errors.New("unknown scheme")
53
54         schemes = make(map[string]func(*url.URL) Client)
55 )
56
57 func RegisterClientScheme(scheme string, newFunc func(*url.URL) Client) {
58         schemes[scheme] = newFunc
59 }
60
61 // Returns ErrBadScheme if the tracker scheme isn't recognised.
62 func New(rawurl string) (cl Client, err error) {
63         url_s, err := url.Parse(rawurl)
64         if err != nil {
65                 return
66         }
67         newFunc, ok := schemes[url_s.Scheme]
68         if !ok {
69                 err = ErrBadScheme
70                 return
71         }
72         cl = newFunc(url_s)
73         return
74 }