2016-08-10 03:34:16 +02:00
|
|
|
package storage
|
2016-08-04 21:54:30 +02:00
|
|
|
|
|
|
|
import (
|
2017-02-21 06:58:57 +01:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
2017-06-03 22:15:43 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
2017-02-03 03:09:25 +01:00
|
|
|
"github.com/chihaya/chihaya/pkg/stop"
|
2016-08-04 21:54:30 +02:00
|
|
|
)
|
|
|
|
|
2017-02-21 06:58:57 +01:00
|
|
|
var (
|
|
|
|
driversM sync.RWMutex
|
|
|
|
drivers = make(map[string]Driver)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Driver is the interface used to initalize a new type of PeerStore.
|
|
|
|
type Driver interface {
|
|
|
|
NewPeerStore(cfg interface{}) (PeerStore, error)
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:54:30 +02:00
|
|
|
// ErrResourceDoesNotExist is the error returned by all delete methods in the
|
|
|
|
// store if the requested resource does not exist.
|
2016-08-05 07:47:04 +02:00
|
|
|
var ErrResourceDoesNotExist = bittorrent.ClientError("resource does not exist")
|
2016-08-04 21:54:30 +02:00
|
|
|
|
2017-02-21 06:58:57 +01:00
|
|
|
// ErrDriverDoesNotExist is the error returned by NewPeerStore when a peer
|
|
|
|
// store driver with that name does not exist.
|
|
|
|
var ErrDriverDoesNotExist = errors.New("peer store driver with that name does not exist")
|
|
|
|
|
2016-08-04 21:54:30 +02:00
|
|
|
// PeerStore is an interface that abstracts the interactions of storing and
|
|
|
|
// manipulating Peers such that it can be implemented for various data stores.
|
|
|
|
type PeerStore interface {
|
2016-09-08 15:33:58 +02:00
|
|
|
// PutSeeder adds a Seeder to the Swarm identified by the provided
|
|
|
|
// infoHash.
|
2016-08-04 21:54:30 +02:00
|
|
|
PutSeeder(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
|
|
|
|
2016-09-08 15:33:58 +02:00
|
|
|
// DeleteSeeder removes a Seeder from the Swarm identified by the
|
|
|
|
// provided infoHash.
|
2016-08-04 21:54:30 +02:00
|
|
|
//
|
|
|
|
// If the Swarm or Peer does not exist, this function should return
|
|
|
|
// ErrResourceDoesNotExist.
|
|
|
|
DeleteSeeder(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
|
|
|
|
|
|
|
// PutLeecher adds a Leecher to the Swarm identified by the provided
|
|
|
|
// infoHash.
|
|
|
|
PutLeecher(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
|
|
|
|
2016-09-08 15:33:58 +02:00
|
|
|
// DeleteLeecher removes a Leecher from the Swarm identified by the
|
|
|
|
// provided infoHash.
|
2016-08-04 21:54:30 +02:00
|
|
|
//
|
|
|
|
// If the Swarm or Peer does not exist, this function should return
|
|
|
|
// ErrResourceDoesNotExist.
|
|
|
|
DeleteLeecher(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
|
|
|
|
2016-09-08 15:33:58 +02:00
|
|
|
// GraduateLeecher promotes a Leecher to a Seeder in the Swarm
|
|
|
|
// identified by the provided infoHash.
|
2016-08-04 21:54:30 +02:00
|
|
|
//
|
2016-09-08 15:33:58 +02:00
|
|
|
// If the given Peer is not present as a Leecher, add the Peer as a
|
|
|
|
// Seeder and return no error.
|
2016-08-04 21:54:30 +02:00
|
|
|
GraduateLeecher(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
|
|
|
|
|
|
|
// AnnouncePeers is a best effort attempt to return Peers from the Swarm
|
2016-09-08 15:33:58 +02:00
|
|
|
// identified by the provided infoHash. The returned Peers are required
|
|
|
|
// to be either all IPv4 or all IPv6.
|
2016-08-04 21:54:30 +02:00
|
|
|
//
|
|
|
|
// The returned Peers should strive be:
|
|
|
|
// - as close to length equal to numWant as possible without going over
|
2016-08-10 02:26:47 +02:00
|
|
|
// - all IPv4 or all IPv6 depending on the provided peer
|
2016-08-04 21:54:30 +02:00
|
|
|
// - if seeder is true, should ideally return more leechers than seeders
|
2016-09-08 15:33:58 +02:00
|
|
|
// - if seeder is false, should ideally return more seeders than
|
|
|
|
// leechers
|
2016-09-28 09:02:37 +02:00
|
|
|
//
|
|
|
|
// Returns ErrResourceDoesNotExist if the provided infoHash is not tracked.
|
2016-08-10 02:26:47 +02:00
|
|
|
AnnouncePeers(infoHash bittorrent.InfoHash, seeder bool, numWant int, p bittorrent.Peer) (peers []bittorrent.Peer, err error)
|
2016-08-04 21:54:30 +02:00
|
|
|
|
2016-09-08 15:33:58 +02:00
|
|
|
// ScrapeSwarm returns information required to answer a scrape request
|
|
|
|
// about a swarm identified by the given infohash.
|
2016-11-28 20:55:04 +01:00
|
|
|
// The AddressFamily indicates whether or not the IPv6 swarm should be
|
2016-09-08 15:33:58 +02:00
|
|
|
// scraped.
|
|
|
|
// The Complete and Incomplete fields of the Scrape must be filled,
|
|
|
|
// filling the Snatches field is optional.
|
|
|
|
// If the infohash is unknown to the PeerStore, an empty Scrape is
|
|
|
|
// returned.
|
2016-11-28 20:55:04 +01:00
|
|
|
ScrapeSwarm(infoHash bittorrent.InfoHash, addressFamily bittorrent.AddressFamily) bittorrent.Scrape
|
2016-09-08 15:33:58 +02:00
|
|
|
|
2017-02-03 03:09:25 +01:00
|
|
|
// stop is an interface that expects a Stop method to stop the
|
2016-09-08 15:33:58 +02:00
|
|
|
// PeerStore.
|
2017-02-03 03:09:25 +01:00
|
|
|
// For more details see the documentation in the stop package.
|
|
|
|
stop.Stopper
|
2017-06-03 22:15:43 +02:00
|
|
|
|
|
|
|
// LogFields returns a loggable version of the data used to configure and
|
|
|
|
// operate a particular peer store.
|
|
|
|
LogFields() log.Fields
|
2016-08-04 21:54:30 +02:00
|
|
|
}
|
2017-02-21 06:58:57 +01:00
|
|
|
|
|
|
|
// RegisterDriver makes a Driver available by the provided name.
|
|
|
|
//
|
|
|
|
// If called twice with the same name, the name is blank, or if the provided
|
|
|
|
// Driver is nil, this function panics.
|
|
|
|
func RegisterDriver(name string, d Driver) {
|
|
|
|
if name == "" {
|
|
|
|
panic("storage: could not register a Driver with an empty name")
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
panic("storage: could not register a nil Driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
driversM.Lock()
|
|
|
|
defer driversM.Unlock()
|
|
|
|
|
|
|
|
if _, dup := drivers[name]; dup {
|
|
|
|
panic("storage: RegisterDriver called twice for " + name)
|
|
|
|
}
|
|
|
|
|
|
|
|
drivers[name] = d
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPeerStore attempts to initialize a new PeerStore with given a name from
|
|
|
|
// the list of registered Drivers.
|
|
|
|
//
|
|
|
|
// If a driver does not exist, returns ErrDriverDoesNotExist.
|
|
|
|
func NewPeerStore(name string, cfg interface{}) (ps PeerStore, err error) {
|
|
|
|
driversM.RLock()
|
|
|
|
defer driversM.RUnlock()
|
|
|
|
|
|
|
|
var d Driver
|
|
|
|
d, ok := drivers[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrDriverDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.NewPeerStore(cfg)
|
|
|
|
}
|