Example #1
0
bool task_list::load(const std::string& filename)
{
	std::string load_json_string;

	if(msl::file_to_string(filename,load_json_string))
	{
		msl::json load_json(load_json_string);

		std::vector<task> temp_list;

		for(unsigned int ii=0;ii<(unsigned int)msl::to_int(load_json.get("size"));++ii)
		{
			msl::json task_json=load_json.get(msl::to_string(ii));

			date temp_date(msl::to_int(task_json.get("due_day")),msl::to_int(task_json.get("due_month")),msl::to_int(task_json.get("due_year")));

			task temp_task;
				temp_task.due_date=temp_date;
				temp_task.info=task_json.get("info");
				temp_task.name=task_json.get("name");
				temp_task.time_estimate=msl::to_int(task_json.get("time_estimate"));
				temp_task.time_working=msl::to_int(task_json.get("time_working"));


			temp_list.push_back(temp_task);
		}

		list_=temp_list;

		return true;
	}

	return false;
}
Example #2
0
Mesh* json_parse_mesh(const jsonvalue& json) {
    auto mesh = new Mesh();
    if(json.object_contains("json_mesh")) {
        json_texture_path_push(json.object_element("json_mesh").as_string());
        mesh = json_parse_mesh(load_json(json.object_element("json_mesh").as_string()));
        json_texture_path_pop();
    }
    json_set_optvalue(json, mesh->frame, "frame");
    json_set_optvalue(json, mesh->pos, "pos");
    json_set_optvalue(json, mesh->norm, "norm");
    json_set_optvalue(json, mesh->texcoord, "texcoord");
    json_set_optvalue(json, mesh->triangle, "triangle");
    json_set_optvalue(json, mesh->quad, "quad");
    json_set_optvalue(json, mesh->point, "point");
    json_set_optvalue(json, mesh->line, "line");
    json_set_optvalue(json, mesh->spline, "spline");
    if(json.object_contains("material")) mesh->mat = json_parse_material(json.object_element("material"));
    json_set_optvalue(json, mesh->subdivision_catmullclark_level, "subdivision_catmullclark_level");
    json_set_optvalue(json, mesh->subdivision_catmullclark_smooth, "subdivision_catmullclark_smooth");
    json_set_optvalue(json, mesh->subdivision_bezier_level, "subdivision_bezier_level");
    json_set_optvalue(json, mesh->subdivision_bezier_uniform, "subdivision_bezier_uniform");
    if(json.object_contains("animation")) mesh->animation = json_parse_frame_animation(json.object_element("animation"));
    if(json.object_contains("skinning")) mesh->skinning = json_parse_mesh_skinning(json.object_element("skinning"));
    if(json.object_contains("json_skinning")) mesh->skinning = json_parse_mesh_skinning(load_json(json.object_element("json_skinning").as_string()));
    if(json.object_contains("simulation")) mesh->simulation = json_parse_mesh_simulation(json.object_element("simulation"));
    if (mesh->skinning) {
        if (mesh->skinning->vert_rest_pos.empty()) mesh->skinning->vert_rest_pos = mesh->pos;
        if (mesh->skinning->vert_rest_norm.empty()) mesh->skinning->vert_rest_norm = mesh->norm;
        if (mesh->pos.empty()) mesh->pos = mesh->skinning->vert_rest_pos;
        if (mesh->norm.empty()) mesh->norm = mesh->skinning->vert_rest_norm;
    }
    return mesh;
}
Example #3
0
bool load_opentyrian_config( void )
{
    // defaults
    fullscreen_enabled = false;
#ifdef __BLACKBERRY__
#else
    set_scaler_by_name("3x");
#ifdef ENABLE_CONFIGURATION
    cJSON *root = load_json("opentyrian.conf");
    if (root == NULL)
        return false;

    cJSON *section = cJSON_GetObjectItem(root, "video");
    if (section != NULL)
    {
        cJSON *setting;

        if ((setting = cJSON_GetObjectItem(section, "fullscreen")))
            fullscreen_enabled = (setting->type == cJSON_True);

        if ((setting = cJSON_GetObjectItem(section, "scaler")))
            set_scaler_by_name(setting->valuestring);
    }

    cJSON_Delete(root);
#endif /*ENABLE_CONFIGURATION*/
#endif
    return true;
}
Example #4
0
/**
 * Answers /Hash_Array.json POST request by answering to the client needed
 * hashs
 * @param server_struct is the main structure for the server.
 * @param connection is the connection in MHD
 * @param received_data is a gchar * string to the data that was received
 *        by the POST request.
 */
