From c765abc0380460a241f96ec931db203700c81b70 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期四, 20 一月 2022 17:03:40 +0800
Subject: [PATCH] performance
---
src/interface_bus_api.cpp | 122 ++++++++++++++++++++--------------------
1 files changed, 62 insertions(+), 60 deletions(-)
diff --git a/src/interface_bus_api.cpp b/src/interface_bus_api.cpp
index 20a4659..65dd669 100644
--- a/src/interface_bus_api.cpp
+++ b/src/interface_bus_api.cpp
@@ -3,20 +3,14 @@
using namespace nng_wrap;
#include "common.h"
+#include <tuple>
using namespace std;
#include "bhome_msg.pb.h"
#include "bhome_msg_api.pb.h"
using namespace bhome_msg;
-struct bus{
- _ps pub{};
- _ps_sub sub{};
- _sv sv{};
- _rr rr{};
- string proc_id{};
-};
-
+using bus = tuple<array<thread, 0>, _ps, _ps_sub, vector<tuple<float>>, deque<map<string, void*>>, _sv, _rr, unordered_map<void*, unordered_set<string*>>, string>;
void* bus_register(const void *proc_info,
const int proc_info_len,
void **reply,
@@ -36,19 +30,19 @@
/////////////////////////////////////////////////////////////////////////
bus *b = new bus;
bhome_msg::ProcInfo pi;
- if (pi.ParseFromArray(proc_info, proc_info_len)) b->proc_id = pi.proc_id();
+ if (pi.ParseFromArray(proc_info, proc_info_len)) get<8>(*b) = pi.proc_id();
const auto& url_hb = get_url(URLHeartBeat);
- respond_survey(url_hb,string{(char*)proc_info,(size_t)proc_info_len},&b->sv);
+ respond_survey(url_hb,string{(char*)proc_info,(size_t)proc_info_len},&get<5>(*b));
const auto& url_pub_proxy = get_url(URLPubProxy);
- publish(url_pub_proxy, NULL, 0, &b->pub);
+ publish(url_pub_proxy, NULL, 0, &get<1>(*b));
const auto& url_sub_queue = get_url(URLSubQueue);
- subscribe_center(url_sub_queue, &b->sub);
+ subscribe_center(url_sub_queue, &get<2>(*b));
// temporary
- port = 0;
- start_reply(b->proc_id, port, &b->rr);
+ // port = 0;
+ start_reply(get<8>(*b), port, &get<6>(*b));
return b;
}
@@ -96,7 +90,7 @@
return false;
}
- if (b->proc_id.empty()) {
+ if (get<8>(*b).empty()) {
PRNTVITAG("proc_id is null");
return false;
}
@@ -108,7 +102,7 @@
}
bhome_msg::MsgTopicList mtl2;
- mtl2.add_topic_list(b->proc_id);
+ mtl2.add_topic_list(get<8>(*b));
for(int i = 0; i < mtl.topic_list_size(); i++){
mtl2.add_topic_list(mtl.topic_list(i));
}
@@ -139,15 +133,15 @@
auto url(get_url(URLQueryTopic));
- if (remote && remote_len > 0){
- BHAddress addr;
- if (addr.ParseFromArray(remote, remote_len)){
- if (!addr.ip().empty() && addr.port() > 0){
- // url = "tcp://" + addr.ip() + ":" + to_string(addr.port());
- printf("======>> bus_query_topic_address use remote address %s\n", url.c_str());
- }
- }
- }
+ // if (remote && remote_len > 0){
+ // BHAddress addr;
+ // if (addr.ParseFromArray(remote, remote_len)){
+ // if (!addr.ip().empty() && addr.port() > 0){
+ // // url = "tcp://" + addr.ip() + ":" + to_string(addr.port());
+ // printf("======>> bus_query_topic_address use remote address %s\n", url.c_str());
+ // }
+ // }
+ // }
return simple_request(url, topics, topics_len, reply, reply_len, timeout_ms);
}
@@ -173,25 +167,25 @@
auto url(get_url(URLQueryProcs));
- if (remote && remote_len > 0){
- BHAddress addr;
- if (addr.ParseFromArray(remote, remote_len)){
- if (!addr.ip().empty() && addr.port() > 0){
- // url = "tcp://" + addr.ip() + ":" + to_string(addr.port());
- printf("======>> bus_query_procs use remote address %s\n", url.c_str());
- }
- }
- }
+ // if (remote && remote_len > 0){
+ // BHAddress addr;
+ // if (addr.ParseFromArray(remote, remote_len)){
+ // if (!addr.ip().empty() && addr.port() > 0){
+ // // url = "tcp://" + addr.ip() + ":" + to_string(addr.port());
+ // printf("======>> bus_query_procs use remote address %s\n", url.c_str());
+ // }
+ // }
+ // }
return simple_request(url, query, query_len, reply, reply_len, timeout_ms);
}
-int bus_subscribe_topics(void* handle, const void *topics,
+static int sub(const string& url,void* handle, const void *topics,
const int topics_len,
void **reply,
int *reply_len,
- const int timeout_ms)
-{
+ const int timeout_ms){
+
TAG;
bus* b = get(handle);
if (!b){
@@ -214,13 +208,11 @@
}
for(int i = 0; i < mtl.topic_list_size(); i ++){
- subscribe_topic(mtl.topic_list(i), &b->sub);
+ subscribe_topic(mtl.topic_list(i), &get<2>(*b));
}
- return true;
-
///////////////////////////////////////////////////
- auto ret = simple_request(get_url(URLSubLocal), topics, topics_len, reply, reply_len, timeout_ms);
+ auto ret = simple_request(url, topics, topics_len, reply, reply_len, timeout_ms);
if (!ret){
printf("bus_subscribe_topics simple_request failed\n");
}
@@ -228,13 +220,22 @@
}
+
+int bus_subscribe_topics(void* handle, const void *topics,
+ const int topics_len,
+ void **reply,
+ int *reply_len,
+ const int timeout_ms)
+{
+ return sub(get_url(URLSubLocal), handle, topics, topics_len, reply, reply_len, timeout_ms);
+}
int bus_subscribe_topics_net(void* handle, const void *topics,
const int topics_len,
void **reply,
int *reply_len,
const int timeout_ms)
{
- return bus_subscribe_topics(handle, topics, topics_len, reply, reply_len, timeout_ms);
+ return sub(get_url(URLSubNet), handle, topics, topics_len, reply, reply_len, timeout_ms);
}
int bus_heartbeat_easy(void* handle, const int timeout_ms){ return 1; }
@@ -250,7 +251,7 @@
PRNTVITAG("handle is null");
return false;
}
- if (b->proc_id.empty()){
+ if (get<8>(*b).empty()){
PRNTVITAG("proc_id is null");
return false;
}
@@ -265,12 +266,13 @@
return false;
}
+ // msgpublish(msgpublish(msg))
MsgPublish newPub;
- newPub.set_topic(b->proc_id);
+ newPub.set_topic(get<8>(*b));
newPub.set_data(string{(const char*)msgpub, (const size_t)msgpub_len});
string msg(newPub.SerializeAsString());
- auto ret = publish(pub.topic(), msg.data(), msg.size(), &b->pub);
+ auto ret = publish(pub.topic(), msg.data(), msg.size(), &get<1>(*b));
if (ret > 0) return true;
return false;
}
@@ -289,7 +291,7 @@
}
string topic, msg;
- auto ret = subscribe_read(&topic, &msg, timeout_ms, &b->sub);
+ auto ret = subscribe_read(&topic, &msg, timeout_ms, &get<2>(*b));
if (ret < 0) return false;
MsgPublish newPub;
@@ -325,21 +327,20 @@
return false;
}
+ string url{};
// BHQueryTopicAddress鑾峰彇proc_id
string procid{};
- if (!get_proc_id_from_MsgRequestTopic(request, request_len, timeout_ms, &procid)){
- PRNTVITAG("get_proc_id_from_MsgRequestTopic failed");
- return false;
+ if (get_proc_id_from_MsgRequestTopic(request, request_len, timeout_ms, &procid)){
+ // PRNTVITAG("get_proc_id_from_MsgRequestTopic failed");
+ // return false;
+ url = "ipc:///tmp/" + procid;
}
-
- auto url("ipc:///tmp/" + procid);
-
if (remote && remote_len > 0){
BHAddress addr;
if (addr.ParseFromArray(remote, remote_len)){
if (!addr.ip().empty() && addr.port() > 0){
- // url = "tcp://" + addr.ip() + ":" + to_string(addr.port());
- printf("======>> BHRequest use remote address %s\n", url.c_str());
+ url = "tcp://" + addr.ip() + ":" + to_string(addr.port());
+ // printf("======>> BHRequest use remote address %s\n", url.c_str());
}
}
}
@@ -366,16 +367,17 @@
}
string msg;
- auto ret = read_request(src, &msg, timeout_ms, &b->rr);
+ auto ret = read_request(src, &msg, timeout_ms, &get<6>(*b));
+ // printf("bus_recv_request read_request ret %d msg %s\n", ret, msg.c_str());
if (ret != 0) return false;
string procid{};
- if (!get_proc_id_from_MsgRequestTopic(msg.data(), msg.size(), timeout_ms, &procid)){
- PRNTVITAG("get_proc_id_from_MsgRequestTopic failed");
- return false;
+ if (get_proc_id_from_MsgRequestTopic(msg.data(), msg.size(), timeout_ms, &procid)){
+ // PRNTVITAG("get_proc_id_from_MsgRequestTopic failed");
+ // return false;
+ copy_memory(proc_id, proc_id_len, procid.data(), procid.size());
}
- copy_memory(proc_id, proc_id_len, procid.data(), procid.size());
copy_memory(request, request_len, msg.data(), msg.size());
return true;
@@ -397,7 +399,7 @@
return false;
}
- auto ret = send_reply(src, reply, reply_len, &b->rr);
+ auto ret = send_reply(src, reply, reply_len, &get<6>(*b));
if (ret < 0) return false;
return true;
--
Gitblit v1.8.0