예제 #1
0
XTDBError
XTDBInitHandle(const char* dbName,const char* dir,XTDBHandle** out) {
    XTDBError rv;
    XTDBHandle* h = malloc(sizeof(XTDBHandle));
    int rc;
    //InitQueryModule();
    if (!h) {
        return XTDB_NO_MEM;
    }
    memset(h,0,sizeof(*h));
    StrInit(&h->dbName);
    if (StrAppend(&h->dbName,dbName)) {
        rv = XTDB_NO_MEM;
        goto error;
    }
    StrInit(&h->dataDir);
    if (StrAppend(&h->dataDir,dir)) {
        rv = XTDB_NO_MEM;
        goto error;
    }
    StrInit(&h->descName);
    if (StrAppendFmt(&h->descName,"%s/%s.desc",dir,dbName)) {
        rv = XTDB_NO_MEM;
        goto error;
    }
    StrInit(&h->mainDbFileName);
    if (StrAppendFmt(&h->mainDbFileName,"%s/%s.main.data",dir,dbName)) {
        rv = XTDB_NO_MEM;
        goto error;
    }

    h->indexes = tcmapnew2(23);
    if (!h->indexes) {
        rv = XTDB_NO_MEM;
        goto error;
    }
    //h->indexes = hash_table_new(stringhash,string_equal);
    //hash_table_register_free_functions(h->indexes,free,NULL);
    if (!(h->mainDB = DBInit(h->mainDbFileName.ptr,BDBOCREAT|BDBOWRITER))) {
        rv = XTDB_NO_MEM;
        goto error;
    }

    if (!(rc=DBOpen(h->mainDB,BDBOCREAT|BDBOWRITER))) {
        rv = DBGetLastError(h->mainDB);
        goto error;
    }

    if (!XTDBLoadIndexes(h)) {
        rv = h->error;
        goto error;
    }
    *out = h;
    return XTDB_OK;

error:
    XTDBFreeHandle(h);
    *out = NULL;
    return rv;
}
예제 #2
0
static uint8_t
XTDBAddIndex(XTDBHandle* h,char* fieldName) {
    String idxDBName;
    BinaryStr out;
    uint8_t success=False;
    DataBaseBE* db;

    DataBaseBE* be = XTDBGetIndex(h,fieldName);
    if (be) {
        // XXX Set error
        // Index already exists
        assert(0);
        return False;
    }
    StrToBStr(fieldName,&out);
    GetIndexDBName(h,fieldName,&idxDBName);
    db = DBInit(STR(&idxDBName),BDBOWRITER);
    //print_trace();

    if (db) {
        uint8_t rv = DBOpen(db,BDBOWRITER);
        //printf("Return value %d,%s\n",rv,tcbdberrmsg(rv));
        if (!rv) {
            //h->error = DBErrorToXTDBError(rv);
            h->error = DBGetLastError(db);
            DBFree(db);
            assert(0);
            return False;
        }
        // No error for this function
        tcmapput(h->indexes,out.data,out.len,&db,sizeof(db));
        int a;
        assert(tcmapget(h->indexes,out.data,out.len,&a));

        /*char* newStr = strdup(fieldName);
        if(hash_table_insert(h->indexes,newStr,db)) {
        }
        assert(hash_table_lookup(h->indexes,newStr));*/
    } else {
        h->error = XTDB_NO_MEM;
        return False;
    }
    StrFree(&idxDBName);
    assert(tcmaprnum(h->indexes));
    return True;
}
예제 #3
0
// checked
uint8_t
XTDBLoadIndexes(XTDBHandle* h) {
    void* idxList = NULL;
    BinaryStr bStr,out;
    uint8_t rv;

    h->descDB = DBInit(STR(&h->descName),BDBOCREAT|BDBOWRITER);
    if (!h->descDB) {
        h->error = XTDB_NO_MEM;
        return False;
    }
    rv = DBOpen(h->descDB,BDBOCREAT|BDBOWRITER|BDBOTSYNC);
    if (!rv) {
        // XXX revisit
        h->error = DBGetLastError(h->descDB);
        if (h->error == XTDB_FILE_NOT_FOUND) {
            h->error = XTDB_DESC_NOT_FOUND;
        }
        return False;
    }

    StrToBStr(IDXFIELDNAME,&bStr);

    if (DBGetList(h->descDB,&bStr,&idxList)) {
        int idxLen = DBListLen(h->descDB,idxList);
        int i;
        for (i=0; i<idxLen; i++) {
            DBListVal(h->descDB,idxList,i,&out);
            char* idxName = BStrData(&out);
            //printf("IDX in descriptor %s\n",idxName);
            if (!XTDBAddIndex(h,idxName)) {
                return False;
            }
            assert(XTDBGetIndex(h,idxName));

        }
        DBListFree(h->descDB,idxList);
    }
    return True;
}
예제 #4
0
/**
**  The main program: initialize, parse options and arguments.
*/
int main(int argc, char **argv)
{
	int status;
	int i;

	Server.Port = DEFAULT_PORT;
	Server.MaxConnections = DEFAULT_MAX_CONN;
	Server.IdleTimeout = DEFAULT_SESSION_TIMEOUT;
	Server.PollingDelay = DEFAULT_POLLING_DELAY;

	//
	// Standard SDL Init.
	//
	if (SDL_Init(0) == -1) {
		printf("SDL_Init: %s\n", SDL_GetError());
		exit(1);
	}
	atexit(SDL_Quit);

	//
	// Parse the command line.
	//
	while ((i = getopt(argc, argv, ":p:m:i:d:")) != -1) {
		switch (i) {
			case 'p':
				Server.Port = atoi(optarg);
				if (Server.Port <= 0) {
					Server.Port = DEFAULT_PORT;
				}
				break;
			case 'm':
				Server.MaxConnections = atoi(optarg);
				break;
			case 'i':
				Server.IdleTimeout = atoi(optarg);
				break;
			case 'd':
				Server.PollingDelay = atoi(optarg);
				break;
			case ':':
				printf("Missing argument for %c\n", optopt);
				exit(0);
				break;
			case '?':
				printf("Unrecognized option: -%c\n", optopt);
				break;
		}
    }

	// Initialize the database
	if (DBInit()) {
		fprintf(stderr, "DBInit failed\n");
		exit(1);
	}
	atexit(DBQuit);

	//
	// Initialize server.
	//
	// Open the server to connections.
	//
	if ((status = ServerInit(Server.Port)) != 0) {
		if (status > 0) {
			fprintf(stderr, "ERROR %d: %s\n", errno, strerror(errno));		// > 0
		} else {
			fprintf(stderr, "ERROR: %s\n", SDL_GetError());				// < 0
		}
		exit(status);
	}
	atexit(ServerQuit);

	printf("Stratagus Metaserver Initialized on port %d.\n", Server.Port);

	//
	// Uncomment this line for MSVC (or other default)
	// debugging of segmentation violations.
	//
	// signal(SIGSEGV, SIG_DFL);

	MainLoop();

	//
	// Server tasks done.
	//
	// "atexit" will take over from here for cleanup.
	//
	printf("Stratagus Metaserver Done.\n");

	return 0;
}
예제 #5
0
// XXX revisit after DB eror
uint8_t
XTDBCreateIndex(XTDBHandle* handle,char* fieldName) {
    String idxDBName;
    String descKey;
    bson idxDesc;
    BinaryStr key,value,data;
    DataBaseBE* db;
    int rc;

    StrInit(&descKey);
    if(StrAppendFmt(&descKey,"index.value.%s",fieldName)) {
        handle->error = XTDB_NO_MEM;
        return False;
    }
    StrToBStr(descKey.ptr,&key);
    if (DBGet(handle->descDB,&key,&value)) {
        // Index already exists
        printf("Index already exists.\n");
        StrFree(&descKey);
        BinaryStrFree(&value);
        handle->error= XTDB_INDEX_EXISTS;
        return False;
    }
    bson_init(&idxDesc);
    if (bson_append_string(&idxDesc,"name",fieldName)) {
        handle->error = XTDB_NO_MEM;
        bson_destroy(&idxDesc);
        StrFree(&descKey);
        return False;
    }
    if (bson_finish(&idxDesc)) {
        handle->error = XTDB_NO_MEM;
        bson_destroy(&idxDesc);
        StrFree(&descKey);
        return False;
    }

    BsonToBStr(&idxDesc,&value);
    if (!DBSet(handle->descDB,&key,&value,False)) {
        printf("Error Adding to index names list\n");
        handle->error = DBGetLastError(handle->descDB);
        bson_destroy(&idxDesc);
        StrFree(&descKey);
        return False;
    }
    StrFree(&descKey);
    StrToBStr(IDXFIELDNAME,&key);
    StrToBStr(fieldName,&value);
    if (!DBSet(handle->descDB,&key,&value,True)) {
        //printf("Error Adding to index names list\n");
        handle->error = DBGetLastError(handle->descDB);
        //handle->error = XTDB_IO_ERR;
        bson_destroy(&idxDesc);
        return False;
    }
    bson_destroy(&idxDesc);

    if (GetIndexDBName(handle,fieldName,&idxDBName)) {
        handle->error = XTDB_NO_MEM;
        return False;
    }

    db = DBInit(STR(&idxDBName),BDBOWRITER | BDBOCREAT);
    StrFree(&idxDBName);
    if (!db) {
        handle->error = XTDB_NO_MEM;
        return False;
    }

    rc = DBOpen(db,BDBOCREAT|BDBOWRITER);
    if (!rc) {
        handle->error = DBGetLastError(db);
        DBFree(db);
        return False;
    }
    DBClose(db);
    DBFree(db);
    if (!XTDBAddIndex(handle,fieldName)) {
        // AddIndex failed return
        printf("Error Adding to index names list\n");
        return False;
    }
    assert(XTDBGetIndex(handle,fieldName));
    void* iter = DBIter(handle->mainDB);
    while (DBIterCur(handle->mainDB,iter,&key,&data)) {
        //printf("Adding value to index\n");
        bson obj;
        BStrToBson(&data,&obj);
        if (!XTDBInsertToIndex(handle,&key,&data)) {
            return False;
        }
        BinaryStrFree(&key);
        BinaryStrFree(&data);
        DBIterNext(handle->mainDB,iter);
    }
    DBIterFree(handle->mainDB,iter);
    return True;
}
예제 #6
0
/*
 * This routine reads the specified file into a database and returns a
 * pointer to that database.
 */
