diff --git a/database/yanic/main.go b/database/yanic/main.go new file mode 100644 index 0000000..61b867c --- /dev/null +++ b/database/yanic/main.go @@ -0,0 +1,89 @@ +package yanic + +/** + * This database type is for injecting into another yanic instance. + */ +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.Conn +} + +type Config map[string]interface{} + +func (c Config) Address() string { + return c["address"].(string) +} + +func init() { + database.RegisterAdapter("yanic", Connect) +} + +func Connect(configuration map[string]interface{}) (database.Connection, error) { + var config Config + config = configuration + + conn, err := net.Dial("udp6", config.Address()) + if err != nil { + return nil, err + } + + return &Connection{conn: conn, config: config}, nil +} + +func (conn *Connection) InsertNode(node *runtime.Node) { + res := &data.ResponseData{ + NodeInfo: node.Nodeinfo, + Statistics: node.Statistics, + Neighbours: node.Neighbours, + } + var b bytes.Buffer + writer := bufio.NewWriter(&b) + + flater, err := flate.NewWriter(writer, flate.NoCompression) + if err != nil { + log.Printf("[database-yanic] could not create flater: %s", err) + return + } + defer flater.Close() + err = json.NewEncoder(flater).Encode(res) + if err != nil { + nodeid := "unknown" + if node.Nodeinfo != nil && node.Nodeinfo.NodeID != "" { + nodeid = node.Nodeinfo.NodeID + } + log.Printf("[database-yanic] could not send %s node: %s", nodeid, err) + return + } + flater.Flush() + writer.Flush() + conn.conn.Write(b.Bytes()) + +} + +func (conn *Connection) InsertLink(link *runtime.Link, time time.Time) { +} + +func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string) { +} + +func (conn *Connection) PruneNodes(deleteAfter time.Duration) { +} + +func (conn *Connection) Close() { + conn.conn.Close() +} diff --git a/database/yanic/main_test.go b/database/yanic/main_test.go new file mode 100644 index 0000000..e6e21e2 --- /dev/null +++ b/database/yanic/main_test.go @@ -0,0 +1,25 @@ +package yanic + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestStart(t *testing.T) { + assert := assert.New(t) + + conn, err := Connect(map[string]interface{}{ + "address": "fasfs", + }) + assert.Nil(conn) + assert.Error(err) + + conn, err = Connect(map[string]interface{}{ + "address": "[::1]:11001", + }) + assert.NoError(err) + + conn.Close() + +}