Exemple #1
0
int VSTest::init() {
	char msg[256];

	port_num = var_server_get_port();
	hostest = var_server_get_hostname();

	memset(&bob, '\0', sizeof(TCDevice));

	bob.hostname = const_cast<char*>(hostest);
	bob.port = port_num;

	//std::cout << bob.hostname << bob.port << std::endl;

	bob.disable_handshaking = TC_COMM_TRUE;
	bob.disabled = TC_COMM_FALSE;

	tc_connect(&bob);

	if ( tc_isValid (&bob) ) {
		printf ("connection is valid\n");
	} else {
		printf ("connection is NOT valid\n");
	}

	// default cycle rate
	cycle_rate = 0.01;
	sprintf(msg,"trick.var_cycle(%.2f)\n", cycle_rate);
	vs_write(msg);	

	return (0);
}
Exemple #2
0
/** @par Detailed Design: */
int Trick::MonteCarlo::shutdown() {
    /** <ul><li> If this is a slave, run the shutdown jobs. */
    if (enabled && is_slave()) {
        connection_device.port = master_port;
        if (tc_connect(&connection_device) == TC_SUCCESS) {
            int exit_status = MonteRun::COMPLETE;
            if (verbosity >= ALL) {
                message_publish(MSG_INFO, "Monte [%s:%d] Sending run exit status to master: %d\n",
                                machine_name.c_str(), slave_id, exit_status) ;
            }
            int id = htonl(slave_id);
            tc_write(&connection_device, (char*)&id, (int)sizeof(id));
            exit_status = htonl(exit_status);
            tc_write(&connection_device, (char*)&exit_status, (int)sizeof(exit_status));
            run_queue(&slave_post_queue, "in slave_post queue");
            tc_disconnect(&connection_device);
        } else {
            if (verbosity >= ERROR)
                message_publish(
                  MSG_ERROR,
                  "Monte [%s:%d] Failed to connect to master.\n",
                  machine_name.c_str(), slave_id);
        }
    }
    return 0;
}
Exemple #3
0
int SatGraphicsComm::connect() {

    int ret, num_attempts;

    system(syscmd);
    std::cout << "SatGraphicsComm: Starting graphics server using: \"" << syscmd << "\"" << std::endl;

    tc_error( &connection, 0);

    num_attempts = 0;
    while(1) {
        ret = tc_connect( &connection);
        if  (ret == TC_SUCCESS) {
            std::cerr << "SatGraphicsComm: CONNECT." << std::endl;
            break;
        } else {
            num_attempts ++;
            if ( num_attempts == 30 ) {
                std::cerr << "SatGraphicsComm: Couldn't connect to graphics server." << std::endl;
                return 1;
            }
        }
        sleep(1);
    }

    return 0;
}
Exemple #4
0
TEST_F( TCConnectTest, testNullHostname ) { 

   device->hostname = NULL;

   int connect_status = tc_connect( device );

   EXPECT_EQ( connect_status, TC_CONN_DISABLED );
}
Exemple #5
0
TEST_F( TCConnectTest, testDisabledDevice ) {

   device->disabled = TC_COMM_TRUE;

   int connect_status = tc_connect( device );

   EXPECT_EQ( connect_status, TC_CONN_DISABLED );

}
Exemple #6
0
int main(int argc, char *argv[], char *envp[])
{
	tc_init();
	enum tc_opt_mode mode =
		tc_opt_init(&tc.opt, argc, argv, envp);

	/*tc_validate();*/

	int rc = 0;
	switch (mode) {
	case TC_OPT_USAGE:
		tc_opt_usage();
		break;
	case TC_OPT_VERSION:
		tc_opt_version();
		break;
	case TC_OPT_WAL_CAT:
		rc = tc_store_cat();
		break;
#if 0
	case TC_OPT_RPL:
		tc_connect();
		rc = tc_store_remote();
		break;
	case TC_OPT_WAL_PLAY:
		tc_connect();
		rc = tc_store_play();
		break;
#endif
	case TC_OPT_CMD:
		tc_connect();
		rc = tc_clicmdv();
		break;
	case TC_OPT_INTERACTIVE:
		tc_connect();
		tc_motd();
		rc = tc_cli();
		break;
	}

	tc_shutdown();
	return rc;
}
Exemple #7
0
int main(int argc, char *argv[])
{
	tc_init();

	int rc;
	enum tc_opt_mode mode = tc_opt_init(&tc.opt, argc, argv);
	switch (mode) {
	case TC_OPT_USAGE:
		tc_opt_usage();
		break;
	case TC_OPT_RPL:
		tc_connect();
		rc = tc_wal_remote();
		break;
	case TC_OPT_WAL_CAT:
		rc = tc_wal_cat();
		break;
	case TC_OPT_WAL_PLAY:
		tc_connect();
		rc = tc_wal_play();
		break;
	case TC_OPT_CMD:
		tc_connect();
		tc_connect_admin();
		rc = tc_cli_cmdv();
		break;
	case TC_OPT_INTERACTIVE:
		tc_connect();
		tc_connect_admin();
		rc = tc_cli();
		break;
	}

	tc_free();
	return rc;
}
void Trick::MonteCarlo::dispatch_run_to_slave(MonteRun *in_run, MonteSlave *in_slave) {
    if (in_slave && in_run) {
        current_run = in_run->id;
        if (prepare_run(in_run) == -1) {
            return;
        }
        in_slave->state = MonteSlave::RUNNING;
        connection_device.hostname = (char*)in_slave->machine_name.c_str();
        connection_device.port = in_slave->port;
        if (tc_connect(&connection_device) == TC_SUCCESS) {
            std::stringstream buffer_stream;
            buffer_stream << run_directory << "/RUN_" << std::setw(5) << std::setfill('0') << in_run->id;
            std::string buffer = "";
            for (std::vector<std::string>::size_type j = 0; j < in_run->variables.size(); ++j) {
                buffer += in_run->variables[j] + "\n";
            }
            buffer += std::string("trick.set_output_dir(\"") + buffer_stream.str() + std::string("\");\n");
            buffer_stream.str("");
            buffer_stream << in_run->id ;
            buffer += std::string("trick.mc_set_current_run(") + buffer_stream.str() + std::string(");\n");

            if (verbosity >= INFORMATIONAL) {
                message_publish(MSG_INFO, "Monte [Master] Dispatching run %d to %s:%d.\n",
		     in_run->id, in_slave->machine_name.c_str(), in_slave->id) ;
            }
            int command = htonl(MonteSlave::PROCESS_RUN);
            tc_write(&connection_device, (char *)&command, (int)sizeof(command));
            int num_bytes = htonl(buffer.length());
            tc_write(&connection_device, (char*)&num_bytes, (int)sizeof(num_bytes));
            tc_write(&connection_device, (char*)buffer.c_str(), (int)buffer.length());
            tc_disconnect(&connection_device);

            ++in_slave->num_dispatches;
            in_slave->current_run = in_run;

            struct timeval time_val;
            gettimeofday(&time_val, NULL);
            in_run->start_time = time_val.tv_sec + (double)time_val.tv_usec / 1000000;
            ++in_run->num_tries;
        } else {
            in_slave->state = Trick::MonteSlave::DISCONNECTED;
            if (verbosity >= ERROR) {
                message_publish(MSG_ERROR, "Monte [Master] Lost connection to %s:%d while dispatching run.\n",
                                in_slave->machine_name.c_str(), in_slave->id) ;
            }
        }
    }
}
Exemple #9
0
int main( int narg, char** args ) {

  DataFiles df ;
  DataFiles headers ;
  TCDevice connection ;
  int isAsciiData ;
  int ret ;
  char* file_name ;
  char* msg ;

  /* Zero out device */
  memset(&connection, '\0', sizeof(TCDevice));

  /* Initialize some stuff  */
  file_name = ( char* ) malloc ( 256 ) ;
  msg       = ( char* ) malloc ( 1024 ) ;
  df.n_files = 0 ;

  /* Parse args */
  if ( narg != 3  ) {
    fprintf(stderr, "USAGE: dr_client <host> <port> \n");
    exit(-1);
  }
  connection.hostname = (char*) malloc ( strlen(args[1]) + 1 ) ;
  strcpy(connection.hostname, args[1]);
  connection.port = atoi(args[2]) ;

  /* Connect to server */
  ret = tc_connect(&connection);
  if ( ret != TC_SUCCESS ) {
     printf("ERROR: Couldn't connect to server on host \"%s\" port %d. \n",
             connection.hostname, connection.port);
     exit(-1);
  }

  /* Read data from server and dump data */
  dump_data( &df, &connection, &file_name, &msg);

  return 0 ;
}
Exemple #10
0
int cannon_init_graphics(
      CANNON* C )
{
        int ret, num_attempts ;

        /* Launch graphics server */
        system("cd $HOME/trick_models/cannon/graphics ; cannon &");

        /* Initialize clien't connection to server */
        C->connection.port = 9000 ;
        C->connection.hostname = (char*) malloc( 16 ) ;
        strcpy( C->connection.hostname, "localhost");

        /* Shutup status messages */
        tc_error( &C->connection, 0 ) ;

        /* Client connect to server : try for 5 seconds */
        num_attempts = 0 ;
        while ( 1 ) {

                ret = tc_connect( &C->connection ) ;
                if ( ret == TC_SUCCESS ) {
                        break ;
                } else {
                        num_attempts++ ;
                        if ( num_attempts == 500000 ) {
                                fprintf(stderr, "Couldn't connect to "
                                                 "server...\n");
                                exit(-1);
                        }
                        RELEASE_1(); /* Pause a microsecond */
                }
        }

        return 0 ; 
}
Exemple #11
0
int tc_multiconnect(TCDevice * dev_ptr, char *my_tag, char *other_tag, TrickErrorHndlr * not_used)
{
    int status;
    int num_try = 0;
    TCDevice *server_device;
    char name[80];

    int size;
    char *cptr;

    /* Multicast vars */
    struct sockaddr_in addr;
    int fd, nbytes;
    struct ip_mreq mreq;
    BC_INFO bc_info, *bc_copy;
    SEND_ME read_me;
    int i_am_client;
    int value;
    int num_attempts = 10;
    int found_conn;
#ifdef __WIN32__
    HANDLE thread;
    DWORD threadId;
    int curr_pid;
    WSADATA wsaData;
#else
    pthread_t thread;
    pid_t curr_pid;
#endif

    (void) not_used;                   /* unused */

    if (!dev_ptr) {
        TrickErrorHndlr *temp_error_hndlr = NULL;
        trick_error_report(temp_error_hndlr, TRICK_ERROR_ALERT, __FILE__, __LINE__, "Trying to connect a NULL device");
        return (-1);
    }

    if (dev_ptr->disabled != 0) {
        trick_error_report(dev_ptr->error_handler,
                           TRICK_ERROR_ALERT, __FILE__, __LINE__,
                           "%s| cannot connect disabled device %s\n", my_tag, other_tag);
        return (TC_SUCCESS);
    }

    trick_error_report(dev_ptr->error_handler,
                       TRICK_ERROR_ADVISORY, __FILE__, __LINE__,
                       "%s| multiconnecting to %s\n", my_tag, other_tag);

    /* Create a TrickComm listen device Determine port for listen device */
    server_device = (TCDevice *) malloc(sizeof(TCDevice));
    memset((void *) server_device, '\0', sizeof(TCDevice));
    server_device->error_handler = dev_ptr->error_handler;

    server_device->port = 0;
    if ((status = tc_init(server_device)) != TC_SUCCESS) {
        trick_error_report(server_device->error_handler,
                           TRICK_ERROR_ALERT, __FILE__, __LINE__, "could not open listen device!\n");
        return (status);
    }

    /* 
     * Initialize Broadcast Info Structure
     */
    gethostname(name, (size_t) 80);

    strcpy(bc_info.send_me.addr, name);
    bc_info.send_me.port = server_device->port;
    bc_info.send_me.pid = curr_pid = getpid();
    bc_info.send_me.conn_initiated = 0;

    if (my_tag != (char *) NULL) {
        strncpy(bc_info.send_me.my_tag, my_tag, (size_t) TC_TAG_LENGTH);
        bc_info.send_me.my_tag[TC_TAG_LENGTH - 1] = '\0';
    } else {
        bc_info.send_me.my_tag[0] = '\0';
    }

    if (other_tag != (char *) NULL) {
        strncpy(bc_info.send_me.other_tag, other_tag, (size_t) TC_TAG_LENGTH);
        bc_info.send_me.other_tag[TC_TAG_LENGTH - 1] = '\0';
    } else {
        bc_info.send_me.other_tag[0] = '\0';
    }

    bc_info.device = dev_ptr;

#ifdef __WIN32__
    /* Initiate use of the Windows socket DLL */
    if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR) {
        perror("tc_multiconnect: WSAStartup");
        WSACleanup();
        return (TC_COULD_NOT_ACCEPT);
    }
