mock interface complete

This commit is contained in:
Jimmy Zelinskie 2013-11-30 00:54:08 -05:00
parent 81b1e4aa4d
commit 1eac367711
4 changed files with 138 additions and 5 deletions

View file

@ -6,6 +6,7 @@ package mock
import ( import (
"github.com/pushrax/chihaya/storage" "github.com/pushrax/chihaya/storage"
"github.com/pushrax/chihaya/storage/tracker"
) )
type Conn struct { type Conn struct {
@ -19,7 +20,8 @@ func (c *Conn) FindUser(passkey string) (*storage.User, bool, error) {
if !ok { if !ok {
return nil, false, nil return nil, false, nil
} }
return user, true, nil u := *user
return &u, true, nil
} }
func (c *Conn) FindTorrent(infohash string) (*storage.Torrent, bool, error) { func (c *Conn) FindTorrent(infohash string) (*storage.Torrent, bool, error) {
@ -29,10 +31,13 @@ func (c *Conn) FindTorrent(infohash string) (*storage.Torrent, bool, error) {
if !ok { if !ok {
return nil, false, nil return nil, false, nil
} }
return torrent, true, nil t := *torrent
return &t, true, nil
} }
func (c *Conn) ClientWhitelisted(peerID string) (bool, error) { func (c *Conn) ClientWhitelisted(peerID string) (bool, error) {
c.whitelistM.RLock()
defer c.whitelistM.RUnlock()
_, ok := c.whitelist[peerID] _, ok := c.whitelist[peerID]
if !ok { if !ok {
return false, nil return false, nil
@ -41,69 +46,193 @@ func (c *Conn) ClientWhitelisted(peerID string) (bool, error) {
} }
func (c *Conn) RecordSnatch(u *storage.User, t *storage.Torrent) error { func (c *Conn) RecordSnatch(u *storage.User, t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Snatches++
t.Snatches++
return nil return nil
} }
func (c *Conn) MarkActive(t *storage.Torrent) error { func (c *Conn) MarkActive(t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Active = true
t.Active = true
return nil
}
func (c *Conn) MarkInactive(t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Active = false
t.Active = false
return nil return nil
} }
func (c *Conn) AddLeecher(t *storage.Torrent, p *storage.Peer) error { func (c *Conn) AddLeecher(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Leechers[storage.PeerMapKey(p)] = *p
t.Leechers[storage.PeerMapKey(p)] = *p
return nil return nil
} }
func (c *Conn) AddSeeder(t *storage.Torrent, p *storage.Peer) error { func (c *Conn) AddSeeder(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Leechers[storage.PeerMapKey(p)] = *p
t.Leechers[storage.PeerMapKey(p)] = *p
return nil return nil
} }
func (c *Conn) RemoveLeecher(t *storage.Torrent, p *storage.Peer) error { func (c *Conn) RemoveLeecher(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
delete(torrent.Leechers, storage.PeerMapKey(p))
delete(t.Leechers, storage.PeerMapKey(p))
return nil return nil
} }
func (c *Conn) RemoveSeeder(t *storage.Torrent, p *storage.Peer) error { func (c *Conn) RemoveSeeder(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
delete(torrent.Seeders, storage.PeerMapKey(p))
delete(t.Seeders, storage.PeerMapKey(p))
return nil return nil
} }
func (c *Conn) SetLeecher(t *storage.Torrent, p *storage.Peer) error { func (c *Conn) SetLeecher(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Leechers[storage.PeerMapKey(p)] = *p
t.Leechers[storage.PeerMapKey(p)] = *p
return nil return nil
} }
func (c *Conn) SetSeeder(t *storage.Torrent, p *storage.Peer) error { func (c *Conn) SetSeeder(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Seeders[storage.PeerMapKey(p)] = *p
t.Seeders[storage.PeerMapKey(p)] = *p
return nil return nil
} }
func (c *Conn) IncrementSlots(u *storage.User) error { func (c *Conn) IncrementSlots(u *storage.User) error {
c.usersM.Lock()
defer c.usersM.Unlock()
user, ok := c.users[u.Passkey]
if !ok {
return tracker.ErrMissingResource
}
user.Slots++
u.Slots++
return nil return nil
} }
func (c *Conn) DecrementSlots(u *storage.User) error { func (c *Conn) DecrementSlots(u *storage.User) error {
c.usersM.Lock()
defer c.usersM.Unlock()
user, ok := c.users[u.Passkey]
if !ok {
return tracker.ErrMissingResource
}
user.Slots--
u.Slots--
return nil return nil
} }
func (c *Conn) LeecherFinished(t *storage.Torrent, p *storage.Peer) error { func (c *Conn) LeecherFinished(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Seeders[storage.PeerMapKey(p)] = *p
delete(torrent.Leechers, storage.PeerMapKey(p))
t.Seeders[storage.PeerMapKey(p)] = *p
delete(t.Leechers, storage.PeerMapKey(p))
return nil return nil
} }
func (c *Conn) AddTorrent(t *storage.Torrent) error { func (c *Conn) AddTorrent(t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent := *t
c.torrents[t.Infohash] = &torrent
return nil return nil
} }
func (c *Conn) RemoveTorrent(t *storage.Torrent) error { func (c *Conn) RemoveTorrent(t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
delete(c.torrents, t.Infohash)
return nil return nil
} }
func (c *Conn) AddUser(u *storage.User) error { func (c *Conn) AddUser(u *storage.User) error {
c.usersM.Lock()
defer c.usersM.Unlock()
user := *u
c.users[u.Passkey] = &user
return nil return nil
} }
func (c *Conn) RemoveUser(u *storage.User) error { func (c *Conn) RemoveUser(u *storage.User) error {
c.usersM.Lock()
defer c.usersM.Unlock()
delete(c.users, u.Passkey)
return nil return nil
} }
func (c *Conn) WhitelistClient(peerID string) error { func (c *Conn) WhitelistClient(peerID string) error {
c.whitelistM.Lock()
defer c.whitelistM.Unlock()
c.whitelist[peerID] = true
return nil return nil
} }
func (c *Conn) UnWhitelistClient(peerID string) error { func (c *Conn) UnWhitelistClient(peerID string) error {
c.whitelistM.Lock()
defer c.whitelistM.Unlock()
delete(c.whitelist, peerID)
return nil return nil
} }

View file

@ -19,6 +19,6 @@ func (d *driver) New(conf *config.DataStore) tracker.Pool {
return &Pool{ return &Pool{
users: make(map[string]*storage.User), users: make(map[string]*storage.User),
torrents: make(map[string]*storage.Torrent), torrents: make(map[string]*storage.Torrent),
whitelist: make(map[string]string), whitelist: make(map[string]bool),
} }
} }

View file

@ -18,7 +18,8 @@ type Pool struct {
torrents map[string]*storage.Torrent torrents map[string]*storage.Torrent
torrentsM sync.RWMutex torrentsM sync.RWMutex
whitelist map[string]string whitelist map[string]bool
whitelistM sync.RWMutex
} }
func (p *Pool) Get() (tracker.Conn, error) { func (p *Pool) Get() (tracker.Conn, error) {

View file

@ -7,6 +7,7 @@
package tracker package tracker
import ( import (
"errors"
"fmt" "fmt"
"github.com/pushrax/chihaya/config" "github.com/pushrax/chihaya/config"
@ -14,7 +15,8 @@ import (
) )
var ( var (
drivers = make(map[string]Driver) ErrMissingResource = errors.New("tracker: resource missing")
drivers = make(map[string]Driver)
) )
type Driver interface { type Driver interface {
@ -65,6 +67,7 @@ type Conn interface {
// Writes // Writes
RecordSnatch(u *storage.User, t *storage.Torrent) error RecordSnatch(u *storage.User, t *storage.Torrent) error
MarkActive(t *storage.Torrent) error MarkActive(t *storage.Torrent) error
MarkInactive(t *storage.Torrent) error
AddLeecher(t *storage.Torrent, p *storage.Peer) error AddLeecher(t *storage.Torrent, p *storage.Peer) error
AddSeeder(t *storage.Torrent, p *storage.Peer) error AddSeeder(t *storage.Torrent, p *storage.Peer) error
RemoveLeecher(t *storage.Torrent, p *storage.Peer) error RemoveLeecher(t *storage.Torrent, p *storage.Peer) error