2013-12-02 11:00:28 +01:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
2014-06-24 04:47:43 +02:00
|
|
|
// Package mock implements the models interface for a BitTorrent tracker's
|
|
|
|
// backend models. It can be used in production, but isn't recommended.
|
2013-12-02 11:00:28 +01:00
|
|
|
// Stored values will not persist if the tracker is restarted.
|
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
2014-05-07 14:42:41 +02:00
|
|
|
"sync"
|
|
|
|
|
2013-12-02 11:00:28 +01:00
|
|
|
"github.com/chihaya/chihaya/config"
|
2014-06-24 04:47:43 +02:00
|
|
|
"github.com/chihaya/chihaya/drivers/backend"
|
|
|
|
"github.com/chihaya/chihaya/models"
|
2013-12-02 11:00:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type driver struct{}
|
|
|
|
|
2014-05-07 14:42:41 +02:00
|
|
|
// Mock is a concrete implementation of the backend.Conn interface (plus some
|
|
|
|
// debugging methods) that stores deltas in memory.
|
|
|
|
type Mock struct {
|
2014-06-24 04:47:43 +02:00
|
|
|
deltaHistory []*models.AnnounceDelta
|
2014-05-07 14:42:41 +02:00
|
|
|
deltaHistoryM sync.RWMutex
|
|
|
|
}
|
2014-02-23 05:47:11 +01:00
|
|
|
|
2014-07-03 23:37:13 +02:00
|
|
|
func (d *driver) New(conf *config.DriverConfig) (backend.Conn, error) {
|
|
|
|
return &Mock{}, nil
|
2014-02-23 05:47:11 +01:00
|
|
|
}
|
|
|
|
|
2014-05-07 14:42:41 +02:00
|
|
|
// Close returns nil.
|
|
|
|
func (m *Mock) Close() error {
|
2014-02-23 05:47:11 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:42:41 +02:00
|
|
|
// RecordAnnounce adds a delta to the history.
|
2014-06-24 04:47:43 +02:00
|
|
|
func (m *Mock) RecordAnnounce(delta *models.AnnounceDelta) error {
|
2014-05-07 14:42:41 +02:00
|
|
|
m.deltaHistoryM.Lock()
|
|
|
|
defer m.deltaHistoryM.Unlock()
|
|
|
|
|
|
|
|
m.deltaHistory = append(m.deltaHistory, delta)
|
|
|
|
|
2013-12-02 11:00:28 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:42:41 +02:00
|
|
|
// DeltaHistory safely copies and returns the history of recorded deltas.
|
2014-06-24 04:47:43 +02:00
|
|
|
func (m *Mock) DeltaHistory() []models.AnnounceDelta {
|
2014-05-07 14:42:41 +02:00
|
|
|
m.deltaHistoryM.Lock()
|
|
|
|
defer m.deltaHistoryM.Unlock()
|
|
|
|
|
2014-06-24 04:47:43 +02:00
|
|
|
cp := make([]models.AnnounceDelta, len(m.deltaHistory))
|
2014-05-07 14:42:41 +02:00
|
|
|
for index, delta := range m.deltaHistory {
|
|
|
|
cp[index] = *delta
|
|
|
|
}
|
|
|
|
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTorrents returns (nil, nil).
|
2014-06-24 04:47:43 +02:00
|
|
|
func (m *Mock) LoadTorrents(ids []uint64) ([]*models.Torrent, error) {
|
2014-02-23 05:47:11 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:42:41 +02:00
|
|
|
// LoadAllTorrents returns (nil, nil).
|
2014-06-24 04:47:43 +02:00
|
|
|
func (m *Mock) LoadAllTorrents() ([]*models.Torrent, error) {
|
2014-02-23 05:47:11 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:42:41 +02:00
|
|
|
// LoadUsers returns (nil, nil).
|
2014-06-24 04:47:43 +02:00
|
|
|
func (m *Mock) LoadUsers(ids []uint64) ([]*models.User, error) {
|
2014-02-23 05:47:11 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:42:41 +02:00
|
|
|
// LoadAllUsers returns (nil, nil).
|
2014-06-24 04:47:43 +02:00
|
|
|
func (m *Mock) LoadAllUsers(ids []uint64) ([]*models.User, error) {
|
2014-02-23 05:47:11 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2013-12-02 11:00:28 +01:00
|
|
|
func init() {
|
|
|
|
backend.Register("mock", &driver{})
|
|
|
|
}
|