#endif

    /* Create the broadcast socket */
    if ((bc_info.fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("tc_multiconnect socket");
    }

    /* Set up destination address */
    memset(&bc_info.addr, 0, sizeof(bc_info.addr));
    bc_info.addr.sin_family = AF_INET;
    bc_info.addr.sin_addr.s_addr = inet_addr(TC_MULT_GROUP);
    bc_info.addr.sin_port = htons(TC_MULT_PORT);

    /* 
     * Create socket to listen for connections 
     */
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("tc_multiconnect socket");
    }

    value = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &value, (socklen_t) sizeof(value)) < 0) {
        perror("setsockopt: reuseaddr");
    }
#ifdef SO_REUSEPORT
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &value, sizeof(value)) < 0) {
        perror("setsockopt: reuseport");
    }
#endif

    /* Set up destination address */
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(TC_MULT_PORT);

    /* Bind to receive address */
    if (bind(fd, (struct sockaddr *) &addr, (socklen_t) sizeof(addr)) < 0) {
        perror("tc_multiconnect bind");
    }

    /* Use setsockopt() to request that the kernel join a multicast group */
    mreq.imr_multiaddr.s_addr = inet_addr(TC_MULT_GROUP);
    mreq.imr_interface.s_addr = htonl(INADDR_ANY);
    size = sizeof(mreq);
    cptr = (char *) &mreq;
    if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, cptr, (socklen_t) size) < 0) {
#ifdef __WIN32__
        TCHAR szError[100];     // Error message string

        wsprintf(szError, TEXT("setsockopt failed! Error: %d"), WSAGetLastError());
        MessageBox(NULL, szError, TEXT("Error"), MB_OK);
#endif
        perror("setsockopt: ip_add_membership");
    }

    /* Create thread that will continually broadcast connection info on multicast port for prospective clients. */
    /* make a copy of info to broadcast */
    bc_copy = (BC_INFO *) malloc(sizeof(BC_INFO));
    memcpy(bc_copy, &bc_info, sizeof(BC_INFO));
