| | |
| | | #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) |
| | | { |
| | | //反序列化入参 |
| | |
| | | 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;//实际在mtr_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, |
| | |
| | | |
| | | 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)) |
| | |
| | | |
| | | //调用函数实现 |
| | | 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; |
| | | } |