Ejemplo n.º 1
0
int
ccnet_session_prepare (CcnetSession *session, const char *config_dir_r, gboolean test_config)
{
    char *misc_path;
    int ret;

    if (ccnet_session_load_config (session, config_dir_r) < 0)
        return -1;

    misc_path = g_build_filename (session->config_dir, "misc", NULL);
    if (checkdir_with_mkdir (misc_path) < 0) {
        ccnet_error ("mkdir %s error", misc_path);
        return -1;
    }

    /* config db */
    session->config_db = ccnet_session_config_open_db (misc_path);
    if (!session->config_db) {
        ccnet_warning ("Failed to open config db.\n");
        return -1;
    }
    
    /* call subclass prepare */
    ret = CCNET_SESSION_GET_CLASS (session)->prepare(session);
    if (ret < 0)
        return ret;

    /* peer */
    ccnet_peer_manager_prepare(session->peer_mgr);
    g_signal_connect (session->peer_mgr, "peer-auth-done",
                      G_CALLBACK(on_peer_auth_done), session);

    /* permission manager */
    ccnet_perm_manager_prepare (session->perm_mgr);

    g_free (misc_path);

    if (test_config) {
        return 0;
    } else {
        /* Open localhost, if failed, then the program will exists. This is used
         * to prevent two instance of ccnet on the same port.
         */
        listen_on_localhost (session);
        
        /* refresh pubinfo on every startup */
        save_pubinfo (session);
    }

    return 0;
}
Ejemplo n.º 2
0
void
ccnet_session_save_config (CcnetSession *session)
{
    GError *error = NULL;
    char *str;
    FILE *fp;

    ccnet_message ("[Session] Saving configure file\n");
    if (session->saving_pub) {
        /* only update timestamp when pubinfo changes */
        save_pubinfo (session);
        session->saving_pub = 0;
    }

    g_key_file_set_string (session->keyf, "General", "NAME",
                           session->base.name);
    g_key_file_set_string (session->keyf, "General", "ID", 
                           session->base.id);
    g_key_file_set_string (session->keyf, "General", "USER_NAME", 
                           session->base.user_name);
#ifdef CCNET_SERVER
    g_key_file_set_string (session->keyf, "General", "SERVICE_URL",
                           session->base.service_url?session->base.service_url:"");
#endif
    g_key_file_set_integer (session->keyf, "Network", "PORT",
                            session->base.public_port);

    g_key_file_set_integer (session->keyf, "Client", "PORT", session->local_port);

    str = g_key_file_to_data (session->keyf, NULL, &error);
    if (error) {
        ccnet_warning ("Can't save unauth peer info: %s\n",
                       error->message);
        return;
    }

    if ((fp = g_fopen (session->config_file, "wb")) == NULL) {
        ccnet_warning ("Can't save session conf: %s\n", strerror(errno));
        g_free (str);
        return;
    }

    fputs (str, fp);
    fclose (fp);

    g_free (str);
    return;
}