tracker/server/store/client_store.go

50 lines
1.5 KiB
Go
Raw Normal View History

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"
"github.com/chihaya/chihaya"
)
2016-01-25 06:41:39 +01:00
var clientStoreDrivers = make(map[string]ClientStoreDriver)
// ClientStore represents an interface for manipulating clientIDs.
type ClientStore interface {
CreateClient(clientID string) error
FindClient(peerID chihaya.PeerID) (bool, error)
2016-01-25 06:41:39 +01:00
DeleteClient(clientID string) error
}
// ClientStoreDriver represents an interface for creating a handle to the
// storage of swarms.
type ClientStoreDriver interface {
New(*Config) (ClientStore, error)
}
// RegisterClientStoreDriver 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 RegisterClientStoreDriver(name string, driver ClientStoreDriver) {
if driver == nil {
panic("store: could not register nil ClientStoreDriver")
}
if _, dup := clientStoreDrivers[name]; dup {
panic("store: could not register duplicate ClientStoreDriver: " + name)
}
clientStoreDrivers[name] = driver
}
// OpenClientStore returns a ClientStore specified by a configuration.
2016-02-16 21:47:40 +01:00
func OpenClientStore(cfg *Config) (ClientStore, error) {
driver, ok := clientStoreDrivers[cfg.ClientStore]
2016-01-25 06:41:39 +01:00
if !ok {
2016-02-16 21:47:40 +01:00
return nil, fmt.Errorf("store: unknown driver %q (forgotten import?)", cfg.ClientStore)
2016-01-25 06:41:39 +01:00
}
return driver.New(cfg)
}