Application.cpp 3.5 KB
#include "Application.h"

#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>

#include <libxml/xmlmemory.h>

#include "../TimeManager/TimeManager.h"
#include "../RequestManager/RequestManager.h"
#include "../Common/Toolbox.h"

using namespace std;
using namespace TREPS::RequestManager;
using namespace TREPS::Common;
using namespace TREPS::TimeManager;

namespace TREPS
{
	namespace Application
	{
		ApplicationClass::ApplicationClass(void) : conf(NULL), log(NULL)
		{
			//Init random number generator
			srand(time(NULL));
			//Set time zone for UTC
			setenv("TZ", "", 1);
			//init xml
			xmlInitParser();
		}

		ApplicationClass::~ApplicationClass(void)
		{
			//kill time manager instance
			TimeManagerClass *timeMgr = TimeManagerClass::getInstance();
			timeMgr->kill();
			//delete application config loader
			if (this->conf != NULL)
				delete this->conf;
			//delete application logger
			if (this->log != NULL)
			{
				LOG4CXX_INFO(this->log->getPtr(), "Delete logger");
				delete this->log;
			}
			//clean xml lib
			xmlCleanupParser();
		}

		bool ApplicationClass::init(const char *confFile)
		{
			//create application config loader
			this->conf = new ApplicationConfigClass(this->getApplicationPath().c_str());

			//load configuration file
			if (!this->conf->load(confFile))
				return false;

			//load logger and logger configuration file
			this->log  = new ApplicationLoggerClass();
	
			if (!this->log->load())
				return false;

			//Init Time manager
			string timePath = this->conf->getTimesFilePath();

			if (timePath.compare("") == 0)
			{
				LOG4CXX_ERROR(this->log->getPtr(),"Cannot get time file path");
				return false;
			}

			TimeManagerClass *timeMgr = TimeManagerClass::getInstance();

			if (!timeMgr->init(timePath.c_str()))
			{
				LOG4CXX_ERROR(this->log->getPtr(),"Cannot init time manager");
				timeMgr->kill();
				return false;
			}

			return true;
		}
		
		bool ApplicationClass::run(int argc, char *argv[])
		{
			if ((argc != 2))
			{
				LOG4CXX_ERROR(this->log->getPtr(), "Usage : " << argv[0] << " request_path");
				return false;
			}
		
			//Create and init the request manager
			RequestManagerClass *reqMgr = new RequestManagerClass();
		
			if (!reqMgr->init(argv[1]))
			{
				LOG4CXX_ERROR(this->log->getPtr(), "Cannot init request");
				delete reqMgr;
				return false;
			}

			//re-use cache result if necessary
			string cacheRes = reqMgr->getCacheResult();

			if ((cacheRes.compare("") == 0) || !this->getConf()->getUseCache())
			{
				//Run the request
				bool runRes;	
				if (!(runRes = reqMgr->run()))
					LOG4CXX_ERROR(this->log->getPtr(), "Error detected during the request execution");
			
				// Write the result of the request
				string res = reqMgr->writeResult(runRes);
			
				if (res.compare("") == 0)
				{
					LOG4CXX_ERROR(this->log->getPtr(), "Cannot write result");
					delete reqMgr;
					return false;
				}

				cout << "TREPS_RESULT " << res << endl;
			}
			else
			{
				LOG4CXX_INFO(this->log->getPtr(), "Use cache result");
				cout << "TREPS_RESULT " << cacheRes << endl;
			}
			
			delete reqMgr;
			
			return true;
		}

		ApplicationLoggerClass *ApplicationClass::getLog(void)
		{
			return this->log;
		}

		ApplicationConfigClass *ApplicationClass::getConf(void)
		{
			return this->conf;
		}

		string ApplicationClass::getApplicationPath(void)
		{
			return getExecPath();
		}
	}
}