Exemple #1
0
uint8_t _main_flight(json_t *data) {
    json_t *_source, *_destination, *_set_start, *_set_end, *_pilot, *_task;
    const char *source;
    char *destination, *pilot;
    size_t set_start, set_end;

    _source = json_object_get(data, "source");
    _destination = json_object_get(data, "destination");
    _set_start = json_object_get(data, "set_start");
    _set_end = json_object_get(data, "set_end");
    _pilot = json_object_get(data, "pilot");
    _task = json_object_get(data, "task");

    if (!json_is_string(_source)) {
        fprintf(stderr, "{\"error\": \"No source file provided\"}");
        return 0;
    }

    source = json_string_value(_source);

    if (!json_is_string(_destination)) {
        destination = calloc(strlen(source) + 1, sizeof(char));
        strcpy(destination, source);
        dirname(destination);
    } else {
        destination = (char *) json_string_value(_destination);
    }

    if (!json_is_string(_pilot)) {
        pilot = (char *) json_string_value(_pilot);
    }

    char out_file_1[strlen(destination) + 20];
    char out_file_2[strlen(destination) + 20];
    char out_file_3[strlen(destination) + 20];
    char out_file_4[strlen(destination) + 20];
    sprintf(out_file_1, "%s%s", destination, "/track.js");
    sprintf(out_file_2, "%s%s", destination, "/track.kml");
    sprintf(out_file_3, "%s%s", destination, "/track_earth.kml");
    sprintf(out_file_4, "%s%s", destination, "/track_split.kml");

    char *igc_file = load_file(source);
    if (igc_file != NULL) {
        coordinate_set_t *set = NEW(coordinate_set_t, 1);
        coordinate_set_init(set);

        task_t *read_task = NULL;
        coordinate_set_parse_igc(set, igc_file, &read_task);

        size_t initial_length = set->length;
        coordinate_set_trim(set);
        coordinate_set_repair(set);
        coordinate_set_simplify(set, 1500);
        coordinate_set_extrema(set);

        json_t *_section = json_object_get(data, "section");
        size_t section;
        if (_section) {
            section = json_integer_value(_section);
            coordinate_set_select_section(set, section, 0);
        }

        if (set->subset_count == 1) {

            task_t *task = parse_task(_task);
            if (!task) {
                task = read_task;
            }

            distance_map_t *map = NEW(distance_map_t, 1);
            distance_map_init(map, set);

            char *ids;

            printf("{");
            printf("\"validated\": %d,", validate_file(source));
            printf("\"total_points\": %d,", initial_length);
            printf("\"sets\": %d,", set->subset_count);
            printf("\"date\": \"%04d-%02d-%02d\",", set->year, set->month, set->day);
            printf("\"start_time\": %d,", set->first->timestamp);
            printf("\"duration\": %d,", set->last->timestamp - set->first->timestamp);
            printf("\"points\": %d,", set->length);
            printf("\"stats\": {", set->length);
            printf("\"height\" : {\"min\": %d, \"max\": %d},", set->first->ele, set->last->ele);
            printf("\"speed\" : {\"min\": %d, \"max\": %d},", set->first->speed, set->last->speed);
            printf("\"climb_rate\" : {\"min\": %d, \"max\": %d}", set->first->climb_rate, set->last->climb_rate);
            printf("},");
            printf("\"task\": {", set->length);

            task_t *od, *or, *tr, *ft;

            od = distance_map_score_open_distance_3tp(map);
            or = distance_map_score_out_and_return(map);
            tr = distance_map_score_triangle(map, 0.28);
            ft = distance_map_score_triangle(map, 0);

            format_task(od, "open_distance", OPEN_DISTANCE);
            printf(",");

            format_task(or, "out_and_return", OUT_AND_RETURN);
            printf(",");

            format_task(tr, "triangle", TRIANGLE);
            printf(",");

            format_task(ft, "flat_triangle", FLAT_TRIANGLE);

            if (task) {
                printf(",");
                format_task(task, "declared", task->type);
                printf(", \"complete\": %d", task_completes_task(task, set));
            }

            printf("}, \"output\": {\"js\": \"%s\",\"kml\": \"%s\",\"earth\": \"%s\"}", out_file_1, out_file_2, out_file_3);

            formatter_t *formatter;

            formatter = NEW(formatter_t, 1);
            formatter_js_init(formatter, set, 1, od, or, tr, ft);
            formatter_js_output(formatter, out_file_1);
            free(formatter);

            formatter = NEW(formatter_t, 1);
            formatter_kml_init(formatter, set, pilot ?: "N/A", od, or, tr, ft, task);
            formatter_kml_output(formatter, out_file_2);
            free(formatter);

            formatter = NEW(formatter_t, 1);
            formatter_kml_earth_init(formatter, set, pilot ?: "N/A", od, or, tr, ft, task);
            formatter_kml_earth_output(formatter, out_file_3);
            free(formatter);

            printf("}");

            if (od) {
                task_deinit(od);
            }
            if (or) {
                task_deinit(or);
            }
            if (tr) {
                task_deinit(tr);
            }
            distance_map_deinit(map);

        } else {
Exemple #2
0
int main(int argc, char *argv[])
{
	try
	{
		setlocale(LC_ALL, "Russian");
		std::vector<std::string> addresses;
		std::vector< std::stack<int> > ports;
		if( !read_net(addresses, ports) )
            throw simple_exception("Error : reading grid net description failed");

		grid_client gc;
		gc.run(addresses, ports);

		menu_t menu = get_menu();
		while(1)
		{
			std::string user_str;
			std::getline(std::cin, user_str);
			boost::to_lower(user_str);
			std::vector<std::string> split_vec;
			boost::algorithm::split(split_vec, user_str, boost::algorithm::is_any_of(" \t"));
			
			if( split_vec.empty() ) continue;

			std::string user_command = split_vec.front();
			std::string task_name = split_vec.size() > 1 ? split_vec[1] : std::string();
			grid_task gt;

			//***********************************************************************
			if( user_command == menu[QUIT].command )
			{
				break;
			}
			//***********************************************************************
			else if( user_command == menu[APPLY_TASK].command )
			{
				if( !task_name.empty() )
				{
					std::ifstream fin(task_name.data());
					if( fin )
					{
						if( parse_task(gt, fin) )
							gc.apply_task(gt);
					}
					else
						std::cerr << "Error : cannot open task.txt" << std::endl;
					fin.close();
				}
				else
					std::cout << menu[APPLY_TASK].definition << std::endl;
			}
			//***********************************************************************
			else if( user_command == menu[REMOVE_TASK].command )
			{
				if( !task_name.empty() )
					gc.remove_task(task_name);
				else
					std::cout << menu[REMOVE_TASK].definition << std::endl;
			}
			//***********************************************************************
			else if( user_command == menu[REFRESH_STATUS].command )
			{
				if( !task_name.empty() )
					gc.refresh_status(task_name);
				else
					std::cout << menu[REFRESH_STATUS].definition << std::endl;
			}
			//***********************************************************************
			else if( user_command == menu[GET_RESULT].command )
			{
				if( !task_name.empty() )
					gc.get_result(task_name);
				else
					std::cout << menu[GET_RESULT].definition << std::endl;
			}
			//***********************************************************************
			else if( user_command == menu[STATUS].command )
			{
				if( !task_name.empty() )
					std::cout << gc.task_status_message(task_name) << std::endl;
				else
					std::cout << menu[STATUS].definition << std::endl;
			}
			//***********************************************************************
			else if( user_command == menu[TASKS].command )
			{
				grid_client::pair_string_vector tasks;
				gc.tasks(tasks);
				for(grid_client::pair_string_vector::const_iterator i = tasks.begin(); i < tasks.end(); ++i)
					std::cout << i->first << '\t' << i->second << std::endl;
			}
			//***********************************************************************
			else
			{
				for(menu_t::const_iterator i = menu.begin(); i != menu.end(); ++i)
					std::cout << i->second.command << '\t' << i->second.definition << std::endl;
			}
			//***********************************************************************
		}

		gc.stop();
	}
	catch(const std::exception &ex)
	{
		std::cerr << ex.what() << std::endl;
	}
	return 0;
}
int
main (int argc, char **argv)
{
  error_t err;
  task_t task;
  int search = 0;
  unsigned show = 0;		/* what info we print */
  mach_port_type_t only = 0, target_only = 0; /* Which names to show */
  task_t xlate_task = MACH_PORT_NULL;
  int no_translation_errors = 0; /* inhibit complaints about bad names */
  struct port_name_xlator *xlator = 0;

  /* Parse our options...  */
  error_t parse_opt (int key, char *arg, struct argp_state *state)
    {
      switch (key)
	{
	case 'v': show |= PORTINFO_DETAILS; break;
	case 'm': show |= PORTINFO_MEMBERS; break;
	case 'x': show |= PORTINFO_HEX_NAMES; break;

	case 'r': only |= MACH_PORT_TYPE_RECEIVE; break;
	case 's': only |= MACH_PORT_TYPE_SEND; break;
	case 'o': only |= MACH_PORT_TYPE_SEND_ONCE; break;
	case 'd': only |= MACH_PORT_TYPE_DEAD_NAME; break;
	case 'p': only |= MACH_PORT_TYPE_PORT_SET; break;

	case 'R': target_only |= MACH_PORT_TYPE_RECEIVE; break;
	case 'S': target_only |= MACH_PORT_TYPE_SEND; break;
	case 'O': target_only |= MACH_PORT_TYPE_SEND_ONCE; break;

	case 't': xlate_task = parse_task (arg); break;
	case 'a': search = 1; break;
	case 'E': no_translation_errors = 1; break;

	case '*':
	  hold = 1;
	  while (hold)
	    sleep (1);
	  break;

	case ARGP_KEY_NO_ARGS:
	  argp_usage (state);
	  return EINVAL;

	case ARGP_KEY_ARG:
	  if (state->arg_num == 0)
	    /* The task  */
	    {
	      task = parse_task (arg);

	      if (only == 0)
		only = ~0;
	      if (target_only == 0)
		target_only = ~0;

	      if (xlate_task != MACH_PORT_NULL)
		{
		  if (search)
		    argp_error (state,
				"Both --search and --translate specified");
		  err = port_name_xlator_create (task, xlate_task, &xlator);
		  if (err)
		    error (13, err, "Cannot setup task translation");
		}

	      if (state->next == state->argc)
		/* No port names specified, print all of them.  */
		{
		  if (xlator)
		    err = print_xlated_task_ports_info (xlator, only,
							show, stdout);
		  else
		    err = print_task_ports_info (task, only, show, stdout);
		  if (err)
		    error (12, err, "%s", arg);
		}
	      break;
	    }

	  /* A port name  */
	  {
	    char *end;
	    mach_port_t name = strtoul (arg, &end, 0);
	    if (name == 0)
	      error (0, 0, "%s: Invalid port name", arg);
	    else
	      {
		if (xlator)
		  {
		    err = print_xlated_port_info (name, 0, xlator,
						  show, stdout);
		    if (err && no_translation_errors)
		      break;
		  }
		else
		  err = print_port_info (name, 0, task, show, stdout);
		if (err)
		  error (0, err, "%s", arg);
	      }
	  }
	  break;

	default:
	  return ARGP_ERR_UNKNOWN;
	}
      return 0;
    }
  const struct argp argp = { options, parse_opt, args_doc, doc };

  /* Parse our arguments.  */
  argp_parse (&argp, argc, argv, 0, 0, 0);

  exit (0);
}