#ifdef __WIN32__
    thread = CreateThread(NULL, 0, tc_broadcast_conninfo, &bc_copy, 0, &threadId);
#else
    pthread_create(&thread, NULL, tc_broadcast_conninfo, bc_copy);
#endif

    /* Wait for other peers wanting to connect to me */
    i_am_client = 0;
    found_conn = 0;
    while (found_conn == 0) {

        do {
            nbytes = recvfrom(fd, &read_me, sizeof(SEND_ME), 0, NULL, NULL);
        } while ((nbytes == -1) && (tc_errno == TRICKCOMM_EINTR));

        if (nbytes == sizeof(read_me)) {

            /* 
             * Read information from peer
             */
            read_me.port = ntohl((uint32_t) read_me.port);
            read_me.pid = ntohl((uint32_t) read_me.pid);
            read_me.conn_initiated = ntohl((uint32_t) read_me.conn_initiated);

            /* May I connect with this peer? Rules: Don't connect to myself! Don't connect to someone with same tag as 
               myself Do connect to someone with same "other tag" */
            if (curr_pid != read_me.pid &&
                strcmp(bc_info.send_me.my_tag, read_me.my_tag) &&
                !strcmp(bc_info.send_me.other_tag, read_me.other_tag)) {

                /* Decide who will function as client Whoever has smaller pid will be the client If same pid, decide by 
                   a string compare */
                if (curr_pid < read_me.pid) {
                    i_am_client = 1;
                } else if (curr_pid == read_me.pid) {
                    i_am_client = (strcmp(name, read_me.addr) > 0) ? 1 : 0;
                }

                /* 
                 * Client Peer initiates the connection 
                 */
                if (i_am_client == 1) {

                    /* Save off addr and port to connect */
                    size = strlen(read_me.addr) + 1;
                    dev_ptr->hostname = (char *) malloc((size_t) size);
                    strcpy(dev_ptr->hostname, read_me.addr);
                    dev_ptr->port = read_me.port;

                    read_me.conn_initiated = 1;
                    read_me.port = htonl((uint32_t) read_me.port);
                    read_me.pid = htonl((uint32_t) read_me.pid);
                    read_me.conn_initiated = htonl((uint32_t) read_me.conn_initiated);
                    sendto(bc_info.fd, (char *) &read_me, sizeof(SEND_ME), 0,
                           (struct sockaddr *) &bc_info.addr, (socklen_t) sizeof(bc_info.addr));
                    found_conn = 1;
                }

            } else if (curr_pid == read_me.pid &&
                       !strcmp(bc_info.send_me.addr, read_me.addr) &&
                       !strcmp(bc_info.send_me.my_tag, read_me.my_tag) &&
                       !strcmp(bc_info.send_me.other_tag, read_me.other_tag) && read_me.conn_initiated == 1) {

                found_conn = 1;
            }
        }
    }

    if (other_tag != (char *) NULL) {
        strncpy(dev_ptr->client_tag, other_tag, (size_t) TC_TAG_LENGTH);
        dev_ptr->client_tag[TC_TAG_LENGTH - 1] = '\0';
    }

    if (i_am_client == 1) {

        /* Client connects */

        /* Disconnect unneeded listen device */
        tc_disconnect(server_device);
        free((void *) server_device);

        tc_set_blockio_timeout_limit(dev_ptr, MAX_BLOCKIO_TIMEOUT_LIMIT);
        status = 1;
        while ((status != TC_SUCCESS) && (num_try < num_attempts)) {
            status = tc_connect(dev_ptr);
#if __WIN32__
            Sleep(200);
#else
            usleep(200000);
#endif
            num_try++;
        }
    } else {

        /* 
         * Server accepts
         */
        status = 1;
        while (status != TC_SUCCESS && (num_try < num_attempts)) {
            if (tc_listen(server_device)) {
                status = tc_accept(server_device, dev_ptr);
                if (dev_ptr != NULL) {
                    tc_set_blockio_timeout_limit(dev_ptr, MAX_BLOCKIO_TIMEOUT_LIMIT);
                }
            }
            num_try++;
#if __WIN32__
            Sleep(200);
#else
            usleep(200000);
#endif
        }
        tc_disconnect(server_device);
        free((void *) server_device);
    }

    if (status != TC_SUCCESS) {
        trick_error_report(server_device->error_handler,
                           TRICK_ERROR_ALERT, __FILE__, __LINE__, "could not open connection!\n");
        return (status);
    }

    /* Shutdown the broadcast */
    shutdown(bc_info.fd, 2);

    /* Return the pointer to the TCDevice as the tc_id. */
    return (TC_SUCCESS);
}
Exemple #12
0
TEST_F( TCConnectTest, testNoDevice ) {

   int connect_status = tc_connect( NULL );

   EXPECT_EQ( connect_status, -1 );
}