2017-04-27 21:09:46 +02:00
|
|
|
package socket
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This database type is just for,
|
|
|
|
* - debugging without a influxconn
|
|
|
|
* - example for other developers for new databases
|
|
|
|
*/
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/database"
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Connection struct {
|
|
|
|
database.Connection
|
|
|
|
listener net.Listener
|
|
|
|
clients map[net.Addr]net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
database.RegisterAdapter("socket", Connect)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Connect(configuration interface{}) (database.Connection, error) {
|
2017-04-27 22:28:31 +02:00
|
|
|
config := configuration.(map[string]interface{})
|
|
|
|
|
|
|
|
if !config["enable"].(bool) {
|
2017-04-27 21:09:46 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-04-27 22:28:31 +02:00
|
|
|
ln, err := net.Listen(config["type"].(string), config["address"].(string))
|
2017-04-27 21:09:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-27 22:28:31 +02:00
|
|
|
conn := &Connection{listener: ln, clients: make(map[net.Addr]net.Conn)}
|
2017-04-27 21:09:46 +02:00
|
|
|
go conn.handleSocketConnection(ln)
|
|
|
|
|
|
|
|
log.Println("[socket-database] listen on: ", ln.Addr())
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Connection) InsertNode(node *runtime.Node) {
|
|
|
|
conn.sendJSON(EventMessage{Event: "insert_node", Body: node})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time) {
|
|
|
|
conn.sendJSON(EventMessage{Event: "insert_globals", Body: stats})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
|
|
|
|
conn.sendJSON(EventMessage{Event: "prune_nodes"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Connection) Close() {
|
|
|
|
for _, c := range conn.clients {
|
2017-04-27 21:45:44 +02:00
|
|
|
c.Close()
|
2017-04-27 21:09:46 +02:00
|
|
|
}
|
2017-04-27 21:45:44 +02:00
|
|
|
conn.listener.Close()
|
2017-04-27 21:09:46 +02:00
|
|
|
}
|