yanic/database/respondd/main.go

97 lines
2.0 KiB
Go
Raw Normal View History

package respondd
2018-01-15 23:19:31 +01:00
/**
* This database type is for injecting into another yanic instance.
*/
import (
"bufio"
"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{}
2018-01-18 15:25:52 +01:00
func (c Config) Type() string {
return c["type"].(string)
}
2018-01-15 23:19:31 +01:00
func (c Config) Address() string {
return c["address"].(string)
}
func init() {
database.RegisterAdapter("respondd", Connect)
2018-01-15 23:19:31 +01:00
}
func Connect(configuration map[string]interface{}) (database.Connection, error) {
var config Config
config = configuration
2018-01-18 15:25:52 +01:00
conn, err := net.Dial(config.Type(), config.Address())
2018-01-15 23:19:31 +01:00
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,
}
2018-01-19 16:12:27 +01:00
writer := bufio.NewWriterSize(conn.conn, 8192)
2018-01-15 23:19:31 +01:00
2018-01-18 15:25:52 +01:00
flater, err := flate.NewWriter(writer, flate.BestCompression)
2018-01-15 23:19:31 +01:00
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
}
2018-01-19 16:12:27 +01:00
log.Printf("[database-yanic] could not encode %s node: %s", nodeid, err)
2018-01-15 23:19:31 +01:00
return
}
2018-01-19 16:12:27 +01:00
err = flater.Flush()
if err != nil {
log.Printf("[database-yanic] could not compress: %s", err)
}
err = writer.Flush()
if err != nil {
log.Printf("[database-yanic] could not send: %s", err)
}
2018-01-15 23:19:31 +01:00
}
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()
}