diff --git a/database/all/main.go b/database/all/main.go index fdf8ca0..04aa7e1 100644 --- a/database/all/main.go +++ b/database/all/main.go @@ -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" ) diff --git a/database/yanic/main.go b/database/yanic/main.go new file mode 100644 index 0000000..983969a --- /dev/null +++ b/database/yanic/main.go @@ -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() +}