Newer
Older
#include <ProtocolManager.h>
#include <boost/date_time.hpp>
namespace DataImporter_ns
{
//==============================================================================
// ProtocolManager::ProtocolManager()
//==============================================================================
ProtocolManager::ProtocolManager(Tango::DeviceImpl* deviceImpl_p,
Configuration::SP configuration_sp, DBManager::SP dBManager_sp) :
Tango::LogAdapter(deviceImpl_p), m_configuration_sp(configuration_sp),
m_dBManager_sp(dBManager_sp)
{
DEBUG_STREAM << "ProtocolManager::ProtocolManager()" << endl;
m_isAuthorised = false;
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
m_hasMoreData = true;
}
//==============================================================================
// ProtocolManager::ProtocolManager()
//==============================================================================
ProtocolManager::~ProtocolManager()
{
DEBUG_STREAM << "ProtocolManager::~ProtocolManager()" << endl;
}
//==============================================================================
// ProtocolManager::ProtocolManager()
//==============================================================================
ProtocolManager::SP ProtocolManager::create(Tango::DeviceImpl* deviceImpl_p,
Configuration::SP configuration_sp, DBManager::SP dBManager_sp)
{
ProtocolManager::SP d_sp(new ProtocolManager(deviceImpl_p, configuration_sp,
dBManager_sp), ProtocolManager::Deleter());
return d_sp;
}
//==============================================================================
// ProtocolManager::ProtocolManager()
//==============================================================================
void ProtocolManager::setRemoteEndpoint(std::string remoteEndpoint)
{
DEBUG_STREAM << "ProtocolManager::setRemoteEndpoint()" << endl;
m_remoteEndpoint = remoteEndpoint;
}
//==============================================================================
// ProtocolManager::waitBeforeRequest()
//==============================================================================
bool ProtocolManager::waitBeforeRequest()
{
DEBUG_STREAM << "ProtocolManager::waitBeforeRequest()" << endl;
return !m_hasMoreData;
}
//==============================================================================
// ProtocolManager::resetProtocolStatus()
//==============================================================================
void ProtocolManager::resetProtocolStatus()
{
DEBUG_STREAM << "ProtocolManager::resetProtocolStatus()" << endl;
m_isAuthorised = false;
m_hasMoreData = true;
}
//==============================================================================
// ProtocolManager::createRequest()
//==============================================================================
RequestSP ProtocolManager::createRequest()
throw(std::runtime_error, std::out_of_range)
{
DEBUG_STREAM << "ProtocolManager::createRequest()" << endl;
RequestSP request_sp;
if(!m_isAuthorised)
{
request_sp = createAuthroisation();
request_sp = createData();
}
if(!request_sp->IsInitialized())
throw std::runtime_error("Not initialized request!");
return request_sp;
}
//==============================================================================
// ProtocolManager::processResponse()
//==============================================================================
void ProtocolManager::processResponse(ResponseSP response_sp)
throw(std::runtime_error, std::out_of_range)
{
DEBUG_STREAM << "ProtocolManager::processResponse()" << endl;
if(!response_sp->IsInitialized())
throw std::runtime_error("Not initialized response!");
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
154
155
switch(response_sp->type())
{
case Response::AUTHORIZATION:
{
processAuthroisation(response_sp);
break;
}
case Response::DATA:
{
processData(response_sp);
break;
}
default:
throw std::runtime_error("Unknown response type");
}
}
//==============================================================================
// ProtocolManager::createAuthroisation()
//==============================================================================
RequestSP ProtocolManager::createAuthroisation() throw(std::runtime_error)
{
DEBUG_STREAM << "ProtocolManager::createAuthroisation()" << endl;
RequestSP request_sp(new Request);
request_sp->set_type(Request::AUTHORIZATION);
std::string user = m_configuration_sp->getRemoteUsername();
std::string password = m_configuration_sp->getRemotePassword();
#ifdef VERBOSE_DEBUG
INFO_STREAM << "ProtocolManager::createAuthroisation() Send username "
<< user << " password " << password << " to " << m_remoteEndpoint << endl;
#else
INFO_STREAM << "ProtocolManager::createAuthroisation() Send to "
<< m_remoteEndpoint << endl;
#endif
Request::Authorization* authorization = request_sp->mutable_authorization();
authorization->set_username(user);
authorization->set_password(password);
return request_sp;
}
//==============================================================================
// ProtocolManager::createValidation()
//==============================================================================
RequestSP ProtocolManager::createValidation() throw(std::runtime_error)
{
DEBUG_STREAM << "ProtocolManager::createValidation()" << endl;
RequestSP request_sp(new Request);
request_sp->set_type(Request::VALIDATION);
std::string schema = m_configuration_sp->getDatabaseSchema();
std::string table = m_configuration_sp->getDatabaseTable();
INFO_STREAM << "ProtocolManager::createValidation() Send schema "
<< schema << " table " << table << " to " << m_remoteEndpoint << endl;
Request::Validation* validation = request_sp->mutable_validation();
validation->set_schema(schema);
validation->set_table(table);
return request_sp;
}
//==============================================================================
// ProtocolManager::createData()
//==============================================================================
RequestSP ProtocolManager::createData() throw(std::runtime_error, std::out_of_range)
{
DEBUG_STREAM << "ProtocolManager::createData()" << endl;
RequestSP request_sp(new Request);
request_sp->set_type(Request::DATA);
std::string schema = m_configuration_sp->getDatabaseSchema();
std::string table = m_configuration_sp->getDatabaseTable();
return request_sp;
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//==============================================================================
// ProtocolManager::processAuthroisation()
//==============================================================================
void ProtocolManager::processAuthroisation(ResponseSP response_sp)
throw(std::runtime_error)
{
DEBUG_STREAM << "ProtocolManager::processAuthroisation()" << endl;
const Response::Authorization& authorization = response_sp->authorization();
if(authorization.state() == Response::Authorization::ACCEPTED)
{
INFO_STREAM << "ProtocolManager::processAuthroisation() State ACCEPTED "
<< "status " << authorization.status() << " from " << m_remoteEndpoint << endl;
m_isAuthorised = true;
}
else
{
ERROR_STREAM << "ProtocolManager::processAuthroisation() State REJECTED "
<< "status " << authorization.status() << " from " << m_remoteEndpoint << endl;
throw std::runtime_error(authorization.status());
}
}
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//==============================================================================
// ProtocolManager::processValidation()
//==============================================================================
void ProtocolManager::processValidation(ResponseSP response_sp)
throw(std::runtime_error)
{
DEBUG_STREAM << "ProtocolManager::processValidation()" << endl;
const Response::Validation& validation = response_sp->validation();
if(validation.state() == Response::Validation::ACCEPTED)
{
INFO_STREAM << "ProtocolManager::processValidation() State ACCEPTED "
<< "status " << validation.status() << " from " << m_remoteEndpoint << endl;
m_isValidated = true;
}
else
{
ERROR_STREAM << "ProtocolManager::processValidation() State REJECTED "
<< "status " << validation.status() << " from " << m_remoteEndpoint << endl;
throw std::runtime_error(validation.status());
}
}
//==============================================================================
// ProtocolManager::processData()
//==============================================================================
void ProtocolManager::processData(ResponseSP response_sp)
throw(std::runtime_error, std::out_of_range)
{
DEBUG_STREAM << "ProtocolManager::processData()" << endl;
}