lichao
2021-05-06 c2cc00574415c612e82f0955523d422f59594912
box/center.cpp
@@ -18,6 +18,7 @@
#include "center.h"
#include "bh_util.h"
#include "defs.h"
#include "log.h"
#include "shm.h"
#include <chrono>
#include <set>
@@ -60,9 +61,7 @@
      void UpdateState(const int64_t now, const int64_t offline_time, const int64_t kill_time)
      {
         auto diff = now - timestamp_;
#ifndef NDEBUG
         printf("state %p diff: %ld\n", this, diff);
#endif
         LOG_DEBUG() << "state " << this << " diff: " << diff;
         if (diff < offline_time) {
            flag_ = kStateNormal;
         } else if (diff < kill_time) {
@@ -103,7 +102,22 @@
   // center name, no relative to shm.
   const std::string &id() const { return id_; }
   void OnNodeInit(const int64_t msg)
   {
      MQId ssn = msg;
      auto UpdateRegInfo = [&](Node &node) {
         for (int i = 0; i < 10; ++i) {
            node->addrs_.insert(ssn + i);
         }
         node->state_.timestamp_ = NowSec() - offline_time_;
         node->state_.UpdateState(NowSec(), offline_time_, kill_time_);
      };
      Node node(new NodeInfo);
      UpdateRegInfo(node);
      nodes_[ssn] = node;
      LOG_INFO() << "new node ssn (" << ssn << ") init";
   }
   MsgCommonReply Register(const BHMsgHead &head, MsgRegister &msg)
   {
      if (msg.proc().proc_id() != head.proc_id()) {
@@ -132,17 +146,19 @@
            Node node(new NodeInfo);
            UpdateRegInfo(node);
            nodes_[ssn] = node;
         }
         LOG_DEBUG() << "node (" << head.proc_id() << ") ssn (" << ssn << ")";
            printf("new node (%s) ssn (%ld)\n", head.proc_id().c_str(), ssn);
            auto old = online_node_addr_map_.find(head.proc_id());
            if (old != online_node_addr_map_.end()) { // old session
               auto &old_ssn = old->second;
         auto old = online_node_addr_map_.find(head.proc_id());
         if (old != online_node_addr_map_.end()) { // old session
            auto &old_ssn = old->second;
            if (old_ssn != ssn) {
               nodes_[old_ssn]->state_.PutOffline(offline_time_);
               printf("put node (%s) ssn (%ld) offline\n", nodes_[old_ssn]->proc_.proc_id().c_str(), old->second);
               LOG_DEBUG() << "put node (" << nodes_[old_ssn]->proc_.proc_id() << ") ssn (" << old->second << ") offline";
               old_ssn = ssn;
            } else {
               online_node_addr_map_.emplace(head.proc_id(), ssn);
            }
         } else {
            online_node_addr_map_.emplace(head.proc_id(), ssn);
         }
         return MakeReply(eSuccess);
      } catch (...) {
@@ -201,9 +217,9 @@
             for (auto &topic : topics) {
                service_map_[topic].insert(dest);
             }
             printf("node %s ssn %ld serve %d topics:\n", node->proc_.proc_id().c_str(), *node->addrs_.begin(), topics.size());
             LOG_DEBUG() << "node " << node->proc_.proc_id() << " ssn " << *node->addrs_.begin() << " serve " << topics.size() << " topics:\n";
             for (auto &topic : topics) {
                printf("\t %s\n", topic.c_str());
                LOG_DEBUG() << "\t" << topic;
             }
             return MakeReply(eSuccess);
          });
@@ -392,9 +408,18 @@
      EraseMapRec(service_map_, node->services_);
      EraseMapRec(subscribe_map_, node->subscriptions_);
      // remove online record.
      auto pos = online_node_addr_map_.find(node->proc_.proc_id());
      if (pos != online_node_addr_map_.end()) {
         if (node->addrs_.find(pos->second) != node->addrs_.end()) {
            online_node_addr_map_.erase(pos);
         }
      }
      for (auto &addr : node->addrs_) {
         cleaner_(addr);
      }
      node->addrs_.clear();
   }
   std::string id_; // center proc id;
@@ -437,16 +462,24 @@
          msg, head, [&](auto &body) { return center->MsgTag(head, body); }, replyer); \
      return true;
