Newer
Older
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
#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;
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!");
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
154
155
156
157
158
159
160
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::createData()
//==============================================================================
RequestSP ProtocolManager::createData() throw(std::runtime_error, std::out_of_range)
{
DEBUG_STREAM << "ProtocolManager::createData()" << endl;
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//==============================================================================
// 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());
}
}
//==============================================================================
// ProtocolManager::processData()
//==============================================================================
void ProtocolManager::processData(ResponseSP response_sp)
throw(std::runtime_error, std::out_of_range)
{
DEBUG_STREAM << "ProtocolManager::processData()" << endl;
}