コード例 #1
0
int main(int argc, char **argv)
{
	MainArg arg;
	aco_parameters para;

	if(!argument_parse(&arg, argc, argv))
	{
		exit(EXIT_FAILURE);
	}

	init_parameters(&para,
			arg.fon_port,
			arg.aco_port,
			PACKETS_PER_CYCLE,
			CYCLE_INTERVAL,
			PHEROMONE_MAX,
			PHEROMONE_MIN,
			ENDURANCE_MAX,
			ANT_EVAPORATION_RATE);

	AcoDaemon* daemon = aco_daemon_create(&para);

	// run
	aco_daemon_run(daemon);

	return 0;
}
コード例 #2
0
ファイル: material_parser.cpp プロジェクト: Grumbel/viewer
void
MaterialParser::parse(std::istream& in)
{
  bool default_program = true;
  bool has_diffuse_texture  = false;
  bool has_specular_texture = false;
  bool has_reflection_texture = false;
  int current_texture_unit = 0;
  std::string program_vertex   = "src/glsl/default.vert";
  std::string program_fragment = "src/glsl/default.frag";
  std::vector<std::string> program_vertex_defines;
  std::vector<std::string> program_fragment_defines;

  m_material->enable(GL_CULL_FACE);
  m_material->enable(GL_DEPTH_TEST);

  m_material->set_uniform("material.ambient", glm::vec3(1.0f, 1.0f, 1.0f));

  int line_number = 0;
  std::string line;
  while(std::getline(in, line))
  {
    try
    {
      line_number += 1;
      std::vector<std::string> args = argument_parse(line);

      if (!args.empty())
      {
        if (args[0] == "material.diffuse")
        {
          m_material->set_uniform("material.diffuse",
                                  to_vec3(args.begin()+1, args.end(),
                                          glm::vec3(1.0f, 1.0f, 1.0f)));
        }
        else if (args[0] == "material.diffuse_texture")
        {
          has_diffuse_texture = true;
          if (args.size() == 2)
          {
            std::string diffuse_texture_name = to_string(args.begin()+1, args.end());
            if (diffuse_texture_name == "buildin://video-texture")
            {
              m_material->set_video_texture(current_texture_unit);
            }
            else
            {
              m_material->set_texture(current_texture_unit, Texture::from_file(diffuse_texture_name));
            }
          }
          else if (args.size() == 3)
          {
            m_material->set_texture(current_texture_unit,
                                    Texture::from_file(args[1]),
                                    Texture::from_file(args[2]));
          }
          else
          {
            throw std::runtime_error("broken");
          }
          m_material->set_uniform("material.diffuse_texture", current_texture_unit);
          current_texture_unit += 1;
        }
        else if (args[0] == "material.specular")
        {
          m_material->set_uniform("material.specular",
                                  to_vec3(args.begin()+1, args.end(),
                                          glm::vec3(1.0f, 1.0f, 1.0f)));
        }
        else if (args[0] == "material.specular_texture")
        {
          has_specular_texture = true;
          m_material->set_texture(current_texture_unit, Texture::from_file(to_string(args.begin()+1, args.end())));
          m_material->set_uniform("material.specular_texture", current_texture_unit);
          current_texture_unit += 1;
        }
        else if (args[0] == "material.shininess")
        {
          m_material->set_uniform("material.shininess",
                                  to_float(args.begin()+1, args.end()));
        }
        else if (args[0] == "material.reflection_texture")
        {
          has_reflection_texture = true;
          m_material->set_texture(current_texture_unit, Texture::cubemap_from_file(to_string(args.begin()+1, args.end())));
          m_material->set_uniform("material.reflection_texture", current_texture_unit);
          current_texture_unit += 1;
        }
        else if (args[0] == "material.ambient")
        {
          m_material->set_uniform("material.ambient",
                                  to_vec3(args.begin()+1, args.end(),
                                          glm::vec3(1.0f, 1.0f, 1.0f)));
        }
        else if (args[0] == "blend_mode")
        {
          //m_material->set_
        }
        else if (args[0] == "program.vertex")
        {
          program_vertex = args[1];
          if (args.size() > 2)
          {
            program_vertex_defines.insert(program_vertex_defines.end(),
                                          args.begin() + 2, args.end());
          }
          default_program = false;
        }
        else if (args[0] == "program.fragment")
        {
          program_fragment = args[1];
          if (args.size() > 2)
          {
            program_fragment_defines.insert(program_fragment_defines.end(),
                                            args.begin() + 2, args.end());
          }
          default_program = false;
        }
        else if (args[0] == "material.disable")
        {
          if (args[1] == "cull_face")
          {
            m_material->disable(GL_CULL_FACE);
          }
          else
          {
            throw std::runtime_error("unknown token: " + args[1]);
          }
        }
        else if (boost::algorithm::starts_with(args[0], "uniform."))
        {
          std::string uniform_name = args[0].substr(8);
          int count = args.end() - args.begin() - 1;
          if (count == 3)
          {
            m_material->set_uniform(uniform_name,
                                    to_vec3(args.begin()+1, args.end(),
                                            glm::vec3(1.0f, 1.0f, 1.0f)));
          }
          else if (count == 1)
          {
            m_material->set_uniform(uniform_name, to_float(args.begin()+1, args.end()));
          }
          else
          {
            throw std::runtime_error("unknown argument count: " + args[0]);
          }
        }
        else
        {
          throw std::runtime_error("unknown token: " + args[0]);
        }
      }
    }
    catch(const std::exception& err)
    {
      throw std::runtime_error(format("%s:%d: error: %s at line:\n%s", m_filename, line_number, err.what(), line));
    }
  }

  if (default_program)
  {
    if (has_diffuse_texture)
    {
      program_fragment_defines.emplace_back("DIFFUSE_COLOR_FROM_TEXTURE");
    }
    else
    {
      program_fragment_defines.emplace_back("DIFFUSE_COLOR_FROM_MATERIAL");
    }

    if (has_specular_texture)
    {
      program_fragment_defines.emplace_back("SPECULAR_COLOR_FROM_TEXTURE");
    }
    else
    {
      program_fragment_defines.emplace_back("SPECULAR_COLOR_FROM_MATERIAL");
    }

    if (has_reflection_texture)
    {
      program_fragment_defines.emplace_back("REFLECTION_TEXTURE");
    }

    program_fragment_defines.emplace_back("SHADOW_VALUE_4");
  }

  ProgramPtr program = Program::create(Shader::from_file(GL_VERTEX_SHADER, program_vertex, program_vertex_defines),
                                       Shader::from_file(GL_FRAGMENT_SHADER, program_fragment, program_fragment_defines));
  m_material->set_program(program);
}
コード例 #3
0
ファイル: main.c プロジェクト: l3ib/fsniper
int main(int argc, char** argv)
{
    int ifd, len = 0, i = 0, selectret = 0, maxfd, retryselect, pid;
    char buf[BUF_LEN]; 
    char *configdir;
    char *pidfilename;
    char *statusfilename;
    char *statusbin;
    char *error_str;
    char *version_str = PACKAGE_STRING;
    char *pbuf;
    char *filename;
    FILE *pidfile;
    FILE *statusfile;
    fd_set set;
    struct inotify_event *event;
    struct argument *argument = argument_new();
    struct pipe_list *pipe_list_cur;
    struct stat file_stat;
    struct watchnode *node;

    /* alloc pipe list */
    pipe_list_head = malloc(sizeof(struct pipe_list));
    pipe_list_head->next = NULL;

    /* set up signals for exiting/reaping */ 
    signal(SIGINT, &handle_quit_signal); 
    signal(SIGTERM, &handle_quit_signal);
    signal(SIGCHLD, &handle_child_signal);
    signal(SIGHUP, &handle_hup_signal);


    /* add command line arguments */
    argument_register(argument, "help", "Prints this help text.", 0);
    argument_register(argument, "version", "Prints version information.", 0);
    argument_register(argument, "daemon", "Run as a daemon.", 0);
    argument_register(argument, "verbose", "Turns on debug text.", 0);
    argument_register(argument, "sync", "Sync mode (for debugging).", 0);
    argument_register(argument, "log-to-stdout", "Deprecated, use \"--log-to=stdout\" instead", 0);
    argument_register(argument, "log-to", "Log messages with specified way. "
#ifdef USE_SYSLOG
                                "Can be: stdout, file, syslog. \"file\" by default.", 1);
#else
                                "Can be: stdout, file. \"file\" by default.", 1);
