From 08505499d1449f53449e119aefc71324376b348b Mon Sep 17 00:00:00 2001 From: shenxin <shenxin@basic.com> Date: 星期一, 23 八月 2021 19:40:14 +0800 Subject: [PATCH] 增加BHSubscribeNetTopics、BHQueryTopicAddress接口 修改CMakeLists.txt,增加相应文件到编译中。(需要安装protobuf) --- src/bh_api.cc | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 104 insertions(+), 4 deletions(-) diff --git a/src/bh_api.cc b/src/bh_api.cc index f96e7cb..6cf1b22 100644 --- a/src/bh_api.cc +++ b/src/bh_api.cc @@ -4,6 +4,8 @@ #include "proto/bhome_msg.pb.h" #include "proto/bhome_msg_api.pb.h" +using namespace bhome_msg; + int BHRegister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms) { //鍙嶅簭鍒楀寲鍏ュ弬 @@ -151,6 +153,69 @@ return errCode==0?true: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) +{ + //鍙嶅簭鍒楀寲鍏ュ弬 + BHAddress input0; + MsgQueryTopic input1; + if (!input0.ParseFromArray(remote,remote_len)||!input1.ParseFromArray(topic,topic_len)) + return false; + struct _BHAddress + { + unsigned long long mq_id; + long long abs_addr; + const char *ip; + int port; + }_input0; + const char *_input1; + _input0.mq_id=input0.mq_id(); + _input0.abs_addr=input0.abs_addr(); + _input0.ip=input0.ip().c_str(); + _input0.port=input0.port(); + _input1=input1.topic().c_str(); + + //璋冪敤鍑芥暟瀹炵幇 + int errCode=0;//0 is success + //姝ょ浠ュ浐瀹氱粨鏋勮繑鍥炴暟鎹殑鏂瑰紡,鍙兘鏆傛椂鍥哄畾鏁伴噺涓婇檺銆傝繖閲岄檺鍒舵渶澶�128涓� + //鑻ヤ笉闄愭暟閲�,闇�閲嶆柊璁捐鏁版嵁杩斿洖鐨勬柟寮� + struct _MsgQueryTopicReply + { + std::string proc_id; + + unsigned long long mq_id; + long long abs_addr; + std::string ip; + int port; + }mtr_list[128]; + int mtr_list_num=0;//瀹為檯鍦╩tr_list涓~鍐欎簡澶氬皯涓」鐩� + //errCode=call_QueryTopicAddress(&_input0,_input1,mtr_list,&mtr_list_num,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇 + char errString[32]={0}; + if(errCode!=0) + strcpy(errString,"閿欒鍘熷洜鎻忚堪..."); + + //搴忓垪鍖栬繑鍥炴暟鎹� + MsgQueryTopicReply mtr; + mtr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode)); + mtr.mutable_errmsg()->set_errstring(errString); + for(int i=0;i<mtr_list_num;i++) + { + MsgQueryTopicReply_BHNodeAddress *mtrb=mtr.add_node_address(); + mtrb->set_proc_id(mtr_list[i].proc_id); + mtrb->mutable_addr()->set_mq_id(mtr_list[i].mq_id); + mtrb->mutable_addr()->set_abs_addr(mtr_list[i].abs_addr); + mtrb->mutable_addr()->set_ip(mtr_list[i].ip); + mtrb->mutable_addr()->set_port(mtr_list[i].port); + } + *reply_len=mtr.ByteSizeLong(); + *reply=malloc(*reply_len); + mtr.SerializePartialToArray(*reply,*reply_len); + + return errCode==0?true:false; +} + int BHQueryProcs(const void *remote, const int remote_len, const void *query, @@ -226,6 +291,41 @@ int BHSubscribeTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms) { + //鍙嶅簭鍒楀寲鍏ュ弬 + MsgTopicList input; + if(!input.ParseFromArray(topics,topics_len)) + return false; + struct _MsgTopicList + { + int amount; + const char *topics[128]; + }_input; + _input.amount=input.topic_list_size(); + for(int i=0;i<_input.amount;i++) + _input.topics[i]=input.topic_list(i).c_str(); + + //璋冪敤鍑芥暟瀹炵幇 + int errCode=0;//0 is success + //errCode=call_SubscribeTopics(&_input,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇 + char errString[32]={0}; + if(errCode!=0) + strcpy(errString,"閿欒鍘熷洜鎻忚堪..."); + + //搴忓垪鍖栬繑鍥炴暟鎹� + MsgCommonReply mcr; + mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode)); + mcr.mutable_errmsg()->set_errstring(errString); + *reply_len=mcr.ByteSizeLong(); + *reply=malloc(*reply_len); + mcr.SerializePartialToArray(*reply,*reply_len); + + return errCode==0?true:false; +} + +int BHSubscribeNetTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms) +{ + //same as BHSubscribeTopics + //鍙嶅簭鍒楀寲鍏ュ弬 MsgTopicList input; if(!input.ParseFromArray(topics,topics_len)) @@ -348,17 +448,17 @@ //璋冪敤鍑芥暟瀹炵幇 int errCode=0;//0 is success - std::string msg_id; - //errCode=call_AsyncRequest(&_input0,&_input1,&msg_id,&timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇 + std::string MsgID; + //errCode=call_AsyncRequest(&_input0,&_input1,&MsgID,&timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇 if(errCode!=0) return false; //搴忓垪鍖栬繑鍥炴暟鎹� if(msg_id==nullptr||msg_id_len==nullptr)//杩欓噷鏈夊彲鑳借皟鐢ㄨ�呬笉闇�瑕佽繑鍥� return true; - *msg_id_len=msg_id.size(); + *msg_id_len=MsgID.size(); *msg_id=malloc(*msg_id_len); - memcpy(*msg_id,msg_id.data(),*msg_id_len); + memcpy(*msg_id,MsgID.data(),*msg_id_len); return true; } -- Gitblit v1.8.0