velbus / python-velbustcp

Python application that bridges a Velbus installation with TCP
MIT License
2 stars 4 forks source link

Feature request: attempt to rebind listener sockets periodically, if the configured socket is not being listened to #13

Closed nagisa closed 7 months ago

nagisa commented 1 year ago

Hey all,

it is possible to ask velbustcp to bind to a specific interface by specifying its IP address, for example as such:

{
  "connections":[{
    "auth":false,
    "host":"192.168.110.1",
    "port":27015,
    "relay":true,
    "ssl":false
  }]
}

However, it is possible that at the time velbustcp is started that this address is not assigned to any of the interfaces quite yet. This can be something as simple as it being a WiFi address, and the WiFi connection has not been established yet, it could be an address that is (statically) assigned via DHCP (and DHCP did not respond yet), or it could be a simple service start up race condition, where the velbustcp service attempts to start up before the network interfaces are completely set up. I believe an issue also can come up if the network interface that the socket is bound to is disconnected and later reconnected.

In those instances you're prone to see a log print out like this:

Traceback (most recent call last):
   File "/bin/.velbustcp-wrapped", line 9, in <module>
     sys.exit(main())
   File "/lib/python3.10/site-packages/velbustcp/__main__.py", line 72, in main
     main = Main()
   File "/lib/python3.10/site-packages/velbustcp/__main__.py", line 36, in __init__
     self.__bridge.start()
   File "/lib/python3.10/site-packages/velbustcp/lib/connection/bridge.py", line 45, in start
     self.__network_manager.start()
   File "/lib/python3.10/site-packages/velbustcp/lib/connection/tcp/networkmanager.py", line 21, in start
      network.start()
   File "/lib/python3.10/site-packages/velbustcp/lib/connection/tcp/network.py", line 114, in start
     self.__bind_socket.bind(self.__options.address)
OSError: [Errno 99] Cannot assign requested address
2023-09-21 17:19:53,574 - INFO - bus - Serial connection active on port /dev/serial/by-id/usb-Velleman_Projects_VMB1USB_Velbus_USB_interface-if00

and continue running in a state of degraded functionality – it is connected to the velbus installation, but it does not present a server to other clients wanting to connect. It would be ideal if velbustcp attempted to recreate the listening socket occasionally, or on systems that support them used options like IP_FREEBIND.

dukeofphilberg commented 1 year ago

Hi there, I think the current best solution would be to bind to 0.0.0.0 or maybe 127.0.0.1 if you only allow local connections. We're not sure if we can currently implement your specific request. Feel free to open a PR.

nagisa commented 1 year ago

Unfortunately the machine I'm looking to run this on has multiple interfaces and velbus should only be made available on some of them (but it does need to be available outside of local machine.)

We're not sure if we can currently implement your specific request. Feel free to open a PR.

Thank you for letting me know about your availability, I’ll probably hack on this this weekend and see about making that PR.

nagisa commented 1 year ago

I made a pull request for this at #14.