#endif

    if ((error_str = argument_parse(argument, argc, argv))) {
	fprintf(stderr, "Error in arguments: %s", error_str);
	free(error_str);
	return -1;
    }

    if (argument_exists(argument, "help")) {
	char *help_txt = argument_get_help_text(argument);
	printf("%s", help_txt);
	free(help_txt);
	return 0;
    }

    if (argument_exists(argument, "version")) {
	printf("%s\n", version_str);
	return 0;
    }

    if (argument_exists(argument, "verbose")) {
	verbose = 1;
    }

    if (argument_exists(argument, "daemon") && fork())
	return 0;

    if (argument_exists(argument, "sync"))
	syncmode = 1;


    if (argument_exists(argument, "log-to-stdout"))
        fprintf(stderr, "Warning, this option is deprecated, " \
                        "please use new syntax: \"--log-to=stdout\".\n");

    logtype = LOG_FILE;
    if (argument_exists(argument, "log-to") && \
        (log_arg = argument_get_value(argument, "log-to")) != NULL)
    {
        if      (strcmp(log_arg, "stdout") == 0)
            logtype = LOG_STDOUT;
#ifdef USE_SYSLOG
        else if (strcmp(log_arg, "syslog") == 0)
            logtype = LOG_SYS;
#endif
        else /* logtype already set to 'file' above */
            fprintf(stderr, "Warning, selected unknown logging type. " \
                            "Will use \"--log-to=file\" instead.\n");
    }

    /* get config dir (must free this) */
    configdir = get_config_dir();	

    /* if a config file has not been specified, use default */
    if (argument_get_extra(argument))
    {
	configfile = strdup(argument_get_extra(argument));
    }
    else
    {
	configfile = malloc (strlen(configdir) + strlen ("/config") + 1);
	sprintf(configfile, "%s/config", configdir);
    }

    argument_free(argument);
    free(configdir);

    if (access(configfile, R_OK) != 0)
    {
	fprintf(stderr, "error: could not open config file: %s\n", configfile);
	return -1;
    }

    /* create a pid file */
    pidfilename = get_pid_filename();
	
    if (stat(pidfilename, &file_stat) == 0) /* pidfile exists */
    {
	pidfile = fopen(pidfilename, "r");
		
	if (fscanf(pidfile, "%d", &pid) == 1) /* pidfile has a pid inside */
	{
	    char *binaryname;
	    char *scanformat; 
	    if ((binaryname = strrchr(argv[0], '/')) != NULL)
	    {
		binaryname++;
	    }
	    else
	    {
		binaryname = argv[0];
	    }

	    scanformat = malloc(strlen("Name:   %") + strlen(binaryname) + strlen("s") + 1);
	    statusfilename = malloc(strlen("/proc/") + 6 + strlen("/status") + 1);
	    sprintf(statusfilename, "/proc/%d/status", pid);

	    if (stat(statusfilename, &file_stat) != 0) /* write pid file if the process no longer exists */
	    {
		write_pid_file(pidfilename);
	    }
	    else /* process exists, so check owner and binary name */
	    {
		statusfile = fopen(statusfilename, "r");
		statusbin = malloc(strlen(binaryname) + 2); /* the binary name may start with "fsniper" but be longer */
		sprintf(scanformat, "Name:   %%%ds", strlen(binaryname) + 1);
		fscanf(statusfile, scanformat, statusbin);
		free(statusfilename);
		fclose(statusfile);
		fclose(pidfile);
				
		if (strcmp(binaryname, statusbin) == 0 && file_stat.st_uid == getuid())
		    /* exit if the process is fsniper and is owned by the current user */
		{
		    printf("%s: already running instance found with pid %d. exiting.\n", binaryname, pid);
		    exit(1);
		}
		else /* the pid file contains an old pid, one that isn't fsniper, or one not owned by the current user */
		{
		    write_pid_file(pidfilename);
		}
	    }
	}
	else /* pidfile is invalid */
	{
	    fclose(pidfile);
	    write_pid_file(pidfilename);
	}
    }
    else /* the pidfile doesn't exist */
    {
	write_pid_file(pidfilename);
    }
    free(pidfilename);

    /* start up log */
    if (!log_open())
    {
	fprintf(stderr, "Error: could not start log.\n");
	return -1;
    }

    ifd = inotify_init();
    if (ifd < 0)
    {
	perror("inotify_init");
	return -1;
    }

    if (verbose) log_write("Parsing config file: %s\n", configfile);
    config = keyval_parse_file(configfile);

    if ((error_str = keyval_get_error())) {
        fprintf(stderr, "%s", error_str);
        free(error_str);
        exit(1);
    }

    validate_config(config);

    /* add nodes to the inotify descriptor */
    g_watchnode = add_watches(ifd);

    /* wait for events and then handle them */
    while (1)
    {		
	/* set up fds and max */
	FD_ZERO(&set);
	FD_SET(ifd, &set);
	maxfd = ifd;
	for (pipe_list_cur = pipe_list_head->next; pipe_list_cur; pipe_list_cur = pipe_list_cur->next)
	{
	    FD_SET(pipe_list_cur->pfd[0], &set);
	    if (pipe_list_cur->pfd[0] > maxfd)
		maxfd = pipe_list_cur->pfd[0];
	}

	retryselect = 1;
	while (retryselect)
	{
	    /* use select to get activity on any of the fds */
	    selectret = select(maxfd + 1, &set, NULL, NULL, NULL);

	    if (selectret == -1)
	    {
		if (errno == EINTR)
		    retryselect = 1;
		else
		    handle_quit_signal(-2);
	    } else
		retryselect = 0;
	}
		
	/* handle any events on the inotify fd */
	if (FD_ISSET(ifd, &set))
	{
	    len = read(ifd, buf, BUF_LEN);
	    while (i < len)
	    {
		event = (struct inotify_event *) &buf[i];
		if (event->len && (event->mask & IN_CLOSE_WRITE || event->mask & IN_MOVED_TO))
		{
		    /* if sync mode, just call handle_exec */
		    if (syncmode == 1)
		    {
			handle_event(event, fileno(_logfd));
		    }
		    else
		    {
			/* create new pipe_list entry */
			for (pipe_list_cur = pipe_list_head; pipe_list_cur->next != NULL; pipe_list_cur = pipe_list_cur->next) {}

			pipe_list_cur->next = malloc(sizeof(struct pipe_list));
			pipe_list_cur->next->next = NULL;

			/* create pipe */
			pipe(pipe_list_cur->next->pfd);

			if (fork() == 0) 
			{
			    /* child, close 0 */
			    close(pipe_list_cur->next->pfd[0]);					
			    log_close();
			    signal(SIGINT, &handle_child_quit_signal);
			    signal(SIGTERM, &handle_child_quit_signal);
			    handle_event(event, pipe_list_cur->next->pfd[1]);
			} else {
			    /* parent, close 1 */
			    close(pipe_list_cur->next->pfd[1]);
			}
		    }
		}
                else if (event->len && (event->mask & IN_CREATE && event->mask & IN_ISDIR))
                {
                    for (node = g_watchnode->next; node; node = node->next)
                        if (node->wd == event->wd)
                            break;

                    if (node)
                    {
                        /* combine the name inotify gives with the full path to the file */
                        filename = malloc(strlen(node->path) + strlen("/") + strlen(event->name) + 1);
                        sprintf(filename, "%s/%s", node->path, event->name);
                        watch_dir(node, ifd, strdup(filename), node->section);
                        free(filename);
                    }
                }
		else if (event->len && (event->mask & IN_DELETE && event->mask & IN_ISDIR))
                {
                    for (node = g_watchnode->next; node; node = node->next)
                        if (node->wd == event->wd)
                            break;

                    if (node)
                    {
                        /* combine the name inotify gives with the full path to the file */
                        filename = malloc(strlen(node->path) + strlen("/") + strlen(event->name) + 1);
                        sprintf(filename, "%s/%s", node->path, event->name);
                        unwatch_dir(filename, ifd);
                        free(filename);
                    }
                }
                i += EVENT_SIZE + event->len;
            }
	    i = 0;
	}
		
	/* now lets see if we have any pipe activity */
	pipe_list_cur = pipe_list_head->next;
	while (pipe_list_cur)
	{
	    if (FD_ISSET(pipe_list_cur->pfd[0], &set))
	    {
		len = read(pipe_list_cur->pfd[0], buf, BUF_LEN);
		if (len == 0)
		{
		    close(pipe_list_cur->pfd[0]);
		    /* remove this item from the list */
		    pipe_list_cur = pipe_list_remove(pipe_list_head, pipe_list_cur);
					
		} else {
		    /* print it somewhere */
		    pbuf = malloc(len + 1);
		    snprintf(pbuf, len, "%s", buf);
		    log_write("%s\n", pbuf);
		    free(pbuf);
		    pipe_list_cur = pipe_list_cur->next;
		}
	    } else {
		pipe_list_cur = pipe_list_cur->next;

	    }


	}
    }
}