From 7ae5a0117ad13105903c2d52e1accbbb7d252129 Mon Sep 17 00:00:00 2001 From: gichan2-jang Date: Tue, 31 Oct 2023 17:22:06 +0900 Subject: [PATCH] [Hybrid] Reconnect to available node If current connection is lost, find availabe node and reconnect. Previously, This recovery work is done by user application. Let's make the users don't care about this work when using MQTT-HYBRID. *edgesrc and tensor_query_* should be updated after this change. Signed-off-by: gichan2-jang --- .../nnstreamer-edge-internal.c | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/libnnstreamer-edge/nnstreamer-edge-internal.c b/src/libnnstreamer-edge/nnstreamer-edge-internal.c index f0293fc..22413f3 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-internal.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-internal.c @@ -150,6 +150,11 @@ typedef struct nns_edge_conn_s *conn; } nns_edge_thread_data_s; +/** + * @brief Parse the message received from the MQTT broker and connect to the server directly. + */ +static int _mqtt_hybrid_direct_connection (nns_edge_handle_s * eh); + /** * @brief Set socket option. nnstreamer-edge handles TCP connection now. */ @@ -775,8 +780,17 @@ _nns_edge_message_handler (void *thread_data) ("Received error from client, remove connection of client (ID: %lld).", (long long) client_id); _nns_edge_remove_connection (eh, client_id); - nns_edge_event_invoke_callback (eh->event_cb, eh->user_data, - NNS_EDGE_EVENT_CONNECTION_CLOSED, NULL, 0, NULL); + ret = NNS_EDGE_ERROR_CONNECTION_FAILURE; + + if (NNS_EDGE_CONNECT_TYPE_HYBRID == eh->connect_type) { + nns_edge_logi ("Connection lost! Reconnect to available node."); + ret = _mqtt_hybrid_direct_connection (eh); + } + + if (ret != NNS_EDGE_ERROR_NONE) { + nns_edge_event_invoke_callback (eh->event_cb, eh->user_data, + NNS_EDGE_EVENT_CONNECTION_CLOSED, NULL, 0, NULL); + } } return NULL; @@ -1632,6 +1646,12 @@ nns_edge_connect (nns_edge_h edge_h, const char *dest_host, int dest_port) return NNS_EDGE_ERROR_CONNECTION_FAILURE; } + if (NNS_EDGE_ERROR_NONE == nns_edge_is_connected (eh)) { + nns_edge_logi ("NNStreamer-edge is already connected."); + nns_edge_unlock (eh); + return NNS_EDGE_ERROR_NONE; + } + SAFE_FREE (eh->dest_host); eh->dest_host = nns_edge_strdup (dest_host); eh->dest_port = dest_port; -- 2.34.1