reaction/app/pipe.go

121 lines
2.9 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 filter, filterMatches := range matches {
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)
}
for pattern, patternMatches := range filterMatches {
var ps PatternStatus
cs[filter.stream.name][filter.name][pattern] = &ps
2023-05-03 20:03:22 +02:00
ps.Matches = len(patternMatches)
2023-05-03 20:03:22 +02:00
}
}
matchesLock.Unlock()
actionsLock.Lock()
2023-05-03 20:03:22 +02:00
// Painful data manipulation
for action, pendingActions := range actions {
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)
}
for pattern, patternPendingActions := range pendingActions {
if cs[action.filter.stream.name][action.filter.name][pattern] == nil {
var ps PatternStatus
cs[action.filter.stream.name][action.filter.name][pattern] = &ps
2023-05-03 20:03:22 +02:00
}
var ps *PatternStatus
ps = cs[action.filter.stream.name][action.filter.name][pattern]
ps.Actions = make(map[string][]string)
for _, t := range patternPendingActions {
ps.Actions[action.name] = append(ps.Actions[action.name], t.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
}
}