Tracker.Conn interface update to better fit API

This commit is contained in:
Jimmy Zelinskie 2014-07-07 22:44:27 -04:00
parent 9e94643379
commit 99ac8f77c8
5 changed files with 54 additions and 28 deletions

View file

@ -36,7 +36,7 @@ func (c *Conn) FindTorrent(infohash string) (*models.Torrent, error) {
return &*torrent, nil return &*torrent, nil
} }
func (c *Conn) ClientWhitelisted(peerID string) error { func (c *Conn) FindClient(peerID string) error {
c.whitelistM.RLock() c.whitelistM.RLock()
defer c.whitelistM.RUnlock() defer c.whitelistM.RUnlock()
@ -191,11 +191,11 @@ func (c *Conn) AddTorrent(t *models.Torrent) error {
return nil return nil
} }
func (c *Conn) RemoveTorrent(t *models.Torrent) error { func (c *Conn) RemoveTorrent(infohash string) error {
c.torrentsM.Lock() c.torrentsM.Lock()
defer c.torrentsM.Unlock() defer c.torrentsM.Unlock()
delete(c.torrents, t.Infohash) delete(c.torrents, infohash)
return nil return nil
} }
@ -210,16 +210,16 @@ func (c *Conn) AddUser(u *models.User) error {
return nil return nil
} }
func (c *Conn) RemoveUser(u *models.User) error { func (c *Conn) RemoveUser(passkey string) error {
c.usersM.Lock() c.usersM.Lock()
defer c.usersM.Unlock() defer c.usersM.Unlock()
delete(c.users, u.Passkey) delete(c.users, passkey)
return nil return nil
} }
func (c *Conn) WhitelistClient(peerID string) error { func (c *Conn) AddClient(peerID string) error {
c.whitelistM.Lock() c.whitelistM.Lock()
defer c.whitelistM.Unlock() defer c.whitelistM.Unlock()
@ -228,7 +228,7 @@ func (c *Conn) WhitelistClient(peerID string) error {
return nil return nil
} }
func (c *Conn) UnWhitelistClient(peerID string) error { func (c *Conn) RemoveClient(peerID string) error {
c.whitelistM.Lock() c.whitelistM.Lock()
defer c.whitelistM.Unlock() defer c.whitelistM.Unlock()

View file

@ -69,28 +69,30 @@ type Pool interface {
// Conn represents a connection to the data store that can be used // Conn represents a connection to the data store that can be used
// to make reads/writes. // to make reads/writes.
type Conn interface { type Conn interface {
// Reads // Torrent interactions
FindUser(passkey string) (*models.User, error)
FindTorrent(infohash string) (*models.Torrent, error) FindTorrent(infohash string) (*models.Torrent, error)
ClientWhitelisted(clientID string) error AddTorrent(t *models.Torrent) error
RemoveTorrent(infohash string) error
// Writes
IncrementSnatches(t *models.Torrent) error IncrementSnatches(t *models.Torrent) error
MarkActive(t *models.Torrent) error MarkActive(t *models.Torrent) error
AddLeecher(t *models.Torrent, p *models.Peer) error
AddSeeder(t *models.Torrent, p *models.Peer) error
RemoveLeecher(t *models.Torrent, p *models.Peer) error
RemoveSeeder(t *models.Torrent, p *models.Peer) error
SetLeecher(t *models.Torrent, p *models.Peer) error
SetSeeder(t *models.Torrent, p *models.Peer) error
// Priming / Testing AddLeecher(t *models.Torrent, p *models.Peer) error
AddTorrent(t *models.Torrent) error SetLeecher(t *models.Torrent, p *models.Peer) error
RemoveTorrent(t *models.Torrent) 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
// User interactions
FindUser(passkey string) (*models.User, error)
AddUser(u *models.User) error AddUser(u *models.User) error
RemoveUser(u *models.User) error RemoveUser(passkey string) error
WhitelistClient(clientID string) error
UnWhitelistClient(clientID string) error // Whitelist interactions
FindClient(clientID string) error
AddClient(clientID string) error
RemoveClient(clientID string) error
} }
// LeecherFinished moves a peer from the leeching pool to the seeder pool. // LeecherFinished moves a peer from the leeching pool to the seeder pool.

View file

@ -31,7 +31,7 @@ func (t *Tracker) ServeAnnounce(w http.ResponseWriter, r *http.Request, p httpro
} }
if t.cfg.Whitelist { if t.cfg.Whitelist {
err = conn.ClientWhitelisted(ann.ClientID()) err = conn.FindClient(ann.ClientID())
if err == tracker.ErrClientUnapproved { if err == tracker.ErrClientUnapproved {
fail(w, r, err) fail(w, r, err)
return http.StatusOK return http.StatusOK

View file

@ -54,7 +54,7 @@ func loadTestData(tkr *Tracker) (err error) {
return return
} }
err = conn.WhitelistClient("TR2820") err = conn.AddClient("TR2820")
if err != nil { if err != nil {
return return
} }

View file

@ -40,6 +40,18 @@ func (t *Tracker) putTorrent(w http.ResponseWriter, r *http.Request, p httproute
} }
func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
}
err = conn.RemoveTorrent(p.ByName("infohash"))
if err == tracker.ErrTorrentDNE {
return http.StatusNotFound
} else if err != nil {
return http.StatusInternalServerError
}
return http.StatusOK return http.StatusOK
} }
@ -70,6 +82,18 @@ func (t *Tracker) putUser(w http.ResponseWriter, r *http.Request, p httprouter.P
} }
func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
conn, err := t.tp.Get()
if err != nil {
return http.StatusInternalServerError
}
err = conn.RemoveUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
return http.StatusNotFound
} else if err != nil {
return http.StatusInternalServerError
}
return http.StatusOK return http.StatusOK
} }
@ -79,7 +103,7 @@ func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter
return http.StatusInternalServerError return http.StatusInternalServerError
} }
err = conn.WhitelistClient(p.ByName("clientID")) err = conn.FindClient(p.ByName("clientID"))
if err != nil { if err != nil {
return http.StatusInternalServerError return http.StatusInternalServerError
} }
@ -93,7 +117,7 @@ func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter
return http.StatusInternalServerError return http.StatusInternalServerError
} }
err = conn.UnWhitelistClient(p.ByName("clientID")) err = conn.RemoveClient(p.ByName("clientID"))
if err != nil { if err != nil {
return http.StatusInternalServerError return http.StatusInternalServerError
} }