Skip to content

Commit

Permalink
Fix: Resubscribe when mqtt reconnects
Browse files Browse the repository at this point in the history
In case ospd-openvas disconnects from the MQTT
broker it will reconnect, but without subscribing
to the topics again. This will be fixed with this PR
  • Loading branch information
ArnoStiefvater committed Aug 2, 2022
1 parent ac62759 commit 16754c6
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions ospd_openvas/messaging/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ def publish(self, message: Message) -> None:

class MQTTSubscriber(Subscriber):
def __init__(self, client: MQTTClient):
self._client = client
self.client = client
# Save the active subscriptions on subscribe() so we can resubscribe
# after reconnect
self.subscriptions: dict = {}

self.client.on_connect = self.on_connect
self.client.user_data_set(self.subscriptions)

def subscribe(
self, message_class: Type[Message], callback: Callable[[Message], None]
Expand All @@ -96,8 +102,21 @@ def subscribe(

logger.debug("Subscribing to topic %s", message_class.topic)

self._client.subscribe(message_class.topic, qos=QOS_AT_LEAST_ONCE)
self._client.message_callback_add(message_class.topic, func)
self.client.subscribe(message_class.topic, qos=QOS_AT_LEAST_ONCE)
self.client.message_callback_add(message_class.topic, func)

self.subscriptions[message_class.topic] = func

@staticmethod
def on_connect(_client, _userdata, _flags, rc, _properties):
if rc == 0:
# If we previously had active subscription we subscribe to them
# again because they got lost after a broker disconnect.
# Userdata is set in __init__() and filled in subscribe()
if _userdata:
for topic, func in _userdata.items():
_client.subscribe(topic, qos=QOS_AT_LEAST_ONCE)
_client.message_callback_add(topic, func)

@staticmethod
def _handle_message(
Expand Down

0 comments on commit 16754c6

Please sign in to comment.