Check connection before send the data
authorgichan <gichan2.jang@samsung.com>
Tue, 6 Sep 2022 07:56:28 +0000 (16:56 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Wed, 14 Sep 2022 10:26:04 +0000 (19:26 +0900)
Check the connection before sending the data.

Signed-off-by: gichan <gichan2.jang@samsung.com>
src/libnnstreamer-edge/nnstreamer-edge-aitt.c
src/libnnstreamer-edge/nnstreamer-edge-internal.c
src/libnnstreamer-edge/nnstreamer-edge-internal.h

index 00272d468519e3b5282d531dbe32e205f0b72bb2..a4db80182c4c2d83fc8cc0a169dccba97c524fab 100644 (file)
@@ -109,6 +109,31 @@ nns_edge_aitt_close (nns_edge_h edge_h)
   return NNS_EDGE_ERROR_NONE;
 }
 
+/**
+ * @brief Check whether aitt handle exists or not.
+ */
+int
+nns_edge_aitt_is_connected (nns_edge_h edge_h)
+{
+  nns_edge_handle_s *eh;
+  nns_edge_aitt_handle_s *ah;
+
+  eh = (nns_edge_handle_s *) edge_h;
+
+  if (!NNS_EDGE_MAGIC_IS_VALID (eh)) {
+    nns_edge_loge ("Invalid param, given edge handle is invalid.");
+    return NNS_EDGE_ERROR_INVALID_PARAMETER;
+  }
+
+  ah = (nns_edge_aitt_handle_s *) eh->broker_h;
+  if (!ah || !ah->aitt_h) {
+    nns_edge_loge ("AITT handle is not yet connected.");
+    return NNS_EDGE_ERROR_INVALID_PARAMETER;
+  }
+
+  return NNS_EDGE_ERROR_NONE;
+}
+
 /**
  * @brief Publish raw data.
  * @note This is internal function forAITT. You should call this with edge-handle lock.
index 00cd80f81ef41ceefc358f2d3144078ec0d035ad..2af7cc75f6d8dd635c25496b83e38eed1f6078d6 100644 (file)
@@ -1628,6 +1628,33 @@ nns_edge_disconnect (nns_edge_h edge_h)
   return NNS_EDGE_ERROR_NONE;
 }
 
+/**
+ * @brief Thread to send data.
+ */
+static int
+_nns_edge_is_connected (nns_edge_h edge_h)
+{
+  nns_edge_handle_s *eh = (nns_edge_handle_s *) edge_h;
+  nns_edge_conn_data_s *conn_data;
+  nns_edge_conn_s *conn;
+  int connection_cnt = 0;
+
+  if (NNS_EDGE_CONNECT_TYPE_AITT == eh->connect_type &&
+      NNS_EDGE_ERROR_NONE == nns_edge_aitt_is_connected (eh))
+    return 1;
+
+  conn_data = (nns_edge_conn_data_s *) eh->connections;
+  while (conn_data) {
+    conn = conn_data->sink_conn;
+    if (_nns_edge_check_connection (conn)) {
+      connection_cnt++;
+    }
+    conn_data = conn_data->next;
+  }
+
+  return connection_cnt;
+}
+
 /**
  * @brief Send data to desination (broker or connected node), asynchronously.
  */
@@ -1656,6 +1683,12 @@ nns_edge_send (nns_edge_h edge_h, nns_edge_data_h data_h)
     return NNS_EDGE_ERROR_INVALID_PARAMETER;
   }
 
+  if (0 == _nns_edge_is_connected (eh)) {
+    nns_edge_loge ("There is no available connection.");
+    nns_edge_unlock (eh);
+    return NNS_EDGE_ERROR_IO;
+  }
+
   if (!eh->send_thread) {
     nns_edge_loge ("Invalid state, start edge before sending a data.");
     nns_edge_unlock (eh);
index f46c509461331cbb1a2993b933feba4a97e17d43..4641a04f39fe02132096caa6dbbfada3250e78c3 100644 (file)
@@ -168,6 +168,12 @@ int nns_edge_aitt_connect (nns_edge_h edge_h);
  */
 int nns_edge_aitt_close (nns_edge_h edge_h);
 
+/**
+ * @brief Check whether aitt handle exists or not.
+ */
+int
+nns_edge_aitt_is_connected (nns_edge_h edge_h);
+
 /**
  * @brief Publish raw data.
  * @note This is internal function forAITT. You should call this with edge-handle lock.
@@ -184,6 +190,7 @@ int nns_edge_aitt_subscribe (nns_edge_h edge_h);
 #define nns_edge_aitt_close(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
 #define nns_edge_aitt_publish(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
 #define nns_edge_aitt_subscribe(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
+#define nns_edge_aitt_is_connected(...) (NNS_EDGE_ERROR_NOT_SUPPORTED)
 #endif
 
 #ifdef __cplusplus