2021-05-01 12:45:37 +02:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/wiretrustee/wiretrustee/signal/proto"
|
2021-06-10 17:08:40 +02:00
|
|
|
"sync"
|
2021-05-01 12:45:37 +02:00
|
|
|
)
|
|
|
|
|
2021-05-15 12:20:49 +02:00
|
|
|
// Peer representation of a connected Peer
|
2021-05-01 12:45:37 +02:00
|
|
|
type Peer struct {
|
|
|
|
// a unique id of the Peer (e.g. sha256 fingerprint of the Wireguard public key)
|
|
|
|
Id string
|
|
|
|
|
|
|
|
//a gRpc connection stream to the Peer
|
|
|
|
Stream proto.SignalExchange_ConnectStreamServer
|
|
|
|
}
|
|
|
|
|
2021-05-15 12:20:49 +02:00
|
|
|
// NewPeer creates a new instance of a connected Peer
|
2021-05-01 12:45:37 +02:00
|
|
|
func NewPeer(id string, stream proto.SignalExchange_ConnectStreamServer) *Peer {
|
|
|
|
return &Peer{
|
|
|
|
Id: id,
|
|
|
|
Stream: stream,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 12:20:49 +02:00
|
|
|
// Registry registry that holds all currently connected Peers
|
2021-05-01 12:45:37 +02:00
|
|
|
type Registry struct {
|
|
|
|
// Peer.key -> Peer
|
2021-06-10 17:08:40 +02:00
|
|
|
Peers sync.Map
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 12:20:49 +02:00
|
|
|
// NewRegistry creates a new connected Peer registry
|
2021-05-01 12:45:37 +02:00
|
|
|
func NewRegistry() *Registry {
|
2021-06-10 17:08:40 +02:00
|
|
|
return &Registry{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get gets a peer from the registry
|
|
|
|
func (registry *Registry) Get(peerId string) (*Peer, bool) {
|
|
|
|
if load, ok := registry.Peers.Load(peerId); ok {
|
|
|
|
return load.(*Peer), ok
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
2021-06-10 17:08:40 +02:00
|
|
|
return nil, false
|
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 17:08:40 +02:00
|
|
|
func (registry *Registry) IsPeerRegistered(peerId string) bool {
|
|
|
|
if _, ok := registry.Peers.Load(peerId); ok {
|
|
|
|
return ok
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
2021-06-10 17:08:40 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register registers peer in the registry
|
|
|
|
func (registry *Registry) Register(peer *Peer) {
|
|
|
|
// can be that peer already exists but it is fine (e.g. reconnect)
|
|
|
|
// todo investigate what happens to the old peer (especially Peer.Stream) when we override it
|
|
|
|
registry.Peers.Store(peer.Id, peer)
|
|
|
|
log.Printf("registered peer [%s]", peer.Id)
|
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-03 12:23:18 +02:00
|
|
|
// Deregister deregister Peer from the Registry (usually once it disconnects)
|
2021-06-10 17:08:40 +02:00
|
|
|
func (registry *Registry) Deregister(peer *Peer) {
|
|
|
|
_, loaded := registry.Peers.LoadAndDelete(peer.Id)
|
|
|
|
if loaded {
|
2021-05-01 12:45:37 +02:00
|
|
|
log.Printf("deregistered peer [%s]", peer.Id)
|
2021-06-10 17:08:40 +02:00
|
|
|
} else {
|
|
|
|
log.Warnf("attempted to remove non-existent peer [%s]", peer.Id)
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|
2021-06-10 17:08:40 +02:00
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
}
|