Tracker.Conn API made more RESTful

This commit is contained in:
Jimmy Zelinskie 2014-07-08 05:58:00 -04:00
parent 99ac8f77c8
commit f656133a6f
5 changed files with 70 additions and 98 deletions

View file

@ -47,151 +47,120 @@ func (c *Conn) FindClient(peerID string) error {
return nil
}
func (c *Conn) IncrementSnatches(t *models.Torrent) error {
func (c *Conn) IncrementSnatches(infohash string) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
}
torrent.Snatches++
t.Snatches++
return nil
}
func (c *Conn) MarkActive(t *models.Torrent) error {
func (c *Conn) MarkActive(infohash string) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
}
torrent.Active = true
t.Active = true
return nil
}
func (c *Conn) MarkInactive(t *models.Torrent) error {
func (c *Conn) AddLeecher(infohash string, p *models.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
}
torrent.Active = false
t.Active = false
return nil
}
func (c *Conn) AddLeecher(t *models.Torrent, p *models.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrTorrentDNE
}
torrent.Leechers[p.Key()] = *p
t.Leechers[p.Key()] = *p
return nil
}
func (c *Conn) AddSeeder(t *models.Torrent, p *models.Peer) error {
func (c *Conn) AddSeeder(infohash string, p *models.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
}
torrent.Seeders[p.Key()] = *p
t.Seeders[p.Key()] = *p
return nil
}
func (c *Conn) RemoveLeecher(t *models.Torrent, p *models.Peer) error {
func (c *Conn) DeleteLeecher(infohash, peerkey string) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
}
delete(torrent.Leechers, p.Key())
delete(t.Leechers, p.Key())
delete(t.Leechers, peerkey)
return nil
}
func (c *Conn) RemoveSeeder(t *models.Torrent, p *models.Peer) error {
func (c *Conn) DeleteSeeder(infohash, peerkey string) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
}
delete(torrent.Seeders, p.Key())
delete(t.Seeders, p.Key())
delete(t.Seeders, peerkey)
return nil
}
func (c *Conn) SetLeecher(t *models.Torrent, p *models.Peer) error {
func (c *Conn) PutLeecher(infohash string, p *models.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
}
torrent.Leechers[p.Key()] = *p
t.Leechers[p.Key()] = *p
return nil
}
func (c *Conn) SetSeeder(t *models.Torrent, p *models.Peer) error {
func (c *Conn) PutSeeder(infohash string, p *models.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
}
torrent.Seeders[p.Key()] = *p
t.Seeders[p.Key()] = *p
return nil
}
func (c *Conn) AddTorrent(t *models.Torrent) error {
func (c *Conn) PutTorrent(t *models.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent := *t
c.torrents[t.Infohash] = &torrent
c.torrents[t.Infohash] = &*t
return nil
}
func (c *Conn) RemoveTorrent(infohash string) error {
func (c *Conn) DeleteTorrent(infohash string) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
@ -200,17 +169,16 @@ func (c *Conn) RemoveTorrent(infohash string) error {
return nil
}
func (c *Conn) AddUser(u *models.User) error {
func (c *Conn) PutUser(u *models.User) error {
c.usersM.Lock()
defer c.usersM.Unlock()
user := *u
c.users[u.Passkey] = &user
c.users[u.Passkey] = &*u
return nil
}
func (c *Conn) RemoveUser(passkey string) error {
func (c *Conn) DeleteUser(passkey string) error {
c.usersM.Lock()
defer c.usersM.Unlock()
@ -219,7 +187,7 @@ func (c *Conn) RemoveUser(passkey string) error {
return nil
}
func (c *Conn) AddClient(peerID string) error {
func (c *Conn) PutClient(peerID string) error {
c.whitelistM.Lock()
defer c.whitelistM.Unlock()
@ -228,7 +196,7 @@ func (c *Conn) AddClient(peerID string) error {
return nil
}
func (c *Conn) RemoveClient(peerID string) error {
func (c *Conn) DeleteClient(peerID string) error {
c.whitelistM.Lock()
defer c.whitelistM.Unlock()

View file

@ -71,36 +71,32 @@ type Pool interface {
type Conn interface {
// Torrent interactions
FindTorrent(infohash string) (*models.Torrent, error)
AddTorrent(t *models.Torrent) error
RemoveTorrent(infohash string) error
IncrementSnatches(t *models.Torrent) error
MarkActive(t *models.Torrent) error
AddLeecher(t *models.Torrent, p *models.Peer) error
SetLeecher(t *models.Torrent, p *models.Peer) error
RemoveLeecher(t *models.Torrent, p *models.Peer) error
AddSeeder(t *models.Torrent, p *models.Peer) error
SetSeeder(t *models.Torrent, p *models.Peer) error
RemoveSeeder(t *models.Torrent, p *models.Peer) error
PutTorrent(t *models.Torrent) error
DeleteTorrent(infohash string) error
IncrementSnatches(infohash string) error
MarkActive(infohash string) error
PutLeecher(infohash string, p *models.Peer) error
DeleteLeecher(infohash, peerkey string) error
PutSeeder(infohash string, p *models.Peer) error
DeleteSeeder(infohash, peerkey string) error
// User interactions
FindUser(passkey string) (*models.User, error)
AddUser(u *models.User) error
RemoveUser(passkey string) error
PutUser(u *models.User) error
DeleteUser(passkey string) error
// Whitelist interactions
FindClient(clientID string) error
AddClient(clientID string) error
RemoveClient(clientID string) error
PutClient(clientID string) error
DeleteClient(clientID string) error
}
// LeecherFinished moves a peer from the leeching pool to the seeder pool.
func LeecherFinished(c Conn, t *models.Torrent, p *models.Peer) error {
err := c.RemoveLeecher(t, p)
func LeecherFinished(c Conn, infohash string, p *models.Peer) error {
err := c.DeleteLeecher(infohash, p.Key())
if err != nil {
return err
}
err = c.AddSeeder(t, p)
err = c.PutSeeder(infohash, p)
return err
}

View file

@ -86,36 +86,41 @@ func (t *Tracker) ServeAnnounce(w http.ResponseWriter, r *http.Request, p httpro
func updateTorrent(c tracker.Conn, a *models.Announce, p *models.Peer, t *models.Torrent) (created bool, err error) {
if !t.Active && a.Left == 0 {
err = c.MarkActive(t)
err = c.MarkActive(t.Infohash)
if err != nil {
return
}
t.Active = true
}
switch {
case t.InSeederPool(p):
err = c.SetSeeder(t, p)
err = c.PutSeeder(t.Infohash, p)
if err != nil {
return
}
t.Seeders[p.Key()] = *p
case t.InLeecherPool(p):
err = c.SetLeecher(t, p)
err = c.PutLeecher(t.Infohash, p)
if err != nil {
return
}
t.Leechers[p.Key()] = *p
default:
if a.Left == 0 {
err = c.AddSeeder(t, p)
err = c.PutSeeder(t.Infohash, p)
if err != nil {
return
}
t.Seeders[p.Key()] = *p
} else {
err = c.AddLeecher(t, p)
err = c.PutLeecher(t.Infohash, p)
if err != nil {
return
}
t.Leechers[p.Key()] = *p
}
created = true
}
@ -127,27 +132,30 @@ func handleEvent(c tracker.Conn, a *models.Announce, p *models.Peer, u *models.U
switch {
case a.Event == "stopped" || a.Event == "paused":
if t.InSeederPool(p) {
err = c.RemoveSeeder(t, p)
err = c.DeleteSeeder(t.Infohash, p.Key())
if err != nil {
return
}
delete(t.Seeders, p.Key())
}
if t.InLeecherPool(p) {
err = c.RemoveLeecher(t, p)
err = c.DeleteLeecher(t.Infohash, p.Key())
if err != nil {
return
}
delete(t.Leechers, p.Key())
}
case a.Event == "completed":
err = c.IncrementSnatches(t)
err = c.IncrementSnatches(t.Infohash)
if err != nil {
return
}
snatched = true
t.Snatches++
if t.InLeecherPool(p) {
err = tracker.LeecherFinished(c, t, p)
err = tracker.LeecherFinished(c, t.Infohash, p)
if err != nil {
return
}
@ -155,7 +163,7 @@ func handleEvent(c tracker.Conn, a *models.Announce, p *models.Peer, u *models.U
case t.InLeecherPool(p) && a.Left == 0:
// A leecher completed but the event was never received.
err = tracker.LeecherFinished(c, t, p)
err = tracker.LeecherFinished(c, t.Infohash, p)
if err != nil {
return
}

View file

@ -32,21 +32,21 @@ func loadTestData(tkr *Tracker) (err error) {
return
}
err = conn.AddUser(&models.User{
err = conn.PutUser(&models.User{
ID: 1,
Passkey: "yby47f04riwpndba456rqxtmifenqxx1",
})
if err != nil {
return
}
err = conn.AddUser(&models.User{
err = conn.PutUser(&models.User{
ID: 2,
Passkey: "yby47f04riwpndba456rqxtmifenqxx2",
})
if err != nil {
return
}
err = conn.AddUser(&models.User{
err = conn.PutUser(&models.User{
ID: 3,
Passkey: "yby47f04riwpndba456rqxtmifenqxx3",
})
@ -54,7 +54,7 @@ func loadTestData(tkr *Tracker) (err error) {
return
}
err = conn.AddClient("TR2820")
err = conn.PutClient("TR2820")
if err != nil {
return
}
@ -66,12 +66,12 @@ func loadTestData(tkr *Tracker) (err error) {
Leechers: make(map[string]models.Peer),
}
err = conn.AddTorrent(torrent)
err = conn.PutTorrent(torrent)
if err != nil {
return
}
err = conn.AddLeecher(torrent, &models.Peer{
err = conn.PutLeecher(torrent.Infohash, &models.Peer{
ID: "-TR2820-l71jtqkl8xx1",
UserID: 1,
TorrentID: torrent.ID,
@ -83,7 +83,7 @@ func loadTestData(tkr *Tracker) (err error) {
return
}
err = conn.AddLeecher(torrent, &models.Peer{
err = conn.PutLeecher(torrent.Infohash, &models.Peer{
ID: "-TR2820-l71jtqkl8xx3",
UserID: 3,
TorrentID: torrent.ID,

View file

@ -45,7 +45,7 @@ func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httproute
return http.StatusInternalServerError
}
err = conn.RemoveTorrent(p.ByName("infohash"))
err = conn.DeleteTorrent(p.ByName("infohash"))
if err == tracker.ErrTorrentDNE {
return http.StatusNotFound
} else if err != nil {
@ -87,7 +87,7 @@ func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.P
return http.StatusInternalServerError
}
err = conn.RemoveUser(p.ByName("passkey"))
err = conn.DeleteUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
return http.StatusNotFound
} else if err != nil {
@ -103,7 +103,7 @@ func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter
return http.StatusInternalServerError
}
err = conn.FindClient(p.ByName("clientID"))
err = conn.PutClient(p.ByName("clientID"))
if err != nil {
return http.StatusInternalServerError
}
@ -117,7 +117,7 @@ func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter
return http.StatusInternalServerError
}
err = conn.RemoveClient(p.ByName("clientID"))
err = conn.DeleteClient(p.ByName("clientID"))
if err != nil {
return http.StatusInternalServerError
}