2016-01-25 06:41:39 +01:00
|
|
|
// Copyright 2016 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 store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya"
|
|
|
|
)
|
|
|
|
|
|
|
|
var peerStoreDrivers = make(map[string]PeerStoreDriver)
|
|
|
|
|
|
|
|
// PeerStore represents an interface for manipulating peers.
|
|
|
|
type PeerStore interface {
|
2016-02-16 01:49:25 +01:00
|
|
|
PutSeeder(infoHash chihaya.InfoHash, p chihaya.Peer) error
|
2016-02-17 23:39:21 +01:00
|
|
|
DeleteSeeder(infoHash chihaya.InfoHash, p chihaya.Peer) error
|
2016-01-25 06:41:39 +01:00
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
PutLeecher(infoHash chihaya.InfoHash, p chihaya.Peer) error
|
2016-02-17 23:39:21 +01:00
|
|
|
DeleteLeecher(infoHash chihaya.InfoHash, p chihaya.Peer) error
|
2016-01-25 06:41:39 +01:00
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
GraduateLeecher(infoHash chihaya.InfoHash, p chihaya.Peer) error
|
|
|
|
AnnouncePeers(infoHash chihaya.InfoHash, seeder bool, numWant int) (peers, peers6 []chihaya.Peer, err error)
|
2016-01-25 06:41:39 +01:00
|
|
|
CollectGarbage(cutoff time.Time) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeerStoreDriver represents an interface for creating a handle to the storage
|
|
|
|
// of peers.
|
|
|
|
type PeerStoreDriver interface {
|
|
|
|
New(*Config) (PeerStore, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterPeerStoreDriver makes a driver available by the provided name.
|
|
|
|
//
|
|
|
|
// If this function is called twice with the same name or if the driver is nil,
|
|
|
|
// it panics.
|
|
|
|
func RegisterPeerStoreDriver(name string, driver PeerStoreDriver) {
|
|
|
|
if driver == nil {
|
|
|
|
panic("storage: could not register nil PeerStoreDriver")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, dup := peerStoreDrivers[name]; dup {
|
|
|
|
panic("storage: could not register duplicate PeerStoreDriver: " + name)
|
|
|
|
}
|
|
|
|
|
|
|
|
peerStoreDrivers[name] = driver
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenPeerStore returns a PeerStore specified by a configuration.
|
|
|
|
func OpenPeerStore(name string, cfg *Config) (PeerStore, error) {
|
|
|
|
driver, ok := peerStoreDrivers[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"storage: unknown driver %q (forgotten import?)",
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return driver.New(cfg)
|
|
|
|
}
|