ELT *
DBRead(register FILE *file)
{
  register int i;
  register int done;		/* flag for input exhausted */
  register double nx;		/* x holder so x is not set before orienting */
  int type;			/* element type */
  ELT *elist;			/* pointer to the file's elements */
  POINT *plist;			/* pointer for reading in points */
  char string[MAXSTRING], *txt;
  double x, y;			/* x and y are read in point coords */
  int len, brush, size;
  int lastpoint;

  SUNFILE = FALSE;
  elist = DBInit();
  (void) fscanf(file, "%" MAXSTRING_S "s%*[^\n]\n", string);
  if (strcmp(string, "gremlinfile")) {
    if (strcmp(string, "sungremlinfile")) {
      error("`%1' is not a gremlin file", gremlinfile);
      return (elist);
    }
    SUNFILE = TRUE;
  }

  (void) fscanf(file, "%d%lf%lf\n", &size, &x, &y);
  /* ignore orientation and file positioning point */

  done = FALSE;
  while (!done) {
    /* if (fscanf(file,"%" MAXSTRING_S "s\n", string) == EOF) */
    /* I changed the scanf format because the element */
    /* can have two words (e.g. CURVE SPLINE)         */
    if (fscanf(file, "\n%" MAXSTRING_S "[^\n]%*[^\n]\n", string) == EOF) {
      error("`%1', error in file format", gremlinfile);
      return (elist);
    }

    type = DBGetType(string);	/* interpret element type */
    if (type < 0) {		/* no more data */
      done = TRUE;
    } else {
#ifdef UW_FASTSCAN
      (void) xscanf(file, &x, &y);		/* always one point */
#else
      (void) fscanf(file, "%lf%lf\n", &x, &y);	/* always one point */
#endif	/* UW_FASTSCAN */
      plist = PTInit();		/* NULL point list */

      /*
       * Files created on the SUN have point lists terminated by a line
       * containing only an asterik ('*').  Files created on the AED have
       * point lists terminated by the coordinate pair (-1.00 -1.00).
       */
      if (TEXT(type)) {		/* read only first point for TEXT elements */
	nx = xorn(x, y);
	y = yorn(x, y);
	(void) PTMakePoint(nx, y, &plist);
	savebounds(nx, y);

#ifdef UW_FASTSCAN
	while (xscanf(file, &x, &y));
#else
	lastpoint = FALSE;
	do {
	  fgets(string, MAXSTRING, file);
	  if (string[0] == '*') {	/* SUN gremlin file */
	    lastpoint = TRUE;
	  } else {
	    (void) sscanf(string, "%lf%lf", &x, &y);
	    if ((x == -1.00 && y == -1.00) && (!SUNFILE))
	      lastpoint = TRUE;
	    else {
	      if (compatibility_flag)
		savebounds(xorn(x, y), yorn(x, y));
	    }
	  }
	} while (!lastpoint);
#endif	/* UW_FASTSCAN */
      } else {			/* not TEXT element */
#ifdef UW_FASTSCAN
	do {
	  nx = xorn(x, y);
	  y = yorn(x, y);
	  (void) PTMakePoint(nx, y, &plist);
	  savebounds(nx, y);
	} while (xscanf(file, &x, &y));
#else
	lastpoint = FALSE;
	while (!lastpoint) {
	  nx = xorn(x, y);
	  y = yorn(x, y);
	  (void) PTMakePoint(nx, y, &plist);
	  savebounds(nx, y);

	  fgets(string, MAXSTRING, file);
	  if (string[0] == '*') {	/* SUN gremlin file */
	    lastpoint = TRUE;
	  } else {
	    (void) sscanf(string, "%lf%lf", &x, &y);
	    if ((x == -1.00 && y == -1.00) && (!SUNFILE))
	      lastpoint = TRUE;
	  }
	}
#endif	/* UW_FASTSCAN */
      }
      (void) fscanf(file, "%d%d\n", &brush, &size);
      (void) fscanf(file, "%d", &len);	/* text length */
      (void) getc(file);		/* eat blank */
      txt = (char *) malloc((unsigned) len + 1);
      for (i = 0; i < len; ++i) {	/* read text */
        int c = getc(file);
        if (c == EOF)
          break;
	txt[i] = c;
      }
      txt[len] = '\0';
      (void) DBCreateElt(type, plist, brush, size, txt, &elist);
    }				/* end else */
  } /* end while not done */ ;
  return (elist);
}				/* end DBRead */
예제 #7
0
파일: main.cpp 프로젝트: s-alexander/UPGM
void WorkInit()
{
	/* Init logs */
	LogInit();
	
	if (payguide::working) 
		LogWrite(LOGMSG_WARNING, "Initializing payuguide server... ");
	else
		LogWrite(LOGMSG_WARNING, "Restarting payuguide server... ");
		
	/* Load config file */
	LogWrite(LOGMSG_SYSTEM, "Loading configuration file from /etc/payguide.cfg");
	if (ReloadConfigValues("/etc/payguide.cfg")!=0)
		LogWrite(LOGMSG_WARNING, "Can't load  /etc/payguide.cfg - using default values instead.");
	else
		LogWrite(LOGMSG_SYSTEM, "Configuration file /etc/payguide.cfg loaded.");
		
	
	if (pc_init_result==-1)
	{
		if (payguide::bind_interface!="all" && payguide::bind_interface!="ALL")
			pc_init_result=PCInit(payguide::pc_port, 100, payguide::bind_interface.c_str(), payguide::users_filename.c_str(), payguide::package_timeout);
		else pc_init_result=PCInit(payguide::pc_port, 100, NULL, payguide::users_filename.c_str(), payguide::package_timeout);
	}
	
	
	/* Loading pay modules from *.so  files */
	LogWrite(LOGMSG_SYSTEM, "Loading pay modules (*.so files)");
	payguide::modules_list = LoadModules(payguide::modules_path.c_str());
	LogWrite(LOGMSG_SYSTEM, "Pay modules loaded.");
	
	/* Loading default module  */
	payguide::null_pay_sys=GetPaySysBySoName("libnull.so");
	
	if (payguide::null_pay_sys==NULL)
	{
		LogWrite(LOGMSG_CRITICAL, "Critical error - can't load NULL pay system (libnull.so)");
//		std::cout << "Critical error - can't load NULL pay system (libnull.so)" << std::endl;
		payguide::quit=true;
		payguide::working=false;
		RemoveLockFile();
		exit(1);
	}
	
	
	/* Loading operators */
	LogWrite(LOGMSG_SYSTEM, "Loading operators...");
	payguide::operators_list = LoadOperators(payguide::operators_path.c_str());
	LogWrite(LOGMSG_SYSTEM, "Operators loaded.");
	
	
	/* Creating thread_min workers */
	{
		char logmsg[256]; snprintf(logmsg, 255, "Creating %i threads...",payguide::thread_min);LogWrite(LOGMSG_SYSTEM,logmsg);
	}
	sem_wait(&payguide::free_workers_lock);
	payguide::workers_list = CreateWorkers(payguide::thread_min);
	payguide::working_workers=0;
	
	LogWrite(LOGMSG_SYSTEM, "Threads created.");
	
	/* Free worker is a first worker in list */
	payguide::workers_list->ResetCursor();
	payguide::free_worker=payguide::workers_list->GetNext();
	sem_post(&payguide::free_workers_lock);	
	
	/* Init MySQL database */
	if (0==DBInit(&payguide::db_host, &payguide::db_name, &payguide::db_user, &payguide::db_password))
		LogWrite(LOGMSG_SYSTEM, "Connection to MySQL database established.");
	else
		LogWrite(LOGMSG_ERROR, "Connection to MySQL database failed.");
		
		
	LoadAllInitSO(payguide::modules_init_path.c_str());
	return;
}
예제 #8
0
파일: main.cpp 프로젝트: ivucica/theoutcast
int main(int argc, char** argv) {


#ifdef DEBUGCONSOLE
if(AllocConsole())
{
    freopen("CONOUT$", "wt", stdout);

    SetConsoleTitle("The Outcast : Debug Console");

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);

}
#endif




	DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "THE OUTCAST 0.4\n");
	DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "===============\n");


    DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "Reading cmd line arguments\n");

    for (int i=1;i<argc;i++) {
        if (!strcmp(argv[i], "sprplayground")) {
        	sprplayground = true;
        }
        #ifndef WIN32
        // linux only arguments:
        if (!strcmp(argv[i], "softwarerenderer")) {
            setenv("LIBGL_ALWAYS_INDIRECT", "1", 1);
        }
        #endif
    }

	DEBUGPRINT(DEBUGPRINT_LEVEL_USEFUL, DEBUGPRINT_NORMAL, "Setting up net\n");
	NetInit();
	DEBUGPRINT(DEBUGPRINT_LEVEL_USEFUL, DEBUGPRINT_NORMAL, "Setting up database\n");
	DBInit();
	DEBUGPRINT(DEBUGPRINT_LEVEL_USEFUL, DEBUGPRINT_NORMAL, "Setting up sound system\n");
	SoundInit(NULL);

	DEBUGPRINT(DEBUGPRINT_LEVEL_USEFUL, DEBUGPRINT_NORMAL, "Setting up windowing system\n");

    win_Init(&argc, argv);
    options.Load();

    win_CreateDisplay();



	DEBUGPRINT(DEBUGPRINT_LEVEL_USEFUL, DEBUGPRINT_NORMAL, "Setting up GL\n");
	GLInit();

	if (!sprplayground) {
	    DEBUGPRINT(DEBUGPRINT_LEVEL_USEFUL, DEBUGPRINT_NORMAL, "Loading skin\n");
	    skin = new Skin;
        skin->Load(options.skin.c_str());
	}


	DEBUGPRINT(DEBUGPRINT_LEVEL_USEFUL, DEBUGPRINT_NORMAL, "Loading mousepointer\n");
	win_SetMousePointer("DEFAULT");

	// game must be inited LAST.
	DEBUGPRINT(DEBUGPRINT_LEVEL_USEFUL, DEBUGPRINT_NORMAL, "Setting up game\n");
	GameInit();



    win_MainLoop();

	return 0;
}