summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/hub/src/spreadspace.org/sfive/s5srv.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5srv.go b/src/hub/src/spreadspace.org/sfive/s5srv.go
new file mode 100644
index 0000000..8bd7904
--- /dev/null
+++ b/src/hub/src/spreadspace.org/sfive/s5srv.go
@@ -0,0 +1,77 @@
+package sfive
+
+import (
+ "bufio"
+ "fmt"
+ "net"
+)
+
+type StatsSinkServer struct {
+}
+
+func handleConnection(store StatsContainer, conn net.Conn) {
+ reader := bufio.NewReader(conn)
+ buffer, err := reader.ReadBytes('\n')
+ if err != nil {
+ fmt.Printf("s5 failed to read from connection: %v\n", err)
+ return
+ }
+ marshaller, err := NewStatefulDecoder(buffer)
+ if err != nil {
+ fmt.Printf("s5 failed initializing decoder with init message: %v\n", err)
+ return
+ }
+
+ for {
+ buffer, err := reader.ReadBytes('\n')
+ if err != nil {
+ fmt.Printf("s5 failed to read from connection: %v\n", err)
+ return
+ }
+
+ fmt.Printf("s5 msg: %v", string(buffer))
+
+ value, err := marshaller.Decode(buffer)
+ if err != nil {
+ fmt.Printf("s5 failed to decode message: %v\n", err)
+ continue
+ }
+
+ err = store.Append(value)
+ if err != nil {
+ fmt.Printf("s5 failed to store data: %v\n", err)
+ return
+ }
+ }
+}
+
+func (self StatsSinkServer) ListenAndServe() {
+ store, err := NewStore()
+ if err != nil {
+ fmt.Printf("s5 failed to connect to persistence layer: %v\n", err)
+ return
+ }
+ defer store.Close()
+
+ ln, err := net.Listen("unix", "/run/sfive/receiver")
+ if err != nil {
+ fmt.Printf("s5 failed to connect to unix pipe: %v", err)
+ return
+ }
+ defer ln.Close()
+
+ for {
+ conn, err := ln.Accept()
+ if err != nil {
+ // ignore
+ continue
+ }
+ go handleConnection(store, conn)
+ }
+}
+
+func NewSfiveServer() (server *StatsSinkServer, err error) {
+ // TODO read configuration and create instance with correct settings
+ server = new(StatsSinkServer)
+ return
+}