Application.cpp
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#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();
}
}
}