Example #1
0
/*
 * Delete all database entries
 */
bool	CDbManager::deleteAllDatabases(CLog* log)
{
	CHECK_DB_MGR_INIT(getDatabase, false);

	CTimestamp	starttime = _LastUpdateTime;
	CTimestamp	endtime;
	endtime.setToCurrent();

	TDatabaseMap::iterator	it;
	for (it=_DatabaseMap.begin(); it!=_DatabaseMap.end(); ++it)
	{
		CDatabase*	db = (*it).second;

		if (db == NULL)
		{
			log->displayNL("Database '%d' left with as NULL", (*it).first);
		}
		else
		{
			// flush db
			if (!db->buildDelta(starttime, endtime))
				nlwarning("failed to build delta for database '%d' '%s'", (*it).first, db->getName().c_str());

			// delete it
			delete db;
		}

		// unreference it
		(*it).second = NULL;
	}

	_DatabaseMap.clear();

	return true;
}
Example #2
0
/*
 * Load a database and adapt to the description if needed
 */
CDatabase*	CDbManager::loadDatabase(TDatabaseId id, const string& description, CLog* log)
{
	CHECK_DB_MGR_INIT(loadDatabase, false);

	nlinfo("CDbManager::loadDatabase(): load/setup database '%d'", id);

	CDatabase*	db = getDatabase(id);

	// database not loaded yet?
	if (db == NULL)
	{
		// create a memory image
		db = createDatabase(id, log);
		if (db == NULL)
		{
			log->displayNL("failed to create database '%d'", id);
			return NULL;
		}

		// if can't load database
		if (!db->loadState())
		{
			nlinfo("CDbManager::loadDatabase(): database '%d' doesn't exist, create new", id);

			// create a new database with the new description
			if (!db->createFromScratch(description))
			{
				log->displayNL("failed to create database '%d' from scratch", id);
				return NULL;
			}

			return db;
		}
	}

	CDatabase*	adapted = db->adapt(description);
	if (adapted ==  NULL)
	{
		log->displayNL("failed to adapt database '%s' to new description", db->getName().c_str());
		return NULL;
	}

	// database changed?
	if (db != adapted)
	{
		// replace old on with new one
		_DatabaseMap[id] = adapted;
		// and delete old
		delete db;
	}

	return adapted;
}
Example #3
0
/*
 * Create a database entry
 */
CDatabase*	CDbManager::createDatabase(TDatabaseId id, CLog* log)
{
	CHECK_DB_MGR_INIT(createDatabase, false);

	// check db doesn't exist yet
	CDatabase*	db = getDatabase(id);
	if (db != NULL)
	{
		log->displayNL("Unable to createDatabase() %d, already exists as '%s'", id, db->getName().c_str());
		return NULL;
	}

	// create database and map it
	db = new CDatabase(id);
	_DatabaseMap[id] = db;

	return db;
}
Example #4
0
/*
 * load a database
 */
bool	CDbManager::loadDatabase(TDatabaseId id, CLog* log)
{
	CHECK_DB_MGR_INIT(loadDatabase, false);

	// check db doesn't exist yet
	CDatabase*	db = getDatabase(id);
	if (db == NULL)
	{
		log->displayNL("Unable to loadDatabase() %d, not created yet", id);
		return false;
	}

	// check database not init'ed
	if (db->initialised())
	{
		log->displayNL("Unable to loadDatabase() %d, already initialised as '%s'", id, db->getName().c_str());
		return false;
	}

	return db->loadState();
}
Example #5
0
/*
 * Update manager
 */
bool	CDbManager::update()
{
	H_AUTO(PDS_DbManager_update);

	CHECK_DB_MGR_INIT(update, false);

	// update stamp
	CTableBuffer::updateCommonStamp();

	TDatabaseMap::iterator	it;

	CDatabase::checkUpdateRates();


	// check evently if database need to write some delta
	TTime	tm = CTime::getLocalTime();
	if (tm >= _NextTimeDelta)
	{

		CTimestamp	starttime = _LastUpdateTime;
		CTimestamp	endtime;
		endtime.setToCurrent();

		std::vector<uint32>	ack;

		for (it=_DatabaseMap.begin(); it!=_DatabaseMap.end(); ++it)
		{
			CDatabase*	database = (*it).second;

			// generate deltas
			if (!database->buildDelta(starttime, endtime))
				nlwarning("failed to build delta for database '%d' '%s'", (*it).first, database->getName().c_str());

			// obsolete? since RBS build references and tells PDS of success/failure
			database->checkReferenceChange();

			// acknowledge last updates
			database->flushUpdates(ack);

			if (!ack.empty() && database->getMappedService().get() != 0xffff)
			{
				CMessage	msgack("PD_ACK_UPD");
				uint32		databaseId = (*it).first;
				msgack.serial(databaseId);
				msgack.serialCont(ack);
				CUnifiedNetwork::getInstance()->send(database->getMappedService(), msgack);
			}
		}

		_NextTimeDelta = tm - (tm%(DeltaUpdateRate*1000)) + (DeltaUpdateRate*1000);

		_LastUpdateTime = endtime;
	}

	CTimestamp	ts;
	ts.setToCurrent();

	// check databases require some delta packing/reference generation
	for (it=_DatabaseMap.begin(); it!=_DatabaseMap.end(); ++it)
	{
		CDatabase*	database = (*it).second;
		database->sendBuildCommands(ts);
	}

	// send messages to RBS if ready
	while (_RBSUp && !_RBSMessages.empty())
	{
		CUnifiedNetwork::getInstance()->send("RBS", *(_RBSMessages.front()));
		delete _RBSMessages.front();
		_RBSMessages.pop_front();
	}

	return true;
}