bool AddCenter(const std::string &id, const NodeCenter::Cleaner &cleaner)
auto MakeReplyer(ShmSocket &socket, BHMsgHead &head, const std::string &proc_id)
{
   auto center_ptr = std::make_shared<Synced<NodeCenter>>(id, cleaner, 60s, 60s * 2);
   auto MakeReplyer = [](ShmSocket &socket, BHMsgHead &head, const std::string &proc_id) {
      return [&](auto &&rep_body) {
         auto reply_head(InitMsgHead(GetType(rep_body), proc_id, head.ssn_id(), head.msg_id()));
         auto remote = head.route(0).mq_id();
         socket.Send(remote, reply_head, rep_body);
      };
   return [&](auto &&rep_body) {
      auto reply_head(InitMsgHead(GetType(rep_body), proc_id, head.ssn_id(), head.msg_id()));
      auto remote = head.route(0).mq_id();
      socket.Send(remote, reply_head, rep_body);
   };
}
bool AddCenter(std::shared_ptr<Synced<NodeCenter>> center_ptr)
{
   auto OnNodeInit = [center_ptr](ShmSocket &socket, MsgI &msg) {
      auto &center = *center_ptr;
      center->OnNodeInit(msg.Offset());
   };
   auto Nothing = [](ShmSocket &socket) {};
   BHCenter::Install("#centetr.Init", OnNodeInit, Nothing, BHInitAddress(), 16);
   auto OnCenterIdle = [center_ptr](ShmSocket &socket) {
      auto &center = *center_ptr;
@@ -466,6 +499,7 @@
      default: return false;
      }
   };
   BHCenter::Install("#center.main", OnCenter, OnCenterIdle, BHTopicCenterAddress(), 1000);
   auto OnBusIdle = [=](ShmSocket &socket) {};
   auto OnPubSub = [=](ShmSocket &socket, MsgI &msg, BHMsgHead &head) -> bool {
@@ -506,7 +540,6 @@
      }
   };
   BHCenter::Install("#center.reg", OnCenter, OnCenterIdle, BHTopicCenterAddress(), 1000);
   BHCenter::Install("#center.bus", OnPubSub, OnBusIdle, BHTopicBusAddress(), 1000);
   return true;
@@ -524,7 +557,12 @@
bool BHCenter::Install(const std::string &name, MsgHandler handler, IdleHandler idle, const MQId mqid, const int mq_len)
{
   Centers()[name] = CenterInfo{name, handler, idle, mqid, mq_len};
   Centers()[name] = CenterInfo{name, handler, MsgIHandler(), idle, mqid, mq_len};
   return true;
}
bool BHCenter::Install(const std::string &name, MsgIHandler handler, IdleHandler idle, const MQId mqid, const int mq_len)
{
   Centers()[name] = CenterInfo{name, MsgHandler(), handler, idle, mqid, mq_len};
   return true;
}
@@ -532,10 +570,13 @@
{
   auto gc = [&](const MQId id) {
      auto r = ShmSocket::Remove(shm, id);
      printf("remove mq %ld : %s\n", id, (r ? "ok" : "failed"));
      if (r) {
         LOG_DEBUG() << "remove mq " << id << " ok\n";
      }
   };
   AddCenter("#bhome_center", gc);
   auto center_ptr = std::make_shared<Synced<NodeCenter>>("#bhome_center", gc, 6s, 6s * 2);
   AddCenter(center_ptr);
   for (auto &kv : Centers()) {
      auto &info = kv.second;
@@ -547,7 +588,11 @@
{
   for (auto &kv : Centers()) {
      auto &info = kv.second;
      sockets_[info.name_]->Start(info.handler_, info.idle_);
      if (info.handler_) {
         sockets_[info.name_]->Start(info.handler_, info.idle_);
      } else {
         sockets_[info.name_]->Start(info.raw_handler_, info.idle_);
      }
   }
   return true;