2018-01-15 22:19:31 +00:00
|
|
|
package respondd
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This database type is for injecting into another yanic instance.
|
|
|
|
*/
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"compress/flate"
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2019-01-17 12:26:16 +00:00
|
|
|
"github.com/bdlm/log"
|
|
|
|
|
2018-01-15 22:19:31 +00:00
|
|
|
"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) Type() string {
|
|
|
|
return c["type"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) Address() string {
|
|
|
|
return c["address"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
database.RegisterAdapter("respondd", Connect)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Connect(configuration map[string]interface{}) (database.Connection, error) {
|
|
|
|
var config Config
|
|
|
|
config = configuration
|
|
|
|
|
|
|
|
conn, err := net.Dial(config.Type(), 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{
|
2019-01-24 01:56:13 +00:00
|
|
|
Nodeinfo: node.Nodeinfo,
|
2018-01-15 22:19:31 +00:00
|
|
|
Statistics: node.Statistics,
|
|
|
|
Neighbours: node.Neighbours,
|
|
|
|
}
|
|
|
|
|
|
|
|
writer := bufio.NewWriterSize(conn.conn, 8192)
|
|
|
|
|
|
|
|
flater, err := flate.NewWriter(writer, flate.BestCompression)
|
|
|
|
if err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.Errorf("[database-yanic] could not create flater: %s", err)
|
2018-01-15 22:19:31 +00:00
|
|
|
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
|
|
|
|
}
|
2019-01-17 12:26:16 +00:00
|
|
|
log.WithField("node_id", nodeid).Errorf("[database-yanic] could not encode node: %s", err)
|
2018-01-15 22:19:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = flater.Flush()
|
|
|
|
if err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.Errorf("[database-yanic] could not compress: %s", err)
|
2018-01-15 22:19:31 +00:00
|
|
|
}
|
|
|
|
err = writer.Flush()
|
|
|
|
if err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.Errorf("[database-yanic] could not send: %s", err)
|
2018-01-15 22:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Connection) InsertLink(link *runtime.Link, time time.Time) {
|
|
|
|
}
|
|
|
|
|
2018-01-17 19:20:35 +00:00
|
|
|
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string, domain string) {
|
2018-01-15 22:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Connection) Close() {
|
|
|
|
conn.conn.Close()
|
|
|
|
}
|