/* ************************************************** */
int get_param_time(char *value, uint64_t *time) {
    uint64_t t1, t2;
    char *endptr;
    
    /* Random? */
    if (strcmp(value, "random") == 0) {
        *time = get_random_time();
        return 0;
    }
    
    /* skip space */
    while (isspace(*value))
        value++;

    /* decode integer */
    t1 = strtoll(value, &endptr, 10);
    if (value == endptr)
        goto missing;
    value = endptr;

    /* skip space */
    while (isspace(*value))
        value++;

    /* decode unit */
    if (!strncmp(value, "s", 1)) {
        t1 = t1 * 1000000000; value += 1;
    } else if (!strncmp(value, "ms", 2)) {
        t1 = t1 * 1000000;    value += 2;
    } else if (!strncmp(value, "us", 2)) {
        t1 = t1 * 1000; value += 2;
    } else if (!strncmp(value, "ns", 2)) {
        value += 2;
    }
    
    /* skip space */
    while (isspace(*value))
        value++;

    /* finished? */
    if (*value == '\0') {
        *time = t1;
        return 0;
    }

    /* range string */
    if (strncmp(value, "..", 2))
        goto garbage;
    value += 2;

    /* skip space */
    while (isspace(*value))
        value++;

    /* decode integer */
    t2 = strtoll(value, &endptr, 10);
    if (value == endptr) {
    missing:
        fprintf(stderr, "get_param_time(): missing time value ('%s')\n", value);
        return -1;
    }
    value = endptr;

    /* skip space */
    while (isspace(*value))
	value++;

    /* decode unit */
    if (!strncmp(value, "s", 1)) {
        t2 = t2 * 1000000000; value += 1;
    }else if (!strncmp(value, "ms", 2)) {
        t2 = t2 * 1000000;    value += 2;
    } else if (!strncmp(value, "us", 2)) {
        t2 = t2 * 1000;       value += 2;
    } else if (!strncmp(value, "ns", 2)) {
        value += 2;
    }

    /* skip space */
    while (isspace(*value))
        value++;

    /* garbage? */
    if (*value != '\0') {
    garbage:
        fprintf(stderr, "get_param_time(): garbage at end of time\n");
        return -1;
    }

    /* Generate random value */
    if (t1 > t2) {
        fprintf(stderr, "get_param_time(): time range must be min..max\n");
        return -1;
    }
    *time = t1 + (t2 - t1) * get_random_double();
    return 0;
}
Ejemplo n.º 2
0
static enum vclient_error_code_e begin_process(int socket_fd_remote, vclients_types_t vclient_type)
{
    vclient_error_code_t err_code = VCLIENT_SUCCESS;
    send_client_type_data_t send_client_type_data = {
        .header = "typ",
        .vclient_type = vclient_type
    };

    /* Send type of client */
    if (-1 == vclient_socket_send(socket_fd_remote,
                                  &send_client_type_data,
                                  sizeof(send_client_type_data)))
    {
        err_code = VCLIENT_SEND_IPC_SOCKET_ERROR;
        goto error;
    }
    VCLIENT_LOG_LOG("Type 'VClient' has been successfully sent to GateWay\n");

    init_random();

