make the daemon log when the UI socket is available or goes down (closes #123)

This commit is contained in:
evilsocket 2018-04-06 14:48:43 +02:00
parent db7f759403
commit 314c526fc4
Failed to generate hash of commit
2 changed files with 39 additions and 22 deletions

View file

@ -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

View file

@ -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())