diff --git a/database/socket/database.go b/database/socket/database.go index c5e6083..7650432 100644 --- a/database/socket/database.go +++ b/database/socket/database.go @@ -1,10 +1,11 @@ package socket -/** - * This database type is just for, - * - debugging without a influxconn - * - example for other developers for new databases +/* + * This socket database is to run another service + * (without flooding the network with respondd packages) + * e.g. https://github.com/FreifunkBremen/freifunkmanager */ + import ( "log" "net" @@ -46,15 +47,15 @@ func Connect(configuration interface{}) (database.Connection, error) { } func (conn *Connection) InsertNode(node *runtime.Node) { - conn.sendJSON(EventMessage{Event: "insert_node", Body: node}) + conn.sendJSON(Message{Event: MessageEventInsertNode, Body: node}) } func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time) { - conn.sendJSON(EventMessage{Event: "insert_globals", Body: stats}) + conn.sendJSON(Message{Event: MessageEventInsertGlobals, Body: stats}) } func (conn *Connection) PruneNodes(deleteAfter time.Duration) { - conn.sendJSON(EventMessage{Event: "prune_nodes"}) + conn.sendJSON(Message{Event: MessageEventPruneNodes}) } func (conn *Connection) Close() { diff --git a/database/socket/database_test.go b/database/socket/database_test.go index 562ee4b..ed3272b 100644 --- a/database/socket/database_test.go +++ b/database/socket/database_test.go @@ -1,6 +1,7 @@ package socket import ( + "encoding/json" "net" "testing" "time" @@ -53,20 +54,27 @@ func TestClient(t *testing.T) { assert.NotNil(client) time.Sleep(time.Duration(3) * time.Microsecond) + decoder := json.NewDecoder(client) + var msg Message + conn.InsertNode(&runtime.Node{}) + decoder.Decode(&msg) + assert.Equal("insert_node", msg.Event) + conn.InsertGlobals(&runtime.GlobalStats{}, time.Now()) + decoder.Decode(&msg) + assert.Equal("insert_globals", msg.Event) + + conn.PruneNodes(time.Hour * 24 * 7) + decoder.Decode(&msg) + assert.Equal("prune_nodes", msg.Event) time.Sleep(time.Duration(3) * time.Microsecond) // to reach in sendJSON removing of disconnection - err = client.Close() - assert.NoError(err, "disconnect should work") - time.Sleep(time.Duration(3) * time.Microsecond) - conn.InsertNode(&runtime.Node{}) - time.Sleep(time.Duration(3) * time.Microsecond) - - // to reach all parts of conn.Close() - client, err = net.Dial("unix", "/tmp/yanic-database2.socket") - time.Sleep(time.Duration(3) * time.Microsecond) - conn.Close() + + conn.InsertNode(&runtime.Node{}) + err = decoder.Decode(&msg) + assert.Error(err) + } diff --git a/database/socket/internal.go b/database/socket/internal.go index 311ce2d..17998ee 100644 --- a/database/socket/internal.go +++ b/database/socket/internal.go @@ -6,11 +6,6 @@ import ( "net" ) -type EventMessage struct { - Event string `json:"event"` - Body interface{} `json:"body,omitempty"` -} - func (conn *Connection) handleSocketConnection(ln net.Listener) { for { c, err := ln.Accept() @@ -24,7 +19,7 @@ func (conn *Connection) handleSocketConnection(ln net.Listener) { } } -func (conn *Connection) sendJSON(msg EventMessage) { +func (conn *Connection) sendJSON(msg Message) { conn.clientMux.Lock() for addr, c := range conn.clients { d := json.NewEncoder(c) diff --git a/database/socket/message.go b/database/socket/message.go new file mode 100644 index 0000000..786c8d3 --- /dev/null +++ b/database/socket/message.go @@ -0,0 +1,12 @@ +package socket + +type Message struct { + Event string `json:"event"` + Body interface{} `json:"body,omitempty"` +} + +const ( + MessageEventInsertNode = "insert_node" + MessageEventInsertGlobals = "insert_globals" + MessageEventPruneNodes = "prune_nodes" +)