    while (1)
    {
        char header_info_vector_data_request[4];
        int ind;

        VCLIENT_LOG_LOG("Wait new commands from GateWay...\n");
        fflush(stdout);
        /* Receive command from GateWay */
        if (-1 == vclient_socket_receive(socket_fd_remote,
                                         header_info_vector_data_request,
                                         sizeof(header_info_vector_data_request)))
        {
            err_code = VCLIENT_RECV_IPC_SOCKET_ERROR;
            goto error;
        }

        /* If command if "end" then finish processing */
        if (!strcmp(header_info_vector_data_request, "end"))
        {
            VCLIENT_LOG_LOG("Received command 'end' from GateWay. Finish processing and close remote connection\n");
            break;
        }
        else
        {
            if (strcmp(header_info_vector_data_request, "vec"))
            {
                VCLIENT_LOG_LOG("Received unknown command from GateWay. Exit with error\n");
                err_code = VCLIENT_RECV_WRONG_DATA_FORMAT_ERROR;
                goto error;
            }
        }

        VCLIENT_LOG_LOG("*****************\n");
        VCLIENT_LOG_LOG("Received command 'vec' with request of vector\n");

        for (ind = 0; ind < VECTOR_SIZE; ind++)
        {
            g_vector_index[ind] = ind;
        }

        g_vector_ind_size = VECTOR_SIZE;

        while (g_vector_ind_size > 0)
        {
            int vector_index;
            send_vector_data_t send_vector_data = {
                .header = "vec"
            };

            vector_index = get_vector_index();

            send_vector_data.vector = (vector_fmt_t) {
                .pos = vector_index,
                .value = g_vector[vector_index]
            };

            VCLIENT_LOG_LOG("Data for sending was configured. HEAD = %s, POS = %d, VAL = %d\n. Wait for sending...\n",
                   send_vector_data.header,
                   send_vector_data.vector.pos,
                   send_vector_data.vector.value);

            usleep(get_random_time() * MICROSECONDS_IN_MILLISECONDS);

            /* Send vector value */
            if (-1 == vclient_socket_send(socket_fd_remote, &send_vector_data, sizeof(send_vector_data)))
            {
                VCLIENT_COMMON_ASSERT(0);
            }
            VCLIENT_LOG_LOG("Data has been successfully sent\n");

            --g_vector_ind_size;
        }

        VCLIENT_LOG_LOG("\nAll elements of vector was successfully sent\n");

    }

    error:

    VCLIENT_LOG_LOG("Finishing processing of 'VClient'\n");

    return err_code;
}

int main(int const argc, char const *argv[])
{
    char *vector_fp = NULL;
    size_t vector_fp_len;
    vclient_error_code_t err_code = VCLIENT_SUCCESS;
    int i;

    if (4 != argc)
    {
        return VCLIENT_WRONG_NUMBER_CLI_PARAMS_ERROR;
    }

    err_code = vclient_log_init_log(basename((char *)argv[3]));
    if (VCLIENT_SUCCESS != err_code)
    {
        goto error_log;
    }

    VCLIENT_LOG_LOG("VClient parameters:\n");

    for (i = 0; i < argc; i++)
    {
        VCLIENT_LOG_LOG("argv[%d] = %s\n", i, argv[i]);
    }

    vector_fp_len = strlen(argv[1]);
    VCLIENT_COMMON_MEM_ALLOC(vector_fp, (vector_fp_len + 1) * sizeof(char));
    strcpy(vector_fp, argv[1]);

    VCLIENT_LOG_LOG("Try reading vector from the file path '%s'...\n", vector_fp);
    err_code = read_vector_from_file(vector_fp);
    VCLIENT_COMMON_DEALLOC_MEM(vector_fp);
    if (VCLIENT_SUCCESS == err_code)
    {
        int socket_fd_remote;

        VCLIENT_LOG_LOG("The next vector has been read:\n");
        print_vector();

        VCLIENT_LOG_LOG("Try to create remote socket for connection with RTS GateWay...\n");
        socket_fd_remote = vclient_socket_create_socket();
        if (-1 == socket_fd_remote)
        {
            err_code = VCLIENT_CREATE_IPC_SOCKET_ERROR;
            goto error;
        }
        VCLIENT_LOG_LOG("Socket for connection with GateWay has been created. SOCKET_FD = %d\n", socket_fd_remote);

        VCLIENT_LOG_LOG("Try to connect to remote RTS GateWay...\n");
        if (-1 == vclient_socket_connect_socket(socket_fd_remote))
        {
            err_code = VCLIENT_CONNECT_IPC_SOCKET_ERROR;
            goto error;
        }
        VCLIENT_LOG_LOG("VClient has been successfully connected to remote GateWay server. SOCKET_FD = %d\n", socket_fd_remote);

        VCLIENT_LOG_LOG("Begin processing...\n");
        err_code = begin_process(socket_fd_remote, (vclients_types_t)atoi(argv[2]));

        vclient_socket_close_socket(socket_fd_remote);
        VCLIENT_LOG_LOG("Socket for remote connection with GateWay has just been closed. SOCKET_FD = %d\n", socket_fd_remote);
    }

    error:

    VCLIENT_LOG_LOG("Finishing 'VClient'\n");

    error_log:

    vclient_log_deinit_log();

    return err_code;
}