mirror of https://github.com/qwc/backive.git
Try abstract unix sockets, failing atm...
This commit is contained in:
parent
fe8f26e4b4
commit
8376d00973
|
@ -29,7 +29,6 @@ var (
|
||||||
config backive.Configuration
|
config backive.Configuration
|
||||||
database backive.Database
|
database backive.Database
|
||||||
events backive.EventHandler
|
events backive.EventHandler
|
||||||
uihdl backive.UIHandler
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func defaultCallback(envMap map[string]string) {
|
func defaultCallback(envMap map[string]string) {
|
||||||
|
@ -141,9 +140,9 @@ func main() {
|
||||||
config.Load()
|
config.Load()
|
||||||
setupLogging()
|
setupLogging()
|
||||||
backive.Init(config, database)
|
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
|
// 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?
|
// init scheduler and check for next needed runs?
|
||||||
// start event loop
|
// start event loop
|
||||||
events.Init(config.Settings.UnixSocketLocation)
|
events.Init(config.Settings.UnixSocketLocation)
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
"fyne.io/fyne/v2/app"
|
"fyne.io/fyne/v2/app"
|
||||||
|
|
||||||
"github.com/qwc/backive"
|
"github.com/qwc/backive"
|
||||||
|
@ -11,9 +15,25 @@ var (
|
||||||
config backive.Configuration
|
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() {
|
func main() {
|
||||||
|
|
||||||
config.Load()
|
config.Load()
|
||||||
|
setupLogging()
|
||||||
app := app.NewWithID("Backive UI")
|
app := app.NewWithID("Backive UI")
|
||||||
backiveui.Init(app, nil, config)
|
backiveui.Init(app, nil, config)
|
||||||
app.Run()
|
app.Run()
|
||||||
|
|
|
@ -53,6 +53,7 @@ func (eh *EventHandler) RegisterCallback(cb func(map[string]string)) {
|
||||||
func (eh *EventHandler) process() {
|
func (eh *EventHandler) process() {
|
||||||
client, err := mockAccept(eh)
|
client, err := mockAccept(eh)
|
||||||
log.Println("Accepted client")
|
log.Println("Accepted client")
|
||||||
|
UiHdl.DisplayMessage("Event debugging", "Catched event...", MsgLevels.Debug)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
|
@ -86,6 +87,7 @@ func (eh *EventHandler) process() {
|
||||||
for k, v := range message["data"].(map[string]interface{}) {
|
for k, v := range message["data"].(map[string]interface{}) {
|
||||||
env[k] = v.(string)
|
env[k] = v.(string)
|
||||||
}
|
}
|
||||||
|
UiHdl.DisplayMessage("Event debugging", "Got udev event msg.", MsgLevels.Debug)
|
||||||
}
|
}
|
||||||
for _, v := range eh.callbacks {
|
for _, v := range eh.callbacks {
|
||||||
if v != nil {
|
if v != nil {
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -42,27 +44,65 @@ func Init(a fyne.App, w fyne.Window, conf backive.Configuration) {
|
||||||
makeTray(app)
|
makeTray(app)
|
||||||
config = conf
|
config = conf
|
||||||
apphomedir, _ := os.UserHomeDir()
|
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()
|
LoadSettings()
|
||||||
go PollConnection()
|
go PollConnection()
|
||||||
fmt.Println("UI started")
|
log.Println("UI started")
|
||||||
}
|
}
|
||||||
|
|
||||||
// PollConnection polls in an endless loop the connection
|
// PollConnection polls in an endless loop the connection
|
||||||
func PollConnection() {
|
func PollConnection() {
|
||||||
var err error
|
var err error
|
||||||
|
// Loop for connection and receiving data
|
||||||
for {
|
for {
|
||||||
|
// connect when client 'c' is nil
|
||||||
if c == nil {
|
if c == nil {
|
||||||
|
log.Println("Creating connection")
|
||||||
c, err = net.Dial("unix", config.Settings.UIUnixSocketLocation)
|
c, err = net.Dial("unix", config.Settings.UIUnixSocketLocation)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("Connection already established")
|
err = fmt.Errorf("Connection already established")
|
||||||
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
// handle error on connection
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
// ignore
|
// ignore
|
||||||
err = nil
|
err = nil
|
||||||
|
c = nil
|
||||||
// sleep a while and then retry
|
// sleep a while and then retry
|
||||||
time.Sleep(10 * time.Second)
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println("Saved Settings.")
|
log.Println("Saved Settings.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadSettings loads the settings from the place where SaveSettings stored them.
|
// LoadSettings loads the settings from the place where SaveSettings stored them.
|
||||||
|
@ -139,7 +179,7 @@ func LoadSettings() {
|
||||||
panic(rferr)
|
panic(rferr)
|
||||||
}
|
}
|
||||||
json.Unmarshal(data, &uisettings)
|
json.Unmarshal(data, &uisettings)
|
||||||
fmt.Println("Loaded Settings.")
|
log.Println("Loaded Settings.")
|
||||||
} /*else if os.IsNotExist(err) {
|
} /*else if os.IsNotExist(err) {
|
||||||
// no data
|
// no data
|
||||||
}*/
|
}*/
|
||||||
|
|
|
@ -20,12 +20,23 @@ const (
|
||||||
DEBUG
|
DEBUG
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MsgLvls struct {
|
||||||
|
Error MsgLevel
|
||||||
|
Finish MsgLevel
|
||||||
|
Remind MsgLevel
|
||||||
|
Info MsgLevel
|
||||||
|
Debug MsgLevel
|
||||||
|
}
|
||||||
|
|
||||||
// UIHandler internal data struct
|
// UIHandler internal data struct
|
||||||
type UIHandler struct {
|
type UIHandler struct {
|
||||||
ls net.Listener
|
ls net.Listener
|
||||||
client net.Conn
|
client net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var UiHdl UIHandler
|
||||||
|
var MsgLevels = MsgLvls{ERROR, FINISH, REMIND, INFO, DEBUG}
|
||||||
|
|
||||||
var mockUIAccept = func(uh *UIHandler) (net.Conn, error) {
|
var mockUIAccept = func(uh *UIHandler) (net.Conn, error) {
|
||||||
log.Println("Calling eh.ls.Accept()")
|
log.Println("Calling eh.ls.Accept()")
|
||||||
return uh.ls.Accept()
|
return uh.ls.Accept()
|
||||||
|
@ -39,30 +50,37 @@ func (uh *UIHandler) Init(socketPath string) error {
|
||||||
CreateDirectoryIfNotExists(dir)
|
CreateDirectoryIfNotExists(dir)
|
||||||
uh.ls, err = net.Listen("unix", socketPath)
|
uh.ls, err = net.Listen("unix", socketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("Error: %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
log.Println("Listening for ui clients")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen starts the Unix socket listener
|
// Listen starts the Unix socket listener
|
||||||
func (uh *UIHandler) Listen() {
|
func (uh *UIHandler) Listen() {
|
||||||
log.Println("Running UIHandler loop")
|
log.Println("Running UIHandler loop")
|
||||||
func() {
|
for {
|
||||||
for {
|
var err error
|
||||||
var err error
|
uh.client, err = uh.ls.Accept()
|
||||||
uh.client, err = uh.ls.Accept()
|
if uh.client == nil {
|
||||||
if err != nil {
|
log.Println("Client is nil, why?")
|
||||||
log.Printf("Accept failed %e\n", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}()
|
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
|
// 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 {
|
if uh.client != nil {
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
data["level"] = level
|
data["level"] = int(level)
|
||||||
data["header"] = header
|
data["header"] = header
|
||||||
data["message"] = message
|
data["message"] = message
|
||||||
b, err := json.Marshal(data)
|
b, err := json.Marshal(data)
|
||||||
|
|
Loading…
Reference in New Issue