reaction/app/pipe.go

113 lines
2.8 KiB
Go
Raw Normal View History

2023-05-03 20:03:22 +02:00
package app
import (
"encoding/gob"
"log"
"net"
2023-05-03 20:03:22 +02:00
"os"
2023-05-05 15:33:00 +02:00
"path"
"time"
2023-05-03 20:03:22 +02:00
)
func genClientStatus() ClientStatus {
cs := make(ClientStatus)
matchesLock.Lock()
2023-05-03 20:03:22 +02:00
// Painful data manipulation
for pf, times := range matches {
pattern, filter := pf.p, pf.f
if cs[filter.stream.name] == nil {
cs[filter.stream.name] = make(map[string]MapPatternStatus)
2023-05-03 20:03:22 +02:00
}
if cs[filter.stream.name][filter.name] == nil {
cs[filter.stream.name][filter.name] = make(MapPatternStatus)
}
cs[filter.stream.name][filter.name][pattern] = &PatternStatus{len(times), nil}
2023-05-03 20:03:22 +02:00
}
matchesLock.Unlock()
actionsLock.Lock()
2023-05-03 20:03:22 +02:00
// Painful data manipulation
for pat := range actions {
pattern, action, then := pat.p, pat.a, pat.t
if cs[action.filter.stream.name] == nil {
cs[action.filter.stream.name] = make(map[string]MapPatternStatus)
}
if cs[action.filter.stream.name][action.filter.name] == nil {
cs[action.filter.stream.name][action.filter.name] = make(MapPatternStatus)
}
if cs[action.filter.stream.name][action.filter.name][pattern] == nil {
cs[action.filter.stream.name][action.filter.name][pattern] = new(PatternStatus)
}
ps := cs[action.filter.stream.name][action.filter.name][pattern]
if ps.Actions == nil {
ps.Actions = make(map[string][]string)
2023-05-03 20:03:22 +02:00
}
ps.Actions[action.name] = append(ps.Actions[action.name], then.Format(time.DateTime))
2023-05-03 20:03:22 +02:00
}
actionsLock.Unlock()
return cs
2023-05-03 20:03:22 +02:00
}
func createOpenSocket() net.Listener {
err := os.MkdirAll(path.Dir(*SocketPath), 0755)
2023-05-05 15:33:00 +02:00
if err != nil {
log.Fatalln("FATAL Failed to create socket directory")
}
_, err = os.Stat(*SocketPath)
2023-05-03 20:03:22 +02:00
if err == nil {
2023-05-05 15:33:00 +02:00
log.Println("WARN socket", SocketPath, "already exists: Is the daemon already running? Deleting.")
err = os.Remove(*SocketPath)
2023-05-03 20:03:22 +02:00
if err != nil {
2023-05-05 15:33:00 +02:00
log.Fatalln("FATAL Failed to remove socket:", err)
2023-05-03 20:03:22 +02:00
}
}
ln, err := net.Listen("unix", *SocketPath)
2023-05-03 20:03:22 +02:00
if err != nil {
2023-05-05 15:33:00 +02:00
log.Fatalln("FATAL Failed to create socket:", err)
2023-05-03 20:03:22 +02:00
}
return ln
2023-05-03 20:03:22 +02:00
}
// Handle connections
func SocketManager() {
ln := createOpenSocket()
defer ln.Close()
2023-05-03 20:03:22 +02:00
for {
conn, err := ln.Accept()
2023-05-03 20:03:22 +02:00
if err != nil {
log.Println("ERROR Failed to open connection from cli:", err)
continue
2023-05-03 20:03:22 +02:00
}
go func(conn net.Conn) {
defer conn.Close()
var request Request
2023-05-03 20:03:22 +02:00
var response Response
err := gob.NewDecoder(conn).Decode(&request)
if err != nil {
log.Println("ERROR Invalid Message from cli:", err)
return
}
2023-05-03 20:03:22 +02:00
switch request.Request {
case Show:
response.ClientStatus = genClientStatus()
2023-05-03 20:03:22 +02:00
case Flush:
// FIXME reimplement flush
response.Number = 0
2023-05-03 20:03:22 +02:00
default:
log.Println("ERROR Invalid Message from cli: unrecognised Request type")
return
}
err = gob.NewEncoder(conn).Encode(response)
if err != nil {
log.Println("ERROR Can't respond to cli:", err)
return
2023-05-03 20:03:22 +02:00
}
}(conn)
2023-05-03 20:03:22 +02:00
}
}