From df3d91bae5dedf9d4084221f2d7def2db62fb7a4 Mon Sep 17 00:00:00 2001 From: gichan Date: Tue, 6 Sep 2022 16:56:28 +0900 Subject: [PATCH] Check connection before send the data Check the connection before sending the data. Signed-off-by: gichan --- src/libnnstreamer-edge/nnstreamer-edge-aitt.c | 25 ++++++++++++++ .../nnstreamer-edge-internal.c | 33 +++++++++++++++++++ .../nnstreamer-edge-internal.h | 7 ++++ 3 files changed, 65 insertions(+) diff --git a/src/libnnstreamer-edge/nnstreamer-edge-aitt.c b/src/libnnstreamer-edge/nnstreamer-edge-aitt.c index 00272d4..a4db801 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-aitt.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-aitt.c @@ -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. diff --git a/src/libnnstreamer-edge/nnstreamer-edge-internal.c b/src/libnnstreamer-edge/nnstreamer-edge-internal.c index 00cd80f..2af7cc7 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-internal.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-internal.c @@ -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); diff --git a/src/libnnstreamer-edge/nnstreamer-edge-internal.h b/src/libnnstreamer-edge/nnstreamer-edge-internal.h index f46c509..4641a04 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-internal.h +++ b/src/libnnstreamer-edge/nnstreamer-edge-internal.h @@ -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 -- 2.34.1