static int answer_hash_array_post_request(server_struct_t *server_struct, struct MHD_Connection *connection, gchar *received_data)
{
    gchar *answer = NULL;         /** gchar *answer : Do not free answer variable as MHD will do it for us !  */
    json_t *root = NULL;          /** json_t *root is the root that will contain all meta data json formatted */
    json_t *array = NULL;         /** json_t *array is the array that will receive base64 encoded hashs       */
    GList *hash_data_list = NULL;


    if (server_struct != NULL && connection != NULL && received_data != NULL)
        {

            root = load_json(received_data);
            hash_data_list = extract_glist_from_array(root, "hash_list", TRUE);
            json_decref(root);

            print_debug(_("Received hash array of %zd bytes size\n"), strlen(received_data));

            array = find_needed_hashs(server_struct, hash_data_list);

            root = json_object();
            insert_json_value_into_json_root(root, "hash_list", array);
            answer = json_dumps(root, 0);
            json_decref(root);
            g_list_free_full(hash_data_list, free_hdt_struct);
        }
    else
        {
            answer = g_strdup_printf(_("Error: could not convert json to metadata\n"));
        }

    return create_MHD_response(connection, answer);
}
Example #5
0
bool save_opentyrian_config( void )
{
#ifdef __BLACKBERRY__
#else
#ifdef ENABLE_CONFIGURATION
    cJSON *root = load_json("opentyrian.conf");
    if (root == NULL)
        root = cJSON_CreateObject();

    cJSON *section;

    section = cJSON_CreateOrGetObjectItem(root, "video");
    cJSON_ForceType(section, cJSON_Object);

    {
        cJSON *setting;

        setting = cJSON_CreateOrGetObjectItem(section, "fullscreen");
        cJSON_SetBoolean(setting, fullscreen_enabled);

        setting = cJSON_CreateOrGetObjectItem(section, "scaler");
        cJSON_SetString(setting, scalers[scaler].name);
    }

    save_json(root, "opentyrian.conf");

    cJSON_Delete(root);
#endif /*ENABLE_CONFIGURATION*/
#endif
    return true;
}
Example #6
0
Scene* json_parse_scene(const jsonvalue& json) {
    // prepare scene
    auto scene = new Scene();
    // camera
    if (json.object_contains("camera")) scene->camera = json_parse_camera(json.object_element("camera"));
    if (json.object_contains("lookat_camera")) scene->camera = json_parse_lookatcamera(json.object_element("lookat_camera"));
    // surfaces
    if(json.object_contains("surfaces")) scene->surfaces = json_parse_surfaces(json.object_element("surfaces"));
    // meshes
    if(json.object_contains("json_meshes")) {
        json_texture_path_push(json.object_element("json_meshes").as_string());
        scene->meshes = json_parse_meshes(load_json(json.object_element("json_meshes").as_string()));
        json_texture_path_pop();
    }
    if(json.object_contains("meshes")) {
        scene->meshes = json_parse_meshes(json.object_element("meshes"));
    }
    // lights
    if(json.object_contains("lights")) scene->lights = json_parse_lights(json.object_element("lights"));
    // animation
    if(json.object_contains("animation")) scene->animation = json_parse_scene_animation(json.object_element("animation"));
    // rendering parameters
    json_set_optvalue(json, scene->image_width, "image_width");
    json_set_optvalue(json, scene->image_height, "image_height");
    json_set_optvalue(json, scene->image_samples, "image_samples");
    json_set_optvalue(json, scene->background, "background");
    json_parse_opttexture(json, scene->background_txt, "background_txt");
    json_set_optvalue(json, scene->ambient, "ambient");
    json_set_optvalue(json, scene->path_max_depth, "path_max_depth");
    json_set_optvalue(json, scene->path_sample_brdf, "path_sample_brdf");
    json_set_optvalue(json, scene->path_shadows, "path_shadows");
    // done
    return scene;
}
Example #7
0
esdm_config_t* esdm_config_init_from_str(const char * config_str){
	esdm_config_t* config = NULL;
	config = (esdm_config_t*) malloc(sizeof(esdm_config_t));
	config->json = load_json(config_str); // parse text into JSON structure

	return config;
}
bool load_opentyrian_config( void )
{
	// defaults
	fullscreen_enabled = false;
	set_scaler_by_name("Scale2x");
	
	cJSON *root = load_json("opentyrian.conf");
	if (root == NULL)
		return false;
	
	cJSON *section = cJSON_GetObjectItem(root, "video");
	if (section != NULL)
	{
		cJSON *setting;
		
		if ((setting = cJSON_GetObjectItem(section, "fullscreen")))
			fullscreen_enabled = (setting->type == cJSON_True);
		
		if ((setting = cJSON_GetObjectItem(section, "scaler")))
			set_scaler_by_name(setting->valuestring);
	}
	
	cJSON_Delete(root);
	
	return true;
}
bool save_opentyrian_config( void )
{
	cJSON *root = load_json("opentyrian.conf");
	if (root == NULL)
		root = cJSON_CreateObject();
	
	cJSON *section;
	
	section = cJSON_CreateOrGetObjectItem(root, "video");
	cJSON_ForceType(section, cJSON_Object);
	
	{
		cJSON *setting;
		
		setting = cJSON_CreateOrGetObjectItem(section, "fullscreen");
		cJSON_SetBoolean(setting, fullscreen_enabled);
		
		setting = cJSON_CreateOrGetObjectItem(section, "scaler");
		cJSON_SetString(setting, scalers[scaler].name);
	}
	
	save_json(root, "opentyrian.conf");
	
	cJSON_Delete(root);
	
	return true;
}
Example #10
0
Scene* load_json_scene(const string& filename) {
    json_texture_cache.clear();
    json_texture_paths = { "" };
    auto scene = json_parse_scene(load_json(filename));
    json_texture_cache.clear();
    json_texture_paths = { "" };
    return scene;
}
Example #11
0
void display ( void )
{
    load_json("elements.json");
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glTranslatef(x,y,z);  
    glColor3f(1.0,0.0,0.0); 
    glPointSize(10.0);
    glBegin(GL_TRIANGLES);
      glVertex2f(-0.8f,-0.8f);
      glVertex2f(0.0f,0.8f);
      glVertex2f(0.8f,-0.8f);
    glEnd();
    counter++;
    if(counter == 1000) define_direction();
    glFlush();
    glutSwapBuffers();
}
Example #12
0
Result json_to_vb_pair(const char *fname, Pair *o_pair)
{
    Result result;

    json_value* value;
    value = NULL;
    result.parse_result.status = PS_NO_PARSE;
    result.parse_result.emsg = "Not yet initialized";
    result.file_result = load_json(fname, &value);

    if (result.file_result.status != FS_SUCCESS) {
        return result;
    }

    result.parse_result = check_json(value);

    if (result.parse_result.status  != PS_SUCCESS) {
        json_value_free(value);
        return result;
    }

    VertexSetPointer vs; BondSetPointer bs;
    vs = (VertexSetPointer) Util_allocate(1, sizeof(VertexSet));
    result.parse_result = vertexset_from_json(value, &vs);

    if (result.parse_result.status  != PS_SUCCESS) {
        json_value_free(value);
        return result;
    }

    bs = (BondSetPointer) Util_allocate(1, sizeof(BondSet));
    result.parse_result = bondset_from_json(*vs, value, &bs);
    if (result.parse_result.status  != PS_SUCCESS) {
        json_value_free(value);
        return result;
    }
    
    if (value != NULL) {
        json_value_free(value);
    }

    *o_pair = Pair_initialize(vs, bs);

    return result;

}
bool load_joystick_assignments( int j )
{
	cJSON *root = load_json("joystick.conf");
	if (root == NULL)
		return false;
	
	cJSON *config = cJSON_GetObjectItem(root, SDL_JoystickName(j));
	if (config == NULL)
	{
		cJSON_Delete(root);
		return false;
	}
	
	cJSON *setting;
	
	if ((setting = cJSON_GetObjectItem(config, "analog")))
		joystick[j].analog = (setting->type == cJSON_True);
	
	if ((setting = cJSON_GetObjectItem(config, "sensitivity")))
		joystick[j].sensitivity = setting->valueint;
	
	if ((setting = cJSON_GetObjectItem(config, "threshold")))
		joystick[j].threshold = setting->valueint;
	
	if ((setting = cJSON_GetObjectItem(config, "assignments")))
	{
		for (uint i = 0; i < COUNTOF(joystick->assignment); ++i)
		{
			cJSON *assignments = cJSON_GetArrayItem(setting, i);
			if (assignments == NULL)
				break;
			
			for (uint k = 0; k < COUNTOF(*joystick->assignment); ++k)
			{
				cJSON *assignment = cJSON_GetArrayItem(assignments, k);
				if (assignment)
					code_to_assignment(&joystick[j].assignment[i][k], assignment->valuestring);
			}
		}
	}
	
	cJSON_Delete(root);
	
	return true;
}
Example #14
0
/**
 *
 *
 * @return Status
 */
