Exemplo n.º 1
0
Arquivo: naev.c Projeto: Elderman/naev
/**
 * @brief The entry point of Naev.
 *
 *    @param[in] argc Number of arguments.
 *    @param[in] argv Array of argc arguments.
 *    @return EXIT_SUCCESS on success.
 */
int main( int argc, char** argv )
{
   char buf[PATH_MAX];

   /* Save the binary path. */
   binary_path = strdup(argv[0]);

   /* Print the version */
   LOG( " "APPNAME" v%s", naev_version(0) );
#ifdef GIT_COMMIT
   DEBUG( " git HEAD at " GIT_COMMIT );
#endif /* GIT_COMMIT */

   /* Initializes SDL for possible warnings. */
   SDL_Init(0);

   /* Initialize the threadpool */
   threadpool_init();

   /* Set up debug signal handlers. */
   debug_sigInit();

   /* Create the home directory if needed. */
   if (nfile_dirMakeExist("%s", nfile_configPath()))
      WARN("Unable to create config directory '%s'", nfile_configPath());

   /* Must be initialized before input_init is called. */
   if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
      WARN("Unable to initialize SDL Video: %s", SDL_GetError());
      return -1;
   }

   /* Get desktop dimensions. */
#if SDL_VERSION_ATLEAST(1,2,10)
   const SDL_VideoInfo *vidinfo = SDL_GetVideoInfo();
   gl_screen.desktop_w = vidinfo->current_w;
   gl_screen.desktop_h = vidinfo->current_h;
#else /* #elif SDL_VERSION_ATLEAST(1,2,10) */
   gl_screen.desktop_w = 0;
   gl_screen.desktop_h = 0;
#endif /* #elif SDL_VERSION_ATLEAST(1,2,10) */

   /* We'll be parsing XML. */
   LIBXML_TEST_VERSION
   xmlInitParser();

   /* Input must be initialized for config to work. */
   input_init();

   /* Set the configuration. */
   nsnprintf(buf, PATH_MAX, "%s"CONF_FILE, nfile_configPath());

#if HAS_UNIX
   /* TODO get rid of this cruft ASAP. */
   int oldconfig = 0;
   if (!nfile_fileExists( buf )) {
      char *home, buf2[PATH_MAX];
      home = SDL_getenv( "HOME" );
      if (home != NULL) {
         nsnprintf( buf2, PATH_MAX, "%s/.naev/"CONF_FILE, home );
         if (nfile_fileExists( buf2 ))
            oldconfig = 1;
      }
   }
#endif /* HAS_UNIX */

   conf_setDefaults(); /* set the default config values */
   conf_loadConfig(buf); /* Lua to parse the configuration file */
   conf_parseCLI( argc, argv ); /* parse CLI arguments */

   /* Enable FPU exceptions. */
#if defined(HAVE_FEENABLEEXCEPT) && defined(DEBUGGING)
   if (conf.fpu_except)
      feenableexcept( FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );
