예제 #1
0
void* comm_send_thread_start( void* data ) {
  thread_t* my_info = reinterpret_cast<thread_t*>(data);
  msg_t outgoing;
  int error = 0;

  // Send loop
  // Communication hasn't been stopped
  while ( my_info->state == T_STATE_ACTIVE ) {
    comm_send_q.wait_and_pop( outgoing );
    
    if ( outgoing.msg_type == MSG_T_TERMINATE ) {
      break;
    }
    
    contact_list_t::iterator contact = comm_contacts.find( outgoing.dest_ID );
    if ( contact == comm_contacts.end() ) {
      cout << "comm_send_thread_start Unknown destination ID " << outgoing.dest_ID << endl;
      continue;
    }
    
    comm_print_send_msg( outgoing );
    
    error = ::send( contact->second.sockfd, &outgoing, sizeof(outgoing), 0 );
    if ( error == -1 ) {
      cout << "comm_send_thread_start Couldn't send message:\n\t";
      comm_print_send_msg( outgoing );
    }
  }

  return NULL;
}
예제 #2
0
msg_t comm_recv() {
  msg_t temp;

  comm_recv_q.wait_and_pop( temp );

  return temp;
}
예제 #3
0
void* comm_send_thread_fun( void* data ) {
  msg_t outgoing;
  int error = 0;

  // Send loop
  // Communication hasn't been stopped
  while ( comm_send_thread.state == T_STATE_ACTIVE ) {
    comm_send_q.wait_and_pop( outgoing );

    if ( outgoing.header == MSG_T_HALT ) {
      break;
    }
    
    if ( !comm_up && (outgoing.header != MSG_T_UNREACHABLE) ) {
      cout << "comm_send_thread_fun Comms are down, message not sent" << endl;
      continue;
    }

    // Check to make sure outgoing destination is a known location and
    // a socket is open for sending
    contact_list_t::iterator contact = comm_contacts.find( outgoing.dest_id );
    if ( contact == comm_contacts.end() ) {
      cout << "comm_send_thread_fun Unknown destination ID " << outgoing.dest_id << endl;
      continue;
    }

    else if ( contact->second.sockfd < 0 ) {
      cout << "comm_send_thread_fun No open connection to destination ID " << outgoing.dest_id << endl;
      continue;
    }

    comm_print_send_msg( outgoing );

    if ( contact->second.type == SOCK_STREAM ) {
      error = ::send( contact->second.sockfd, &outgoing, sizeof(outgoing), 0 );
      if ( error == -1 ) {
        cout << "comm_send_thread_fun Couldn't send tcp message" << endl;
      }
    }
    
    else if ( contact->second.type == SOCK_DGRAM ) {
      error = ::sendto( contact->second.sockfd, &outgoing, sizeof(outgoing), 0, 
        contact->second.server_info->ai_addr, contact->second.server_info->ai_addrlen );
      if ( error == -1 ) {
        cout << "comm_send_thread_fun Couldn't send udp message" << endl;
      }
    }
    
    else {
      cout << "comm_send_thread_fun Unknown socket type " << contact->second.type << endl;
    }
  }

  return NULL;
}