enbility / ship-go

EEBUS SHIP protocol implementation in go
https://enbility.net
MIT License
5 stars 5 forks source link

Improve Avahi interaction and handle dbus disconnection #24

Open DerAndereAndi opened 5 months ago

DerAndereAndi commented 5 months ago

The avahi service integration uses https://github.com/holoplot/go-avahi/ which communicates with the avahi daemon via dBUS.

When the dbus connection gets removed (e.g. avahi daemon stops, crashes, etc.), it will never reconnect. This needs to be improved/handled, and also made sure the SHIP service will be reannounced.

There is an issue for that already in the repository: https://github.com/holoplot/go-avahi/issues/21

DerAndereAndi commented 2 months ago

I think this should be all done in the avahi library itself. Because also re-announcing services and re-starting running services needs be handled, as well as creating a new dbus connection when that is lost. Right now the dbus connection is passed as an argument, instead it should imho be handled by the library itself.

To what out for a dbus or avahi service disconnect, this code will help:

// Get signals for DBus Disconnects
dbusConn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, "type='signal',interface='org.freedesktop.DBus.Local'")
// Get signals for Avahi NameOwnerChanged
dbusConn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, "type='signal',interface='org.freedesktop.DBus'")

For dbus disconnects, the signal.Name value to watch out for is org.freedesktop.DBus.Local.Disconnected. For avahi re-/disconnects it is org.freedesktop.DBus.NameOwnerChanged. The avahi c library can be used as a reference.

In addition avahi offers an option to wait for a avahi dbus connection on startup. It might be a good idea to add this as an option for the avahi only mode as well.