forked from yxkhuijie/ick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlObject.cpp
More file actions
377 lines (334 loc) · 9.14 KB
/
ControlObject.cpp
File metadata and controls
377 lines (334 loc) · 9.14 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
154
155
156
157
158
159
160
161
162
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
197
198
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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "ControlObject.h"
#include "IMutex.h"
#include "Logger.h"
#include "Converter.h"
#ifdef __linux__
#include <stdlib.h>
#endif
// std::map<const std::string, std::vector<ServiceInfo> > ControlObject::m_serviceMap;
// const std::map<const std::string, std::string> ControlObject::m_serviceStr;
ControlObject::ControlObject(void)
{
}
ControlObject::~ControlObject(void)
{
}
void ControlObject::initService()
{
}
void ControlObject::make()
{
TIME_DEBUG(getFullName() + ": make start")
TIME_DEBUG(getFullName() + ": make finish")
}
void ControlObject::verify()
{
TIME_DEBUG(getFullName() + ": verify start")
TIME_DEBUG(getFullName() + ": verify finish")
}
void ControlObject::initialize()
{
TIME_DEBUG(getFullName() + ": initialize start")
TIME_DEBUG(getFullName() + ": initialize finish")
}
void ControlObject::startup()
{
TIME_DEBUG(getFullName() + ": startup start")
TIME_DEBUG(getFullName() + ": startup finish")
}
void ControlObject::shutdown()
{
TIME_DEBUG(getFullName() + ": shutdown start")
TIME_DEBUG(getFullName() + ": shutdown finish")
}
void ControlObject::sleep(int second)
{
#ifdef __windows__
Sleep(second * 1000);
#elif __linux__
sleep(second);
#endif
}
void ControlObject::msleep(int millisecond)
{
#ifdef __windows__
Sleep(millisecond);
#elif __linux__
usleep(millisecond * 1000);
#endif
}
void ControlObject::setNotSimulated(bool simulated)
{
this->setSimulated(!simulated);
}
void ControlObject::call(std::string serviceName, std::string params)
{
std::map<const std::string, std::vector<ServiceInfo> >&
serviceMap =this->getServiceMap();
std::string className = this->getClassName();
std::map<const std::string, std::vector<ServiceInfo> >::iterator itServiceMap;
std::map<std::string, ServiceParam>* serviceParamMap = NULL;
std::vector<ServiceInfo>::iterator itServiceInfo;
std::vector<ServiceInfo> serviceInfoList;
ServiceParam serviceParam;
if ((itServiceMap = serviceMap.find(className)) != serviceMap.end())
{
serviceInfoList = itServiceMap->second;
for (itServiceInfo = serviceInfoList.begin();
itServiceInfo != serviceInfoList.end(); ++itServiceInfo)
{
if (itServiceInfo->getName().compare(serviceName) == 0)
{
serviceParamMap = itServiceInfo->getParams();
break;
}
}
}
std::map<std::string, Service*>::iterator itService = m_services.find(serviceName);
if (itService == m_services.end())
{
std::cout<< "Run service failure! service " << serviceName << " can not be found!!" << std::endl;
return;
}
if (serviceParamMap != NULL)
{
std::vector<std::string> param = split(params, ",");
if (serviceParamMap->size() != param.size())
{
std::string msg = std::string("Run service failure! param format error! service param size: ") +
Converter::convertToString((int)serviceParamMap->size()) + std::string(", param size: ") + Converter::convertToString((int)param.size());
Logger::getInstance()->Error(msg);
return;
}
for (int i = 0; i < param.size(); i++)
{
std::map<std::string, ServiceParam>::iterator itParam;
for (itParam = serviceParamMap->begin(); itParam != serviceParamMap->end(); ++itParam)
{
if (i == itParam->second.getParamIndex())
{
switch (itParam->second.getType())
{
case PARAM_TYPE_INT:
itService->second->Params[itParam->second.getParamName()].setValue(atoi(this->decodeParam(param[i]).c_str()));
break;
case PARAM_TYPE_DOUBLE:
itService->second->Params[itParam->second.getParamName()].setValue(atof(this->decodeParam(param[i]).c_str()));
break;
case PARAM_TYPE_STRING:
itService->second->Params[itParam->second.getParamName()].setValue(this->decodeParam(param[i]));
break;
}
break;
}
}
}
this->call(itService->second);
}
/*
std::map<std::string, Service*>::iterator it;
if ((it = m_services.find(serviceName)) != m_services.end())
{
std::vector<std::string> param = split(params, ",");
if (it->second->Params.size() != param.size())
{
std::cout << "Run service failure! param format error!";
return;
}
else
{
for (int i = 0; i < param.size(); i++)
{
std::map<std::string, ServiceParam>::iterator itParam;
for (itParam = it->second->Params.begin(); itParam != it->second->Params.end(); ++itParam)
{
if (i == itParam->second.getParamIndex())
{
switch (itParam->second.getType())
{
case ServiceParamType::PARAM_TYPE_INT:
itParam->second.setValue(atoi(param[i].c_str()));
break;
case ServiceParamType::PARAM_TYPE_DOUBLE:
itParam->second.setValue(atof(param[i].c_str()));
break;
case ServiceParamType::PARAM_TYPE_STRING:
itParam->second.setValue(param[i]);
break;
}
break;
}
}
}
this->call(it->second);
}
}
*/
}
void ControlObject::call(Service* service)
{
if (service != NULL)
{
this->mutex_service.lock();
service->execute();
this->mutex_service.unlock();
}
}
void ControlObject::start(Service* service)
{
if (service != NULL)
{
#ifdef __windows__
CreateThread(NULL, 0, ThreadHandler, service, 0, NULL);
#endif
}
}
#ifdef __windows__
DWORD WINAPI ControlObject::ThreadHandler(LPVOID lpParamter)
{
Service* service = (Service*)lpParamter;
if (service != NULL)
{
ControlObject* object = service->getOwner();
if(object != NULL) object->mutex_service.lock();
service->execute();
if(object != NULL) object->mutex_service.unlock();
}
return 0;
}
#endif
void ControlObject::registerService(const std::string className, ServiceInfo serviceInfo)
{
IMutex mutex;
std::map<const std::string, std::vector<ServiceInfo> >& serviceMap = getServiceMap();
std::map <const std::string, std::vector<ServiceInfo> >::iterator it1;
// it1 = ControlObject::m_serviceMap.find(owner);
if ((it1 = serviceMap.find(className)) != serviceMap.end())
{
it1->second.push_back(serviceInfo);
}
else
{
std::vector<ServiceInfo> serviceInfoList;
serviceInfoList.push_back(serviceInfo);
serviceMap.insert(std::pair<std::string, std::vector<ServiceInfo> >(className, serviceInfoList));
}
/*
std::map<const std::string, std::string>::const_iterator it2;
if ((it2 = m_serviceStr.find(className)) != m_serviceStr.end())
{
// it2->second=className;
}
else
{
// m_serviceStr.insert(std::pair<const std::string, std::string >(className, className));
}
ControlObject::m_serviceMap = serviceMap;
*/
}
std::map <const std::string, std::vector<ServiceInfo> >& ControlObject::getServiceMap()
{
static std::map<const std::string, std::vector<ServiceInfo> > serviceMap;
return serviceMap;
}
void ControlObject::addService(std::string name, Service* service)
{
if (service == NULL) return;
if (this->m_services.find(name) == m_services.end())
{
m_services.insert(std::pair<std::string, Service*>(name, service));
}
else
{
m_services[name] = service;
}
}
void ControlObject::addServiceParam(std::string serviceName, std::string paramName, std::string paramType)
{
std::map<std::string, Service*>::iterator it;
if ((it=this->m_services.find(serviceName)) != m_services.end())
{
it->second->addServiceParam(paramName, paramType);
}
}
INSERT_SERVICE_INFO::INSERT_SERVICE_INFO(std::vector<ServiceInfo> &v, ServiceInfo &info,
const std::string &service, const std::string &dp)
{
std::vector<ServiceInfo>::iterator it;
bool exist = false;
for (it = v.begin(); it != v.end(); ++it)
{
if (it->getName() == service)
{
exist = true;
}
}
if (!exist)
{
info.setName(service);
v.push_back(info);
}
}
REG_SERVICEINFO_LIST::REG_SERVICEINFO_LIST(const std::string & owner, std::vector<ServiceInfo>& curInfo)
{
std::map<const std::string, std::vector<ServiceInfo> >& serviceMap = ControlObject::getServiceMap();
std::map <const std::string, std::vector<ServiceInfo> >::iterator it1;
// it1 = ControlObject::m_serviceMap.find(owner);
if (!serviceMap.empty() && (((it1 = serviceMap.find(owner)) != serviceMap.end())))
{
it1->second = curInfo;
}
else
{
serviceMap.insert(std::pair<std::string, std::vector<ServiceInfo> >(owner, curInfo));
}
serviceMap = ControlObject::getServiceMap();
}
INIT_SERVICEINFO_LIST::INIT_SERVICEINFO_LIST(ControlObject * ctrlObj, std::vector<ServiceInfo>& curInfo)
{
}
INSERT_PARAM_INFO::INSERT_PARAM_INFO(std::vector<ServiceInfo>& v, ServiceInfo & info,
const std::string & service, const std::string & param, const std::string & type, const std::string & dp)
{
info.setName(service);
info.addServiceParam(param, type);
std::vector<ServiceInfo>::iterator it;
for (it = v.begin(); it != v.end(); ++it)
{
if (it->getName() == service)
{
*it = info;
return;
}
}
v.push_back(info);
}
std::string ControlObject::decodeParam(std::string data)
{
std::string res = "";
for (int i = 0; i < data.length(); i++)
{
if (i < data.length() - 2 &&
data.at(i) == '%' && data.at(i + 1) == '3' && data.at(i + 2) == 'A')
{
res += ":";
i += 2;
}
else if (i < data.length() - 2 &&
data.at(i) == '%' && data.at(i + 1) == '2' && data.at(i + 2) == 'C')
{
res += ",";
i += 2;
}
else
{
res += data.at(i);
}
}
return res;
}
IMPLEMENT_CLASS_DYNAMIC(ControlObject, CmdTarget)
IMPLEMENT_MESSAGE_LIST(ControlObject, CmdTarget)
BEGIN_MESSAGE_LIST(ControlObject)
SET_B(setNotSimulated, ControlObject)
END_MESSAGE_LIST