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 memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2016-02-28 09:38:50 +01:00
|
|
|
"github.com/chihaya/chihaya"
|
2016-01-25 06:41:39 +01:00
|
|
|
"github.com/chihaya/chihaya/pkg/clientid"
|
|
|
|
"github.com/chihaya/chihaya/server/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
store.RegisterClientStoreDriver("memory", &clientStoreDriver{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientStoreDriver struct{}
|
|
|
|
|
2016-03-11 21:09:49 +01:00
|
|
|
func (d *clientStoreDriver) New(_ *store.DriverConfig) (store.ClientStore, error) {
|
2016-01-25 06:41:39 +01:00
|
|
|
return &clientStore{
|
|
|
|
clientIDs: make(map[string]struct{}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientStore struct {
|
|
|
|
clientIDs map[string]struct{}
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ store.ClientStore = &clientStore{}
|
|
|
|
|
|
|
|
func (s *clientStore) CreateClient(clientID string) error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
s.clientIDs[clientID] = struct{}{}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-28 09:38:50 +01:00
|
|
|
func (s *clientStore) FindClient(peerID chihaya.PeerID) (bool, error) {
|
|
|
|
clientID := clientid.New(string(peerID))
|
2016-02-19 14:15:40 +01:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
2016-01-25 06:41:39 +01:00
|
|
|
|
|
|
|
_, ok := s.clientIDs[clientID]
|
|
|
|
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *clientStore) DeleteClient(clientID string) error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
delete(s.clientIDs, clientID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|