esdm_status esdm_layout_stat(char* desc)
{
	ESDM_DEBUG(__func__);	
	ESDM_DEBUG("received metadata lookup request");


	// parse text into JSON structure
	json_t *root = load_json(desc);

	if (root) {
		// print and release the JSON structure
		print_json(root);
		json_decref(root);
	}


	return ESDM_SUCCESS;
}
bool save_joystick_assignments( int j )
{
	cJSON *root = load_json("joystick.conf");
	if (root == NULL)
		root = cJSON_CreateObject();
	
	cJSON *config = cJSON_CreateOrGetObjectItem(root, SDL_JoystickName(j));
	cJSON_ForceType(config, cJSON_Object);
	
	cJSON *setting;
	
	setting = cJSON_CreateOrGetObjectItem(config, "analog");
	cJSON_SetBoolean(setting, joystick[j].analog);
	
	setting = cJSON_CreateOrGetObjectItem(config, "sensitivity");
	cJSON_SetNumber(setting, joystick[j].sensitivity);
	
	setting = cJSON_CreateOrGetObjectItem(config, "threshold");
	cJSON_SetNumber(setting, joystick[j].threshold);
	
	setting = cJSON_CreateOrGetObjectItem(config, "assignments");
	cJSON_ForceType(setting, cJSON_Array);
	cJSON_ClearArray(setting);
	
	for (uint i = 0; i < COUNTOF(joystick->assignment); ++i)
	{
		cJSON *assignments;
		cJSON_AddItemToArray(setting, assignments = cJSON_CreateArray());
		
		for (uint k = 0; k < COUNTOF(*joystick->assignment); ++k)
		{
			if (joystick[j].assignment[i][k].type == NONE)
				continue;
			
			cJSON_AddItemToArray(assignments, cJSON_CreateString(assignment_to_code(&joystick[j].assignment[i][k])));
		}
	}
	
	save_json(root, "joystick.conf");
	
	cJSON_Delete(root);
	
	return true;
}
Example #16
0
void inviteMsg(TPTransportCTX *context, char* username) {
    TPUserID userID;
    TPTransportMsg *message;
    
    memset(&userID, 0, sizeof(userID));
    strcpy((char*)userID,username);
    
    if (strcmp((char *)&userID, "NULL") != 0) {
        message = TPMsgGet(context, &userID, TP_MSG_TYPE_CHATCONTROL);
    }
    else {
        message = TPMsgGet(context, NULL, TP_MSG_TYPE_CHATCONTROL);
    }
    if (message != NULL) {
        message->msg[message->bytesCount] = '\0';
        printf("'%s'->'%s': '%s' @%lu\n", message->from, message->to, message->msg, message->timestamp);
        
        TPGroupID groupRef;
        memset(&groupRef, 0, sizeof(groupRef));
//        strcpy((char *)groupRef,message->msg);
        
//        printf("json-msg(recv): %s\n",message->msg);
        json_t *root = load_json(message->msg);
        json_t *result = json_object_get(root,"groupID");
        char* myresult = (char*)json_string_value(result);
        strcpy((char *)groupRef,myresult);

        int ret = TPGroupJoin(context, &userID, &groupRef);
        if (ret != 0) {
            fprintf(stderr, "Client joinGroup return (%d)\n", ret);
        }
        else{
            printf("'%s' join group '%s' done!!\n",userID,groupRef);
        }
        
        TPMsgFree(message);
    } else {
//        printf("not found message\n");
    }
}
Example #17
0
qdb cmd_t::toquads ( const strings& args ) {
	return toquads ( load_json ( args ) );
}
Example #18
0
qdb cmd_t::convert ( const string& s ) {
	if ( fnamebase ) opts.base = pstr ( string ( L"file://" ) + s + L"#" );
	qdb r = convert ( load_json ( s ) );
	return r;
}
int main(int argc, char *argv[])
{
    char ch;
    char str[STR_BUFFSIZE];

    char plom_help_string[] =
        "PLOM ksimplex\n"
        "usage:\n"
        "ksimplex [implementation] [--no_dem_sto] [--no_white_noise] [--no_diff]\n"
        "                          [-s, --DT <float>] [--eps_abs <float>] [--eps_rel <float>]\n"
        "                          [-p, --path <path>] [-i, --id <integer>]\n"
        "                          [-g, --freeze_forcing <float>]\n"
        "                          [--prior] [--transf]\n"
        "                          [-l, --LIKE_MIN <float>] [-S, --size <float>] [-M, --iter <integer>]\n"
	"                          [-q, --quiet] [-P, --pipe]"
        "                          [-h, --help]\n"
        "where implementation is 'sde' (default)\n"
        "options:\n"
	"\n"
        "-q, --quiet          no verbosity\n"
        "-P, --pipe           pipe mode (echo theta.json on stdout)\n"
	"\n"
        "--no_dem_sto       turn off demographic stochasticity (if possible)\n"
        "--no_white_noise       turn off environmental stochasticity (if any)\n"
        "--no_diff         turn off drift (if any)\n"
	"\n"
        "-s, --DT           Initial integration time step\n"
	"--eps_abs          Absolute error for adaptive step-size control\n"
	"--eps_rel          Relative error for adaptive step-size control\n"
        "-g, --freeze_forcing  freeze the metadata to their value at the specified time\n"
	"\n"
        "--prior            to maximize posterior density in natural space\n"
        "--transf           to maximize posterior density in transformed space (if combined with --prior)\n"
        "-p, --path         path where the outputs will be stored\n"
        "-i, --id           general id (unique integer identifier that will be appended to the output files)\n"
        "-l, --LIKE_MIN     particles with likelihood smaller that LIKE_MIN are considered lost\n"
        "-M, --iter         maximum number of iterations\n"
        "-S, --size         simplex size used as a stopping criteria\n"
        "-b, --no_traces    do not write the traces\n"
	"-o, --nb_obs       number of observations to be fitted (for tempering)"
        "--help             print the usage on stdout\n";

    // simplex options
    M = 10;
    CONVERGENCE_STOP_SIMPLEX = 1e-6;

    // general options
    GENERAL_ID =0;
    snprintf(SFR_PATH, STR_BUFFSIZE, "%s", DEFAULT_PATH);
    J=1;
    LIKE_MIN = 1e-17;
    LOG_LIKE_MIN = log(1e-17);
    int nb_obs = -1;
    double freeze_forcing = -1.0;

    // options
    OPTION_PRIOR = 0;
    OPTION_TRANSF = 0;

    double dt = 0.0, eps_abs = PLOM_EPS_ABS, eps_rel = PLOM_EPS_REL;

    enum plom_print print_opt = PLOM_PRINT_BEST;

    enum plom_implementations implementation;
    enum plom_noises_off noises_off = 0;


    static struct option long_options[] = {
        {"help",       no_argument,       0, 'h'},
        {"no_trace",   no_argument,  0, 'b'},

	{"no_dem_sto", no_argument,       0, 'x'},
	{"no_white_noise", no_argument,       0, 'y'},
	{"no_diff",   no_argument,       0, 'z'},


	{"DT",         required_argument, 0, 's'},
	{"eps_abs",    required_argument, 0, 'v'},
	{"eps_rel",    required_argument, 0, 'w'},

	{"freeze_forcing", required_argument, 0, 'g'},

        {"path",       required_argument, 0, 'p'},
        {"id",         required_argument, 0, 'i'},

        {"prior",  no_argument, &OPTION_PRIOR,  1},
        {"transf", no_argument, &OPTION_TRANSF, 1},

        {"LIKE_MIN", required_argument, 0, 'l'},
        {"iter",     required_argument,   0, 'M'},
        {"size",     required_argument,   0, 'S'},
	{"nb_obs", required_argument,  0, 'o'},

	{"quiet",  no_argument,       0, 'q'},
	{"pipe",  no_argument,       0, 'P'},

        {0, 0, 0, 0}
    };

    int option_index = 0;
    while ((ch = getopt_long (argc, argv, "qPhxyzs:v:w:i:l:p:S:M:o:bg:", long_options, &option_index)) != -1) {
        switch (ch) {
        case 0:
            break;

        case 'x':
            noises_off = noises_off | PLOM_NO_DEM_STO;
            break;
        case 'y':
            noises_off = noises_off | PLOM_NO_ENV_STO;
            break;
        case 'z':
            noises_off = noises_off | PLOM_NO_DRIFT;
            break;
        case 's':
            dt = atof(optarg);
            break;
        case 'v':
            eps_abs = atof(optarg);
            break;
        case 'w':
            eps_rel = atof(optarg);
            break;
        case 'h':
            print_log(plom_help_string);
            return 1;
        case 'b':
            print_opt &= ~PLOM_PRINT_BEST;
            break;
        case 'p':
            snprintf(SFR_PATH, STR_BUFFSIZE, "%s", optarg);
            break;
        case 'i':
            GENERAL_ID = atoi(optarg);
            break;
        case 'g':
            freeze_forcing = atof(optarg);
            break;
	case 'o':
	    nb_obs = atoi(optarg);
            break;
        case 'l':
            LIKE_MIN = atof(optarg);
            LOG_LIKE_MIN = log(LIKE_MIN);
            break;
        case 'M':
            M = atoi(optarg);
            break;
        case 'S':
            CONVERGENCE_STOP_SIMPLEX = atof(optarg);
            break;

        case 'q':
	    print_opt |= PLOM_QUIET;
            break;
        case 'P':
	    print_opt |= PLOM_PIPE | PLOM_QUIET;
            break;

        case '?':
            /* getopt_long already printed an error message. */
            return 1;

        default:
            snprintf(str, STR_BUFFSIZE, "Unknown option '-%c'\n", optopt);
            print_err(str);
            return 1;
        }
    }
    argc -= optind;
    argv += optind;

    if(argc == 0) {
	implementation = PLOM_ODE; //with Kalman the SDE uses f_pred of PLOM_ODE (OK will do better)...
    } else {
        if (!strcmp(argv[0], "sde")) {
            implementation = PLOM_ODE;
        } else {
            print_log(plom_help_string);
            return 1;
        }
    }
    plom_unlink_done(SFR_PATH, GENERAL_ID);
    json_t *settings = load_settings(PATH_SETTINGS);

    int64_t time_begin, time_end;
    if (!(print_opt & PLOM_QUIET)) {
	snprintf(str, STR_BUFFSIZE, "Starting plom-ksimplex with the following options: i = %d, LIKE_MIN = %g", GENERAL_ID, LIKE_MIN);
	print_log(str);
	time_begin = s_clock();
    }

    json_t *theta = load_json();
    struct s_kalman *p_kalman = build_kalman(theta, settings, implementation, noises_off, OPTION_PRIOR, dt, eps_abs, eps_rel, freeze_forcing, nb_obs);
    json_decref(settings);

    simplex(p_kalman->p_best, p_kalman->p_data, p_kalman, f_simplex_kalman, CONVERGENCE_STOP_SIMPLEX, M, print_opt);

    if (!(print_opt & PLOM_QUIET)) {
	time_end = s_clock();
	struct s_duration t_exec = time_exec(time_begin, time_end);
	snprintf(str, STR_BUFFSIZE, "Done in:= %dd %dh %dm %gs", t_exec.d, t_exec.h, t_exec.m, t_exec.s);
	print_log(str);
    }

    plom_print_done(theta, p_kalman->p_data, p_kalman->p_best, SFR_PATH, GENERAL_ID, print_opt);

    if (!(print_opt & PLOM_QUIET)) {
	print_log("clean up...");
    }

    json_decref(theta);

    clean_kalman(p_kalman);

    return 0;
}
Example #20
0
/**
 * Function that process the received data from the POST command and
 * answers to the client.
 * Here we may do something with this data (we may want to store it
 * somewhere).
 *
 * @param server_struct is the main structure for the server.
 * @param connection is the connection in MHD
 * @param url is the requested url
 * @param received_data is a gchar * string to the data that was received
 *        by the POST request.
 */
