Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
	if(argc != 3)
	{
		printf("Incorrect set of arguments\n");
		printf("Usage:\n./player <master-machine-name> <port-number>\n");
		exit(-1);
	}

	initPlayer(argv);	//this will start the player initializing the data structures and variables
	
	if(pthread_create(&left_thread,NULL,leftNeighborConn,NULL) !=0 )	{
		printf("Thread creation failed\n");
		exit(-1);
	}	
	
	#ifdef DEBUG
		printf("Left Thread created\n");	
	#endif
	
	setupNetwork();

	#ifdef DEBUG
		printPlayerInfo();	
		printf("Out here\n");
	#endif
	
	wait_for_message();

	#ifdef DEBUG
		printf("Exiting program\n");
	#endif

	return 0;
}
Ejemplo n.º 2
0
/* Fill tcpData array, return message length */
uint16_t waitTCPMessage_blocking()
{
  uint16_t len;
  wait_for_message("+IPD,",0);
  wait_for_message(",",0);
  len = getMessageLength(); 
  
  uint16_t t16;
  for(t16=0;t16 < len;t16++)
  {
    while(RingBuffer_GetCount(&Buffer) == 0);
    tcpData[t16] = RingBuffer_Remove(&Buffer);
  }

  wait_for_message("OK\r\n",10000);

  return len;
}
Ejemplo n.º 3
0
/*---------------------------------------------------------------------------*/
void send_TCPData(uint8_t* msg,uint8_t len)
{
  xprintf("AT+CIPSEND=0,%d\r\n",len);

  wait_for_message("> ",10000);

  xprintf("%s",msg);

  fail_if_not_ok();
}
Ejemplo n.º 4
0
void BasicTest::run(){
	if (sink_node){
		std::cout << "BasicTest: sink waiting for message" << std::endl;
		std::cout << wait_for_message()->to_string() << std::endl;
	} else {
		std::cout << "BasicTest: source sending message" << std::endl;
		send_message(new Basic_message("TEST MESSAGE PLEASE IGNORE"));
	}
	send_message(new Basic_message("KILL"));
	std::cout << "BasicTest: exiting" << std::endl;
}
Ejemplo n.º 5
0
void ParrotTest::run(){
	if (m_sink_node){
		std::cout << "ParrotTest: sink waiting for message" << std::endl;
		if (wait_for_message()->to_string() == "TEST"){
			std::cout << "ParrotTest: received message" << std::endl;
			move(Direction::FORWARD, 0.5, 1);
			while(!hasFinishedMoving()){}
			turn(180);
			while(!hasFinishedMoving()){}
		}
	} else {
		std::cout << "ParrotTest: source sending message" << std::endl;
		send_message(new Basic_message("TEST"));
	}
	send_message(new Basic_message("KILL"));
	std::cout << "ParrotTest: exiting" << std::endl;
	kill();
}
Ejemplo n.º 6
0
ANTStatus ant_tx_message(ANT_U8 ucLen, ANT_U8 *pucMesg)
{
    ANTStatus   status;

    /* Socket for sending HCI commands */
    int tx_socket = -1;

    int lockResult;

    ANT_FUNC_START();

    ANT_DEBUG_V("getting txLock in %s", __FUNCTION__);
    lockResult = pthread_mutex_lock(&txLock);
    if (lockResult)
    {
        ANT_ERROR("ant_tx_message, could not get txLock: %s", strerror(lockResult));
        return ANT_STATUS_FAILED;
    }

    ANT_DEBUG_V("got txLock in %s", __FUNCTION__);

    if(RADIO_STATUS_ENABLED != get_and_set_radio_status())
    {
        ANT_DEBUG_E("ant_tx_message, ANT not enabled - ABORTING. Radio status = %d",
                    radio_status);
        ANT_DEBUG_V("releasing txLock in %s", __FUNCTION__);
        pthread_mutex_unlock(&txLock);
        ANT_DEBUG_V("released txLock in %s", __FUNCTION__);
        return ANT_STATUS_FAILED_BT_NOT_INITIALIZED;
    }

    // Open socket
    tx_socket = ant_open_tx_transport();

    if(tx_socket < 0)
    {
        ANT_ERROR("Could not open Tx socket");
        ANT_DEBUG_V("releasing txLock in %s", __FUNCTION__);
        pthread_mutex_unlock(&txLock);
        ANT_DEBUG_V("released txLock in %s", __FUNCTION__);
        return ANT_STATUS_FAILED;
    }

    // Send HCI packet
    ANT_BOOL retryRx;
    ANT_BOOL retryTx;
    status = ANT_STATUS_FAILED;

    int MAX_RETRIES_WRITE_FAIL = 10;
    int MAX_RETRY_TIME_SECS = 10;
    int commandWriteFailRetries = 0;

    // Start New timed retry code:
    time_t startTime = time(NULL);
    time_t endTime = 0;
    if((time_t)-1 != startTime)
    {
        endTime = startTime + MAX_RETRY_TIME_SECS;
    }
    else
    {
        ANT_ERROR("failed to get current time");
    }

    do // while (retryTx)
    {
        retryTx = ANT_FALSE;

        if(ANT_STATUS_SUCCESS == write_data(pucMesg, ucLen))
        {
            do // while (retryRx)
            {
                retryRx = ANT_FALSE;

                if(ANT_TRUE == wait_for_message(tx_socket))
                {
                    ANT_DEBUG_D("New HCI data available, reading...");

                    status = get_command_complete_result(tx_socket);
                    switch (status)
                    {
                    case ANT_STATUS_NO_VALUE_AVAILABLE:
                    {
                        ANT_WARN("Did not get an expected response for write, trying again");
                        retryRx = ANT_TRUE;
                        break;
                    }
                    case ANT_STATUS_FAILED:
                    {
                        ANT_ERROR("Command Complete: ANT_STATUS_FAILED");
                        break;
                    }
                    case ANT_STATUS_COMMAND_WRITE_FAILED:
                    {
                        ANT_ERROR("Command Complete: ANT_STATUS_WRITE_FAILED");

                        if(++commandWriteFailRetries < MAX_RETRIES_WRITE_FAIL)
                        {
                            ANT_DEBUG_D("Retrying.  Retry count = %d",
                                        commandWriteFailRetries);

                            retryTx = ANT_TRUE;
                        }
                        else
                        {
                            ANT_ERROR("Aborting.  Retry count = %d.  Max retries = %d",
                                      commandWriteFailRetries, MAX_RETRIES_WRITE_FAIL);
                        }
                        break;
                    }
                    case ANT_STATUS_TRANSPORT_UNSPECIFIED_ERROR:
                    {
                        ANT_DEBUG_D("Command Complete: ANT_STATUS_UNSPECIFIED_ERROR");

                        // Give the chip a break before we try to resend data.
                        nanosleep((struct timespec[]) {
                            {
                                0, 50000000
                            }
                        }, NULL);

                        time_t currentTime = time(NULL);

                        if((time_t)-1 != currentTime)
                        {
                            if(currentTime < endTime)
                            {
                                ANT_DEBUG_V("Retrying. Current time = %d. "
                                            "End time = %d", (int)currentTime, (int)endTime);

                                retryTx = ANT_TRUE;
                            }
                            else
                            {
                                ANT_ERROR("Command Complete: ANT_STATUS_UNSPECIFIED_ERROR");
                                ANT_ERROR("Aborting. Current time = %d. End Time = %d",
                                          (int)currentTime, (int)endTime);
                            }
                        }
                        else
                        {
                            ANT_ERROR("Command Complete: failed to get current time");
                        }

                        break;
                    }
                    case ANT_STATUS_SUCCESS:
                    {
                        break;
                    }
                    default:
                    {
                        ANT_ERROR("Unhandled command complete status");
                        break;
                    }
                    }
                }
                else
                {
                    ANT_WARN("Command Complete: wait for message failed");
                }
            } while (retryRx);
Ejemplo n.º 7
0
int main_client_thread(void)
{
    int clone_err = 0;
    int clone_var;
    int stack_index;
    int i,j,k,s;
    int rc;
    int exit_rc = 0;
    int s0;                   /* control socket */
    int wait_status;
    int clients_created = 0;
    struct sembuf mysembuf1;
    struct sembuf mysembuf2;
    unsigned char control_buffer;

    struct timezone tz1;
    struct timeval  tv1;
    struct timezone tz2;
    struct timeval  tv2;

    /*
     * create the control socket used to talk to the server.
     */

    exit_rc = create_client_socket (&s0);
    if (exit_rc)
        goto out_main;
    /*
     * tell the server how many rooms and messages.
     */
    start_message.nrooms = nrooms;
    start_message.nmessages = nmessages;
    rc = send (s0, &start_message, sizeof(start_message), 0);
    if (rc != sizeof(start_message)) {
        exit_rc = errno;
        perror ("CLI: send() start_message ");
        goto out_main;
    }

    /*
     * create threads via __clone that will connect to the server thread.
     * loop creates enough clients to satisfy the user specified
     * number of rooms.
     */

    for (i = 0 ; i < nrooms ; i++) {
        printf ("Creating room number %u ...\n", i+1);
        for (k = 0 ; k < CLIENTS_PER_ROOM ; k++) {
            clone_var   = i*CLIENTS_PER_ROOM+k;
            stack_index = (clone_var*STACK_SIZE)+STACK_SIZE;

            exit_rc = create_client_socket (&s);
            if (exit_rc)
                goto out_main;

            ti[clone_var].snd_socket = s;
            ti[clone_var].rcv_socket = s;

            clone_err = __clone(&cli_snd,
                                &c_send_stack[stack_index],
                                CLONE_FLAGS,
                                (void*)clone_var);

            if (clone_err == -1) {
                exit_rc = errno;
                perror ("CLI: clone() ");
                goto out_main;
            }

            clients_created++;
            clone_err = __clone(&cli_rcv,
                                &c_receive_stack[stack_index],
                                CLONE_FLAGS,
                                (void*)clone_var);

            if (clone_err == -1) {
                exit_rc = errno;
                perror ("CLI: clone() ");
                goto out_main;
            }

            clients_created++;
        }
        printf ("%u connections so far.\n", (i+1)*CLIENTS_PER_ROOM);
    }

    /* wait for the server to let us start. */

    if (cdebug) printf ("CLI: waiting for CLIENT_START msg from srv\n");

    exit_rc = wait_for_message(s0, CLIENT_START);
    if (exit_rc)
        goto out_main;

    if (cdebug) printf ("CLI: rcved CLIENT_START msg from srvr\n");

    /* clear the sema4 to allow client threads to start together. */

    mysembuf1.sem_num = 0;
    mysembuf1.sem_op  = clients_created;
    mysembuf1.sem_flg = 0;

    mysembuf2.sem_num = 0;
    mysembuf2.sem_op  = 0;
    mysembuf2.sem_flg = 0;

    printf ("Running the test ...\n");

    /* get start time */

    rc = gettimeofday (&tv1, &tz1);
    if (rc) {
        exit_rc = errno;
        perror ("gettimeofday failed on tv1 ");
        goto out_main;
    }

    /* start all of the clients */

    rc = semop (clientsemall, &mysembuf1, 1);
    if (rc == -1) {
        exit_rc = errno;
        perror ("CLI: semop(clientsemall) failed ");
        goto out_main;
    }

    /* wait until all of the clients complete */

    rc = semop (c_donesem, &mysembuf2, 1);
    if (rc == -1) {
        exit_rc = errno;
        perror ("CLI: semop(c_donesem) failed ");
        goto out_main;
    }

    /* get the end time */

    rc = gettimeofday (&tv2, &tz2);
    if (rc) {
        exit_rc = rc;
        perror ("gettimeofday failed on tv2 ");
        goto out_main;
    }

    printf ("Test complete.\n\n");

    timersub(&tv2, &tv1, &tvr); /* tvr now contains result of tv2-tv1 */

    for (i = 0 ; i < nrooms * CLIENTS_PER_ROOM ; i++) {
        rc = waitpid (-1, &wait_status, __WCLONE);
        if (rc == -1) {
            exit_rc = errno;
            perror ("SRV: wait() ");
        }
        rc = waitpid (-1, &wait_status, __WCLONE);
        if (rc == -1) {
            exit_rc = errno;
            perror ("SRV: wait() ");
        }
    }

    for (i = 0 ; i < nrooms * CLIENTS_PER_ROOM ; i++) {
        close(ti[i].snd_socket);
    }

    /*
     * send a control character to the main_server_thread to let him know
     * we are done;
     */

    control_buffer = CLIENT_END;

    rc = send (s0, &control_buffer, 1, 0);
    if (rc != 1) {
        exit_rc = errno;
        perror ("CLI: send() CLIENT_END control byte ");
        goto out_main;
    }

out_main:

    close(s0);
    return(exit_rc);
}