2015-12-29 04:08:03 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-02-19 11:30:42 +01:00
|
|
|
"flag"
|
2015-12-29 04:08:03 +01:00
|
|
|
"log"
|
2016-02-25 22:06:15 +01:00
|
|
|
"net"
|
2016-02-19 11:13:30 +01:00
|
|
|
"net/http"
|
2016-02-25 21:06:37 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2016-03-15 23:26:30 +01:00
|
|
|
"github.com/FreifunkBremen/respond-collector/data"
|
|
|
|
"github.com/FreifunkBremen/respond-collector/models"
|
|
|
|
"github.com/FreifunkBremen/respond-collector/respond"
|
|
|
|
"github.com/FreifunkBremen/respond-collector/websocketserver"
|
2015-12-29 14:05:47 +01:00
|
|
|
)
|
2016-02-25 21:06:37 +01:00
|
|
|
|
2016-02-19 11:30:42 +01:00
|
|
|
var (
|
2016-03-07 09:52:52 +01:00
|
|
|
configFile string
|
|
|
|
config *models.Config
|
|
|
|
wsserverForNodes *websocketserver.Server
|
2016-03-09 03:49:27 +01:00
|
|
|
multiCollector *respond.MultiCollector
|
2016-03-12 03:36:02 +01:00
|
|
|
statsDb *StatsDb
|
2016-03-07 09:52:52 +01:00
|
|
|
nodes = models.NewNodes()
|
2016-03-07 02:29:31 +01:00
|
|
|
//aliases = models.NewNodes()
|
2016-02-19 11:30:42 +01:00
|
|
|
)
|
2016-02-25 21:06:37 +01:00
|
|
|
|
|
|
|
func main() {
|
2016-03-12 16:40:41 +01:00
|
|
|
flag.StringVar(&configFile, "config", "config.yml", "path of configuration file (default:config.yaml)")
|
2016-02-19 11:30:42 +01:00
|
|
|
flag.Parse()
|
2016-03-07 09:52:52 +01:00
|
|
|
config = models.ConfigReadFile(configFile)
|
2015-12-29 14:05:47 +01:00
|
|
|
|
2016-03-07 09:52:52 +01:00
|
|
|
if config.Nodes.Enable {
|
2016-03-07 12:05:53 +01:00
|
|
|
go nodes.Saver(config)
|
2016-03-07 09:52:52 +01:00
|
|
|
}
|
2016-02-19 11:30:42 +01:00
|
|
|
|
2016-03-07 09:52:52 +01:00
|
|
|
if config.Webserver.Enable {
|
|
|
|
if config.Webserver.WebsocketNode {
|
|
|
|
wsserverForNodes = websocketserver.NewServer("/nodes")
|
|
|
|
go wsserverForNodes.Listen()
|
2016-02-25 21:06:37 +01:00
|
|
|
}
|
2016-03-07 09:52:52 +01:00
|
|
|
http.Handle("/", http.FileServer(http.Dir(config.Webserver.Webroot)))
|
|
|
|
}
|
2016-02-25 21:06:37 +01:00
|
|
|
|
2016-03-12 03:36:02 +01:00
|
|
|
if config.Influxdb.Enable {
|
|
|
|
statsDb = NewStatsDb()
|
|
|
|
}
|
|
|
|
|
2016-03-09 03:26:08 +01:00
|
|
|
if config.Respondd.Enable {
|
2016-03-12 03:58:36 +01:00
|
|
|
collectInterval := time.Second * time.Duration(config.Respondd.CollectInterval)
|
|
|
|
multiCollector = respond.NewMultiCollector(collectInterval, onReceive)
|
2016-03-07 09:52:52 +01:00
|
|
|
}
|
2016-02-25 21:06:37 +01:00
|
|
|
|
2016-03-12 03:58:36 +01:00
|
|
|
// TODO bad
|
2016-03-07 09:52:52 +01:00
|
|
|
if config.Webserver.Enable {
|
|
|
|
log.Fatal(http.ListenAndServe(net.JoinHostPort(config.Webserver.Address, config.Webserver.Port), nil))
|
|
|
|
}
|
2016-03-12 03:58:36 +01:00
|
|
|
|
|
|
|
// Wait for INT/TERM
|
2016-02-25 21:06:37 +01:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
|
|
|
log.Println("received", sig)
|
|
|
|
|
|
|
|
// Close everything at the end
|
2016-03-07 10:28:24 +01:00
|
|
|
if wsserverForNodes != nil {
|
|
|
|
wsserverForNodes.Close()
|
|
|
|
}
|
2016-03-09 03:49:27 +01:00
|
|
|
if multiCollector != nil {
|
|
|
|
multiCollector.Close()
|
2016-03-07 10:28:24 +01:00
|
|
|
}
|
2016-03-12 03:36:02 +01:00
|
|
|
if statsDb != nil {
|
|
|
|
statsDb.Close()
|
|
|
|
}
|
2015-12-29 04:08:03 +01:00
|
|
|
}
|
2016-03-12 03:58:36 +01:00
|
|
|
|
|
|
|
// called for every parsed announced-message
|
|
|
|
func onReceive(addr net.UDPAddr, msg interface{}) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
|
|
|
case *data.NodeInfo:
|
|
|
|
nodes.Get(msg.NodeId).Nodeinfo = msg
|
|
|
|
|
2016-03-12 13:32:55 +01:00
|
|
|
case *data.Neighbours:
|
2016-03-12 03:58:36 +01:00
|
|
|
nodes.Get(msg.NodeId).Neighbours = msg
|
|
|
|
|
2016-03-12 13:32:55 +01:00
|
|
|
case *data.Statistics:
|
2016-03-12 03:58:36 +01:00
|
|
|
nodes.Get(msg.NodeId).Statistics = msg
|
|
|
|
|
|
|
|
// store data?
|
|
|
|
if statsDb != nil {
|
|
|
|
statsDb.Add(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Println("unknown message:", msg)
|
|
|
|
}
|
|
|
|
}
|