Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] add database respondd for forwarding (#120)
- Loading branch information
Showing
5 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package respondd | ||
|
||
/** | ||
* 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{} | ||
|
||
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{ | ||
NodeInfo: node.Nodeinfo, | ||
Statistics: node.Statistics, | ||
Neighbours: node.Neighbours, | ||
} | ||
|
||
writer := bufio.NewWriterSize(conn.conn, 8192) | ||
|
||
flater, err := flate.NewWriter(writer, flate.BestCompression) | ||
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 encode %s node: %s", nodeid, err) | ||
return | ||
} | ||
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) | ||
} | ||
} | ||
|
||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package respondd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/FreifunkBremen/yanic/data" | ||
"github.com/FreifunkBremen/yanic/runtime" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStart(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
conn, err := Connect(map[string]interface{}{ | ||
"type": "udp6", | ||
"address": "fasfs", | ||
}) | ||
assert.Nil(conn) | ||
assert.Error(err) | ||
|
||
conn, err = Connect(map[string]interface{}{ | ||
"type": "udp", | ||
"address": "localhost:11001", | ||
}) | ||
assert.NoError(err) | ||
|
||
conn.InsertNode(&runtime.Node{ | ||
Nodeinfo: &data.NodeInfo{ | ||
NodeID: "73deadbeaf13", | ||
Hostname: "inject-test", | ||
Network: data.Network{ | ||
Mac: "73:de:ad:be:af:13", | ||
Addresses: []string{"a", "b"}, | ||
}, | ||
}, | ||
Statistics: &data.Statistics{ | ||
NodeID: "73deadbeaf13", | ||
Clients: data.Clients{ | ||
Total: 1000, | ||
Wifi: 500, | ||
Wifi24: 100, | ||
Wifi5: 300, | ||
}, | ||
}, | ||
}) | ||
|
||
conn.Close() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters