コード例 #1
0
ファイル: lcm-who.c プロジェクト: PennPanda/libbot2
int main(int argc, char **argv)
{
    state_t *app = calloc(1, sizeof(state_t));
    app->lcmurl = strdup(get_default_lcm_url());

    if (0 != lcm_parse_url(app->lcmurl, &app->mc_addr, &app->mc_port)) {
        fprintf (stderr, "invalid URL [%s]\n", app->lcmurl);
        return 1;
    }

    struct in_addr ia;
    ia = app->mc_addr;
    printf("MC group: %s port: %d\n", inet_ntoa(ia), app->mc_port);
    if(0 != setup_socket(app)) {
        return 1;
    }

    app->hosts = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, 
            (GDestroyNotify)host_destroy);

    app->mainloop = g_main_loop_new(NULL, FALSE);

    app->ioc = g_io_channel_unix_new(app->recvfd);
    app->sid = g_io_add_watch(app->ioc, G_IO_IN, (GIOFunc)on_message_ready, 
            app);

    g_timeout_add(100, on_timer, app);
    app->report_interval_usec = DEFAULT_REPORT_INTERVAL_SECONDS * 1000000;
    app->next_report_utime = timestamp_now() + 1000000;

    bot_signal_pipe_glib_quit_on_kill(app->mainloop);
    g_main_loop_run(app->mainloop);

    g_io_channel_unref(app->ioc);
    g_source_remove(app->sid);

    shutdown(app->recvfd, SHUT_RDWR);

    g_hash_table_destroy(app->hosts);
    free(app->lcmurl);
    free(app);
    return 0;
}
コード例 #2
0
ファイル: lcm.c プロジェクト: mlab-upenn/arch-apex
lcm_t * 
lcm_create (const char *url)
{
    if (!g_thread_supported ()) g_thread_init (NULL);

#ifdef WIN32
	WSADATA	wsd;
    int status = WSAStartup ( MAKEWORD ( 2, 0 ), &wsd );
    if ( status )
    {
		return NULL;
    }
#endif

    char * provider_str = NULL;
    char * network = NULL;
    GHashTable * args = g_hash_table_new_full (g_str_hash, g_str_equal,
            free, free);
    GPtrArray * providers = g_ptr_array_new ();
    lcm_t *lcm = NULL;

    // initialize the list of providers
    lcm_udpm_provider_init (providers);
    lcm_logprov_provider_init (providers);
    lcm_tcpq_provider_init (providers);
    if (providers->len == 0) {
        fprintf (stderr, "Error: no LCM providers found\n");
        goto fail;
    }

    if (!url || !strlen(url)) 
        url = getenv ("LCM_DEFAULT_URL");
    if (!url || !strlen(url))
        url = LCM_DEFAULT_URL;

    if (0 != lcm_parse_url (url, &provider_str, &network, args)) {
        fprintf (stderr, "%s:%d -- invalid URL [%s]\n",
                __FILE__, __LINE__, url);
        goto fail;
    }

    lcm_provider_info_t * info = NULL;
    /* Find a matching provider */
    for (unsigned int i = 0; i < providers->len; i++) {
        lcm_provider_info_t * pinfo = (lcm_provider_info_t *) g_ptr_array_index (providers, i);
        if (!strcmp (pinfo->name, provider_str)) {
            info = pinfo;
            break;
        }
    }
    if (!info) {
        fprintf (stderr, "Error: LCM provider \"%s\" not found\n",
                provider_str);
        g_ptr_array_free (providers, TRUE);
        free (provider_str);
        free (network);
        g_hash_table_destroy (args);
        return NULL;
    }

    lcm = (lcm_t *) calloc (1, sizeof (lcm_t));

    lcm->vtable = info->vtable;
    lcm->handlers_all = g_ptr_array_new();
    lcm->handlers_map = g_hash_table_new (g_str_hash, g_str_equal);

    g_static_rec_mutex_init (&lcm->mutex);
    g_static_rec_mutex_init (&lcm->handle_mutex);

    lcm->provider = info->vtable->create (lcm, network, args);
    lcm->in_handle = 0;

    free (provider_str);
    free (network);
    g_ptr_array_free (providers, TRUE);
    g_hash_table_destroy (args);

    if (!lcm->provider) {
        lcm_destroy (lcm);
        return NULL;
    }

    lcm->default_max_num_queued_messages = 30;

    return lcm;

fail:
    free (provider_str);
    free (network);
    if (args)
        g_hash_table_destroy (args);
    if (providers)
        g_ptr_array_free (providers, TRUE);
//    if (lcm)
//        lcm_destroy (lcm);
    return NULL;
}