#endif /* defined(HAVE_FEENABLEEXCEPT) && defined(DEBUGGING) */

   /* Open data. */
   if (ndata_open() != 0)
      ERR("Failed to open ndata.");

   /* Load the start info. */
   if (start_load())
      ERR("Failed to load module start data.");

   /* Load the data basics. */
   LOG(" %s", ndata_name());
   DEBUG();

   /* Display the SDL Version. */
   print_SDLversion();
   DEBUG();

   /* random numbers */
   rng_init();

   /*
    * OpenGL
    */
   if (gl_init()) { /* initializes video output */
      ERR("Initializing video output failed, exiting...");
      SDL_Quit();
      exit(EXIT_FAILURE);
   }
   window_caption();
   gl_fontInit( NULL, NULL, FONT_SIZE ); /* initializes default font to size */
   gl_fontInit( &gl_smallFont, NULL, FONT_SIZE_SMALL ); /* small font */

   /* Display the load screen. */
   loadscreen_load();
   loadscreen_render( 0., "Initializing subsystems..." );
   time_ms = SDL_GetTicks();


   /*
    * Input
    */
   if ((conf.joystick_ind >= 0) || (conf.joystick_nam != NULL)) {
      if (joystick_init()) WARN("Error initializing joystick input");
      if (conf.joystick_nam != NULL) { /* use the joystick name to find a joystick */
         if (joystick_use(joystick_get(conf.joystick_nam))) {
            WARN("Failure to open any joystick, falling back to default keybinds");
            input_setDefault();
         }
         free(conf.joystick_nam);
      }
      else if (conf.joystick_ind >= 0) /* use a joystick id instead */
         if (joystick_use(conf.joystick_ind)) {
            WARN("Failure to open any joystick, falling back to default keybinds");
            input_setDefault();
         }
   }


   /*
    * OpenAL - Sound
    */
   if (conf.nosound) {
      LOG("Sound is disabled!");
      sound_disabled = 1;
      music_disabled = 1;
   }
   if (sound_init()) WARN("Problem setting up sound!");
   music_choose("load");

   /* FPS stuff. */
   fps_setPos( 15., (double)(gl_screen.h-15-gl_defFont.h) );

   /* Misc graphics init */
   if (nebu_init() != 0) { /* Initializes the nebula */
      /* An error has happened */
      ERR("Unable to initialize the Nebula subsystem!");
      /* Weirdness will occur... */
   }
   gui_init(); /* initializes the GUI graphics */
   toolkit_init(); /* initializes the toolkit */
   map_init(); /* initializes the map. */
   cond_init(); /* Initialize conditional subsystem. */
   cli_init(); /* Initialize console. */

   /* Data loading */
   load_all();

   /* Generate the CVS. */
   if (conf.devcsv)
      dev_csv();

   /* Unload load screen. */
   loadscreen_unload();

   /* Start menu. */
   menu_main();

   /* Force a minimum delay with loading screen */
   if ((SDL_GetTicks() - time_ms) < NAEV_INIT_DELAY)
      SDL_Delay( NAEV_INIT_DELAY - (SDL_GetTicks() - time_ms) );
   fps_init(); /* initializes the time_ms */

#if HAS_UNIX
   /* Tell the player to migrate their configuration files out of ~/.naev */
   /* TODO get rid of this cruft ASAP. */
   if (oldconfig) {
      char path[PATH_MAX], *script, *home;
      uint32_t scriptsize;
      int ret;

      nsnprintf( path, PATH_MAX, "%s/naev-confupdate.sh", ndata_getDirname() );
      home = SDL_getenv("HOME");
      ret = dialogue_YesNo( "Warning", "Your configuration files are in a deprecated location and must be migrated:\n"
            "   \er%s/.naev/\e0\n\n"
            "The update script can likely be found in your Naev data directory:\n"
            "   \er%s\e0\n\n"
            "Would you like to run it automatically?", home, path );

      /* Try to run the script. */
      if (ret) {
         ret = -1;
         /* Running from ndata. */
         if (ndata_getPath() != NULL) {
            script = ndata_read( "naev-confupdate.sh", &scriptsize );
            if (script != NULL)
               ret = system(script);
         }

         /* Running from laid-out files or ndata_read failed. */
         if ((nfile_fileExists(path)) && (ret == -1)) {
            script = nfile_readFile( (int*)&scriptsize, path );
            if (script != NULL)
               ret = system(script);
         }

         /* We couldn't find the script. */
         if (ret == -1) {
            dialogue_alert( "The update script was not found at:\n\er%s\e0\n\n"
                  "Please locate and run it manually.", path );
         }
         /* Restart, as the script succeeded. */
         else if (!ret) {
            dialogue_msg( "Update Completed",
                  "Configuration files were successfully migrated. Naev will now restart." );
            execv(argv[0], argv);
         }
         else { /* I sincerely hope this else is never hit. */
            dialogue_alert( "The update script encountered an error. Please exit Naev and move your config and save files manually:\n\n"
                  "\er%s/%s\e0 =>\n   \eD%s\e0\n\n"
                  "\er%s/%s\e0 =>\n   \eD%s\e0\n\n"
                  "\er%s/%s\e0 =>\n   \eD%snebula/\e0\n\n",
                  home, ".naev/conf.lua", nfile_configPath(),
                  home, ".naev/{saves,screenshots}/", nfile_dataPath(),
                  home, ".naev/gen/*.png", nfile_cachePath() );
         }
      }
      else {
         dialogue_alert(
               "To manually migrate your configuration files "
               "please exit Naev and run the update script, "
               "likely found in your Naev data directory:\n"
               "   \er%s/naev-confupdate.sh\e0", home, path );
      }
   }
