Пример #1
0
/**
 * Connects to receive broadcasts on a given object key
 */
void subscribe_socket::connect(std::string objectkey) {
  std::lock_guard<mutex> guard(lock);
  if (publishers.count(objectkey) == 0) {
    std::string local_address = normalize_address(objectkey);
    int ret = nn_connect(z_socket, local_address.c_str());
    if (ret > 0) {
      publishers[objectkey] = ret;
    }
  }
}
Пример #2
0
publish_socket::publish_socket(void* zmq_ctx,
                               graphlab::zookeeper_util::key_value* keyval,
                               std::string alternate_bind_address)
    :z_ctx(zmq_ctx), zk_keyval(keyval) {
  // create a socket
  z_socket = zmq_socket(z_ctx, ZMQ_PUB);
  set_conservative_socket_parameters(z_socket);
#ifdef ZMQ_PUB_NODROP
  {
    // if no drop is defined. Set the HWM to something more sensible
    int nodrop = 1;
    int rc = zmq_setsockopt(z_socket, ZMQ_PUB_NODROP, &nodrop, sizeof(nodrop));
    assert(rc == 0);
  }
#endif

  int hwm = 1024*1024; // 1MB
  int rc = zmq_setsockopt(z_socket, ZMQ_SNDHWM, &hwm, sizeof(hwm));
  assert(rc == 0);
  assert(z_socket != NULL);

  if (!alternate_bind_address.empty()) {
    local_address = normalize_address(alternate_bind_address);
    int rc = zmq_bind(z_socket, local_address.c_str());
    if (rc != 0) {
      print_zmq_error("publish_socket construction: ");
      assert(rc == 0);
    }
  } else {
    // what is the listening socket?
    std::string localip = graphlab::get_local_ip_as_str(true);
    bool ok = false;
    while (!ok) {
      size_t port = get_next_port_number();
      char port_as_string[128];
      sprintf(port_as_string, "%ld", port);
      local_address = "tcp://" + localip + ":" + port_as_string;
      // try to bind
      int rc = zmq_bind(z_socket, local_address.c_str());
      ok = (rc == 0);
      /*if (rc == EADDRINUSE) {
        std::cout << local_address << " in use. Trying another port.\n";
        continue;
      } else if (rc != 0) {
        std::cout << "Unable to bind to " << local_address << ". "
                  << "Error(" << rc << ") = " << zmq_strerror(rc) << "\n";
      }*/
    }
  }
  // std::cout << "Bound to " << local_address << "\n";
}
/**
 * Update and normalize atsi performance information
 *
 * @param address the address to update
 */
