Optimize updating nodes

This commit is contained in:
Julian Kornberger 2016-03-20 16:25:33 +01:00
parent 519b7ee292
commit d4a9a169f1
2 changed files with 30 additions and 17 deletions

21
main.go
View File

@ -78,19 +78,22 @@ func main() {
// called for every parsed announced-message // called for every parsed announced-message
func onReceive(addr net.UDPAddr, res *data.ResponseData) { func onReceive(addr net.UDPAddr, res *data.ResponseData) {
if val := res.Neighbours; val != nil { // Search for NodeID
nodes.Get(val.NodeId).Neighbours = val var nodeId string
}
if val := res.NodeInfo; val != nil { if val := res.NodeInfo; val != nil {
nodes.Get(val.NodeId).Nodeinfo = val nodeId = val.NodeId
} else if val := res.Neighbours; val != nil {
nodeId = val.NodeId
} else if val := res.Statistics; val != nil {
nodeId = val.NodeId
} }
if val := res.Statistics; val != nil { // Updates nodes if NodeID found
nodes.Get(val.NodeId).Statistics = val if nodeId != "" {
nodes.Update(nodeId, res)
}
if statsDb != nil { if val := res.Statistics; val != nil && statsDb != nil {
statsDb.Add(val) statsDb.Add(val)
} }
}
} }

View File

@ -42,8 +42,8 @@ func NewNodes() *Nodes {
return nodes return nodes
} }
// Get a Node by nodeid // Update a Node
func (nodes *Nodes) Get(nodeID string) *Node { func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) {
now := time.Now() now := time.Now()
nodes.Lock() nodes.Lock()
@ -52,9 +52,6 @@ func (nodes *Nodes) Get(nodeID string) *Node {
if node == nil { if node == nil {
node = &Node{ node = &Node{
Firstseen: now, Firstseen: now,
Nodeinfo: &data.NodeInfo{},
Statistics: &data.Statistics{},
Neighbours: &data.Neighbours{},
} }
nodes.List[nodeID] = node nodes.List[nodeID] = node
} }
@ -62,7 +59,20 @@ func (nodes *Nodes) Get(nodeID string) *Node {
node.Lastseen = now node.Lastseen = now
return node // Update neighbours
if val := res.Neighbours; val != nil {
node.Neighbours = val
}
// Update nodeinfo
if val := res.NodeInfo; val != nil {
node.Nodeinfo = val
}
// Update statistics
if val := res.Statistics; val != nil {
node.Statistics = val
}
} }
// Saves the cached DB to json file periodically // Saves the cached DB to json file periodically