mirror of
https://github.com/vacp2p/mvds.git
synced 2026-01-08 19:48:03 -05:00
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.
40 lines
929 B
Go
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
|
|
}
|