lichao
2021-04-08 c338820e4db43ad32c20ff429a038b06bcb980f8
src/pubsub.cpp
@@ -19,118 +19,74 @@
#include "bh_util.h"
#include "defs.h"
namespace bhome_shm
{
using namespace std::chrono_literals;
const int kMaxWorker = 16;
using namespace bhome_msg;
BusManager::BusManager(SharedMemory &shm) :
    shm_(shm), socket_(ShmSocket::eSockBus, shm) {}
BusManager::BusManager() :
    BusManager(BHomeShm()) {}
bool BusManager::Start(const int nworker)
bool SocketPublish::Publish(const std::string &proc_id, const Topic &topic, const void *data, const size_t size, const int timeout_ms)
{
   auto onRecv = [&](MsgI &imsg) {
      BHMsg msg;
      if (!imsg.Unpack(msg)) {
         return;
   try {
      MsgPublish pub;
      pub.set_topic(topic);
      pub.set_data(data, size);
      BHMsgHead head(InitMsgHead(GetType(pub), proc_id));
      MsgI imsg;
      if (imsg.MakeRC(shm(), head, pub)) {
         DEFER1(imsg.Release(shm()));
         return ShmMsgQueue::Send(shm(), BHTopicBusAddress(), imsg, timeout_ms);
      }
   } catch (...) {
   }
   return false;
}
namespace
{
inline void AddRoute(BHMsgHead &head, const MQId &id) { head.add_route()->set_mq_id(&id, sizeof(id)); }
      auto OnSubChange = [&](auto &&update) {
         DataSub sub;
         if (!msg.route().empty() && sub.ParseFromString(msg.body()) && !sub.topics().empty()) {
            assert(sizeof(MQId) == msg.route(0).mq_id().size());
            MQId client;
            memcpy(&client, msg.route(0).mq_id().data(), sizeof(client));
} // namespace
bool SocketSubscribe::Subscribe(const std::string &proc_id, const std::vector<Topic> &topics, const int timeout_ms)
{
   try {
      MsgSubscribe sub;
      for (auto &topic : topics) {
         sub.add_topics(topic);
      }
      BHMsgHead head(InitMsgHead(GetType(sub), proc_id));
      AddRoute(head, mq().Id());
            std::lock_guard<std::mutex> guard(mutex_);
            auto &topics = sub.topics();
            for (auto &topic : topics) {
               try {
                  update(topic, client);
               } catch (...) {
                  //TODO log error
               }
            }
      return Send(&BHTopicBusAddress(), head, sub, timeout_ms);
   } catch (...) {
      return false;
   }
}
bool SocketSubscribe::StartRecv(const TopicDataCB &tdcb, int nworker)
{
   auto AsyncRecvProc = [this, tdcb](ShmSocket &, MsgI &imsg, BHMsgHead &head) {
      if (head.type() == kMsgTypePublish) {
         MsgPublish pub;
         if (imsg.ParseBody(pub)) {
            tdcb(head.proc_id(), pub.topic(), pub.data());
         }
      };
      auto Sub1 = [this](const std::string &topic, const MQId &id) {
         records_[topic].insert(id);
      };
      auto Unsub1 = [this](const std::string &topic, const MQId &id) {
         auto pos = records_.find(topic);
         if (pos != records_.end()) {
            if (pos->second.erase(id) && pos->second.empty()) {
               records_.erase(pos);
            }
         }
      };
      auto OnPublish = [&]() {
         DataPub pub;
         if (!pub.ParseFromString(msg.body())) {
            return;
         }
         auto FindClients = [&](const std::string &topic) {
            Clients dests;
            std::lock_guard<std::mutex> guard(mutex_);
            auto Find1 = [&](const std::string &t) {
               auto pos = records_.find(topic);
               if (pos != records_.end() && !pos->second.empty()) {
                  auto &clients = pos->second;
                  for (auto &cli : clients) {
                     dests.insert(cli);
                  }
               }
            };
            Find1(topic);
            //TODO check and adjust topic on client side sub/pub.
            size_t pos = 0;
            while (true) {
               pos = topic.find(kTopicSep, pos);
               if (pos == topic.npos || ++pos == topic.size()) {
                  // Find1(std::string()); // sub all.
                  break;
               } else {
                  Find1(topic.substr(0, pos));
               }
            }
            return dests;
         };
         auto Dispatch = [&](auto &&send1) {
            const Clients &clients(FindClients(pub.topic()));
            for (auto &cli : clients) {
               send1(cli);
            }
         };
         if (imsg.IsCounted()) {
            Dispatch([&](const MQId &cli) { ShmMsgQueue::Send(shm_, cli, imsg, 100); });
         } else {
            MsgI pubmsg;
            if (!pubmsg.MakeRC(shm_, msg)) { return; }
            DEFER1(pubmsg.Release(shm_));
            Dispatch([&](const MQId &cli) { ShmMsgQueue::Send(shm_, cli, pubmsg, 100); });
         }
      };
      switch (msg.type()) {
      case kMsgTypeSubscribe: OnSubChange(Sub1); break;
      case kMsgTypeUnsubscribe: OnSubChange(Unsub1); break;
      case kMsgTypePublish: OnPublish(); break;
      default: break;
      } else {
         // ignored, or dropped
      }
   };
   return socket_.StartRaw(onRecv, std::min(nworker, kMaxWorker));
   return tdcb && Start(AsyncRecvProc, nworker);
}
} // namespace bhome_shm
bool SocketSubscribe::RecvSub(std::string &proc_id, Topic &topic, std::string &data, const int timeout_ms)
{
   MsgI msg;
   BHMsgHead head;
   if (SyncRecv(msg, head, timeout_ms) && head.type() == kMsgTypePublish) {
      MsgPublish pub;
      if (msg.ParseBody(pub)) {
         head.mutable_proc_id()->swap(proc_id);
         pub.mutable_topic()->swap(topic);
         pub.mutable_data()->swap(data);
         return true;
      }
   }
   return false;
}