Пример #1
0
int
main(void)
{
    const char *na_info_string = NULL;

    char target_addr_string[PATH_MAX], *p;
    FILE *na_config = NULL;
    struct snappy_lookup_args snappy_lookup_args;

    hg_class_t *hg_class;
    hg_context_t *hg_context;

    hg_return_t hg_ret;

    /* Get info string */
    na_info_string = getenv(HG_PORT_NAME);
    if (!na_info_string) {
        fprintf(stderr, HG_PORT_NAME " environment variable must be set");
        exit(0);
    }
    printf("Using %s\n", na_info_string);

    /* Initialize Mercury with the desired network abstraction class */
    hg_class = HG_Init(na_info_string, NA_FALSE);

    /* Create HG context */
    hg_context = HG_Context_create(hg_class);

    /* The connection string is generated after NA_Addr_self()/NA_Addr_to_string(),
     * we must get that string and pass it to  NA_Addr_lookup() */
    na_config = fopen(TEMP_DIRECTORY CONFIG_FILE_NAME, "r");
    if (!na_config) {
        fprintf(stderr, "Could not open config file from: %s\n",
                TEMP_DIRECTORY CONFIG_FILE_NAME);
        exit(0);
    }
    fgets(target_addr_string, PATH_MAX, na_config);
    p = strrchr(target_addr_string, '\n');
    if (p != NULL) *p = '\0';
    printf("Target address is: %s\n", target_addr_string);
    fclose(na_config);

    /* Look up target address */
    snappy_lookup_args.hg_class = hg_class;
    snappy_lookup_args.hg_context = hg_context;
    HG_Addr_lookup(hg_context, snappy_lookup_cb, &snappy_lookup_args,
            target_addr_string, HG_OP_ID_IGNORE);

    /* Poke progress engine and check for events */
    do {
        unsigned int actual_count = 0;
        do {
            hg_ret = HG_Trigger(hg_context, 0 /* timeout */,
                    1 /* max count */, &actual_count);
        } while ((hg_ret == HG_SUCCESS) && actual_count);

        /* Do not try to make progress anymore if we're done */
        if (snappy_compress_done_g) break;

        hg_ret = HG_Progress(hg_context, HG_MAX_IDLE_TIME);
    } while (hg_ret == HG_SUCCESS);

    /* Finalize */
    HG_Addr_free(hg_class, snappy_lookup_args.hg_target_addr);

    HG_Context_destroy(hg_context);
    HG_Finalize(hg_class);

    return EXIT_SUCCESS;
}
Пример #2
0
int
main(void)
{
    const char *na_info_string = NULL;

    na_class_t *na_class;
    na_context_t * na_context;
    char self_addr_string[PATH_MAX];
    na_addr_t self_addr;
    FILE *na_config = NULL;

    hg_class_t *hg_class;
    hg_context_t *hg_context;
    unsigned major;
    unsigned minor;
    unsigned patch;
    hg_return_t hg_ret;
    na_size_t self_addr_string_size = PATH_MAX;

    HG_Version_get(&major, &minor, &patch);

    printf("Server running mercury version %u.%u-%u\n",
        major, minor, patch);

    /* Get info string */
    /* bmi+tcp://localhost:port */
    na_info_string = getenv(HG_PORT_NAME);
    if (!na_info_string) {
        fprintf(stderr, HG_PORT_NAME " environment variable must be set, e.g.:\nMERCURY_PORT_NAME=\"tcp://127.0.0.1:22222\"\n");
        exit(0);
    }

    /* Initialize NA */
    na_class = NA_Initialize(na_info_string, NA_TRUE);

    /* Get self addr to tell client about */
    NA_Addr_self(na_class, &self_addr);
    NA_Addr_to_string(na_class, self_addr_string, &self_addr_string_size, self_addr);
    NA_Addr_free(na_class, self_addr);
    printf("Server address is: %s\n", self_addr_string);

    /* Write addr to a file */
    na_config = fopen(TEMP_DIRECTORY CONFIG_FILE_NAME, "w+");
    if (!na_config) {
        fprintf(stderr, "Could not open config file from: %s\n",
                TEMP_DIRECTORY CONFIG_FILE_NAME);
        exit(0);
    }
    fprintf(na_config, "%s\n", self_addr_string);
    fclose(na_config);

    /* Create NA context */
    na_context = NA_Context_create(na_class);

    /* Initialize Mercury with the desired network abstraction class */
    hg_class = HG_Init_na(na_class, na_context);

    /* Create HG context */
    hg_context = HG_Context_create(hg_class);

    /* Register RPC */
    snappy_compress_register(hg_class);

    /* Poke progress engine and check for events */
    do {
        unsigned int actual_count = 0;
        do {
            hg_ret = HG_Trigger(hg_context, 0 /* timeout */,
                    1 /* max count */, &actual_count);
        } while ((hg_ret == HG_SUCCESS) && actual_count);

        /* Do not try to make progress anymore if we're done */
        if (snappy_compress_done_target_g) break;

        hg_ret = HG_Progress(hg_context, HG_MAX_IDLE_TIME);

    } while (hg_ret == HG_SUCCESS);

    /* Finalize */
    HG_Context_destroy(hg_context);
    HG_Finalize(hg_class);

    NA_Context_destroy(na_class, na_context);
    NA_Finalize(na_class);

    return EXIT_SUCCESS;
}