2013-11-06 22:55:29 +08:00
|
|
|
package tracker
|
|
|
|
|
|
|
|
import (
|
2013-12-14 19:21:45 +08:00
|
|
|
"errors"
|
2013-11-06 22:55:29 +08:00
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AnnounceRequest struct {
|
|
|
|
InfoHash [20]byte
|
|
|
|
PeerId [20]byte
|
|
|
|
Downloaded int64
|
|
|
|
Left int64
|
|
|
|
Uploaded int64
|
|
|
|
Event AnnounceEvent
|
|
|
|
IPAddress int32
|
|
|
|
Key int32
|
2013-12-16 15:46:55 +08:00
|
|
|
NumWant int32 // How many peer addresses are desired. -1 for default.
|
2013-11-06 22:55:29 +08:00
|
|
|
Port int16
|
|
|
|
}
|
|
|
|
|
|
|
|
type AnnounceResponse struct {
|
2013-12-16 15:46:55 +08:00
|
|
|
Interval int32 // Minimum seconds the local peer should wait before next announce.
|
2013-11-06 22:55:29 +08:00
|
|
|
Leechers int32
|
|
|
|
Seeders int32
|
|
|
|
Peers []Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
type AnnounceEvent int32
|
|
|
|
|
|
|
|
type Peer struct {
|
|
|
|
IP net.IP
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2013-12-16 15:46:55 +08:00
|
|
|
None AnnounceEvent = iota
|
|
|
|
Completed // The local peer just completed the torrent.
|
|
|
|
Started // The local peer has just resumed this torrent.
|
|
|
|
Stopped // The local peer is leaving the swarm.
|
2013-11-06 22:55:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client interface {
|
2013-12-16 15:46:55 +08:00
|
|
|
// Returns ErrNotConnected if Connect needs to be called.
|
2013-11-06 22:55:29 +08:00
|
|
|
Announce(*AnnounceRequest) (AnnounceResponse, error)
|
2013-12-14 19:21:45 +08:00
|
|
|
Connect() error
|
2014-05-20 22:52:49 +08:00
|
|
|
String() string
|
2014-11-21 13:39:56 +08:00
|
|
|
URL() string
|
2013-11-06 22:55:29 +08:00
|
|
|
}
|
|
|
|
|
2013-12-14 19:21:45 +08:00
|
|
|
var (
|
|
|
|
ErrNotConnected = errors.New("not connected")
|
|
|
|
ErrBadScheme = errors.New("unknown scheme")
|
|
|
|
|
|
|
|
schemes = make(map[string]func(*url.URL) Client)
|
|
|
|
)
|
2013-11-06 22:55:29 +08:00
|
|
|
|
|
|
|
func RegisterClientScheme(scheme string, newFunc func(*url.URL) Client) {
|
2013-12-14 19:21:45 +08:00
|
|
|
schemes[scheme] = newFunc
|
2013-11-06 22:55:29 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 15:47:23 +08:00
|
|
|
// Returns ErrBadScheme if the tracker scheme isn't recognised.
|
|
|
|
func New(rawurl string) (cl Client, err error) {
|
|
|
|
url_s, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newFunc, ok := schemes[url_s.Scheme]
|
2013-12-14 19:21:45 +08:00
|
|
|
if !ok {
|
|
|
|
err = ErrBadScheme
|
|
|
|
return
|
|
|
|
}
|
2013-12-16 15:47:23 +08:00
|
|
|
cl = newFunc(url_s)
|
2013-12-14 19:21:45 +08:00
|
|
|
return
|
2013-11-06 22:55:29 +08:00
|
|
|
}
|