From 314c526fc4fd1ebf5a56efda70b99bc0c67d4140 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Fri, 6 Apr 2018 14:48:43 +0200 Subject: [PATCH] make the daemon log when the UI socket is available or goes down (closes #123) --- Makefile | 4 ++-- daemon/ui/client.go | 57 +++++++++++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 96965aa7..d28401d3 100644 --- a/Makefile +++ b/Makefile @@ -21,5 +21,5 @@ test: mkdir rules make clear - python ui/main.py & - sudo ./daemon/daemon + python ui/main.py --socket /tmp/osui.sock & + sudo ./daemon/daemon -ui-socket-path /tmp/osui.sock diff --git a/daemon/ui/client.go b/daemon/ui/client.go index 9bb95ab6..d9721d75 100644 --- a/daemon/ui/client.go +++ b/daemon/ui/client.go @@ -46,29 +46,49 @@ func NewClient(path string, stats *statistics.Statistics) *Client { return c } +func (c *Client) Connected() bool { + c.Lock() + defer c.Unlock() + if c.con == nil || c.con.GetState() != connectivity.Ready { + return false + } + return true +} + func (c *Client) poller() { log.Debug("UI service poller started for socket %s", c.socketPath) t := time.NewTicker(time.Second * 1) + + wasConnected := false for ts := range t.C { + isConnected := c.Connected() + if wasConnected != isConnected { + c.onStatusChange(isConnected) + wasConnected = isConnected + } + + // connect and create the client if needed if err := c.connect(); err != nil { log.Warning("Error while connecting to UI service: %s", err) - } else if c.con.GetState() == connectivity.Ready { + } else if c.Connected() == true { + // if the client is connected and ready, send a ping if err := c.ping(ts); err != nil { log.Warning("Error while pinging UI service: %s", err) - } else { - log.Debug("Got pong") } - } else { - log.Debug("Skipped ping/pong, connection not ready.") } } } -func (c *Client) connect() (err error) { - c.Lock() - defer c.Unlock() +func (c *Client) onStatusChange(connected bool) { + if connected { + log.Info("Connected to the UI service on %s", c.socketPath) + } else { + log.Error("Connection to the UI service lost.") + } +} - if c.con != nil { +func (c *Client) connect() (err error) { + if c.Connected() { return } @@ -86,13 +106,13 @@ func (c *Client) connect() (err error) { } func (c *Client) ping(ts time.Time) (err error) { - c.Lock() - defer c.Unlock() - - if c.con == nil || c.client == nil { + if c.Connected() == false { return fmt.Errorf("service is not connected.") } + c.Lock() + defer c.Unlock() + ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() reqId := uint64(ts.UnixNano()) @@ -133,16 +153,13 @@ func (c *Client) ping(ts time.Time) (err error) { } func (c *Client) Ask(con *conman.Connection) (*rule.Rule, bool) { - c.Lock() - defer c.Unlock() - - if c.con == nil || c.con.GetState() != connectivity.Ready { - if c.con != nil { - log.Debug("Client state: %v", c.con.GetState()) - } + if c.Connected() == false { return clientDisconnectedRule, false } + c.Lock() + defer c.Unlock() + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() reply, err := c.client.AskRule(ctx, con.ToRequest())