Files
mvds/peers/persistence.go
Andrea Maria Piana 9e1addbac0 Don't rely on the presence of GroupID for non-Messages types
This commits changes the behavior so that we don't rely on the presence
of GroupID in the payload.

Adds some tests to verify the basic functionalities of BATCH and
INTERACTIVE mode, but more are required to ensure the correct
functioning.

Error handling and state management is probably also to be inspected
more carefully.
2019-07-25 12:42:46 +02:00

40 lines
929 B
Go

package peers
import (
"github.com/vacp2p/mvds/state"
)
type Persistence interface {
Add(state.GroupID, state.PeerID) error
GetByGroupID(group state.GroupID) ([]state.PeerID, error)
Exists(state.GroupID, state.PeerID) (bool, error)
}
type MemoryPersistence struct {
peers map[state.GroupID][]state.PeerID
}
func NewMemoryPersistence() *MemoryPersistence {
return &MemoryPersistence{
peers: make(map[state.GroupID][]state.PeerID),
}
}
func (p *MemoryPersistence) Add(groupID state.GroupID, peerID state.PeerID) error {
p.peers[groupID] = append(p.peers[groupID], peerID)
return nil
}
func (p *MemoryPersistence) Exists(groupID state.GroupID, peerID state.PeerID) (bool, error) {
for _, peer := range p.peers[groupID] {
if peer == peerID {
return true, nil
}
}
return false, nil
}
func (p *MemoryPersistence) GetByGroupID(groupID state.GroupID) ([]state.PeerID, error) {
return p.peers[groupID], nil
}