static int process_received_data(server_struct_t *server_struct, struct MHD_Connection *connection, const char *url, gchar *received_data)
{
    gchar *answer = NULL;                   /** gchar *answer : Do not free answer variable as MHD will do it for us ! */
    int success = MHD_NO;
    gboolean debug = FALSE;
    hash_data_t *hash_data = NULL;
    json_t *root = NULL;
    GList *hash_data_list = NULL;
    GList *head = NULL;
    a_clock_t *elapsed = NULL;

    if (g_strcmp0(url, "/Meta.json") == 0 && received_data != NULL)
        {
            success = answer_meta_json_post_request(server_struct, connection, received_data);
        }
    else if (g_strcmp0(url, "/Hash_Array.json") == 0 && received_data != NULL)
        {
            success = answer_hash_array_post_request(server_struct, connection, received_data);
            /* Here we will try to answer which hashs are needed and then
             * send thoses hashs back in the answer
             */
        }
    else if (g_strcmp0(url, "/Data.json") == 0 && received_data != NULL)
        {

            hash_data = convert_string_to_hash_data(received_data);

            if (get_debug_mode() == TRUE)
                {
                    print_received_data_for_hash(hash_data->hash, hash_data->read);
                }

            /**
             * Sending received_data into the queue in order to be treated by
             * the corresponding thread. hash_data is freed by data_thread
             * and should not be used after this "call" here.
             */
            g_async_queue_push(server_struct->data_queue, hash_data);


            /**
             * creating an answer for the client to say that everything went Ok!
             */
            answer = g_strdup_printf(_("Ok!"));
            success = create_MHD_response(connection, answer);
        }
    else if (g_strcmp0(url, "/Data_Array.json") == 0 && received_data != NULL)
        {
            /* print_debug("/Data_Array.json: %s\n", received_data); */
            elapsed = new_clock_t();
            root = load_json(received_data);
            end_clock(elapsed, "load_json");
            hash_data_list = extract_glist_from_array(root, "data_array", FALSE);
            head = hash_data_list;
            json_decref(root);
            debug = get_debug_mode();

            while (hash_data_list != NULL)
                {
                    hash_data = hash_data_list->data;

                    if (debug == TRUE)
                        {
                            /* Only for debbugging ! */
                            print_received_data_for_hash(hash_data->hash, hash_data->read);
                        }

                    /** Sending hash_data into the queue. */
                    g_async_queue_push(server_struct->data_queue, hash_data);
                    hash_data_list = g_list_next(hash_data_list);
                }

            g_list_free(head);

            /**
             * creating an answer for the client to say that everything went Ok!
             */

            answer = g_strdup_printf(_("Ok!"));
            success = create_MHD_response(connection, answer);
        }
    else
        {
            /* The url is unknown to the server and we can not process the request ! */
            print_error(__FILE__, __LINE__, "Error: invalid url: %s\n", url);
            answer = g_strdup_printf(_("Error: invalid url!\n"));
            success = create_MHD_response(connection, answer);
        }

    return success;
}
Example #21
0
/**
 * Sends data as requested by the server 'cdpfglserver'.
 * @param main_struct : main structure of the program.
 * @param hash_data_list : list of hash_data_t * pointers containing
 *                          all the data to be saved.
 * @param answer is the request sent back by server when we had send
 *        meta data.
 * @note using directly main_struct->comm->buffer -> not threadable as is.
 */
