예제 #1
0
nexus::behavior_type nexus::make_behavior() {
  return {
    [=](const node_info& ni) {
      if (ni.source_node == caf::invalid_node_id) {
        cerr << "node_info received with invalid source node" << endl;
        return;
      }
      if (! silent_)
        aout(this) << "received node_info: " << to_string(ni) << endl;
      data_[ni.source_node].node = ni;
      auto ls = current_element_->sender;
      probes_[ls] = ls ? ls->node() : invalid_node_id;
      monitor(ls);
      broadcast(ni);
    },
    HANDLE_UPDATE(ram_usage, ram),
    HANDLE_UPDATE(work_load, load),
    [=](const new_actor_published& msg) {
      CHECK_SOURCE(actor_published, msg);
      auto addr = msg.published_actor;
      auto nid = msg.source_node;
      if (! addr) {
        cerr << "received actor_published "
             << "with invalid actor address"
             << endl;
        return;
      }
      if (data_[nid].known_actors.insert(addr).second) {
        monitor(addr);
      }
      data_[nid].published_actors.insert(std::make_pair(addr, msg.port));
      broadcast(msg);
    },
    [=](const new_route& route) {
      CHECK_SOURCE(new_route, route);
      if (route.is_direct
          && data_[route.source_node].direct_routes.insert(route.dest).second) {
        broadcast(route);
      }
    },
    [=](const route_lost& route) {
      CHECK_SOURCE(route_lost, route);
      if (data_[route.source_node].direct_routes.erase(route.dest) > 0) {
        if (! silent_)
          aout(this) << "new route" << endl;
        broadcast(route);
      }
    },
    [=](const new_message& msg) {
      // TODO: reduce message size by avoiding the complete msg
      CHECK_SOURCE(new_message, msg);
      if (! silent_)
        aout(this) << "new message: " << to_string(msg.msg) << endl;
      broadcast(msg);
    },
    [=](add_atom, actor x) {
      if (! silent_)
        aout(this) << "new dynamically typed listener: "
                   << to_string(x) << endl;
      add(actor_cast<listener_type>(std::move(x)));
    },
    [=](add_atom, listener_type x) {
      if (! silent_)
        aout(this) << "new statically typed listener: "
                   << to_string(x) << endl;
      add(std::move(x));
    },
    [=](const node_disconnected& nd) {
      if (! silent_)
        aout(this) << "node_disconnected: " << to_string(nd) << endl;
      data_.erase(nd.source_node);
      broadcast(nd);
    }
  };
}
예제 #2
0
파일: nexus.cpp 프로젝트: ezhangle/riac
nexus::behavior_type nexus::make_behavior() {
  return {
    [=](const riac::node_info& ni) {
      if (ni.source_node == caf::invalid_node_id) {
        cerr << "node_info received with invalid source node" << endl;
        return;
      }
      cout << "received node_info " << endl;
      data_[ni.source_node].node = ni;
      auto& ls = current_sender();
      probes_[ls] = ls.node();
      monitor(ls);
      broadcast();
    },
    HANDLE_UPDATE(riac::ram_usage, ram),
    HANDLE_UPDATE(riac::work_load, load),
    [=](const riac::new_actor_published& msg) {
      CHECK_SOURCE(riac::actor_published, msg);
      auto addr = msg.published_actor;
      auto nid = msg.source_node;
      if (addr == invalid_actor_addr) {
        cerr << "received riac::actor_published "
             << "with invalid actor address"
             << endl;
        return;
      }
      if (data_[nid].known_actors.insert(addr).second) {
        monitor(addr);
      }
      data_[nid].published_actors.insert(std::make_pair(addr, msg.port));
      broadcast();
    },
    [=](const riac::new_route& route) {
      CHECK_SOURCE(riac::new_route, route);
      if (route.is_direct
          && data_[route.source_node].direct_routes.insert(route.dest).second) {
        broadcast();
      }
    },
    [=](const riac::route_lost& route) {
      CHECK_SOURCE(riac::route_lost, route);
      if (data_[route.source_node].direct_routes.erase(route.dest) > 0) {
        cout << "new route" << endl;
        broadcast();
      }
    },
    [=](const riac::new_message& msg) {
      // TODO: reduce message size by avoiding the complete msg
      CHECK_SOURCE(riac::new_message, msg);
      cout << "new message" << endl;
      broadcast();
    },
    [=](const riac::add_listener& req) {
      //cout << "new listerner" << endl;
      add_listener(actor_cast<riac::listener_type>(req.listener));
    },
    [=](const riac::add_typed_listener& req) {
      add_listener(req.listener);
    },
    [=](const down_msg& dm) {
      if (listeners_.erase(actor_cast<riac::listener_type>(dm.source)) > 0) {
        cout << format_down_msg("listener", dm) << endl;
        return;
      }
      auto probe_addr = probes_.find(dm.source);
      if (probe_addr != probes_.end()) {
        cout << format_down_msg("probe", dm) << endl;
        riac::node_disconnected nd{probe_addr->second};
        send(this, nd);
        auto i = data_.find(probe_addr->second);
        if (i != data_.end()
            && i->second.known_actors.erase(probe_addr->first) > 0) {
          return;
        }
      }
    },
    [=](const riac::node_disconnected& nd) {
      data_.erase(nd.source_node);
      broadcast();
    }
  };
}