#include "interface_bus_api.h" #include "nng_wrap.h" using namespace nng_wrap; #include "common.h" #include using namespace std; #include "bhome_msg.pb.h" #include "bhome_msg_api.pb.h" using namespace bhome_msg; using bus = tuple, _ps, _ps_sub, vector>, deque>, _sv, _rr, unordered_map>, string>; void* bus_register(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms) { if (!proc_info || proc_info_len <= 0) return NULL; void* out{}; int out_len{}; auto ret = simple_request(get_url(URLReg), proc_info, proc_info_len, &out, &out_len, timeout_ms); if (ret){ constexpr int port_size = sizeof(unsigned short); unsigned short port = *(unsigned short*)(out); copy_memory(reply, reply_len, (char*)out + port_size, out_len - port_size); bus_free(out, out_len); ///////////////////////////////////////////////////////////////////////// bus *b = new bus; bhome_msg::ProcInfo pi; if (pi.ParseFromArray(proc_info, proc_info_len)) get<8>(*b) = pi.proc_id(); std::move(pi); const auto& url_hb = get_url(URLHeartBeat); 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, &get<1>(*b)); const auto& url_sub_queue = get_url(URLSubQueue); subscribe_center(url_sub_queue, &get<2>(*b)); // temporary // port = 0; start_reply(get<8>(*b), port, &get<6>(*b)); return b; } return NULL; } static bus* get(void* h){ return static_cast(h); } int bus_unregister(void* handle, const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms) { TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } if (!proc_info || proc_info_len <= 0) { PRNTVITAG("proc_info is null"); return false; } return simple_request(get_url(URLDeReg), proc_info, proc_info_len, reply, reply_len, timeout_ms); } int bus_register_topics(void* handle, const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms) { TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } if (!topics || topics_len <= 0) { PRNTVITAG("topics is null"); return false; } const auto& url = get_url(URLRegTopic); if (!url) { set_last_error("bus_register_topics url empty"); return false; } if (get<8>(*b).empty()) { PRNTVITAG("proc_id is null"); return false; } bhome_msg::MsgTopicList mtl; if (!mtl.ParseFromArray(topics, topics_len)){ PRNTVITAG("MsgTopicList ParseFromArray failed"); return false; } bhome_msg::MsgTopicList mtl2; 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)); } std::move(mtl); string msg(mtl2.SerializeAsString()); std::move(mtl2); return simple_request(url, msg.data(), msg.size(), reply, reply_len, timeout_ms); } int bus_query_topic_address(void* handle, const void *remote, const int remote_len, const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms) { TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } if (!topics || topics_len <= 0) { PRNTVITAG("topics is null"); return false; } const auto& url = get_url(URLQueryTopic); if (!url) { set_last_error("bus_query_topic_address url empty"); return false; } // 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); } int bus_query_procs(void* handle, const void *remote, const int remote_len, const void *query, const int query_len, void **reply, int *reply_len, const int timeout_ms) { TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } // if (!query || query_len <= 0) { // PRNTVITAG("query is null"); // return false; // } const auto& url = get_url(URLQueryProcs); if (!url) { set_last_error("bus_query_procs url empty"); return false; } // 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); } static int sub(const string& url,void* handle, const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms){ TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } if (!topics || topics_len <= 0) { PRNTVITAG("topics is null"); return false; } bhome_msg::MsgTopicList mtl; if (!mtl.ParseFromArray(topics, topics_len)){ PRNTVITAG("bus_subscribe_topics ParseFromArray failed"); return false; } if (mtl.topic_list_size() == 0){ PRNTVITAG("bus_subscribe_topics topic_list_size zero"); return false; } for(int i = 0; i < mtl.topic_list_size(); i ++){ subscribe_topic(mtl.topic_list(i), &get<2>(*b)); } std::move(mtl); /////////////////////////////////////////////////// auto ret = simple_request(url, topics, topics_len, reply, reply_len, timeout_ms); if (!ret){ printf("bus_subscribe_topics simple_request failed\n"); } return ret; } 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 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; } int bus_heartbeat(void* handle, const void *proc_info,const int proc_info_len,void **reply,int *reply_len,const int timeout_ms){ return 1; } int bus_publish(void* handle, const void *msgpub, const int msgpub_len, const int timeout_ms) { TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } if (get<8>(*b).empty()){ PRNTVITAG("proc_id is null"); return false; } if (!msgpub || msgpub_len <= 0) { PRNTVITAG("msgpub is null"); return false; } bhome_msg::MsgPublish pub; if (!pub.ParseFromArray(msgpub, msgpub_len)){ PRNTVITAG("MsgPublish is null"); return false; } // msgpublish(msgpublish(msg)) MsgPublish newPub; newPub.set_topic(get<8>(*b)); newPub.set_data(string{(const char*)msgpub, (const size_t)msgpub_len}); string msg(newPub.SerializeAsString()); std::move(newPub); auto ret = publish(pub.topic(), msg.data(), msg.size(), &get<1>(*b)); if (ret > 0) return true; return false; } int bus_recv_pubmsg(void* handle, void **proc_id, int *proc_id_len, void **msgpub, int *msgpub_len, const int timeout_ms) { TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } if (!proc_id && !proc_id_len && !msgpub && !msgpub_len) return subscribe_read(NULL, NULL, timeout_ms, &get<2>(*b)) == 0; string topic, msg; auto ret = subscribe_read(&topic, &msg, timeout_ms, &get<2>(*b)); if (ret < 0) return false; MsgPublish newPub; if (newPub.ParseFromString(msg)){ copy_memory(proc_id, proc_id_len, newPub.topic().data(), newPub.topic().size()); copy_memory(msgpub, msgpub_len, newPub.data().data(), newPub.data().size()); return true; } return false; } extern int get_proc_id_from_MsgRequestTopic(const void* request, const int request_len, const int timeout_ms, string* proc_id); int bus_request(void* handle, const void *remote, const int remote_len, const void *request, const int request_len, void **proc_id, int *proc_id_len, void **reply, int *reply_len, const int timeout_ms) { TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } if (!request || request_len <= 0) { PRNTVITAG("request is null"); return false; } string url{}; // BHQueryTopicAddress获取proc_id string 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()); } } } else 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; } if (url.empty()) { set_last_error("bus_request url empty"); return false; } int rc = request2(url, request, request_len, reply, reply_len, timeout_ms); if (rc < 0) return false; copy_memory(proc_id, proc_id_len, procid.data(), procid.size()); return true; } int bus_recv_request(void* handle, void **proc_id, int *proc_id_len, void **request, int *request_len, void **src, const int timeout_ms) { TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } if (!proc_id && !proc_id_len && !request && !request_len && !src) return read_request(NULL, NULL, timeout_ms, &get<6>(*b)) == 0; string msg; 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; if (ret == REPLY_IPC){ string procid{}; 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(request, request_len, msg.data(), msg.size()); return true; } int bus_send_reply(void* handle, void *src, const void *reply, const int reply_len) { TAG; bus* b = get(handle); if (!b){ PRNTVITAG("handle is null"); return false; } if (!src || !reply || reply_len <= 0) { PRNTVITAG("src or reply is null"); return false; } auto ret = send_reply(src, reply, reply_len, &get<6>(*b)); if (ret < 0) return false; return true; } void bus_cleanup(void* handle){ bus *b = get(handle); if (b) delete b; } void bus_free(void *buf, int size){ free_nng(buf, size); } int bus_last_error(void **msg, int *msg_len){ int ec; get_last_error(&ec, msg, msg_len); return true; }