| | |
| | | center accept request and route.; |
| | | //*/ |
| | | const uint32_t kMsgTag = 0xf1e2d3c4; |
| | | const uint32_t kMsgPrefixLen = 4; |
| | | |
| | | inline void AddRoute(BHMsg &msg, const MQId &id) { msg.add_route()->set_mq_id(&id, sizeof(id)); } |
| | | |
| | | std::string RandId() |
| | | void *MsgI::Pack(SharedMemory &shm, |
| | | const uint32_t head_len, const ToArray &headToArray, |
| | | const uint32_t body_len, const ToArray &bodyToArray) |
| | | { |
| | | boost::uuids::uuid id = boost::uuids::random_generator()(); |
| | | return std::string((char *) &id, sizeof(id)); |
| | | } |
| | | BHMsg InitMsg(MsgType type, const std::string &msgid = RandId()) |
| | | { |
| | | BHMsg msg; |
| | | msg.set_msg_id(msgid); |
| | | msg.set_type(type); |
| | | time_t tm = 0; |
| | | msg.set_timestamp(time(&tm)); |
| | | return msg; |
| | | } |
| | | |
| | | BHMsg MakeRequest(const MQId &src_id, const std::string &topic, const void *data, const size_t size) |
| | | { |
| | | BHMsg msg(InitMsg(kMsgTypeRequestTopic)); |
| | | AddRoute(msg, src_id); |
| | | MsgRequestTopic req; |
| | | req.set_topic(topic); |
| | | req.set_data(data, size); |
| | | msg.set_body(req.SerializeAsString()); |
| | | return msg; |
| | | } |
| | | |
| | | BHMsg MakeRegister(const MQId &src_id, ProcInfo info, const std::vector<std::string> &topics) |
| | | { |
| | | BHMsg msg(InitMsg(kMsgTypeRegister)); |
| | | AddRoute(msg, src_id); |
| | | MsgRegister reg; |
| | | reg.mutable_proc()->Swap(&info); |
| | | for (auto &t : topics) { |
| | | reg.add_topics(t); |
| | | void *addr = shm.Alloc(sizeof(head_len) + head_len + sizeof(body_len) + body_len); |
| | | if (addr) { |
| | | auto p = static_cast<char *>(addr); |
| | | auto Pack1 = [&p](auto len, auto &writer) { |
| | | Put32(p, len); |
| | | p += sizeof(len); |
| | | writer(p, len); |
| | | p += len; |
| | | }; |
| | | Pack1(head_len, headToArray); |
| | | Pack1(body_len, bodyToArray); |
| | | } |
| | | msg.set_body(reg.SerializeAsString()); |
| | | return msg; |
| | | return addr; |
| | | } |
| | | |
| | | BHMsg MakeHeartbeat(const MQId &src_id, ProcInfo info) |
| | | bool MsgI::ParseHead(BHMsgHead &head) const |
| | | { |
| | | BHMsg msg(InitMsg(kMsgTypeHeartbeat)); |
| | | AddRoute(msg, src_id); |
| | | MsgHeartbeat reg; |
| | | reg.mutable_proc()->Swap(&info); |
| | | msg.set_body(reg.SerializeAsString()); |
| | | return msg; |
| | | } |
| | | |
| | | BHMsg MakeReply(const std::string &src_msgid, const void *data, const size_t size) |
| | | { |
| | | assert(data && size); |
| | | BHMsg msg(InitMsg(kMsgTypeRequestTopicReply, src_msgid)); |
| | | MsgRequestTopicReply reply; |
| | | reply.set_data(data, size); |
| | | msg.set_body(reply.SerializeAsString()); |
| | | return msg; |
| | | } |
| | | |
| | | BHMsg MakeSubUnsub(const MQId &client, const std::vector<std::string> &topics, const MsgType sub_unsub) |
| | | { |
| | | assert(sub_unsub == kMsgTypeSubscribe || sub_unsub == kMsgTypeUnsubscribe); |
| | | BHMsg msg(InitMsg(sub_unsub)); |
| | | AddRoute(msg, client); |
| | | MsgSub subs; |
| | | for (auto &t : topics) { |
| | | subs.add_topics(t); |
| | | } |
| | | msg.set_body(subs.SerializeAsString()); |
| | | return msg; |
| | | } |
| | | |
| | | BHMsg MakeSub(const MQId &client, const std::vector<std::string> &topics) { return MakeSubUnsub(client, topics, kMsgTypeSubscribe); } |
| | | BHMsg MakeUnsub(const MQId &client, const std::vector<std::string> &topics) { return MakeSubUnsub(client, topics, kMsgTypeUnsubscribe); } |
| | | |
| | | BHMsg MakePub(const std::string &topic, const void *data, const size_t size) |
| | | { |
| | | assert(data && size); |
| | | BHMsg msg(InitMsg(kMsgTypePublish)); |
| | | MsgPub pub; |
| | | pub.set_topic(topic); |
| | | pub.set_data(data, size); |
| | | msg.set_body(pub.SerializeAsString()); |
| | | return msg; |
| | | } |
| | | |
| | | BHMsg MakeQueryTopic(const MQId &client, const std::string &topic) |
| | | { |
| | | BHMsg msg(InitMsg(kMsgTypeQueryTopic)); |
| | | AddRoute(msg, client); |
| | | MsgQueryTopic query; |
| | | query.set_topic(topic); |
| | | msg.set_body(query.SerializeAsString()); |
| | | return msg; |
| | | } |
| | | BHMsg MakeQueryTopicReply(const std::string &mqid, const std::string &msgid) |
| | | { |
| | | BHMsg msg(InitMsg(kMsgTypeQueryTopicReply, msgid)); |
| | | MsgQueryTopicReply reply; |
| | | reply.mutable_address()->set_mq_id(mqid); |
| | | msg.set_body(reply.SerializeAsString()); |
| | | return msg; |
| | | } |
| | | |
| | | void *Pack(SharedMemory &shm, const BHMsg &msg) |
| | | { |
| | | uint32_t msg_size = msg.ByteSizeLong(); |
| | | void *p = shm.Alloc(4 + msg_size); |
| | | if (p) { |
| | | Put32(p, msg_size); |
| | | if (!msg.SerializeToArray(static_cast<char *>(p) + kMsgPrefixLen, msg_size)) { |
| | | shm.Dealloc(p); |
| | | p = 0; |
| | | } |
| | | } |
| | | return p; |
| | | } |
| | | |
| | | bool MsgI::Unpack(BHMsg &msg) const |
| | | { |
| | | void *p = ptr_.get(); |
| | | auto p = static_cast<char *>(ptr_.get()); |
| | | assert(p); |
| | | uint32_t msg_size = Get32(p); |
| | | return msg.ParseFromArray(static_cast<char *>(p) + kMsgPrefixLen, msg_size); |
| | | p += 4; |
| | | return head.ParseFromArray(p, msg_size); |
| | | } |
| | | |
| | | // with ref count; |
| | | bool MsgI::MakeRC(SharedMemory &shm, const BHMsg &msg) |
| | | bool MsgI::MakeRC(SharedMemory &shm, void *p) |
| | | { |
| | | void *p = Pack(shm, msg); |
| | | if (!p) { |
| | | return false; |
| | | } |
| | |
| | | return true; |
| | | } |
| | | |
| | | bool MsgI::Make(SharedMemory &shm, const BHMsg &msg) |
| | | bool MsgI::Make(SharedMemory &shm, void *p) |
| | | { |
| | | void *p = Pack(shm, msg); |
| | | if (!p) { |
| | | return false; |
| | | } |