static GList *send_data_to_server(main_struct_t *main_struct, GList *hash_data_list, gchar *answer)
{
    json_t *root = NULL;
    GList *hash_list = NULL;         /** hash_list is local to this function */
    GList *head = NULL;
    gint success = CURLE_FAILED_INIT;
    GList *iter = NULL;
    hash_data_t *found = NULL;
    hash_data_t *hash_data = NULL;

    if (main_struct != NULL && main_struct->comm != NULL && answer != NULL &&  hash_data_list!= NULL)
    {
        root = load_json(answer);

        if (root != NULL)
        {
            /* This hash_list is the needed hashs from server */
            hash_list = extract_glist_from_array(root, "hash_list", TRUE);
            json_decref(root);
            head = hash_list;

            while (hash_list != NULL)
            {
                hash_data = hash_list->data;
                /* hash_data_list contains all hashs and their associated data */
                iter = find_hash_in_list(hash_data_list, hash_data->hash);
                found = iter->data;

                /* readbuffer is the buffer sent to server  */
                main_struct->comm->readbuffer = convert_hash_data_t_to_string(found);
                success = post_url(main_struct->comm, "/Data.json");

                if (success != CURLE_OK)
                {
                    db_save_buffer(main_struct->database, "/Data.json", main_struct->comm->readbuffer);
                }

                free_variable(main_struct->comm->readbuffer);

                hash_data_list = g_list_remove_link(hash_data_list, iter);
                /* iter is now a single element list and we can delete
                 * data in this element and then remove this single element list
                 */
                g_list_free_full(iter, free_hdt_struct);

                main_struct->comm->buffer = free_variable(main_struct->comm->buffer);
                hash_list = g_list_next(hash_list);
            }

            if (head != NULL)
            {
                g_list_free_full(head, free_hdt_struct);
            }
        }
        else
        {
            print_error(__FILE__, __LINE__, _("Error while loading JSON answer from server\n"));
        }
    }

    return hash_data_list;

}
Example #22
0
/**
 * Sends data as requested by the server 'cdpfglserver' in a buffered way.
 * @param main_struct : main structure of the program.
 * @param hash_data_list : list of hash_data_t * pointers containing
 *                          all the data to be saved.
 * @param answer is the request sent back by server when we had send
 *        meta data.
 * @note using directly main_struct->comm->buffer -> not threadable as is.
 */
