Esempio n. 1
0
int
callback_max_test()
{
    int in, out, *comp, finalComp, retResult, answer, i;
    grpc_object_handle_t_np objectHandle;
    grpc_error_t ret;
    int rVal = 1;
    int handleInitialized = 0;

    comp = &callback_max_comp_value;
    finalComp = 0;
    retResult = 1;

    ret = grpc_object_handle_default_np(
        &objectHandle, MODULE_NAME "/callback_max_test");
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_object_handle_default_np() error. (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }
    handleInitialized = 1;

    ret = grpc_invoke_np(&objectHandle, "initialize");
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_invoke_np() error (1). (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }

    /* Compute  (1 + 2 + ... + 32) * 2 */
    answer = 0;
    for (i = 1; i <= 32; i++) {
        answer += i;
    }
    answer *= 2;

    *comp = 0;
    in = 1;
    out = 0;

    ret = grpc_invoke_np(&objectHandle, "max_method2x",
        in, &out, callback_max_start, callback_max_get_func,
        callback_max_put_func, callback_max_end);
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_invoke_np() error (2). (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }

    finalComp = *comp;

    if (out != 1) {
        retResult = 0;
    }

    if (finalComp != answer) {
        retResult = 0;
    }

    if (verbose) {
        printf("\nout = %d, answer = %d, comp = %d ",
            out, answer, finalComp);
    }

    out = 0;

    ret = grpc_invoke_np(&objectHandle, "get_result", &out);
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_invoke_np() error (3). (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }

    if (out != 1) {
        retResult = 0;
    }

    if (verbose) {
        printf("\nresult = %d, ret = %d ", out, retResult);
    }

    /* Compute  (1 + 2 + ... + 32) * 4 */
    answer = 0;
    for (i = 1; i <= 32; i++) {
        answer += i;
    }
    answer *= 4;

    *comp = 0;
    in = 1;
    out = 0;

    ret = grpc_invoke_np(&objectHandle, "max_method4x",
        in, &out, callback_max_start, callback_max_get_func,
        callback_max_put_func, callback_max_end);
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_invoke_np() error (4). (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }

    finalComp = *comp;

    if (out != 1) {
        retResult = 0;
    }

    if (finalComp != answer) {
        retResult = 0;
    }

    if (verbose) {
        printf("\nout = %d, answer = %d, comp = %d ",
            out, answer, finalComp);
    }

    out = 0;

    ret = grpc_invoke_np(&objectHandle, "get_result", &out);
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_invoke_np() error (5). (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }

    if (out != 1) {
        retResult = 0;
    }

    if (verbose) {
        printf("\nresult = %d, ret = %d ", out, retResult);
    }

    ret = grpc_invoke_np(&objectHandle, "finalize");
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_invoke_np() error (6). (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }

    if (out != 1) {
        retResult = 0;
    }

    if (verbose) {
        printf("\nfinal ret = %d ", retResult);
    }

finalize:
    if (handleInitialized != 0) {
        ret = grpc_object_handle_destruct_np(&objectHandle);
        if (ret != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_object_handle_destruct_np() error. (%s)\n",
                grpc_error_string(ret));
            rVal = 0;
        }
    }

    if (retResult == 0) {
        rVal = 0;
    }

    return rVal;
}
Esempio n. 2
0
int
main(int argc, char *argv[])
{
    grpc_object_handle_t_np *handles;
    grpc_sessionid_t *ids = NULL;
    grpc_error_t result = GRPC_NO_ERROR;
    char **hosts = NULL;
    double pi;
    long *count = NULL;
    long times, sum;
    int n;
    int i, j;

    if (argc < 3){
        fprintf(stderr, "USAGE: %s TIMES HOSTNAME\n", argv[0]);
        fprintf(stderr, "USAGE: %s TIMES HOSTNAME1 HOSTANME2...\n", argv[0]);
        exit(2);
    }

    n = argc - 2;
    hosts = (char **)malloc(sizeof(char *) * n);
    if (hosts == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage for hostname.\n");
        exit(2);
    }
    for (i = 2, j = 0; j < n; i++, j++) {
        hosts[j] = NULL;
        hosts[j] = strdup(argv[i]);
        if (*hosts == NULL) {
            fprintf(stderr,
                 "strdup: Can't allocate the storage for hostname.\n");
            exit(2);
        }
    }

    times = atol(argv[1]) / n;

    handles = (grpc_object_handle_t_np *)malloc(
        sizeof(grpc_object_handle_t_np) * n);
    if (handles == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage for handles.\n");
        exit(2);
    }

    count = (long *)malloc(sizeof(long) * n);
    if (count == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage.\n");
        exit(2);
    }

    ids = (grpc_sessionid_t *)malloc(sizeof(grpc_sessionid_t) * n);
    if (ids == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage.\n");
        exit(2);
    }

    /* Initialize */
    result = grpc_initialize(config_file);
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_initialize() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Initialize Object handles */
    for (i = 0; i < n; i++) {
        result = grpc_object_handle_init_np(&handles[i], hosts[i],
            func_name);
        if (result != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_object_handle_init_np() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }      
    }

    /* Asynchronous call */
    for (i = 0; i < n; i++) {
        result = grpc_invoke_async_np(&handles[i], "pi_trial",
            &ids[i], i, times, &count[i]);
        if (result != GRPC_NO_ERROR){
            fprintf(stderr, "grpc_invoke_async_np() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }
    }

    /* wait for session */
    result = grpc_wait_all();
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_wait_all() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Destruct Function handles */
    for (i = 0; i < n; i++) {
        result = grpc_object_handle_destruct_np(&handles[i]);
        if (result != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_object_handle_destruct_np() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }
    }

    /* Compute and display pi. */
    for (i = 0, sum = 0; i < n; i++) {
        sum += count[i];
    }
    pi = 4.0 * (sum / ((double) times * n));
    printf("PI = %f\n", pi);

    /* Finalize */
    result = grpc_finalize();
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_finalize() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    for (i = 0; i < n; i++) {
        free(hosts[i]);
    }
    free(hosts);
    free(handles);
    free(count);
    free(ids);

    return 0;
}