Exemplo n.º 2
0
centernode_t * centernode_init (const char *progname, const char *cfgfile)
{
    char filename[100];

    centernode_t *cn = (centernode_t *) calloc(sizeof(centernode_t), sizeof(char));
    if (!cn) abort();

    //Initialize the global mutex.
    pthread_mutex_init(&cn->seqlock, 0);
    pthread_mutex_init(&cn->objlock, 0);
    pthread_mutex_init(&cn->reglock, 0);
    pthread_mutex_init(&cn->evtlock, 0);
    pthread_mutex_init(&cn->glock, 0);
    pthread_mutex_init(&cn->sqllock, 0);

    //Initialiaze and load the config file.
    cn->config = calloc(sizeof(config_t), sizeof(char));
    if (!cn->config) abort();
    config_init(cn->config); /* initialize configuration */
    if (!config_read_file(cn->config, cfgfile)) {
#ifdef CDNDEBUG
        fprintf(stderr, "%s.\n", config_error_text(cn->config));
#endif
        return (free(cn), NULL);
    }
    if (centernode_config(cn))  /* load config */
        return NULL;
    strncpy(cn->confile, cfgfile, sizeof(cn->confile) -1);
    if (archive_init(cn->homepath, progname, ATASR|ATLOG, (cbsink_t)NULL, cn))
        return NULL; /* archive module */
    /*    
          if (mysqldb_init(cn->dbhost, cn->dbname, cn->dbuser, cn->dbpasswd)) // db 
          return (centernode_cleanup(cn), NULL);
          */  
    mongoc_init ();
    cn->MongoNum = cn->threads;
    cn->Mongo = (struct Mongo*)calloc (sizeof(struct Mongo)*cn->MongoNum, sizeof(char));
    int i = 0;
    for (i=0; i<cn->MongoNum; i++) {
        cn->Mongo[i].MongoClient = mongoc_client_new (cn->mongoString);
        cn->Mongo[i].MongoCollection = mongoc_client_get_collection (cn->Mongo[i].MongoClient, "mydb", "mycoll");
        pthread_mutex_init (&cn->Mongo[i].MongoLock, 0);
        if (cn->Mongo[i].MongoClient == NULL )
            abort();
    }
    cn->rdb_conn = (redisContext *) redisConnect(cn->rdb_host, cn->rdb_port);//, cn->rdb_pswd);
    printf("init cn %p rdb %p\n", cn, cn->rdb_conn);
    if (cn->rdb_conn == NULL)
    {
        LOG("WR", "Conn redis er\n");
        abort();
        return NULL;
    }
    pthread_mutex_init (&cn->rdb_lk, 0);
    redis_get_udpinfo(cn, (char **)&cn->udp_addr, &cn->udp_port);


    //Allocate management object
    cn->accesses = gdsl_hash_alloc(NULL, NULL, NULL, (gdsl_key_func_t)keyHash,
            (gdsl_hash_func_t)hashHash, (gdsl_hash_comp_t)compHash, 20000);
    cn->events = gdsl_queue_alloc(NULL, NULL, NULL);
    cn->registration = gdsl_queue_alloc(NULL, NULL, NULL);
    cn->oemconfig_lt = gdsl_list_alloc(NULL, NULL, NULL);
    cn->service_lt = gdsl_list_alloc(NULL,NULL,NULL);

    if (!(cn->tpool = threadpool_init(cn->threads, 4))) 
        return NULL;

    cn->rdbInterval = 0;
    cn->rdbLastNull = time(NULL);
    cn->rdbStatus   = 0;
    //Bind the network interface.
    cnaccess_t *ca = NULL;
    //Connect management server
    for (i=0; i<cn->local_num; ++i) 
    { /* Bind the network devices */
        ca = centernode_server(cn, cn->local[i].host, cn->local[i].port, NT_INETSRV, cn->local[i].isp);
        if (!ca) 
        {    
            return NULL;
        }    
    }  

#if 0
    if ((cn->isservice & 0x02) > 0) {
        for( i=0; i<cn->service_num; ++i )
        {
            ca = centernode_connect(cn, cn->service[i].host, cn->service[i].port, 0, NT_SERVICE_CLIENT, 0);
            if( ca )
            {
                centernode_cmd_service_startup(cn, ca,NULL,i);
                //break;
            }
        }
        if( !ca )
            return (centernode_cleanup(cn), NULL);    
    }
#endif
    sprintf(filename, "%s/%s.pid", cn->homepath, progname);
    FILE *fp = fopen(filename, "w+"); /* version number */
    assert(fp && fprintf(fp, "%u", getpid()) > 0);
    fclose(fp);

    kill(getpid(), SIGUSR1);
#ifdef CDNDEBUG
    LOG("WR", "Service CCN %s startup.\n", cn->nodeid);
#endif

    //if (appinit(cn) != 0)
    //return (centernode_cleanup(cn), NULL);
    return cn;
}
Exemplo n.º 3
0
int cmd_gethost(int argc, char **argv)
{
	int i = 1;
	char *arg;
	
	struct gethost_job *pjob = NULL;

	if ( pthread_mutex_init(&(cmd_gethost_stat.lock), NULL) ){
		printf("error: mutex init \n");
		return -1;
	}
	if( NULL == (thread_pool = threadpool_init(15, 100)) ){
		printf("error: thread pool init \n");
		return -2;
	}

	wf_registe_exit_signal(gethost_exit);
	cmd_gethost_stat.start = wf_getsys_uptime(NULL);
	while(1)
	{
		if(argv[2])
			arg = argv[++i];
		else{
			memset(asc_buf, 0, sizeof(asc_buf));
			arg = fgets(asc_buf, sizeof(asc_buf), stdin);
		}
		if( !arg )
			break;
		++cmd_gethost_stat.all_cnt;
		wipe_off_CRLF_inEnd(arg);
		if(0 == strlen(arg))
			continue;
		++cmd_gethost_stat.valid_cnt;

		pjob = (struct gethost_job *)malloc(sizeof(struct gethost_job));
		if(pjob){
			pjob->id = cmd_gethost_stat.all_cnt;
			pjob->name = strdup(arg);
			if(!pjob->name){
				free(pjob);
				goto fail_done;
			}
		}
		else{
			goto fail_done;
		}
		
		
		if( threadpool_add_job(thread_pool, gethost_job, pjob, NULL) < 0)
			goto fail_done;
		else
			continue;

	fail_done:
		pthread_mutex_lock(&(cmd_gethost_stat.lock));
		++cmd_gethost_stat.fail_cnt;
		pthread_mutex_unlock(&(cmd_gethost_stat.lock));
		continue;
	}
	printf(">>>>>>>>>>>>>>>>>>>>>>> add job num: %d \n", cmd_gethost_stat.all_cnt);

	while(cmd_gethost_stat.valid_cnt != cmd_gethost_stat.ok_cnt + cmd_gethost_stat.fail_cnt)	sleep(1);
	threadpool_destroy(thread_pool);
	gethost_result();
	
	return 0;
}