static GList *send_all_data_to_server(main_struct_t *main_struct, GList *hash_data_list, gchar *answer)
{
    json_t *root = NULL;
    json_t *array = NULL;
    GList *hash_list = NULL;      /** hash_list is local to this function and contains the needed hashs as answer by server */
    GList *head = NULL;
    GList *iter = NULL;
    hash_data_t *found = NULL;
    hash_data_t *hash_data = NULL;
    gint bytes = 0;
    json_t *to_insert = NULL;
    gint64 limit = 0;
    a_clock_t *elapsed = NULL;

    if (answer != NULL && hash_data_list != NULL && main_struct != NULL && main_struct->opt != NULL)
    {
        root = load_json(answer);

        limit = main_struct->opt->buffersize;

        if (root != NULL)
        {
            /* This hash_list is the needed hashs from server */
            hash_list = extract_glist_from_array(root, "hash_list", TRUE);
            json_decref(root);

            array = json_array();

            head = hash_list;

            while (hash_list != NULL)
            {
                hash_data = hash_list->data;
                /* hash_data_list contains all hashs and their associated data for the file
                 * being processed */
                iter = find_hash_in_list(hash_data_list, hash_data->hash);
                found = iter->data;

                to_insert = convert_hash_data_t_to_json(found);
                json_array_append_new(array, to_insert);

                bytes = bytes + found->read;

                hash_data_list = g_list_remove_link(hash_data_list, iter);
                /* iter is now a single element list and we can delete
                 * data in this element and then remove this single element list
                 */
                g_list_free_full(iter, free_hdt_struct);

                if (bytes >= limit)
                {
                    /* when we've got opt->buffersize bytes of data send them ! */
                    elapsed = new_clock_t();
                    insert_array_in_root_and_send(main_struct, array);
                    array = json_array();
                    bytes = 0;
                    end_clock(elapsed, "insert_array_in_root_and_send");
                }

                hash_list = g_list_next(hash_list);
            }

            if (bytes > 0)
            {
                /* Send the rest of the data (less than opt->buffersize bytes) */
                elapsed = new_clock_t();
                insert_array_in_root_and_send(main_struct, array);
                end_clock(elapsed, "insert_array_in_root_and_send");
            }
            else
            {
                json_decref(array);
            }

            if (head != NULL)
            {
                g_list_free_full(head, free_hdt_struct);
            }
        }
        else
        {
            print_error(__FILE__, __LINE__, _("Error while loading JSON answer from server\n"));
        }
    }

    return hash_data_list;
}
Example #23
0
pobj cmd_t::nodemap ( const strings& args ) {
	return nodemap ( load_json ( args[2] ) );
}
Example #24
0
Scene* load_json_scene(const string& filename) {
    auto scene = json_parse_scene(load_json(filename));
    return scene;
}
Example #25
0
esdm_status esdm_scheduler_enqueue_read(esdm_instance_t *esdm, io_request_status_t * status, int frag_count, esdm_fragment_t** read_frag, void * buf, esdm_dataspace_t * buf_space){
	GError * error;

	status->pending_ops += frag_count;

	int i, x;
	for (i = 0; i < frag_count; i++){
		esdm_fragment_t * f = read_frag[i];
		json_t *root = load_json(f->metadata->json);
		json_t * elem;
		elem = json_object_get(root, "plugin");
		//const char * plugin_type = json_string_value(elem);
		elem = json_object_get(root, "id");
		const char * plugin_id = json_string_value(elem);
		elem = json_object_get(root, "offset");
		//const char * offset_str = json_string_value(elem);
		elem = json_object_get(root, "size");
		//const char * size_str = json_string_value(elem);

		if(!plugin_id){
			printf("Backend ID needs to be given\n");
			exit(1);
		}

		// find the backend for the fragment
		esdm_backend* backend_to_use = NULL;
		for (x = 0; x < esdm->modules->backend_count; x++){
			esdm_backend* b_tmp = esdm->modules->backends[x];
			if (strcmp(b_tmp->config->id, plugin_id) == 0){
				DEBUG("Found plugin %s", plugin_id);
				backend_to_use = b_tmp;
				break;
			}
		}
		if(backend_to_use == NULL){
			printf("Error no backend found for ID: %s\n", plugin_id);
			exit(1);
		}

		// esdm_fragment_print(read_frag[i]);
		// printf("\n");

		DEBUG("OFFSET/SIZE: %s %s\n", offset_str, size_str);

		//for verification purposes, we could read back the metadata stored and compare it...
		//esdm_dataspace_t * space = NULL;
		//esdm_status ret = esdm_dataspace_overlap_str(parent_space, 'x', (char*)offset_str, (char*)size_str, & space);
		//assert(ret == ESDM_SUCCESS);

		uint64_t size = esdm_dataspace_size(f->dataspace);
		//printf("SIZE: %ld\n", size);
		f->backend = backend_to_use;

		io_work_t * task = (io_work_t*) malloc(sizeof(io_work_t));
		task->parent = status;
		task->op = ESDM_OP_READ;
		task->fragment = f;
		if(f->in_place){
			DEBUG("inplace!", "");
			task->callback = NULL;
			f->buf = buf;
		}else{
			f->buf = malloc(size);
			task->callback = read_copy_callback;
			task->data.mem_buf = buf;
			task->data.buf_space = buf_space;
		}
		if (backend_to_use->threads == 0){
			backend_thread(task, backend_to_use);
		}else{
			g_thread_pool_push(backend_to_use->threadPool, task, & error);
		}
	}

	return ESDM_SUCCESS;
}
Example #26
0
pobj cmd_t::load_json ( const strings& args ) {
	return load_json ( args.size() > 2 ? args[2] : L"" );
}
int main(int argc, char *argv[])
{
    char ch;
    char str[STR_BUFFSIZE];

    char plom_help_string[] =
        "PLOM Kalman\n"
        "usage:\n"
        "kalman [implementation] [--no_dem_sto] [--no_white_noise] [--no_diff]\n"
        "                        [-s, --DT <float>] [--eps_abs <float>] [--eps_rel <float>]\n"
        "                        [-g, --freeze_forcing <float>]\n"
        "                        [-r, --traj] [-p, --path <path>] [-i, --id <integer>]\n"
        "                        [-b, --no_trace] [-e, --no_hat] [--prior] [--transf]\n"
	"                        [-q, --quiet] [-P, --pipe]"
        "                        [-h, --help]\n"
        "where implementation is 'sde' (default)\n"
        "options:\n"
	"\n"
        "-q, --quiet        no verbosity\n"
        "-P, --pipe         pipe mode (echo theta.json on stdout)\n"
	"\n"
        "--no_dem_sto       turn off demographic stochasticity (if possible)\n"
        "--no_white_noise       turn off environmental stochasticity (if any)\n"
        "--no_diff         turn off drift (if any)\n"
	"\n"
        "-s, --DT           Initial integration time step\n"
	"--eps_abs          Absolute error for adaptive step-size contro\n"
	"--eps_rel          Relative error for adaptive step-size contro\n"
        "-g, --freeze_forcing  freeze the metadata to their value at the specified time\n"
	"\n"
        "-r, --traj         print the trajectories\n"
        "--prior            add log(prior) to the estimated loglik\n"
        "--transf           add log(JacobianDeterminant(transf)) to the estimated loglik. (combined to --prior, gives posterior density in transformed space)\n"
        "-p, --path         path where the outputs will be stored\n"
        "-b, --no_trace     do not write trace_<id>.output file\n"
        "-h, --no_hat       do not write hat_<general_id>.output file\n"
        "-d, --no_pred_res  do not write pred_res_<general_id>.output file (prediction residuals)\n"
        "-i, --id           general id (unique integer identifier that will be appended to the output files)\n"
        "-l, --LIKE_MIN     particles with likelihood smaller that LIKE_MIN are considered lost\n"
	"-o, --nb_obs       number of observations to be fitted (for tempering)"
        "-h, --help         print the usage on stdout\n";


    // general options
    GENERAL_ID =0;
    snprintf(SFR_PATH, STR_BUFFSIZE, "%s", DEFAULT_PATH);
    LIKE_MIN = 1e-17;
    LOG_LIKE_MIN = log(LIKE_MIN);
    enum plom_print print_opt = PLOM_PRINT_BEST | PLOM_PRINT_HAT | PLOM_PRINT_PRED_RES;

    // options
    OPTION_PRIOR = 0;
    OPTION_TRANSF = 0;
    int nb_obs = -1;
    double freeze_forcing = -1.0;

    double dt = 0.0, eps_abs = PLOM_EPS_ABS, eps_rel = PLOM_EPS_REL;

    J = 1; //not an option, needed for print_X



    enum plom_implementations implementation;
    enum plom_noises_off noises_off = 0;


    static struct option long_options[] = {
	{"traj",       no_argument,       0, 'r'},
	{"no_dem_sto", no_argument,       0, 'x'},
	{"no_white_noise", no_argument,       0, 'y'},
	{"no_diff",   no_argument,       0, 'z'},

	{"DT",         required_argument, 0, 's'},
	{"eps_abs",    required_argument, 0, 'v'},
	{"eps_rel",    required_argument, 0, 'w'},
	{"freeze_forcing", required_argument, 0, 'g'},
        {"help",       no_argument,       0, 'h'},
        {"path",       required_argument, 0, 'p'},
        {"id",         required_argument, 0, 'i'},
	{"no_trace",   no_argument,       0, 'b'},
	{"no_hat",     no_argument,       0, 'e'},
	{"no_pred_res",no_argument,       0, 'd'},

        {"prior", no_argument, &OPTION_PRIOR, 1},
        {"transf", no_argument, &OPTION_TRANSF, 1},
	{"nb_obs", required_argument,  0, 'o'},

	{"quiet",  no_argument,       0, 'q'},
	{"pipe",  no_argument,       0, 'P'},

        {"LIKE_MIN",   required_argument, 0, 'l'},

        {0, 0, 0, 0}
    };

    int option_index = 0;
    while ((ch = getopt_long (argc, argv, "qPxyzs:v:w:i:l:p:rbhedo:g:", long_options, &option_index)) != -1) {
        switch (ch) {
        case 0:
            break;

        case 'x':
            noises_off = noises_off | PLOM_NO_DEM_STO;
            break;
        case 'y':
            noises_off = noises_off | PLOM_NO_ENV_STO;
            break;
        case 'z':
            noises_off = noises_off | PLOM_NO_DRIFT;
            break;
	case 'o':
	    nb_obs = atoi(optarg);
            break;

        case 'g':
            freeze_forcing = atof(optarg);
            break;

        case 's':
            dt = atof(optarg);
            break;
        case 'v':
            eps_abs = atof(optarg);
            break;
        case 'w':
            eps_rel = atof(optarg);
            break;

        case 'h':
            print_log(plom_help_string);
            return 1;

        case 'p':
            snprintf(SFR_PATH, STR_BUFFSIZE, "%s", optarg);
            break;
        case 'i':
            GENERAL_ID = atoi(optarg);
            break;
        case 'l':
            LIKE_MIN = atof(optarg);
            LOG_LIKE_MIN = log(LIKE_MIN);
            break;
        case 'r':
	    print_opt |= PLOM_PRINT_X;
            break;
        case 'b':
	    print_opt &= ~PLOM_PRINT_BEST;
            break;
        case 'e':
	    print_opt &= ~PLOM_PRINT_HAT;
            break;
        case 'd':
	    print_opt &= ~PLOM_PRINT_PRED_RES;
            break;

        case 'q':
	    print_opt |= PLOM_QUIET;
            break;
        case 'P':
	    print_opt |= PLOM_PIPE | PLOM_QUIET;
            break;


        case '?':
            /* getopt_long already printed an error message. */
            return 1;

        default:
            snprintf(str, STR_BUFFSIZE, "Unknown option '-%c'\n", optopt);
            print_err(str);
            return 1;
        }
    }
    argc -= optind;
    argv += optind;


    if(argc == 0) {
	implementation = PLOM_ODE; //with Kalman the SDE uses f_pred of PLOM_ODE (OK will do better)...
    } else {
        if (!strcmp(argv[0], "sde")) {
            implementation = PLOM_ODE;
        } else {
            print_log(plom_help_string);
            return 1;
        }
    }

    plom_unlink_done(SFR_PATH, GENERAL_ID);
    json_t *settings = load_settings(PATH_SETTINGS);

    if (!(print_opt & PLOM_QUIET)) {
	snprintf(str, STR_BUFFSIZE, "Starting plom-Kalman with the following options: i = %d, LIKE_MIN = %g", GENERAL_ID, LIKE_MIN);
	print_log(str);
    }

    json_t *theta = load_json();
    struct s_kalman *p_kalman = build_kalman(theta, settings, implementation, noises_off, OPTION_PRIOR, dt, eps_abs, eps_rel, freeze_forcing, nb_obs);
    json_decref(settings);

    int64_t time_begin, time_end;
    if (!(print_opt & PLOM_QUIET)) {
	time_begin = s_clock();
    }

    back_transform_theta2par(p_kalman->p_par, p_kalman->p_best->mean, p_kalman->p_data->p_it_all, p_kalman->p_data);
    linearize_and_repeat(p_kalman->p_X, p_kalman->p_par, p_kalman->p_data, p_kalman->p_data->p_it_par_sv);
    prop2Xpop_size(p_kalman->p_X, p_kalman->p_data, p_kalman->calc[0]);
    theta_driftIC2Xdrift(p_kalman->p_X, p_kalman->p_best->mean, p_kalman->p_data);

    FILE *p_file_X = (print_opt & PLOM_PRINT_X) ? plom_fopen(SFR_PATH, GENERAL_ID, "X", "w", header_X, p_kalman->p_data): NULL;
    FILE *p_file_hat = (print_opt & PLOM_PRINT_HAT) ? plom_fopen(SFR_PATH, GENERAL_ID, "hat", "w", header_hat, p_kalman->p_data): NULL;
    FILE *p_file_pred_res = (print_opt & PLOM_PRINT_PRED_RES) ? plom_fopen(SFR_PATH, GENERAL_ID, "pred_res", "w", header_prediction_residuals_ekf, p_kalman->p_data): NULL;

    double log_like = run_kalman(p_kalman->p_X, p_kalman->p_best, p_kalman->p_par, p_kalman->p_kalman_update, p_kalman->p_data, p_kalman->calc, f_prediction_ode, 0, p_file_X, p_file_hat, p_file_pred_res, print_opt);


    if (print_opt & PLOM_PRINT_X) {
        plom_fclose(p_file_X);
    }

    if (print_opt & PLOM_PRINT_HAT) {
        plom_fclose(p_file_hat);
    }

    if (print_opt & PLOM_PRINT_PRED_RES) {
        plom_fclose(p_file_pred_res);
    }

    if (!(print_opt & PLOM_QUIET)) {
	time_end = s_clock();
	struct s_duration t_exec = time_exec(time_begin, time_end);
	sprintf(str, "logV: %g", log_like);
	print_log(str);

	sprintf(str, "Done in:= %dd %dh %dm %gs", t_exec.d, t_exec.h, t_exec.m, t_exec.s);
	print_log(str);
    }

    if (print_opt & PLOM_PRINT_BEST) {
        FILE *p_file_trace = plom_fopen(SFR_PATH, GENERAL_ID, "trace", "w", header_trace, p_kalman->p_data);
        print_trace(p_file_trace, 0, p_kalman->p_best, p_kalman->p_data, log_like);
        plom_fclose(p_file_trace);
    }

    plom_print_done(theta, p_kalman->p_data, p_kalman->p_best, SFR_PATH, GENERAL_ID, print_opt);

    if (!(print_opt & PLOM_QUIET)) {
	print_log("clean up...");
    }

    json_decref(theta);
    clean_kalman(p_kalman);

    return 0;
}
AnimationList& AnimationLoader::load_json_from_file(const char* filename) {

  return load_json(load_text_file_content(filename));
}