mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-02 02:20:00 -07:00
repurpose drivers from mock to no-op and memory
This commit is contained in:
206
drivers/tracker/memory/conn.go
Normal file
206
drivers/tracker/memory/conn.go
Normal file
@@ -0,0 +1,206 @@
|
||||
// Copyright 2014 The Chihaya Authors. All rights reserved.
|
||||
// Use of this source code is governed by the BSD 2-Clause license,
|
||||
// which can be found in the LICENSE file.
|
||||
|
||||
package memory
|
||||
|
||||
import (
|
||||
"github.com/chihaya/chihaya/drivers/tracker"
|
||||
"github.com/chihaya/chihaya/models"
|
||||
)
|
||||
|
||||
// Conn implements a connection to a memory-based tracker data store.
|
||||
type Conn struct {
|
||||
*Pool
|
||||
}
|
||||
|
||||
func (c *Conn) FindUser(passkey string) (*models.User, error) {
|
||||
c.usersM.RLock()
|
||||
defer c.usersM.RUnlock()
|
||||
|
||||
user, exists := c.users[passkey]
|
||||
if !exists {
|
||||
return nil, tracker.ErrUserDNE
|
||||
}
|
||||
return &*user, nil
|
||||
}
|
||||
|
||||
func (c *Conn) FindTorrent(infohash string) (*models.Torrent, error) {
|
||||
c.torrentsM.RLock()
|
||||
defer c.torrentsM.RUnlock()
|
||||
|
||||
torrent, exists := c.torrents[infohash]
|
||||
if !exists {
|
||||
return nil, tracker.ErrTorrentDNE
|
||||
}
|
||||
return &*torrent, nil
|
||||
}
|
||||
|
||||
func (c *Conn) FindClient(peerID string) error {
|
||||
c.whitelistM.RLock()
|
||||
defer c.whitelistM.RUnlock()
|
||||
|
||||
_, ok := c.whitelist[peerID]
|
||||
if !ok {
|
||||
return tracker.ErrClientUnapproved
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) IncrementSnatches(infohash string) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
t, ok := c.torrents[infohash]
|
||||
if !ok {
|
||||
return tracker.ErrTorrentDNE
|
||||
}
|
||||
t.Snatches++
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) MarkActive(infohash string) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
t, ok := c.torrents[infohash]
|
||||
if !ok {
|
||||
return tracker.ErrTorrentDNE
|
||||
}
|
||||
t.Active = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) AddLeecher(infohash string, p *models.Peer) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
t, ok := c.torrents[infohash]
|
||||
if !ok {
|
||||
return tracker.ErrTorrentDNE
|
||||
}
|
||||
t.Leechers[p.Key()] = *p
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) AddSeeder(infohash string, p *models.Peer) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
t, ok := c.torrents[infohash]
|
||||
if !ok {
|
||||
return tracker.ErrTorrentDNE
|
||||
}
|
||||
t.Seeders[p.Key()] = *p
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) DeleteLeecher(infohash, peerkey string) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
t, ok := c.torrents[infohash]
|
||||
if !ok {
|
||||
return tracker.ErrTorrentDNE
|
||||
}
|
||||
delete(t.Leechers, peerkey)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) DeleteSeeder(infohash, peerkey string) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
t, ok := c.torrents[infohash]
|
||||
if !ok {
|
||||
return tracker.ErrTorrentDNE
|
||||
}
|
||||
delete(t.Seeders, peerkey)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) PutLeecher(infohash string, p *models.Peer) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
t, ok := c.torrents[infohash]
|
||||
if !ok {
|
||||
return tracker.ErrTorrentDNE
|
||||
}
|
||||
t.Leechers[p.Key()] = *p
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) PutSeeder(infohash string, p *models.Peer) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
t, ok := c.torrents[infohash]
|
||||
if !ok {
|
||||
return tracker.ErrTorrentDNE
|
||||
}
|
||||
t.Seeders[p.Key()] = *p
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) PutTorrent(t *models.Torrent) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
c.torrents[t.Infohash] = &*t
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) DeleteTorrent(infohash string) error {
|
||||
c.torrentsM.Lock()
|
||||
defer c.torrentsM.Unlock()
|
||||
|
||||
delete(c.torrents, infohash)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) PutUser(u *models.User) error {
|
||||
c.usersM.Lock()
|
||||
defer c.usersM.Unlock()
|
||||
|
||||
c.users[u.Passkey] = &*u
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) DeleteUser(passkey string) error {
|
||||
c.usersM.Lock()
|
||||
defer c.usersM.Unlock()
|
||||
|
||||
delete(c.users, passkey)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) PutClient(peerID string) error {
|
||||
c.whitelistM.Lock()
|
||||
defer c.whitelistM.Unlock()
|
||||
|
||||
c.whitelist[peerID] = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) DeleteClient(peerID string) error {
|
||||
c.whitelistM.Lock()
|
||||
defer c.whitelistM.Unlock()
|
||||
|
||||
delete(c.whitelist, peerID)
|
||||
|
||||
return nil
|
||||
}
|
||||
27
drivers/tracker/memory/driver.go
Normal file
27
drivers/tracker/memory/driver.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright 2014 The Chihaya Authors. All rights reserved.
|
||||
// Use of this source code is governed by the BSD 2-Clause license,
|
||||
// which can be found in the LICENSE file.
|
||||
|
||||
// Package memory implements a Chihaya tracker storage driver within memory.
|
||||
// Stored values will not persist if the tracker is restarted.
|
||||
package memory
|
||||
|
||||
import (
|
||||
"github.com/chihaya/chihaya/config"
|
||||
"github.com/chihaya/chihaya/drivers/tracker"
|
||||
"github.com/chihaya/chihaya/models"
|
||||
)
|
||||
|
||||
type driver struct{}
|
||||
|
||||
func (d *driver) New(conf *config.DriverConfig) tracker.Pool {
|
||||
return &Pool{
|
||||
users: make(map[string]*models.User),
|
||||
torrents: make(map[string]*models.Torrent),
|
||||
whitelist: make(map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
tracker.Register("memory", &driver{})
|
||||
}
|
||||
33
drivers/tracker/memory/pool.go
Normal file
33
drivers/tracker/memory/pool.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright 2014 The Chihaya Authors. All rights reserved.
|
||||
// Use of this source code is governed by the BSD 2-Clause license,
|
||||
// which can be found in the LICENSE file.
|
||||
|
||||
package memory
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/chihaya/chihaya/drivers/tracker"
|
||||
"github.com/chihaya/chihaya/models"
|
||||
)
|
||||
|
||||
type Pool struct {
|
||||
users map[string]*models.User
|
||||
usersM sync.RWMutex
|
||||
|
||||
torrents map[string]*models.Torrent
|
||||
torrentsM sync.RWMutex
|
||||
|
||||
whitelist map[string]bool
|
||||
whitelistM sync.RWMutex
|
||||
}
|
||||
|
||||
func (p *Pool) Get() (tracker.Conn, error) {
|
||||
return &Conn{
|
||||
Pool: p,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Pool) Close() error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user