From 58a8efc17adcce50b09b7feb6d71c95b214a7a52 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Tue, 28 Apr 2026 19:55:45 +0800 Subject: [PATCH] refactor(socket): decouple command handling and add message sending - Move command handler to variable for better flexibility - Add SendMsg function for sending messages through socket - Fix missing return statements in command execution - Improve error handling in monitor exception reporting --- command/commandParse.go | 5 ++- main.go | 5 +++ monitor/monitor.go | 7 +++- socket/server.go | 78 +++++++++++++++++++++++++++-------------- 4 files changed, 67 insertions(+), 28 deletions(-) diff --git a/command/commandParse.go b/command/commandParse.go index 06aacd4..7fac33e 100644 --- a/command/commandParse.go +++ b/command/commandParse.go @@ -50,7 +50,8 @@ func ExecuteCommand(input string) error { err := monitor.AddServiceMonitor(serviceName) if err != nil { return fmt.Errorf("failed to add service monitor: %v", err) - } + } + return nil } case "monitor.remove": serviceName := getContent(input, "serviceName") @@ -60,9 +61,11 @@ func ExecuteCommand(input string) error { if err != nil { return fmt.Errorf("failed to remove service monitor: %v", err) } + return nil } case "watchdog.shutdown": os.Exit(0) + return nil } return fmt.Errorf("unknown command") } \ No newline at end of file diff --git a/main.go b/main.go index 40dea0a..e8a89b3 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "Watchdog_Linux-systemd/command" "Watchdog_Linux-systemd/postLog" "Watchdog_Linux-systemd/socket" "fmt" @@ -44,6 +45,10 @@ func main() { } postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", softwareInfo.Name, softwareInfo.Version, softwareInfo.BuildVer, softwareInfo.BuildType, softwareInfo.Developer)) + // Set command handler + socket.CommandHandler = command.ExecuteCommand + // End of command handler + go func() { err := socket.BootSocket(Type, listenAddr, listenPort) if err != nil { diff --git a/monitor/monitor.go b/monitor/monitor.go index 190f196..90a88dd 100644 --- a/monitor/monitor.go +++ b/monitor/monitor.go @@ -2,6 +2,7 @@ package monitor import ( "Watchdog_Linux-systemd/postLog" + "Watchdog_Linux-systemd/socket" "fmt" "os/exec" "strings" @@ -123,7 +124,11 @@ func checkServiceLogs(serviceName string) (bool, error) { func throwException(serviceName, errorContent string) error { postLog.Error(fmt.Sprintf("[Monitor] Service: %s - Exception: %s", serviceName, errorContent)) - return fmt.Errorf("service %s exception: %s", serviceName, errorContent) + err := socket.SendMsg(fmt.Sprintf("service %s exception: %s", serviceName, errorContent)) + if err != nil { + return fmt.Errorf("failed to send exception message: %v", err) + } + return nil } func GetActiveMonitors() []string { diff --git a/socket/server.go b/socket/server.go index ea8aab9..b83ce35 100644 --- a/socket/server.go +++ b/socket/server.go @@ -1,7 +1,6 @@ package socket import ( - "Watchdog_Linux-systemd/command" "Watchdog_Linux-systemd/postLog" "bufio" "fmt" @@ -9,6 +8,11 @@ import ( "strings" ) +var ( + Conn net.Conn + CommandHandler func(string) error +) + func BootSocket(networkType, listenAddr string, listenPort int) error { listen, err := net.Listen(networkType, fmt.Sprintf("%s:%d", listenAddr, listenPort)) if err != nil { @@ -20,42 +24,64 @@ func BootSocket(networkType, listenAddr string, listenPort int) error { postLog.Info(fmt.Sprintf("Server is running on %s:%d", listenAddr, listenPort)) for { - conn, err := listen.Accept() + Conn, err = listen.Accept() if err != nil { - postLog.Error(fmt.Sprintf("Failed to accept: %v, err: %v", conn, err)) + postLog.Error(fmt.Sprintf("Failed to accept: %v, err: %v", Conn, err)) } - go handleRequest(conn) + go handleRequest() } } -func handleRequest(conn net.Conn) { - defer conn.Close() +func handleRequest() { + defer Conn.Close() - reader := bufio.NewReader(conn) + reader := bufio.NewReader(Conn) - for { - data, err := reader.ReadBytes('\n') - if err != nil { - return - } + for { + data, err := reader.ReadBytes('\n') + if err != nil { + return + } - recvMsg := strings.TrimSpace(string(data)) + recvMsg := strings.TrimSpace(string(data)) - responseMsg := "" + responseMsg := "" if len(recvMsg) != 0 { postLog.Debug(fmt.Sprintf("Received message: %s", recvMsg)) if recvMsg == "watchdogAgentConnectionTest" { - responseMsg = "success" - } else { - err := command.ExecuteCommand(recvMsg) - if err != nil { - responseMsg = fmt.Sprintf("error: %v", err) - } else { - responseMsg = "success" - } - } + responseMsg = "success" + } else { + if CommandHandler != nil { + err := CommandHandler(recvMsg) + if err != nil { + responseMsg = fmt.Sprintf("error: %v", err) + } else { + responseMsg = "success" + } + } else { + responseMsg = "error: command handler not initialized" + } + } } - - conn.Write([]byte(responseMsg + "\n")) - } + + Conn.Write([]byte(responseMsg + "\n")) + } +} + +func SendMsg(msg string) error { + if Conn == nil { + return fmt.Errorf("connection is nil") + } + + data := []byte(msg + "\n") + n, err := Conn.Write(data) + if err != nil { + return fmt.Errorf("failed to write message: %v", err) + } + + if n != len(data) { + return fmt.Errorf("incomplete write: wrote %d bytes out of %d", n, len(data)) + } + + return nil }