From f600bb176c1d2f0eeb5180637bdc09605b3d21bd Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期三, 14 十二月 2022 14:58:28 +0800
Subject: [PATCH] restruct for easy use v1.1

---
 main.cpp |   87 +++++++++++++++++--------------------------
 1 files changed, 34 insertions(+), 53 deletions(-)

diff --git a/main.cpp b/main.cpp
index 84578e5..84f63fb 100644
--- a/main.cpp
+++ b/main.cpp
@@ -13,33 +13,20 @@
 #include "bhome_msg_api.pb.h"
 using namespace bhome_msg;
 
-static cproc* make_proc(const char* name, const char* id){
-    cproc* pinfo = (cproc*)calloc(1,sizeof(cproc));
-    auto assign = [](char** d, size_t* l, const char* tmp){
-        *l = strlen(tmp);
-        *d = (char*)malloc(*l);
-        memcpy(*d, tmp, *l);
-    };
-    assign(&pinfo->name.str, &pinfo->name.size, name);
-    assign(&pinfo->id.str, &pinfo->id.size, id);
-
-    return pinfo;
-}
-
 template <class F> void ignoref(F&& f){}
 
 static void pub(const vector<string>& topics){
     ignoref(pub);
 
-    creg reg;
-    memset(&reg, 0, sizeof(reg));
-    reg.pinfo = make_proc("pub", "pubid");
-    reg.topic_pub = cstr_arr_new(topics.size());
-    size_t i = 0;
-    for(; i < topics.size(); i++){
-        cstr_arr_add(&reg.topic_pub, topics.at(0).data(), topics.at(0).size(), i);
-    }
-    void* handle = bus_client_init(NULL, 0, &reg);
+    vector<const char*> pubtpc;
+    for(auto& t : topics) pubtpc.push_back(t.c_str());
+
+    creg* reg = make_creg(make_cproc("pub", "pubid"),
+        NULL, 0, &pubtpc[0], pubtpc.size(), NULL, 0, NULL, 0);
+
+    void* handle = bus_client_init(NULL, 0, reg);
+    creg_free(reg);
+
     size_t count = 0;
     string base_msg("test_pub_sub==");
     this_thread::sleep_for(chrono::seconds(3));
@@ -62,21 +49,18 @@
 static void sub(const vector<string>& topics){
     ignoref(sub);
 
-    creg reg;
-    memset(&reg, 0, sizeof(reg));
-    reg.pinfo = make_proc("sub", "subid");
+    vector<const char*> pubtpc;
+    for(auto& t : topics) pubtpc.push_back(t.c_str());
 
-    reg.topic_sub = cstr_arr_new(topics.size());
-    size_t i = 0;
-    for(; i < topics.size(); i++){
-        cstr_arr_add(&reg.topic_sub, topics.at(0).data(), topics.at(0).size(), i);
-    }
+    creg* reg = make_creg(make_cproc("sub", "subid"),
+        NULL, 0, NULL, 0, &pubtpc[0], pubtpc.size(), NULL, 0);
 
-    void* handle = bus_client_init(NULL, 0, &reg);
+    void* handle = bus_client_init(NULL, 0, reg);
+    creg_free(reg);
 
     while (true) {
         auto msg = bus_client_get_submsg(handle);
-        printf("SUB msg topic [%s] data [%s]\n", msg->topic.str, msg->msg.str);
+        printf("SUB msg topic [%s] data [%s]\n", msg->topic, msg->msg);
     }
 
     bus_client_free(handle);
@@ -85,25 +69,22 @@
 static void req(const char* topic){
     ignoref(req);
 
-    string strtpc(topic);
-    creg reg;
-    memset(&reg, 0, sizeof(reg));
-    reg.pinfo = make_proc("request", "requestid");
-    // reg.channel = cstr_arr_new(1);
-    // size_t i = 0;
-    // for(; i < 1; i++){
-    //     cstr_arr_add(&reg.topic_pub, topic, strlen(topic), i);
-    // }
-    void* handle = bus_client_init(NULL, 0, &reg);
+    const auto topicl = strlen(topic);
+    creg* reg = make_creg(make_cproc("request", "requestid"),
+        NULL, 0, NULL, 0, NULL, 0, NULL, 0);
+
+    void* handle = bus_client_init(NULL, 0, reg);
+    creg_free(reg);
+
     size_t count = 0;
     string base_msg("test_request==");
     this_thread::sleep_for(chrono::seconds(3));
     while (true) {
         auto msg = base_msg + "request message -> msg-"+to_string(count++);
-        auto reqmsg = make_req_msg(strtpc.data(), strtpc.size(), msg.data(), msg.size());
+        auto reqmsg = make_req_msg(topic, topicl, msg.data(), msg.size());
         crepmsg* repmsg = NULL;
         if (bus_client_request(handle, reqmsg, &repmsg)){
-            printf("======>> bus_client_reqest [%s] get [%s]\n", msg.c_str(), repmsg->data.str);
+            printf("======>> bus_client_reqest [%s] get [%s]\n", msg.c_str(), repmsg->data);
         }
         free_reqmsg(reqmsg);
         free_reply_msg(repmsg);
@@ -114,15 +95,15 @@
 static void reply(const char* topic){
     ignoref(reply);
 
-    creg reg;
-    memset(&reg, 0, sizeof(reg));
-    reg.pinfo = make_proc("reply", "replyid");
-    reg.channel = cstr_arr_new(1);
-    cstr_arr_add(&reg.channel, topic, strlen(topic), 0);
-    reg.topic_pub = cstr_arr_new(1);
-    cstr_arr_add(&reg.topic_pub, topic, strlen(topic), 0);
+    const auto topicl = strlen(topic);
+    vector<const char*> pubtpc{topic};
 
-    void* handle = bus_client_init(NULL, 0, &reg);
+    creg* reg = make_creg(make_cproc("reply", "replyid"),
+        &pubtpc[0], pubtpc.size(), &pubtpc[0], pubtpc.size(), NULL, 0, NULL, 0);
+
+    void* handle = bus_client_init(NULL, 0, reg);
+    creg_free(reg);
+
     size_t count = 0;
     this_thread::sleep_for(chrono::seconds(3));
     while (true) {
@@ -131,7 +112,7 @@
         auto repmsg = make_reply_msg(0, NULL, 0, "recv request", 12);
         bus_client_reply_msg(handle, src, repmsg);
         free_reply_msg(repmsg);
-        printf("REPREQ msg [%s] \n", msg->msg.str);
+        printf("REPREQ msg [%s] \n", msg->msg);
 
         free_reqmsg(msg);
         this_thread::sleep_for(chrono::seconds{2});

--
Gitblit v1.8.0