示例#1
0
comm_server::comm_server(std::vector<std::string> zkhosts, 
                         std::string name, 
                         std::string alternate_bind_address,
                         std::string alternate_control_address,
                         std::string alternate_publish_address,
                         std::string secret_key):

    started(false),
    comm_server_debug_mode(std::getenv("GRAPHLAB_COMM_SERVER_DEBUG_MODE") != NULL)
    {


  object_socket = new nanosockets::async_reply_socket(
          boost::bind(&comm_server::callback, this, _1, _2),
          1, // 2 threads. one to handle pings, one to handle real messages
          alternate_bind_address);

  if(alternate_bind_address.size() == 0) {
    alternate_bind_address = object_socket->get_bound_address();
  }
  logstream(LOG_INFO) << "my alt bind address: " << alternate_bind_address << std::endl;
  control_socket = new nanosockets::async_reply_socket(
        boost::bind(&comm_server::callback, this, _1, _2), 1,
        (alternate_control_address.length()==0) ?
            generate_aux_address(alternate_bind_address, "_control") :
            alternate_control_address);
  publishsock = new nanosockets::publish_socket(
        (alternate_publish_address.length()==0) ?
            generate_aux_address(alternate_bind_address, "_status") :
            alternate_publish_address);
  get_srv_running_command().store(0);
  get_cancel_bit_checked().store(false);


  logstream(LOG_EMPH) << "Server listening on: " 
                      << object_socket->get_bound_address() << std::endl;
  logstream(LOG_INFO) << "Server Control listening on: " 
                      << control_socket->get_bound_address() << std::endl;
  logstream(LOG_INFO) << "Server status published on: " 
                      << publishsock->get_bound_address() << std::endl;

  // there is a chicken and egg problem here. We can't use the object 
  // factory to create the object factory. So, here it is: manual construction 
  // and registration of the object factory
  object_factory = new object_factory_impl(*this);
  register_type<object_factory_base>([&]() { 
                                     return new object_factory_impl(*this); } );
  auto deleter = +[](void* v) {
                    if (v != NULL) {
                      delete reinterpret_cast<object_factory_impl*>(v);
                    }
                  };
  std::shared_ptr<void> object_ptr(object_factory, deleter);
  registered_objects.insert({0, object_ptr});

  std::random_device rd;
  lcg_seed = (size_t(rd()) << 32) + rd();
}
示例#2
0
comm_server::comm_server(std::vector<std::string> zkhosts, 
                         std::string name, 
                         std::string alternate_bind_address,
                         std::string alternate_control_address,
                         std::string alternate_publish_address,
                         std::string secret_key):

    started(false),
    zmq_ctx(zmq_ctx_new()), 
    keyval(zkhosts.empty() ?  // make a keyval only if zkhosts is not empty
               NULL :         // null otherwise
           new graphlab::zookeeper_util::key_value(zkhosts, "cppipc", name)),
    comm_server_debug_mode(std::getenv("GRAPHLAB_COMM_SERVER_DEBUG_MODE") != NULL)
    {


  object_socket = new libfault::async_reply_socket(zmq_ctx, keyval,
          boost::bind(&comm_server::callback, this, _1, _2),
          1, // 2 threads. one to handle pings, one to handle real messages
          alternate_bind_address, secret_key);
  if(alternate_bind_address.size() == 0) {
    alternate_bind_address = object_socket->get_bound_address();
  }
  logstream(LOG_INFO) << "my alt bind address: " << alternate_bind_address << std::endl;
  control_socket = new libfault::async_reply_socket(zmq_ctx, keyval,
        boost::bind(&comm_server::callback, this, _1, _2), 1,
        (keyval==NULL && alternate_control_address.length()==0) ?
        generate_aux_address(alternate_bind_address, "_control") :
        alternate_control_address);
  publishsock = new libfault::publish_socket(zmq_ctx, keyval,
        // honestly, this syntax is *terrible*.
        // If Zookeeper is not used, and alternate_publish_address not
        // provided, we generate one based on the bind address
        (keyval==NULL && alternate_publish_address.length()==0) ?
        generate_aux_address(alternate_bind_address, "_status") :
        alternate_publish_address);
  get_srv_running_command().store(0);
  get_cancel_bit_checked().store(false);
  pollset = new libfault::socket_receive_pollset;

  if (keyval != NULL && !object_socket->register_key("call")) {
    logstream(LOG_ERROR) 
        << "Unable to register the zookeeper key for the main server. "
           "Perhaps there is already a server with this name?";
    throw("Unable to register with zookeeper");
  }
  if (keyval != NULL && !control_socket->register_key("control")) {
    logstream(LOG_ERROR) 
        << "Unable to register the zookeeper key for the main server's control socket. "
           "Perhaps there is already a server with this name?";
    throw("Unable to register with zookeeper");
  }
  if (keyval != NULL && !publishsock->register_key("status")) {
    logstream(LOG_ERROR) 
        << "Unable to register the zookeeper key for the publishsock. "
           "Perhaps there is already a server with this name?";
    throw("Unable to register with zookeeper");
  }
  object_socket->add_to_pollset(pollset);
  control_socket->add_to_pollset(pollset);

  logstream(LOG_EMPH) << "Server listening on: " 
                      << object_socket->get_bound_address() << std::endl;
  logstream(LOG_INFO) << "Server Control listening on: " 
                      << control_socket->get_bound_address() << std::endl;
  logstream(LOG_INFO) << "Server status published on: " 
                      << publishsock->get_bound_address() << std::endl;

  // there is a chicken and egg problem here. We can't use the object 
  // factory to create the object factory. So, here it is: manual construction 
  // and registration of the object factory
  object_factory = new object_factory_impl(*this);
  register_type<object_factory_base>([&]() { 
                                     return new object_factory_impl(*this); } );
  auto deleter = +[](void* v) {
                    if (v != NULL) {
                      delete reinterpret_cast<object_factory_impl*>(v);
                    }
                  };
  std::shared_ptr<void> object_ptr(object_factory, deleter);
  registered_objects.insert({0, object_ptr});

  std::random_device rd;
  lcg_seed = (size_t(rd()) << 32) + rd();
}