2024-12-14 17:35:23 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-12-28 19:11:50 +01:00
|
|
|
"daemonSocketExample/daemonconn"
|
|
|
|
"daemonSocketExample/msghandlers"
|
2024-12-14 17:35:23 +01:00
|
|
|
"github.com/sevlyar/go-daemon"
|
|
|
|
"log"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx := &daemon.Context{
|
|
|
|
PidFileName: "liberum-daemon.pid",
|
|
|
|
PidFilePerm: 0644,
|
|
|
|
LogFileName: "liberum-daemon.log",
|
|
|
|
LogFilePerm: 0640,
|
|
|
|
WorkDir: "./",
|
|
|
|
Umask: 027,
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := ctx.Reborn()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func(ctx *daemon.Context) {
|
|
|
|
slog.Info("Stopping daemon")
|
|
|
|
_ = ctx.Release()
|
|
|
|
}(ctx)
|
|
|
|
log.Println("Daemon started")
|
|
|
|
|
|
|
|
_ = os.Remove("/tmp/liberum.sock")
|
|
|
|
|
2024-12-28 19:11:50 +01:00
|
|
|
connChan, err := daemonconn.UnixSocketListen(daemonconn.DefaultUnixSocketPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-12-14 17:35:23 +01:00
|
|
|
go func() {
|
2024-12-28 19:11:50 +01:00
|
|
|
for conn := range connChan {
|
|
|
|
err := msghandlers.HandleDaemonConn(conn)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-12-14 17:35:23 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-12-28 19:11:50 +01:00
|
|
|
conn, err := daemonconn.UnixSocketConnect(daemonconn.DefaultUnixSocketPath)
|
2024-12-14 17:35:23 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-12-29 03:06:56 +01:00
|
|
|
err = conn.WriteMessage(daemonconn.EchoRequest{EchoByte: 123, UpdatesCount: 16})
|
2024-12-28 19:11:50 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-12-14 17:35:23 +01:00
|
|
|
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
}
|