Try abstract unix sockets, failing atm...

This commit is contained in:
Marcel Otte 2022-09-02 13:22:05 +02:00
parent fe8f26e4b4
commit 8376d00973
5 changed files with 96 additions and 17 deletions

View File

@ -29,7 +29,6 @@ var (
config backive.Configuration
database backive.Database
events backive.EventHandler
uihdl backive.UIHandler
)
func defaultCallback(envMap map[string]string) {
@ -141,9 +140,9 @@ func main() {
config.Load()
setupLogging()
backive.Init(config, database)
uihdl.Init(config.Settings.UIUnixSocketLocation)
backive.UiHdl.Init(config.Settings.UIUnixSocketLocation)
// Start UIHandler to be able to inform users through notifications
go uihdl.Listen()
go backive.UiHdl.Listen()
// init scheduler and check for next needed runs?
// start event loop
events.Init(config.Settings.UnixSocketLocation)

View File

@ -1,6 +1,10 @@
package main
import (
"log"
"os"
"path"
"fyne.io/fyne/v2/app"
"github.com/qwc/backive"
@ -11,9 +15,25 @@ var (
config backive.Configuration
)
func setupLogging() {
apphomedir, _ := os.UserHomeDir()
apphomedir = path.Join(apphomedir, ".config", "backive")
logname := path.Join(apphomedir, "backiveui.log")
logdir, _ := path.Split(logname)
backive.CreateDirectoryIfNotExists(logdir)
logfile, err := os.OpenFile(logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
log.Fatal(err)
panic("no logfile no info")
}
log.SetOutput(logfile)
log.Println("Logging initialized")
}
func main() {
config.Load()
setupLogging()
app := app.NewWithID("Backive UI")
backiveui.Init(app, nil, config)
app.Run()

View File

@ -53,6 +53,7 @@ func (eh *EventHandler) RegisterCallback(cb func(map[string]string)) {
func (eh *EventHandler) process() {
client, err := mockAccept(eh)
log.Println("Accepted client")
UiHdl.DisplayMessage("Event debugging", "Catched event...", MsgLevels.Debug)
if err != nil {
log.Println(err)
return
@ -86,6 +87,7 @@ func (eh *EventHandler) process() {
for k, v := range message["data"].(map[string]interface{}) {
env[k] = v.(string)
}
UiHdl.DisplayMessage("Event debugging", "Got udev event msg.", MsgLevels.Debug)
}
for _, v := range eh.callbacks {
if v != nil {

View File

@ -1,8 +1,10 @@
package ui
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net"
"os"
@ -42,27 +44,65 @@ func Init(a fyne.App, w fyne.Window, conf backive.Configuration) {
makeTray(app)
config = conf
apphomedir, _ := os.UserHomeDir()
apphomedir += string(os.PathSeparator) + ".config" + string(os.PathSeparator) + "backive" + string(os.PathSeparator) + "ui.json"
apphomedir = path.Join(apphomedir, ".config", "backive", "ui.json")
LoadSettings()
go PollConnection()
fmt.Println("UI started")
log.Println("UI started")
}
// PollConnection polls in an endless loop the connection
func PollConnection() {
var err error
// Loop for connection and receiving data
for {
// connect when client 'c' is nil
if c == nil {
log.Println("Creating connection")
c, err = net.Dial("unix", config.Settings.UIUnixSocketLocation)
} else {
err = fmt.Errorf("Connection already established")
log.Println(err)
}
// handle error on connection
if err != nil {
log.Println(err)
// ignore
err = nil
c = nil
// sleep a while and then retry
time.Sleep(10 * time.Second)
}
// receive msgs
if c != nil {
data := make([]byte, 2048)
for {
buf := make([]byte, 512)
nr, err := c.Read(buf)
log.Printf("Read %d bytes...", nr)
if err == io.ErrClosedPipe {
c = nil
err = nil
break
}
if err != nil && err != io.EOF {
log.Println(err)
break
}
data = append(data, buf[0:nr]...)
if err == io.EOF {
break
}
}
sdata := string(bytes.Trim(data, "\x00"))
var message map[string]string
log.Printf("Reading JSON: %s", sdata)
errjson := json.Unmarshal([]byte(sdata), &message)
if errjson != nil {
log.Println(errjson)
continue
}
ShowNotification(message)
}
}
}
@ -127,7 +167,7 @@ func SaveSettings() {
if err != nil {
panic(err)
}
fmt.Println("Saved Settings.")
log.Println("Saved Settings.")
}
// LoadSettings loads the settings from the place where SaveSettings stored them.
@ -139,7 +179,7 @@ func LoadSettings() {
panic(rferr)
}
json.Unmarshal(data, &uisettings)
fmt.Println("Loaded Settings.")
log.Println("Loaded Settings.")
} /*else if os.IsNotExist(err) {
// no data
}*/

View File

@ -20,12 +20,23 @@ const (
DEBUG
)
type MsgLvls struct {
Error MsgLevel
Finish MsgLevel
Remind MsgLevel
Info MsgLevel
Debug MsgLevel
}
// UIHandler internal data struct
type UIHandler struct {
ls net.Listener
client net.Conn
}
var UiHdl UIHandler
var MsgLevels = MsgLvls{ERROR, FINISH, REMIND, INFO, DEBUG}
var mockUIAccept = func(uh *UIHandler) (net.Conn, error) {
log.Println("Calling eh.ls.Accept()")
return uh.ls.Accept()
@ -39,30 +50,37 @@ func (uh *UIHandler) Init(socketPath string) error {
CreateDirectoryIfNotExists(dir)
uh.ls, err = net.Listen("unix", socketPath)
if err != nil {
log.Printf("Error: %s", err)
return err
}
log.Println("Listening for ui clients")
return nil
}
// Listen starts the Unix socket listener
func (uh *UIHandler) Listen() {
log.Println("Running UIHandler loop")
func() {
for {
var err error
uh.client, err = uh.ls.Accept()
if err != nil {
log.Printf("Accept failed %e\n", err)
}
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())
}
if err != nil {
log.Printf("Accept failed %e\n", err)
}
log.Print("Accepted UI client")
}
}
// DisplayMessage is the method to use inside the service to display messages, with intended level
func (uh *UIHandler) DisplayMessage(header string, message string, level int) error {
func (uh *UIHandler) DisplayMessage(header string, message string, level MsgLevel) error {
if uh.client != nil {
var data map[string]interface{}
data["level"] = level
data["level"] = int(level)
data["header"] = header
data["message"] = message
b, err := json.Marshal(data)