Ejemplo n.º 1
0
/*
 * Start Master init event, and wait client
 */
int StartMaster(void *arg) {
    char *ip = ((SERVER_START_ARG *)arg)->ip;
    int port = ((SERVER_START_ARG *)arg)->port;
    int max_conn = ((SERVER_START_ARG *)arg)->max_conn;
    event_callback_fn recv_callback_fn = ((SERVER_START_ARG *)arg)->recv_callback_fn;
    // prepare socket
    int server_sockfd = PrepareSocket();
    if ( InitServer(server_sockfd, ip, port, max_conn) != 0 ) {
        printf("Master start error.\n");
        return -1;
    }

    // make non_blocking
    evutil_make_listen_socket_reuseable(server_sockfd);

    // init event
    struct event_base *base = event_base_new();
    assert(base != NULL);
    struct event *listen_event;
    callback_arg callarg;
    callarg.base = base;
    callarg.callback_fn = recv_callback_fn;
    listen_event = event_new(base, server_sockfd, EV_READ|EV_PERSIST, MasterAcceptHandle, (void *)&callarg);
    event_add(listen_event, NULL);

    // start event
    printf("Master Start\n");
    event_base_dispatch(base);

    return server_sockfd;
}
Ejemplo n.º 2
0
BOOL GenericConnection::CreateSocket()
{
	if(m_soSocket == INVALID_SOCKET)
		return PrepareSocket();
	else
		return TRUE;
}
Ejemplo n.º 3
0
int InitClient(int sockfd, char* addr, int port) 
{
    PrepareSocket(sockfd, addr, port);
    int ret = connect(sockfd, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr));
    if(ret == -1)
    {
        printf("Connect Error!\n");
        exit(1);
    }
    return sockfd;
}
Ejemplo n.º 4
0
int InitServer(int sockfd, char* addr, int port)
{
    PrepareSocket(sockfd, addr, port);
    int ret = bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr));
    if(ret == -1) 
    {
        printf("Bind Error!\n");
        close(sockfd);
        exit(1);
    } 
    listen(sockfd, MAX_CONNECT_QUEUE);
    return sockfd;
}
Ejemplo n.º 5
0
/*
 * Init client
 */
int InitializeClient(char *ip, int port) {
    // prepare socket
    int client_sockfd = PrepareSocket();

    // init
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr.s_addr = inet_addr(ip);
    memset(&server_addr.sin_zero, 0, 8);

    // connect to server
    if ( connect(client_sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0 ) {
        printf("Connect error.\n");
        close(client_sockfd);
        return -1;
    }
    printf("Connect to server(%s:%d) successfully\n", ip, port);
    return client_sockfd;
}
Ejemplo n.º 6
0
 iSocket( std::string const &name ) : name_( name )
 {
   PrepareSocket();
 }
Ejemplo n.º 7
0
 iSocket( void )
 {
   PrepareSocket();
 }
Ejemplo n.º 8
0
/**
 * note the serialized block's size is different from others, it has tail info.
 * exchange merger is at the end of one segment of plan, so it's the "stage_src"
 * for this stage
 */
bool ExchangeMerger::Open(const PartitionOffset& partition_offset) {
  unsigned long long int start = curtick();
  RegisterExpandedThreadToAllBarriers();
  if (TryEntryIntoSerializedSection()) {  // first arrived thread dose
    exhausted_lowers = 0;
    this->partition_offset_ = partition_offset;
    lower_num_ = state_.lower_id_list_.size();
    socket_fd_lower_list_ = new int[lower_num_];
    for (int i = 0; i < lower_num_; ++i) {
      socket_fd_lower_list_[i] = -1;
    }
    // buffer all deserialized blocks come from every socket
    all_merged_block_buffer_ = new BlockStreamBuffer(
        state_.block_size_, BUFFER_SIZE_IN_EXCHANGE, state_.schema_);
    ExpanderTracker::getInstance()->addNewStageEndpoint(
        pthread_self(),
        LocalStageEndPoint(stage_src, "Exchange", all_merged_block_buffer_));

    // if one of block_for_socket is full, it will be deserialized into
    // block_for_deserialization and sended to all_merged_data_buffer
    block_for_deserialization =
        BlockStreamBase::createBlock(state_.schema_, state_.block_size_);

    // store block for each socket and the received block is serialized.
    block_for_socket_ = new BlockContainer* [lower_num_];
    for (unsigned i = 0; i < lower_num_; ++i) {
      block_for_socket_[i] = new BlockContainer(
          block_for_deserialization->getSerializedBlockSize());
    }
    if (PrepareSocket() == false) return false;
    if (SetSocketNonBlocking(sock_fd_) == false) {
      return false;
    }

    LOG(INFO) << "exchange_id = " << state_.exchange_id_
              << " partition_offset = " << partition_offset
              << " Open: exhausted lower senders num = " << exhausted_lowers
              << " lower sender num = " << lower_num_ << std::endl;

    if (RegisterExchange() == false) {
      LOG(ERROR) << "Register Exchange with ID = " << state_.exchange_id_
                 << " fails!" << std::endl;
    }

    if (IsMaster()) {
      /*  According to a bug reported by dsc, the master exchange upper should
       * check whether other uppers have registered to exchangeTracker.
       * Otherwise, the lower may fail to connect to the exchangeTracker of some
       * uppers when the lower nodes receive the exchange lower, as some uppers
       *  have not register the exchange_id to the exchangeTracker.
       */
      LOG(INFO) << " exchange_id = " << state_.exchange_id_
                << " partition_offset = " << partition_offset
                << "Synchronizing...." << std::endl;
      IsOtherMergersRegistered();
      LOG(INFO) << " exchange_id = " << state_.exchange_id_
                << " partition_offset = " << partition_offset
                << " Synchronized! Then serialize and send its next segment "
                   "plan to all its lower senders" << std::endl;
      if (SerializeAndSendPlan() == false) return false;
    }
    if (CreateReceiverThread() == false) {
      return false;
    }
    CreatePerformanceInfo();
  }
  /// A synchronization barrier, in case of multiple expanded threads
  BarrierArrive();
  return true;
}