reaction/app/pipe.go

124 lines
3.4 KiB
Go
Raw Permalink Normal View History

2023-05-03 20:03:22 +02:00
package app
import (
"encoding/gob"
"net"
2023-05-03 20:03:22 +02:00
"os"
2023-05-05 15:33:00 +02:00
"path"
2023-10-04 12:00:00 +02:00
"sync"
"time"
2023-10-12 12:00:00 +02:00
"framagit.org/ppom/reaction/logger"
2023-05-03 20:03:22 +02:00
)
2023-10-04 12:00:00 +02:00
func genClientStatus(local_actions ActionsMap, local_matches MatchesMap, local_actionsLock, local_matchesLock *sync.Mutex) ClientStatus {
cs := make(ClientStatus)
2023-10-04 12:00:00 +02:00
local_matchesLock.Lock()
2023-05-03 20:03:22 +02:00
// Painful data manipulation
2023-10-04 12:00:00 +02:00
for pf, times := range local_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
}
2023-10-04 12:00:00 +02:00
local_matchesLock.Unlock()
local_actionsLock.Lock()
2023-05-03 20:03:22 +02:00
// Painful data manipulation
2023-10-04 12:00:00 +02:00
for pa, times := range local_actions {
2023-10-01 12:00:00 +02:00
pattern, action := pa.p, pa.a
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
}
2023-10-04 12:00:00 +02:00
for then := range times {
2023-10-01 12:00:00 +02:00
ps.Actions[action.name] = append(ps.Actions[action.name], then.Format(time.DateTime))
}
2023-05-03 20:03:22 +02:00
}
2023-10-04 12:00:00 +02:00
local_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 {
2023-10-12 12:00:00 +02:00
logger.Fatalln("Failed to create socket directory")
2023-05-05 15:33:00 +02:00
}
_, err = os.Stat(*SocketPath)
2023-05-03 20:03:22 +02:00
if err == nil {
2023-10-12 12:00:00 +02:00
logger.Println(logger.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-10-12 12:00:00 +02:00
logger.Fatalln("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-10-12 12:00:00 +02:00
logger.Fatalln("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
2023-10-01 12:00:00 +02:00
func SocketManager(streams map[string]*Stream) {
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 {
2023-10-12 12:00:00 +02:00
logger.Println(logger.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 {
2023-10-12 12:00:00 +02:00
logger.Println(logger.ERROR, "Invalid Message from cli:", err)
return
}
2023-05-03 20:03:22 +02:00
switch request.Request {
case Show:
2023-10-04 12:00:00 +02:00
response.ClientStatus = genClientStatus(actions, matches, &actionsLock, &matchesLock)
2023-05-03 20:03:22 +02:00
case Flush:
le := LogEntry{time.Now(), 0, request.Pattern, "", "", 0, false}
2023-10-04 12:00:00 +02:00
matchesC := FlushMatchOrder{request.Pattern, make(chan MatchesMap)}
actionsC := FlushActionOrder{request.Pattern, make(chan ActionsMap)}
flushToMatchesC <- matchesC
flushToActionsC <- actionsC
2023-10-01 12:00:00 +02:00
flushToDatabaseC <- le
2023-10-04 12:00:00 +02:00
var lock sync.Mutex
response.ClientStatus = genClientStatus(<-actionsC.ret, <-matchesC.ret, &lock, &lock)
2023-05-03 20:03:22 +02:00
default:
2023-10-12 12:00:00 +02:00
logger.Println(logger.ERROR, "Invalid Message from cli: unrecognised Request type")
return
}
err = gob.NewEncoder(conn).Encode(response)
if err != nil {
2023-10-12 12:00:00 +02:00
logger.Println(logger.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
}
}