From b74a2480cd3f8e2234c50e1848de6e3375f3d7ff Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Fri, 19 Feb 2016 11:30:42 +0100 Subject: [PATCH] init --- announced.go | 4 ++-- main.go | 35 +++++++++++++++++++++++++++++------ nodes.go | 12 +++++++++--- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/announced.go b/announced.go index c57534c..1c3eb29 100644 --- a/announced.go +++ b/announced.go @@ -12,7 +12,7 @@ type Announced struct { collectors []*Collector } -func NewAnnounced(ns NodeServer) *Announced { +func NewAnnounced(ns *NodeServer) *Announced { collects := []*Collector{ NewCollector("statistics"), NewCollector("nodeinfo"), @@ -21,7 +21,7 @@ func NewAnnounced(ns NodeServer) *Announced { return &Announced{ ns, NewNodes(), - output, + "webroot/nodes.json", time.Second * time.Duration(15), time.Second * time.Duration(15), collects, diff --git a/main.go b/main.go index ecb7d95..a5ca00b 100644 --- a/main.go +++ b/main.go @@ -1,19 +1,42 @@ package main import ( + "flag" "log" + "time" "net/http" ) - +var ( + nodeserver = NewNodeServer("/nodes") + nodes = NewNodes() + outputFile string + collectInterval time.Duration + saveInterval time.Duration +) func main(){ - node := NewNodeServer("/nodes") - go node.Listen() - - annouced := NewAnnouced(node) - go annouced.Run() + var collectSeconds, saveSeconds int + flag.StringVar(&outputFile, "output", "nodes.json", "path output file") + flag.IntVar(&collectSeconds, "collectInterval", 15, "interval for data collections") + flag.IntVar(&saveSeconds, "saveInterval", 5, "interval for data saving") + flag.Parse() + + collectInterval = time.Second * time.Duration(collectSeconds) + saveInterval = time.Second * time.Duration(saveSeconds) + + collectors := []*Collector{ + NewCollector("statistics"), + NewCollector("nodeinfo"), + NewCollector("neighbours"), + } + + go nodeserver.Listen() + // static files http.Handle("/", http.FileServer(http.Dir("webroot"))) log.Fatal(http.ListenAndServe(":8080", nil)) + for _, c := range collectors { + c.Close() + } } diff --git a/nodes.go b/nodes.go index 2f16f07..0069569 100644 --- a/nodes.go +++ b/nodes.go @@ -70,12 +70,18 @@ func (nodes *Nodes) save() { nodes.Unlock() if err !=nil{ - log.Panic(e) + log.Panic(err) } log.Println("saving", len(nodes.List), "nodes") tmpFile := outputFile + ".tmp" - check(ioutil.WriteFile(tmpFile, data, 0644)) - check(os.Rename(tmpFile, outputFile)) + err = ioutil.WriteFile(tmpFile, data, 0644) + if err !=nil{ + log.Panic(err) + } + err = os.Rename(tmpFile, outputFile) + if err !=nil{ + log.Panic(err) + } }