[TASK] add yanic database for injection

This commit is contained in:
Martin Geno 2017-07-08 11:01:39 +02:00
parent 890a2d6043
commit 2676da918b
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
2 changed files with 83 additions and 0 deletions

View File

@ -5,4 +5,5 @@ import (
_ "github.com/FreifunkBremen/yanic/database/influxdb"
_ "github.com/FreifunkBremen/yanic/database/logging"
_ "github.com/FreifunkBremen/yanic/database/socket"
_ "github.com/FreifunkBremen/yanic/database/yanic"
)

82
database/yanic/main.go Normal file
View File

@ -0,0 +1,82 @@
package yanic
import (
"bufio"
"bytes"
"compress/flate"
"encoding/json"
"log"
"net"
"time"
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/runtime"
)
type Connection struct {
database.Connection
config Config
conn *net.UDPConn
}
type Config map[string]interface{}
func (c Config) Enable() bool {
return c["enable"].(bool)
}
func (c Config) Address() string {
return c["address"].(string)
}
func init() {
database.RegisterAdapter("yanic", Connect)
}
func Connect(configuration interface{}) (database.Connection, error) {
var config Config
config = configuration.(map[string]interface{})
if !config.Enable() {
return nil, nil
}
udpAddr, err := net.ResolveUDPAddr("udp", config.Address())
if err != nil {
log.Panicf("Invalid yanic address: %s", err)
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
log.Panicf("Unable to dial yanic: %s", err)
}
return &Connection{config: config, conn: conn}, nil
}
func (conn *Connection) InsertNode(node *runtime.Node) {
buf := bytes.Buffer{}
writer := bufio.NewWriter(&buf)
deflater, err := flate.NewWriter(writer, flate.DefaultCompression)
err = json.NewEncoder(deflater).Encode(&data.ResponseData{
Statistics: node.Statistics,
NodeInfo: node.Nodeinfo,
Neighbours: node.Neighbours,
})
if err != nil {
panic(err)
}
deflater.Close()
writer.Flush()
conn.conn.Write(buf.Bytes())
}
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time) {
}
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
}
func (conn *Connection) Close() {
conn.conn.Close()
}