void
GAS_normalization_update_property (struct ATS_Address *address)
{
  const struct GNUNET_ATS_Properties *prop = &address->properties;
  struct PropertyRange range;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Updating properties for peer `%s'\n",
       GNUNET_i2s (&address->peer));
  GAS_plugin_solver_lock ();
  update_avg (prop->delay.rel_value_us,
              &address->norm_delay);
  update_avg (prop->distance,
              &address->norm_distance);
  update_avg (prop->utilization_in,
              &address->norm_utilization_in);
  update_avg (prop->utilization_in,
              &address->norm_utilization_out);

  init_range (&range);
  GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
                                         &find_min_max_it,
                                         &range);
  if (0 != memcmp (&range,
                   &property_range,
                   sizeof (struct PropertyRange)))
  {
    /* limits changed, (re)normalize all addresses */
    property_range = range;
    GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
                                           &normalize_address,
                                           NULL);
    GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
                                           &notify_change,
                                           NULL);
  }
  else
  {
    /* renormalize just this one address */
    normalize_address (NULL,
                       &address->peer,
                       address);
    notify_change (NULL,
                   &address->peer,
                   address);
  }
  GAS_plugin_solver_unlock ();
}
Пример #4
0
reply_socket::reply_socket(void* zmq_ctx,
                           graphlab::zookeeper_util::key_value* keyval,
                           callback_type callback,
                           std::string alternate_bind_address)
    :z_ctx(zmq_ctx), zk_keyval(keyval), callback(callback), associated_pollset(NULL) {
  // create a socket
  z_socket = zmq_socket(z_ctx, ZMQ_ROUTER);
  set_conservative_socket_parameters(z_socket);
  assert(z_socket != NULL);

  if (!alternate_bind_address.empty()) {
    local_address = normalize_address(alternate_bind_address);
    int rc = zmq_bind(z_socket, local_address.c_str());
    if (rc != 0) {
      print_zmq_error("reply_socket construction: ");
      assert(rc == 0);
    }
  } else {
    // what is the listening socket?
    std::string localip = graphlab::get_local_ip_as_str(true);
    bool ok = false;
    while (!ok) {
      size_t port = get_next_port_number();
      char port_as_string[128];
      sprintf(port_as_string, "%ld", port);
      local_address = "tcp://" + localip + ":" + port_as_string;
      // try to bind
      int rc = zmq_bind(z_socket, local_address.c_str());
      ok = (rc == 0);
      /*if (rc == EADDRINUSE) {
        std::cout << local_address << " in use. Trying another port.\n";
        continue;
      } else if (rc != 0) {
        std::cout << "Unable to bind to " << local_address << ". "
                  << "Error(" << rc << ") = " << zmq_strerror(rc) << "\n";
      }*/
    }
  }
  // std::cout << "Bound to " << local_address << "\n";
}
Пример #5
0
void subscribe_socket::timer_callback(socket_receive_pollset* unused,
                                      const zmq_pollitem_t& unused2) {
  if (publisher_info_changed == false) return;
  else {
    // loop though all the subscriptions and check for server changes.
    // disconnecting, and reconnecting if necessary
    boost::lock_guard<boost::recursive_mutex> guard(lock);
    for(size_t i = 0;i < publishers.size(); ++i) {
      if (publishers[i].server_changed &&
          !publishers[i].connected_server.empty()) {
        zmq_disconnect(z_socket, publishers[i].connected_server.c_str());
        publishers[i].connected_server.clear();
      }
      if (!publishers[i].server.empty()) {
        std::string local_address = normalize_address(publishers[i].server);
        zmq_connect(z_socket, local_address.c_str());
        publishers[i].connected_server = publishers[i].server;
      }
      publishers[i].server_changed = false;
    }
    publisher_info_changed = false;
  }
}
Пример #6
0
/**
 * Connects to receive broadcasts on a given object key
 */
void subscribe_socket::connect(std::string objectkey) {
  boost::lock_guard<boost::recursive_mutex> guard(lock);
  for (size_t i = 0;i < publishers.size(); ++i) {
    if (publishers[i].key == objectkey) {
      return;
    }
  }
  publisher_info pi;
  pi.key = objectkey;
  pi.server_changed = false;
  if (zk_keyval) {
    pi.server = zk_keyval->get(objectkey).second;
  } else {
    pi.server = objectkey;
  }
  pi.connected_server = pi.server;
  if (pi.server != "") {
    // TODO check return value
    std::string local_address = normalize_address(pi.server);
    zmq_connect(z_socket, local_address.c_str());
  } 
  publishers.push_back(pi);
}
Пример #7
0
async_reply_socket::async_reply_socket(void* zmq_ctx,
                           graphlab::zookeeper_util::key_value* keyval,
                           callback_type callback,
                           size_t nthreads,
                           std::string alternate_bind_address,
                           std::string secret_key)
    :z_ctx(zmq_ctx), zk_keyval(keyval), callback(callback), associated_pollset(NULL) {
  // create a socket
  z_socket = zmq_socket(z_ctx, ZMQ_ROUTER);
  assert(z_socket);

  if (!secret_key.empty()) {
    const int is_server = 1;
    int rc = zmq_setsockopt (z_socket, ZMQ_CURVE_SERVER, &is_server, sizeof(int));
    assert (rc == 0);

    assert(secret_key.length() == 40);
    rc = zmq_setsockopt (z_socket, ZMQ_CURVE_SECRETKEY, secret_key.c_str(), 40);
    assert (rc == 0);
  }

  set_conservative_socket_parameters(z_socket);
  assert(z_socket != NULL);

  if (!alternate_bind_address.empty()) {
    local_address = normalize_address(alternate_bind_address);
    int rc = zmq_bind(z_socket, local_address.c_str());
    if (rc != 0) {
      print_zmq_error("async_reply_socket construction: ");
      assert(rc == 0);
    }
  } else {
    // what is the listening socket?
    std::string localip = graphlab::get_local_ip_as_str(true);
    bool ok = false;
    while (!ok) {
      size_t port = get_next_port_number();
      char port_as_string[128];
      sprintf(port_as_string, "%ld", port);
      local_address = "tcp://" + localip + ":" + port_as_string;
      // try to bind
      int rc = zmq_bind(z_socket, local_address.c_str());
      ok = (rc == 0);
      /*if (rc == EADDRINUSE) {
        std::cout << local_address << " in use. Trying another port.\n";
        continue;
      } else if (rc != 0) {
        std::cout << "Unable to bind to " << local_address << ". "
                  << "Error(" << rc << ") = " << zmq_strerror(rc) << "\n";
      }*/
    }
  }
  local_address = normalize_address(local_address);
  // std::cout << "Bound to " << local_address << "\n";

  // now construct the threads and the required inproc sockets
  char inprocname[64];
  size_t socket_number = ASYNC_REPLY_SOCKET_CTR.inc();
  sprintf(inprocname, "inproc://async_rep_%ld", socket_number);
  inproc_pull_socket = zmq_socket(zmq_ctx, ZMQ_PULL);
  if (inproc_pull_socket == NULL) {
    print_zmq_error("async_reply_socket");
    assert(inproc_pull_socket != NULL);
  }
  int rc = zmq_bind(inproc_pull_socket, inprocname);
  if (rc != 0) {
    print_zmq_error("async_reply_socket");
    assert(false);
  }

  assert(nthreads > 0);
  threads.resize(nthreads);
  queue_terminate = false;
  for (size_t i = 0;i < threads.size(); ++i) {
    threads[i].parent = this;
    threads[i].inproc_push_socket = zmq_socket(zmq_ctx, ZMQ_PUSH);
    if (threads[i].inproc_push_socket == NULL) {
      print_zmq_error("async_reply_socket");
      assert(threads[i].inproc_push_socket != NULL);
    }
    // connect the push to the pull
    int rc = zmq_connect(threads[i].inproc_push_socket, inprocname);
    if (rc != 0) {
      print_zmq_error("async_reply_socket");
      assert(rc == 0);
    }

    // launch the threads
    threads[i].thread = new boost::thread(boost::bind(&async_reply_socket::thread_function,
                                                      this,
                                                      &threads[i]));
  }
}