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
+26 -58
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()
+15 -19
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
}