begin working on sync handler
This commit is contained in:
parent
b3ac79797c
commit
428aa2a11e
1 changed files with 49 additions and 1 deletions
|
|
@ -16,7 +16,8 @@ type LuxNode struct {
|
|||
router net.LuxRouter
|
||||
running bool
|
||||
|
||||
neighbors map[proto.LuxID]*ipnet.UDPAddr
|
||||
neighbors map[proto.LuxID]*ipnet.UDPAddr
|
||||
neighborLock sync.RWMutex
|
||||
|
||||
state LuxNodeState
|
||||
stateLock sync.RWMutex
|
||||
|
|
@ -68,7 +69,9 @@ func (node *LuxNode) AddNeighbor(id proto.LuxID, udpAddr string) error {
|
|||
}
|
||||
|
||||
// just add to neighbor list to let other nodes know
|
||||
node.neighborLock.Lock()
|
||||
node.neighbors[id] = udpIp
|
||||
node.neighborLock.Unlock()
|
||||
|
||||
log.Infof("added neighbor %s at %s", id.String(), udpIp.String())
|
||||
return nil
|
||||
|
|
@ -107,6 +110,20 @@ func (node *LuxNode) handleHeartbeat(packet *net.LuxPacket) {
|
|||
log.Debugf("heartbeat from %s", packet.Target.String())
|
||||
}
|
||||
|
||||
func (node *LuxNode) handleSync(packet *net.LuxPacket) {
|
||||
if packet.ChannelType != net.LuxChannelInterior {
|
||||
log.Error("sync not on interior!")
|
||||
return
|
||||
}
|
||||
|
||||
sync := NewLuxNodeSync()
|
||||
if err := sync.Read(&packet.Buffer); err != nil {
|
||||
log.Errorf("failed to parse sync packet: %v", err)
|
||||
}
|
||||
|
||||
// check generation
|
||||
}
|
||||
|
||||
func nodeLoop(node *LuxNode) {
|
||||
router := &node.router
|
||||
for node.running {
|
||||
|
|
@ -118,6 +135,8 @@ func nodeLoop(node *LuxNode) {
|
|||
switch packet.Type {
|
||||
case net.LuxPacketTypeHeartbeat:
|
||||
node.handleHeartbeat(&packet)
|
||||
case net.LuxPacketTypeSync:
|
||||
node.handleSync(&packet)
|
||||
default:
|
||||
log.Warningf("unknown packet type %d on chType %d", packet.Type, packet.ChannelType)
|
||||
}
|
||||
|
|
@ -135,3 +154,32 @@ func (node *LuxNode) Stop() {
|
|||
node.running = false
|
||||
node.router.Stop()
|
||||
}
|
||||
|
||||
// Create sync state and muilticast it to all node routes (neighbors)
|
||||
func (node *LuxNode) MulticastSync() error {
|
||||
sync := NewLuxNodeSync()
|
||||
|
||||
// add this node to synced, since we dont wanna receive same sync again
|
||||
sync.Synced[*node.nodeId] = struct{}{}
|
||||
|
||||
// add all neighbors we know
|
||||
node.neighborLock.RLock()
|
||||
for id, udpAddr := range node.neighbors {
|
||||
sync.Neighbors[id] = udpAddr
|
||||
}
|
||||
node.neighborLock.RUnlock()
|
||||
|
||||
// copy our state into sync packet
|
||||
node.stateLock.RLock()
|
||||
sync.SyncState = node.state
|
||||
node.stateLock.RUnlock()
|
||||
|
||||
// multicast to all nodes, since all node routes we have
|
||||
// are populated neighbors
|
||||
packet := net.LuxPacket{
|
||||
Type: net.LuxPacketTypeSync,
|
||||
Buffer: proto.NewLuxBuffer(),
|
||||
}
|
||||
sync.Write(&packet.Buffer)
|
||||
return node.router.Multicast(packet, proto.LuxTypeNode)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue