[TASK] use yanic client from package

This commit is contained in:
Martin Geno 2017-05-29 22:28:24 +02:00
parent 9439c7285a
commit c1d9386698
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
3 changed files with 2 additions and 109 deletions

View File

@ -18,7 +18,7 @@ import (
"github.com/FreifunkBremen/freifunkmanager/runtime"
"github.com/FreifunkBremen/freifunkmanager/ssh"
"github.com/FreifunkBremen/freifunkmanager/websocket"
"github.com/FreifunkBremen/freifunkmanager/yanic"
yanic "github.com/FreifunkBremen/yanic/database/socket/client"
)
var (
@ -54,7 +54,7 @@ func main() {
stats = data
websocket.NotifyStats(data)
}
yanicDialer.Start()
go yanicDialer.Start()
}
// Startwebserver

View File

@ -1,89 +0,0 @@
package yanic
import (
"encoding/json"
"net"
yanicSocket "github.com/FreifunkBremen/yanic/database/socket"
yanic "github.com/FreifunkBremen/yanic/runtime"
"github.com/FreifunkBremen/freifunkmanager/lib/log"
)
type Dialer struct {
conn net.Conn
queue chan yanicSocket.EventMessage
quit chan struct{}
NodeHandler func(*yanic.Node)
GlobalsHandler func(*yanic.GlobalStats)
PruneNodesHandler func()
}
func Dial(ctype, addr string) *Dialer {
conn, err := net.Dial(ctype, addr)
if err != nil {
log.Log.Panicf("yanic dial to %s:%s failed", ctype, addr)
}
dialer := &Dialer{
conn: conn,
queue: make(chan yanicSocket.EventMessage),
quit: make(chan struct{}),
}
return dialer
}
func (d *Dialer) Start() {
go d.reciever()
go d.parser()
}
func (d *Dialer) Close() {
if d != nil {
d.conn.Close()
close(d.queue)
close(d.quit)
}
}
func (d *Dialer) reciever() {
decoder := json.NewDecoder(d.conn)
var msg yanicSocket.EventMessage
for {
select {
case <-d.quit:
return
default:
decoder.Decode(&msg)
d.queue <- msg
}
}
}
func (d *Dialer) parser() {
for msg := range d.queue {
switch msg.Event {
case "insert_node":
if d.NodeHandler != nil {
var node yanic.Node
obj, _ := json.Marshal(msg.Body)
json.Unmarshal(obj, &node)
d.NodeHandler(&node)
}
case "insert_globals":
if d.GlobalsHandler != nil {
var globals yanic.GlobalStats
obj, _ := json.Marshal(msg.Body)
json.Unmarshal(obj, &globals)
d.GlobalsHandler(&globals)
}
case "prune_nodes":
if d.PruneNodesHandler != nil {
d.PruneNodesHandler()
}
}
}
}

View File

@ -1,18 +0,0 @@
package yanic
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestLog(t *testing.T) {
assert := assert.New(t)
d := Dial("unix", "/tmp/yanic-database.socket")
assert.NotNil(d)
d.Start()
time.Sleep(time.Duration(3) * time.Minute)
d.Close()
}