2022-06-07 21:20:16 +02:00
|
|
|
package backive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2022-08-14 21:33:38 +02:00
|
|
|
// MsgLevel type for setting the message level
|
|
|
|
type MsgLevel int
|
|
|
|
|
|
|
|
// Constants for setting the MsgLevel
|
|
|
|
const (
|
|
|
|
ERROR MsgLevel = iota * 10
|
|
|
|
FINISH
|
|
|
|
REMIND
|
|
|
|
INFO
|
|
|
|
DEBUG
|
|
|
|
)
|
|
|
|
|
2022-09-02 13:22:05 +02:00
|
|
|
type MsgLvls struct {
|
|
|
|
Error MsgLevel
|
|
|
|
Finish MsgLevel
|
|
|
|
Remind MsgLevel
|
|
|
|
Info MsgLevel
|
|
|
|
Debug MsgLevel
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:33:38 +02:00
|
|
|
// UIHandler internal data struct
|
2022-06-07 21:20:16 +02:00
|
|
|
type UIHandler struct {
|
|
|
|
ls net.Listener
|
|
|
|
client net.Conn
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:22:05 +02:00
|
|
|
var UiHdl UIHandler
|
|
|
|
var MsgLevels = MsgLvls{ERROR, FINISH, REMIND, INFO, DEBUG}
|
|
|
|
|
2022-06-07 21:20:16 +02:00
|
|
|
var mockUIAccept = func(uh *UIHandler) (net.Conn, error) {
|
|
|
|
log.Println("Calling eh.ls.Accept()")
|
|
|
|
return uh.ls.Accept()
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:33:38 +02:00
|
|
|
// Init initializing the UIHandler
|
2022-06-07 21:20:16 +02:00
|
|
|
func (uh *UIHandler) Init(socketPath string) error {
|
|
|
|
log.Println("Initializing UIHandler")
|
|
|
|
var err error
|
|
|
|
dir, _ := path.Split(socketPath)
|
|
|
|
CreateDirectoryIfNotExists(dir)
|
|
|
|
uh.ls, err = net.Listen("unix", socketPath)
|
|
|
|
if err != nil {
|
2022-09-02 13:22:05 +02:00
|
|
|
log.Printf("Error: %s", err)
|
2022-06-07 21:20:16 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-09-02 13:22:05 +02:00
|
|
|
log.Println("Listening for ui clients")
|
2022-06-07 21:20:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:33:38 +02:00
|
|
|
// Listen starts the Unix socket listener
|
2022-06-07 21:20:16 +02:00
|
|
|
func (uh *UIHandler) Listen() {
|
|
|
|
log.Println("Running UIHandler loop")
|
2022-09-02 13:22:05 +02:00
|
|
|
for {
|
|
|
|
var err error
|
|
|
|
uh.client, err = uh.ls.Accept()
|
|
|
|
if uh.client == nil {
|
|
|
|
log.Println("Client is nil, why?")
|
|
|
|
}
|
|
|
|
if uh.client != nil {
|
|
|
|
log.Printf("client's local addr %s", uh.client.LocalAddr().String())
|
2022-06-07 21:20:16 +02:00
|
|
|
}
|
2022-09-02 13:22:05 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Accept failed %e\n", err)
|
|
|
|
}
|
|
|
|
log.Print("Accepted UI client")
|
|
|
|
}
|
2022-06-07 21:20:16 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 21:33:38 +02:00
|
|
|
// DisplayMessage is the method to use inside the service to display messages, with intended level
|
2022-09-02 13:22:05 +02:00
|
|
|
func (uh *UIHandler) DisplayMessage(header string, message string, level MsgLevel) error {
|
2022-06-07 21:20:16 +02:00
|
|
|
if uh.client != nil {
|
|
|
|
var data map[string]interface{}
|
2022-09-02 13:22:05 +02:00
|
|
|
data["level"] = int(level)
|
2022-06-07 21:20:16 +02:00
|
|
|
data["header"] = header
|
|
|
|
data["message"] = message
|
|
|
|
b, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Problem in sending message to UI: %e", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
uh.client.Write(b)
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-14 21:33:38 +02:00
|
|
|
log.Println("No UI client available, msg did not get delivered.")
|
|
|
|
return fmt.Errorf("No UI client available, msg not delivered")
|
2022-06-07 21:20:16 +02:00
|
|
|
}
|