| | |
| | | #include "google/protobuf/dynamic_message.h" |
| | | #include "google/protobuf/compiler/importer.h" |
| | | |
| | | #include "3dparty/yyjson/yyjson.h" |
| | | #include "3rdparty/yyjson/yyjson.h" |
| | | |
| | | #include "bhome_msg_api.pb.h" |
| | | using namespace bhome_msg; |
| | |
| | | reg->sub_net_count = subnetcnt; |
| | | return reg; |
| | | } |
| | | creg* make_creg_from_cproc(const cproc* proc){ |
| | | auto reg = ptrT<struct creg>(); |
| | | reg->proc = rmConst(proc); |
| | | return reg; |
| | | } |
| | | |
| | | static inline void creg_add_topic(char*** dst, size_t* dstC, const char** src, const size_t srcC){ |
| | | *dst = rmConst(src); |
| | | *dstC = srcC; |
| | | } |
| | | void creg_add_topic_reply(creg* reg, const char** topic, const size_t count){ |
| | | creg_add_topic(®->channel, ®->channel_count, topic, count); |
| | | } |
| | | |
| | | void creg_add_topic_pub(creg* reg, const char** topic, const size_t count){ |
| | | creg_add_topic(®->pub, ®->pub_count, topic, count); |
| | | } |
| | | |
| | | void creg_add_topic_sub(creg* reg, const char** topic, const size_t count){ |
| | | creg_add_topic(®->sub, ®->sub_count, topic, count); |
| | | } |
| | | |
| | | void creg_add_topic_subnet(creg* reg, const char** topic, const size_t count){ |
| | | creg_add_topic(®->sub_net, ®->sub_net_count, topic, count); |
| | | } |
| | | |
| | | const cproc* creg_proc(const creg* reg){ |
| | | if (!reg) return NULL; |
| | | return reg->proc; |
| | | } |
| | | |
| | | char** creg_rep_topic(const creg* reg, size_t* count){ |
| | | char** creg_reply_topic(const creg* reg, size_t* count){ |
| | | if (!reg) return NULL; |
| | | *count = reg->channel_count; |
| | | return reg->channel; |
| | |
| | | char* entry = NULL; |
| | | size_t entry_size = 0; |
| | | json2str(sv, &entry, &entry_size); |
| | | arr[i] = ptrT<char>(entry_size+1); |
| | | memcpy(arr[i], entry, entry_size); |
| | | arr[i] = entry; |
| | | } |
| | | return make_tuple(arr, count); |
| | | }; |
| | |
| | | tie(msg->procid, msg->procidl) = copymemory(pid, pids); |
| | | |
| | | MsgRequestTopic msgRT; |
| | | if (!msgRT.ParseFromArray(data, size)) return NULL; |
| | | if (!msgRT.ParseFromArray(data, size)) { |
| | | free(msg->procid); |
| | | free(msg); |
| | | return NULL; |
| | | } |
| | | tie(msg->msg, msg->msgl) = copymemory(msgRT.data()); |
| | | |
| | | return msg; |
| | |
| | | msgRT.set_topic(topic, topicl); |
| | | msgRT.set_data(data, datal); |
| | | |
| | | auto pbstr = msgRT.SerializeAsString(); |
| | | const auto& pbstr = msgRT.SerializeAsString(); |
| | | tie(msg->msg, msg->msgl) = copymemory(pbstr); |
| | | auto doc = yyjson_read(msg->msg, msg->msgl, 0); |
| | | yyjson_val* root = yyjson_doc_get_root(doc); |
| | | |
| | | auto obj = [](yyjson_val* v, const char* name){return yyjson_obj_get(v, name);}; |
| | | |
| | | auto jp = obj(root, "path"); |
| | | auto jb = obj(root, "body"); |
| | | if (jp && jb){ |
| | | tie(msg->path, msg->pathl) = copymemory(yyjson_get_str(jp), yyjson_get_len(jp)); |
| | | tie(msg->body, msg->bodyl) = copymemory(yyjson_get_str(jb), yyjson_get_len(jb)); |
| | | } |
| | | |
| | | yyjson_doc_free(doc); |
| | | |
| | | return msg; |
| | | } |
| | |
| | | if (msg){ |
| | | free(msg->procid); |
| | | free(msg->msg); |
| | | free(msg->path); |
| | | free(msg->body); |
| | | free(msg); |
| | | } |
| | | } |
| | | |
| | | static tuple<const char*, size_t> json2body(yyjson_doc* doc){ |
| | | ignoreT(json2body); |
| | | |
| | | yyjson_val* root = yyjson_doc_get_root(doc); |
| | | |
| | | auto obj = [](yyjson_val* v, const char* name){return yyjson_obj_get(v, name);}; |
| | |
| | | if (!jp) return make_tuple((const char*)NULL, 0); |
| | | auto jb = obj(root, "body"); |
| | | if (!jb) return make_tuple((const char*)NULL, 0); |
| | | return make_tuple(yyjson_get_str(jb), yyjson_get_len(jp)); |
| | | return make_tuple(yyjson_get_str(jb), yyjson_get_len(jb)); |
| | | } |
| | | |
| | | struct cstackmsgerr* get_reqmsg_stackerr(struct creqmsg* msg){ |
| | | auto doc0 = yyjson_read(msg->msg, msg->msgl, 0); |
| | | auto tpbody = json2body(doc0); |
| | | if (!get<0>(tpbody)) return NULL; |
| | | // auto doc0 = yyjson_read(msg->msg, msg->msgl, 0); |
| | | // auto tpbody = json2body(doc0); |
| | | // if (!get<0>(tpbody)) return NULL; |
| | | // auto doc = yyjson_read(get<0>(tpbody), get<1>(tpbody), 0); |
| | | |
| | | auto doc = yyjson_read(get<0>(tpbody), get<1>(tpbody), 0); |
| | | auto doc = yyjson_read(msg->body, msg->bodyl, 0); |
| | | auto root = yyjson_doc_get_root(doc); |
| | | |
| | | auto obj = [](yyjson_val* v, const char* name){return yyjson_obj_get(v, name);}; |
| | |
| | | tie(smsg->fileid, smsg->fileidl) = copymemory(yyjson_get_str(jfid), yyjson_get_len(jfid)); |
| | | yyjson_doc_free(doc); |
| | | |
| | | yyjson_doc_free(doc0); |
| | | // yyjson_doc_free(doc0); |
| | | |
| | | return smsg; |
| | | } |
| | |
| | | } |
| | | // decode success msg |
| | | struct cstackmsg* get_reqmsg_stack(struct creqmsg* msg){ |
| | | auto doc0 = yyjson_read(msg->msg, msg->msgl, 0); |
| | | auto tpbody = json2body(doc0); |
| | | if (!get<0>(tpbody)) return NULL; |
| | | // auto doc0 = yyjson_read(msg->msg, msg->msgl, 0); |
| | | // auto tpbody = json2body(doc0); |
| | | // if (!get<0>(tpbody)) return NULL; |
| | | // auto doc = yyjson_read(get<0>(tpbody), get<1>(tpbody), 0); |
| | | |
| | | auto doc = yyjson_read(get<0>(tpbody), get<1>(tpbody), 0); |
| | | auto doc = yyjson_read(msg->body, msg->bodyl, 0); |
| | | auto root = yyjson_doc_get_root(doc); |
| | | |
| | | auto obj = [](yyjson_val* v, const char* name){return yyjson_obj_get(v, name);}; |
| | |
| | | smsg->files[i] = file; |
| | | } |
| | | yyjson_doc_free(doc); |
| | | yyjson_doc_free(doc0); |
| | | // yyjson_doc_free(doc0); |
| | | |
| | | return smsg; |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | template <class F> void ignoref(F&& f){} |
| | | static struct cstr make_reply_msg_json(const int success, const char* msg, const size_t msgl, |
| | | const char* data, const size_t datal) |
| | | { |
| | | ignoref(make_reply_msg_json); |
| | | |
| | | auto doc = yyjson_mut_doc_new(NULL); |
| | | auto root = yyjson_mut_obj(doc); |
| | | yyjson_mut_obj_add_bool(doc, root, "success", !!success); |
| | |
| | | yyjson_mut_doc_free(doc); |
| | | return cstr_ref(json, jsonl); |
| | | } |
| | | |
| | | void free_query_procs(struct cqueryprocs* procs, const size_t count){ |
| | | for(size_t i = 0; i < count; i++){ |
| | | free(procs[i].id); |
| | | } |
| | | free(procs); |
| | | } |