Send unicast to nodes that did non answer the multicast
Use the state file to store last known IP address. closes #13
This commit is contained in:
parent
8a5f907cbe
commit
2d43dda380
@ -2,11 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/respond-collector/models"
|
"github.com/FreifunkBremen/respond-collector/models"
|
||||||
"github.com/FreifunkBremen/respond-collector/respond"
|
"github.com/FreifunkBremen/respond-collector/respond"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Usage: respond-query wlp4s0 "[fe80::eade:27ff:dead:beef%wlp4s0]:1001"
|
// Usage: respond-query wlp4s0 "[fe80::eade:27ff:dead:beef%wlp4s0]:1001"
|
||||||
@ -19,7 +21,7 @@ func main() {
|
|||||||
nodes := models.NewNodes(&models.Config{})
|
nodes := models.NewNodes(&models.Config{})
|
||||||
|
|
||||||
collector := respond.NewCollector(nil, nodes, iface)
|
collector := respond.NewCollector(nil, nodes, iface)
|
||||||
collector.SendPacket(dstAddress)
|
collector.SendPacket(net.ParseIP(dstAddress))
|
||||||
|
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/respond-collector/data"
|
"github.com/FreifunkBremen/respond-collector/data"
|
||||||
"github.com/FreifunkBremen/respond-collector/jsontime"
|
"github.com/FreifunkBremen/respond-collector/jsontime"
|
||||||
"github.com/FreifunkBremen/respond-collector/meshviewer"
|
"github.com/FreifunkBremen/respond-collector/meshviewer"
|
||||||
imodels "github.com/influxdata/influxdb/models"
|
imodels "github.com/influxdata/influxdb/models"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Node struct
|
// Node struct
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
Address net.IP `json:"address"` // the last known IP address
|
||||||
Firstseen jsontime.Time `json:"firstseen"`
|
Firstseen jsontime.Time `json:"firstseen"`
|
||||||
Lastseen jsontime.Time `json:"lastseen"`
|
Lastseen jsontime.Time `json:"lastseen"`
|
||||||
Flags meshviewer.Flags `json:"flags"`
|
Flags meshviewer.Flags `json:"flags"`
|
||||||
|
@ -33,7 +33,7 @@ func NewNodes(config *Config) *Nodes {
|
|||||||
return nodes
|
return nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
//Start all services to manage Nodes
|
// Start all services to manage Nodes
|
||||||
func (nodes *Nodes) Start() {
|
func (nodes *Nodes) Start() {
|
||||||
go nodes.worker()
|
go nodes.worker()
|
||||||
}
|
}
|
||||||
@ -114,8 +114,8 @@ func (nodes *Nodes) GetNodesV2() *meshviewer.NodesV2 {
|
|||||||
Version: 2,
|
Version: 2,
|
||||||
Timestamp: jsontime.Now(),
|
Timestamp: jsontime.Now(),
|
||||||
}
|
}
|
||||||
for nodeID := range nodes.List {
|
|
||||||
|
|
||||||
|
for nodeID := range nodes.List {
|
||||||
nodeOrigin := nodes.List[nodeID]
|
nodeOrigin := nodes.List[nodeID]
|
||||||
if nodeOrigin.Statistics == nil {
|
if nodeOrigin.Statistics == nil {
|
||||||
continue
|
continue
|
||||||
@ -132,6 +132,20 @@ func (nodes *Nodes) GetNodesV2() *meshviewer.NodesV2 {
|
|||||||
return meshviewerNodes
|
return meshviewerNodes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Select selects a list of nodes to be returned
|
||||||
|
func (nodes *Nodes) Select(f func(*Node) bool) []*Node {
|
||||||
|
nodes.RLock()
|
||||||
|
defer nodes.RUnlock()
|
||||||
|
|
||||||
|
result := make([]*Node, 0, len(nodes.List))
|
||||||
|
for _, node := range nodes.List {
|
||||||
|
if f(node) {
|
||||||
|
result = append(result, node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// Periodically saves the cached DB to json file
|
// Periodically saves the cached DB to json file
|
||||||
func (nodes *Nodes) worker() {
|
func (nodes *Nodes) worker() {
|
||||||
c := time.Tick(nodes.config.Nodes.SaveInterval.Duration)
|
c := time.Tick(nodes.config.Nodes.SaveInterval.Duration)
|
||||||
|
@ -10,18 +10,19 @@ import (
|
|||||||
|
|
||||||
"github.com/FreifunkBremen/respond-collector/data"
|
"github.com/FreifunkBremen/respond-collector/data"
|
||||||
"github.com/FreifunkBremen/respond-collector/database"
|
"github.com/FreifunkBremen/respond-collector/database"
|
||||||
|
"github.com/FreifunkBremen/respond-collector/jsontime"
|
||||||
"github.com/FreifunkBremen/respond-collector/models"
|
"github.com/FreifunkBremen/respond-collector/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Collector for a specificle respond messages
|
// Collector for a specificle respond messages
|
||||||
type Collector struct {
|
type Collector struct {
|
||||||
connection *net.UDPConn // UDP socket
|
connection *net.UDPConn // UDP socket
|
||||||
queue chan *Response // received responses
|
queue chan *Response // received responses
|
||||||
multicastAddr string
|
iface string
|
||||||
db *database.DB
|
db *database.DB
|
||||||
nodes *models.Nodes
|
nodes *models.Nodes
|
||||||
interval time.Duration // Interval for multicast packets
|
interval time.Duration // Interval for multicast packets
|
||||||
stop chan interface{}
|
stop chan interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCollector creates a Collector struct
|
// NewCollector creates a Collector struct
|
||||||
@ -40,12 +41,12 @@ func NewCollector(db *database.DB, nodes *models.Nodes, iface string) *Collector
|
|||||||
conn.SetReadBuffer(maxDataGramSize)
|
conn.SetReadBuffer(maxDataGramSize)
|
||||||
|
|
||||||
collector := &Collector{
|
collector := &Collector{
|
||||||
connection: conn,
|
connection: conn,
|
||||||
db: db,
|
db: db,
|
||||||
nodes: nodes,
|
nodes: nodes,
|
||||||
multicastAddr: net.JoinHostPort(multiCastGroup+"%"+iface, port),
|
iface: iface,
|
||||||
queue: make(chan *Response, 400),
|
queue: make(chan *Response, 400),
|
||||||
stop: make(chan interface{}),
|
stop: make(chan interface{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
go collector.receiver()
|
go collector.receiver()
|
||||||
@ -82,17 +83,45 @@ func (coll *Collector) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (coll *Collector) sendOnce() {
|
func (coll *Collector) sendOnce() {
|
||||||
coll.SendPacket(coll.multicastAddr)
|
now := jsontime.Now()
|
||||||
|
coll.sendMulticast()
|
||||||
|
|
||||||
|
// Wait for the multicast responses to be processed and send unicasts
|
||||||
|
time.Sleep(coll.interval / 2)
|
||||||
|
coll.sendUnicasts(now)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendPacket send a UDP request to the given unicast or multicast address
|
func (coll *Collector) sendMulticast() {
|
||||||
func (coll *Collector) SendPacket(address string) {
|
log.Println("sending multicast")
|
||||||
addr, err := net.ResolveUDPAddr("udp", address)
|
coll.SendPacket(net.ParseIP(multiCastGroup))
|
||||||
if err != nil {
|
}
|
||||||
log.Panic(err)
|
|
||||||
|
// Send unicast packets to nodes that did not answer the multicast
|
||||||
|
func (coll *Collector) sendUnicasts(seenBefore jsontime.Time) {
|
||||||
|
seenAfter := seenBefore.Add(-time.Minute * 10)
|
||||||
|
|
||||||
|
// Select online nodes that has not been seen recently
|
||||||
|
nodes := coll.nodes.Select(func(n *models.Node) bool {
|
||||||
|
return n.Lastseen.After(seenAfter) && n.Lastseen.Before(seenBefore) && n.Address != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
// Send unicast packets
|
||||||
|
log.Printf("sending unicast to %d nodes", len(nodes))
|
||||||
|
for _, node := range nodes {
|
||||||
|
coll.SendPacket(node.Address)
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendPacket sends a UDP request to the given unicast or multicast address
|
||||||
|
func (coll *Collector) SendPacket(address net.IP) {
|
||||||
|
addr := net.UDPAddr{
|
||||||
|
IP: address,
|
||||||
|
Port: port,
|
||||||
|
Zone: coll.iface,
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := coll.connection.WriteToUDP([]byte("GET nodeinfo statistics neighbours"), addr); err != nil {
|
if _, err := coll.connection.WriteToUDP([]byte("GET nodeinfo statistics neighbours"), &addr); err != nil {
|
||||||
log.Println("WriteToUDP failed:", err)
|
log.Println("WriteToUDP failed:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -151,8 +180,9 @@ func (coll *Collector) saveResponse(addr net.UDPAddr, res *data.ResponseData) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the data
|
// Process the data and update IP address
|
||||||
node := coll.nodes.Update(nodeID, res)
|
node := coll.nodes.Update(nodeID, res)
|
||||||
|
node.Address = addr.IP
|
||||||
|
|
||||||
// Store statistics in InfluxDB
|
// Store statistics in InfluxDB
|
||||||
if coll.db != nil && node.Statistics != nil {
|
if coll.db != nil && node.Statistics != nil {
|
||||||
|
@ -9,7 +9,7 @@ const (
|
|||||||
multiCastGroup = "ff02:0:0:0:0:0:2:1001"
|
multiCastGroup = "ff02:0:0:0:0:0:2:1001"
|
||||||
|
|
||||||
// default udp port used by announced
|
// default udp port used by announced
|
||||||
port = "1001"
|
port = 1001
|
||||||
|
|
||||||
// maximum receivable size
|
// maximum receivable size
|
||||||
maxDataGramSize = 8192
|
maxDataGramSize = 8192
|
||||||
|
Loading…
Reference in New Issue
Block a user