From dc01e4cbb01e96d19b470a366bbe648d426ed171 Mon Sep 17 00:00:00 2001 From: fujuntang <fujuntang@smartai.com> Date: 星期六, 11 九月 2021 10:06:15 +0800 Subject: [PATCH] Add topics sub and request support. --- src/bh_api.cpp | 210 ++++++++++++++++++++++++++++++++++----------------- 1 files changed, 139 insertions(+), 71 deletions(-) diff --git a/src/bh_api.cpp b/src/bh_api.cpp index 5b24903..d25bf8a 100644 --- a/src/bh_api.cpp +++ b/src/bh_api.cpp @@ -139,6 +139,12 @@ gNetmod_socket = net_mod_socket_open(); hashtable_t *hashtable = mm_get_hashtable(); key = hashtable_alloc_key(hashtable); + count = hashtable_alloc_key(hashtable); + rv = hashtable_alloc_key(hashtable); + net_mod_socket_int_set(gNetmod_socket, count); + net_mod_socket_svr_set(gNetmod_socket, rv); + sprintf(pData.int_info, "%d", count); + sprintf(pData.svr_info, "%d", rv); net_mod_socket_bind(gNetmod_socket, key); rv = net_mod_socket_reg(gNetmod_socket, &pData, sizeof(ProcInfo), NULL, 0, timeout_ms, PROC_REG); @@ -154,30 +160,29 @@ memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + return false; } #if defined(PRO_DE_SERIALIZE) - ::bhome_msg::MsgCommonReply mcr; - mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); - mcr.mutable_errmsg()->set_errstring(errString); - *reply_len = mcr.ByteSizeLong(); - *reply = malloc(*reply_len); - mcr.SerializePartialToArray(*reply, *reply_len); + ::bhome_msg::MsgCommonReply mcr; + mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); + mcr.mutable_errmsg()->set_errstring(errString); + *reply_len = mcr.ByteSizeLong(); + *reply = malloc(*reply_len); + mcr.SerializePartialToArray(*reply, *reply_len); #else - min = strlen(errString) + 1; - buf = malloc(min) ; - memcpy(buf, errString, strlen(errString)); - *((char *)buf + min - 1) = '\0'; - - *reply = buf; - *reply_len = min; + min = strlen(errString) + 1; + buf = malloc(min) ; + memcpy(buf, errString, strlen(errString)); + *((char *)buf + min - 1) = '\0'; + + *reply = buf; + *reply_len = min; #endif - - if (rv == 0) - return true; - return false; + return true; + } int BHUnregister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms) @@ -256,6 +261,8 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } #if defined(PRO_DE_SERIALIZE) @@ -275,10 +282,8 @@ *reply_len = min; #endif - if (rv == 0) - return true; - - return false; + return true; + } int BHRegisterTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms) @@ -392,6 +397,8 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } #if defined(PRO_DE_SERIALIZE) @@ -411,10 +418,8 @@ *reply_len = len; #endif - if (rv == 0) - return true; + return true; - return false; } int BHQueryTopicAddress(const void *remote, const int remote_len, const void *topic, const int topic_len, void **reply, int *reply_len, const int timeout_ms) @@ -499,6 +504,8 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } #if defined(PRO_DE_SERIALIZE) @@ -565,10 +572,8 @@ #endif - if (rv == 0) - return true; + return true; - return false; } int BHQueryProcs(const void *remote, const int remote_len, const void *query, const int query_len, void **reply, int *reply_len, const int timeout_ms) @@ -646,6 +651,8 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } #if defined(PRO_DE_SERIALIZE) @@ -730,10 +737,7 @@ } #endif - if (rv == 0) - return true; - - return false; + return true; } @@ -860,6 +864,8 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } #if defined(PRO_DE_SERIALIZE) @@ -879,10 +885,7 @@ *reply_len = len; #endif - if (rv == 0) - return true; - - return false; + return true; } @@ -935,8 +938,8 @@ ::bhome_msg::MsgCommonReply mcr; mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); mcr.mutable_errmsg()->set_errstring(errString); - *reply_len=mcr.ByteSizeLong(); - *reply=malloc(*reply_len); + *reply_len = mcr.ByteSizeLong(); + *reply = malloc(*reply_len); mcr.SerializePartialToArray(*reply,*reply_len); #endif @@ -1045,8 +1048,9 @@ int key; int size; int sec, nsec; - char topics_buf[MAX_STR_LEN] = { 0x00 }; - char data_buf[MAX_STR_LEN * 3] = { 0x00 }; + char buf_temp[100] = { 0x00 }; + char *topics_buf = NULL; + char *data_buf = NULL; struct _ReadSubReply { @@ -1094,11 +1098,43 @@ if (len > size) { len = size; } - strncpy(topics_buf, (char *)buf, len > (sizeof(topics_buf) - 1) ? (sizeof(topics_buf) - 1) : len); + + topics_buf = (char *)malloc(len + 10); + if (topics_buf == NULL) { + + rv = EBUS_NO_MEM; + memset(errString, 0x00, sizeof(errString)); + strncpy(errString, bus_strerror(rv), sizeof(errString)); + + logger->error("in BHRequest: Out of memory!\n"); + + pthread_mutex_unlock(&mutex); + + return false; + } + memset(topics_buf, 0x00, len + 10); + + strncpy(topics_buf, (char *)buf, len); if (len < size) { - len = strlen(topics_buf) + 1; + + data_buf = (char *)malloc(size - len + 10); + if (data_buf == NULL) { + + rv = EBUS_NO_MEM; + memset(errString, 0x00, sizeof(errString)); + strncpy(errString, bus_strerror(rv), sizeof(errString)); + + logger->error("in BHRequest: Out of memory!\n"); + + free(topics_buf); + pthread_mutex_unlock(&mutex); + return false; + } + memset(data_buf, 0x00, size - len + 10); + + len = strlen(topics_buf) + 1; strncpy(data_buf, (char *)buf + len, size - len); } @@ -1106,17 +1142,23 @@ #if defined(PRO_DE_SERIALIZE) rsr.topic = topics_buf; - rsr.data = data_buf; + if (data_buf != NULL) { + rsr.data = data_buf; + } else { + rsr.data = topics_buf; + } - memset(topics_buf, 0x00, sizeof(topics_buf)); - sprintf(topics_buf, "%d", key); + sprintf(buf_temp, "%d", key); if ((proc_id != NULL) && (proc_id_len != NULL)) { - rsr.proc_id = topics_buf; + rsr.proc_id = buf_temp; *proc_id_len = rsr.proc_id.size(); *proc_id = malloc(*proc_id_len); memcpy(*proc_id, rsr.proc_id.data(), *proc_id_len); } + + free(topics_buf); + free(data_buf); ::bhome_msg::MsgPublish Mp; Mp.set_topic(rsr.topic); @@ -1136,14 +1178,17 @@ } *msgpub = ptr; *msgpub_len = len; - + + free(topics_buf); + free(data_buf); + if ((proc_id != NULL) && (proc_id_len != NULL)) { - memset(topics_buf, 0x00, sizeof(topics_buf)); - sprintf(topics_buf, "%d", key); + memset(buf_temp, 0x00, sizeof(buf_temp)); + sprintf(buf_temp, "%d", key); - *proc_id_len = strlen(topics_buf); + *proc_id_len = strlen(buf_temp); *proc_id = malloc(*proc_id_len); - memcpy(*proc_id, topics_buf, *proc_id_len); + memcpy(*proc_id, buf_temp, *proc_id_len); } #endif @@ -1168,6 +1213,7 @@ int val; int len; int min; + int data; int sec, nsec; std::string MsgID; int timeout_ms = 3000; @@ -1270,20 +1316,21 @@ strncpy(topics_buf + strlen(buf_temp) + 1, _input1.data, strlen(_input1.data)); #endif + data = net_mod_socket_svr_get(gNetmod_socket); if (timeout_ms > 0) { sec = timeout_ms / 1000; nsec = (timeout_ms - sec * 1000) * 1000 * 1000; - rv = net_mod_socket_sendto_timeout(gNetmod_socket, topics_buf, len, val, sec, nsec); + rv = net_mod_socket_sendto_timeout(gNetmod_socket, topics_buf, len, val, sec, nsec, SVR_STR, data); } else if (timeout_ms == 0) { - rv = net_mod_socket_sendto_nowait(gNetmod_socket, topics_buf, len, val); + rv = net_mod_socket_sendto_nowait(gNetmod_socket, topics_buf, len, val, SVR_STR, data); } else { - rv = net_mod_socket_sendto(gNetmod_socket, topics_buf, len, val); + rv = net_mod_socket_sendto(gNetmod_socket, topics_buf, len, val, SVR_STR, data); } free(topics_buf); @@ -1338,6 +1385,7 @@ int size; int val; int min, len; + int data; net_node_t node; int node_size; int recv_arr_size; @@ -1430,6 +1478,7 @@ len += strlen(_input1.data); #endif + data = net_mod_socket_svr_get(gNetmod_socket); topics_buf = (char *)malloc(len); if (topics_buf == NULL) { @@ -1515,13 +1564,26 @@ memcpy(*proc_id, rr.proc_id.c_str(), *proc_id_len); } - memset(buf_temp, 0x00, sizeof(buf_temp)); - memcpy(buf_temp, buf, size); - rr.data = buf_temp; + topics_buf = (char *)malloc(size + 10); + if (topics_buf == NULL) { + + rv = EBUS_NO_MEM; + memset(errString, 0x00, sizeof(errString)); + strncpy(errString, bus_strerror(rv), sizeof(errString)); + + logger->error("in BHRequest: Out of memory!\n"); + + pthread_mutex_unlock(&mutex); + + return false; + } + memset(topics_buf, 0x00, size + 10); + + memcpy(topics_buf, buf, size); + rr.data = topics_buf; free(buf); - - printf("BHRequest finished_7\n"); + free(topics_buf); } pthread_mutex_unlock(&mutex); @@ -1533,16 +1595,18 @@ strncpy(errString, bus_strerror(rv), sizeof(errString)); } -#if defined(PRO_DE_SERIALIZE) - ::bhome_msg::MsgRequestTopicReply mrt; - mrt.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); - mrt.mutable_errmsg()->set_errstring(errString); - mrt.set_data(rr.data.data()); - *reply_len = mrt.ByteSizeLong(); - *reply = malloc(*reply_len); - mrt.SerializePartialToArray(*reply, *reply_len); +#if defined(PRO_DE_SERIALIZE) + if (rv == 0) { + ::bhome_msg::MsgRequestTopicReply mrt; + mrt.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); + mrt.mutable_errmsg()->set_errstring(errString); + mrt.set_data(rr.data.data()); + *reply_len = mrt.ByteSizeLong(); + *reply = malloc(*reply_len); + mrt.SerializePartialToArray(*reply, *reply_len); + } #else - if (rv > 0) { + if (rv == 0) { min = strlen(errString) + 1; buf = malloc(min); memcpy(buf, errString, strlen(errString)); @@ -1566,6 +1630,7 @@ int key; int size; int len; + int data; int sec, nsec; char buf_temp[MAX_STR_LEN] = { 0x00 }; char *topics_buf = NULL; @@ -1588,20 +1653,21 @@ return false; } + data = net_mod_socket_svr_get(gNetmod_socket); if (timeout_ms > 0) { sec = timeout_ms / 1000; nsec = (timeout_ms - sec * 1000) * 1000 * 1000; - rv = net_mod_socket_recvfrom_timeout(gNetmod_socket, &buf, &size, &key, sec, nsec); + rv = net_mod_socket_recvfrom_timeout(gNetmod_socket, &buf, &size, &key, sec, nsec, SVR_STR, data); } else if (timeout_ms == 0) { - rv = net_mod_socket_recvfrom_nowait(gNetmod_socket, &buf, &size, &key); + rv = net_mod_socket_recvfrom_nowait(gNetmod_socket, &buf, &size, &key, SVR_STR, data); } else { - rv = net_mod_socket_recvfrom(gNetmod_socket, &buf, &size, &key); + rv = net_mod_socket_recvfrom(gNetmod_socket, &buf, &size, &key, SVR_STR, data); } if (rv == 0) { @@ -1681,6 +1747,7 @@ int BHSendReply(void *src, const void *reply, const int reply_len) { int rv; + int data; const char *_input; #if defined(PRO_DE_SERIALIZE) @@ -1723,7 +1790,8 @@ rv = pthread_mutex_trylock(&mutex); if (rv == 0) { - rv = net_mod_socket_sendto(gNetmod_socket, _input, strlen(_input), *(int *)src); + data = net_mod_socket_svr_get(gNetmod_socket); + rv = net_mod_socket_sendto(gNetmod_socket, _input, strlen(_input), *(int *)src, SVR_STR, data); memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); -- Gitblit v1.8.0