[Hybrid] Reconnect to available node
authorgichan2-jang <gichan2.jang@samsung.com>
Tue, 31 Oct 2023 08:22:06 +0000 (17:22 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Thu, 9 Nov 2023 06:10:24 +0000 (15:10 +0900)
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 <gichan2.jang@samsung.com>
src/libnnstreamer-edge/nnstreamer-edge-internal.c

index f0293fc2506774650aff764f78e6555cda8dbe8c..22413f3ffa221a3d15efb9997dcaf444c0b37123 100644 (file)
@@ -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;