-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceManager.cpp
More file actions
513 lines (421 loc) · 16.6 KB
/
Copy pathServiceManager.cpp
File metadata and controls
513 lines (421 loc) · 16.6 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include"ServiceManager/ServiceManager.h"
#include"ServiceManager/ServiceManagerUI.h"
//#define ALIVE_CHECKER_MS 1
#define LATE_CHECKIN_DEADLINE_SECONDS 5
#define MULTICAST_ADDRESS_START "234.5.6."
#define MAXIMUM_NUMBER_OF_PUBLISHERS 200
#define HOST_INFORMATION_FILE "HostInformation.txt"
namespace ServiceManagerSpace
{
int ServiceManager::portNumberAssigner = 6000;
// --------------------------------------------------------
// init ServiceManagerConfig
// --------------------------------------------------------
int ServiceManagerConfig::serviceManagerPort = MicroMiddleware::MiddlewareSettings::GetServiceManagerPort();
string ServiceManagerConfig::hostInformationFile = HOST_INFORMATION_FILE;
// --------------------------------------------------------
// ServiceManager constructor/destructor
// --------------------------------------------------------
ServiceManager::ServiceManager(const string &name)
: updateLock()
, timeCounter(LATE_CHECKIN_DEADLINE_SECONDS)
, stopServiceManager(false)
, serviceManagerUI(NULL)
, fileName(ServiceManagerConfig::hostInformationFile)
{
initServiceManager();
}
ServiceManager::~ServiceManager(void)
{
// if ServiceManager's destructor is called we assume it was shut down correctly
// therefore, no host information is required upon restart
if(outFile.is_open())
outFile.close();
outFile.open(ServiceManagerConfig::hostInformationFile.c_str(), fstream::out);
if(outFile.is_open())
{
outFile << "" ;
outFile.flush();
outFile.close();
}
}
void ServiceManager::initServiceManager()
{
for(int i = 7; i <= MAXIMUM_NUMBER_OF_PUBLISHERS; i++)
{
stringstream address;
address << MULTICAST_ADDRESS_START << i ;
multicastAddresses.push_back(address.str());
}
parseHostInformationFromDisc(fileName);
outFile.open(fileName.c_str(), fstream::out);
if(outFile.is_open())
{
for(MapHostInformation::iterator it = mapHostInformation.begin(), it_end = mapHostInformation.end(); it != it_end; ++it)
{
HostInformationSpace::WriteHostInformationToDisc(outFile, it->second);
mapAliveCheck[(it->second).GetComponentName()] = 0;
}
}
serviceManagerUI = new ServiceManagerUI(*this);
}
// --------------------------------------------------------
// Thread::run()
// --------------------------------------------------------
void ServiceManager::run()
{
cerr << "ServiceManager::run(): ServiceManager is running. Alive check interval " << ALIVE_CHECK_INTERVAL_MS << endl;
while(true)
{
msleep(ALIVE_CHECK_INTERVAL_MS);
MutexLocker lock(&updateLock);
if(stopServiceManager == true) break;
timeCounter++;
for(MapAliveCheck::iterator it = mapAliveCheck.begin(), it_end = mapAliveCheck.end(); it != it_end; ++it)
{
ASSERT(mapHostInformation.count(it->first));
if(it->second > timeCounter - LATE_CHECKIN_DEADLINE_SECONDS)
{
ASSERT(mapHostInformation.count(it->first));
mapHostInformation[it->first].SetOnlineStatus(HostInformation::ONLINE);
}
else if(mapHostInformation[it->first].GetOnlineStatus() == HostInformation::ONLINE)
{
cerr << it->first << " is assumed OFFLINE (missed several alive messages): " << timeCounter - LATE_CHECKIN_DEADLINE_SECONDS <<endl;
mapHostInformation[it->first].SetOnlineStatus(HostInformation::OFFLINE);
}
}
}
cerr << "ServiceManager::run(): ServiceManager is stopping." << endl;
}
// --------------------------------------------------------
// public aliveChecker
// --------------------------------------------------------
HostInformation ServiceManager::AliveChecker(HostInformation info, string serviceName)
{
MutexLocker lock(&updateLock);
// make sure it is set to ONLINE
info.SetOnlineStatus(HostInformation::ONLINE);
//cout << "ServiceManager::aliveChecker():" << " name : " << componentName << " info: " << info << endl;
// -- start check --
if(info.GetHostDescription() == HostInformation::COMPONENT)
cout << "ServiceManager::AliveChecker(): WARNING! Wrong HostDescription! HostInformation::COMPONENT given for: " << info.GetComponentName() << endl;
if(info.GetComponentName().empty()) throw MiddlewareException("ServiceManager::AliveChecker(info): ERROR! Checked in HostInformation with no componentName!");
// -- end check --
mapAliveCheck[info.GetComponentName()] = timeCounter;
// Update host information
mapHostInformation[info.GetComponentName()] = info;
// host sending the alive check is a publisher/stub
if(info.GetHostDescription() == HostInformation::PUBLISHER ||
info.GetHostDescription() == HostInformation::STUB)
{
//ASSERT(serviceName == "");
return info;
}
// else -> host sending the alive check is a subscriber/proxy,
if(mapHostInformation.count(serviceName) <= 0) // serviceName is OFFLINE
return HostInformation(serviceName);
return mapHostInformation[serviceName];
}
map<string, HostInformation>
ServiceManager::AliveChecker(MapHostInformation &mapRegisteredServices, SetServiceName &subscribedServices)
{
MutexLocker lock(&updateLock);
for(MapHostInformation::iterator it = mapRegisteredServices.begin(), it_end = mapRegisteredServices.end(); it != it_end; ++it)
{
HostInformation &info = it->second;
ASSERT(info.IsValid());
info.SetOnlineStatus(HostInformation::ONLINE); // make sure it is set to ONLINE
//IDEBUG() << " name : " << info.GetComponentName() ;
// -- start check --
if(info.GetHostDescription() == HostInformation::COMPONENT)
cout << "ServiceManager::AliveChecker(): WARNING! Wrong HostDescription! HostInformation::COMPONENT given for: " << info.GetComponentName() << endl;
if(info.GetComponentName().empty()) throw MiddlewareException("ServiceManager::AliveChecker(info): ERROR! Checked in HostInformation with no componentName!");
// -- end check --
mapAliveCheck[info.GetComponentName()] = timeCounter; // Update time counter
mapHostInformation[info.GetComponentName()] = info; // Update host information
}
MapHostInformation mapSubscribedServices;
for(SetServiceName::iterator it = subscribedServices.begin(), it_end = subscribedServices.end(); it != it_end; ++it)
{
string serviceName = *it;
if(mapHostInformation.count(serviceName) <= 0)
{
//IDEBUG() << "WARNING! Service not present " << serviceName ;
continue;
}
HostInformation &info = mapHostInformation[serviceName];
ASSERT(info.IsValid());
mapSubscribedServices.insert(pair<string, HostInformation>(info.GetComponentName(), info));
}
return mapSubscribedServices;
}
// --------------------------------------------------------
// public hostInformation
// --------------------------------------------------------
HostInformation ServiceManager::GetServiceInformation(string serviceName)
{
MutexLocker lock(&updateLock);
for(MapHostInformation::iterator it = mapHostInformation.begin(), it_end = mapHostInformation.end(); it != it_end; ++it)
{
if(it->first != serviceName) continue;
return it->second;
}
return HostInformation(serviceName);
}
// --------------------------------------------------------
// public hostRegistration
// TODO: Write information OFFLINE to disc, and detect it
// when reading from disc.
// --------------------------------------------------------
HostInformation ServiceManager::ServiceRegistration(HostInformation info)
{
MutexLocker lock(&updateLock);
serviceRegistration(info);
return info;
}
void ServiceManager::ServiceRegistration(MapHostInformation &mapServices)
{
for(MapHostInformation::iterator it = mapServices.begin(), it_end= mapServices.end(); it != it_end; ++it)
{
HostInformation &hostInfo = it->second;
MutexLocker lock(&updateLock);
serviceRegistration(hostInfo);
}
}
void ServiceManager::ShutDown()
{
Quit();
}
void ServiceManager::ClearServices()
{
MutexLocker lock(&updateLock);
MapHostInformation removeHostInformation;
for(MapHostInformation::iterator it = mapHostInformation.begin(), it_end = mapHostInformation.end(); it != it_end; ++it)
{
HostInformation info = it->second;
if(info.GetOnlineStatus() == HostInformation::ONLINE) continue;
removeHostInformation.insert(pair<string, HostInformation>(info.GetComponentName(), info));
}
for(MapHostInformation::iterator it = removeHostInformation.begin(), it_end = removeHostInformation.end(); it != it_end; ++it)
{
HostInformation &info = it->second;
if(info.GetHostDescription() == HostInformation::PROXY ||
info.GetHostDescription() == HostInformation::STUB ||
info.GetHostDescription() == HostInformation::SUBSCRIBER)
{
hostOffline(info);
}
else if(info.GetHostDescription() == HostInformation::PUBLISHER)
publisherOffline(info);
}
}
map<string, HostInformation> ServiceManager::GetAllServiceInformation()
{
MutexLocker lock(&updateLock);
MapHostInformation mapHostInfo;
for(MapHostInformation::iterator it = mapHostInformation.begin(), it_end = mapHostInformation.end(); it != it_end; ++it)
mapHostInfo.insert(pair<string, HostInformation>(it->first, it->second));
//cerr << "Returning : " << endl;
//for(MapHostInformation::const_iterator it = mapHostInfo.begin(), it_end = mapHostInfo.end(); it != it_end; ++it)
// cerr << it->second << endl;
return mapHostInfo;
}
// --------------------------------------------------------
// -> not thread safe
// private hostOnline/hostOffline
// -> called from ServiceRegistration
// --------------------------------------------------------
void ServiceManager::serviceRegistration(HostInformation &info)
{
// -- start error handling --
if(info.GetHostName().empty()) throw Exception("ServiceManager::hostRegistration(info): hostName is empty! ");
if(info.GetComponentName().empty()) throw Exception("ServiceManager::hostRegistration(info): serviceName is empty! ");
// -- end error handling --
if(info.GetHostDescription() == HostInformation::PROXY ||
info.GetHostDescription() == HostInformation::STUB ||
info.GetHostDescription() == HostInformation::SUBSCRIBER)
{
if(info.GetOnlineStatus() == HostInformation::ONLINE)
{
hostOnline(info);
HostInformationSpace::WriteHostInformationToDisc(outFile, info);
}
else
{
IDEBUG() << "Service marked OFFLINE " << info.GetComponentName() ;
hostOffline(info);
}
}
else if(info.GetHostDescription() == HostInformation::PUBLISHER)
{
if(info.GetOnlineStatus() == HostInformation::OFFLINE) // OFFLINE --> Publisher going offline
{
publisherOffline(info);
//return HostInformation(info.GetComponentName());
}
else
{
publisherOnline(info); // ONLINE --> Publisher going Online
HostInformationSpace::WriteHostInformationToDisc(outFile, info);
}
}
}
void ServiceManager::hostOnline(HostInformation &info)
{
HostInformation *registered_info = findHostInformation(info);
if(registered_info) // -- already registered
{
if(registered_info->GetOnlineStatus() == HostInformation::ONLINE)
cout << "hostOnline(): WARNING! " << info.GetComponentName() << " already registered ONLINE." << endl;
if(info.GetPortNumber() == -1 && registered_info->GetPortNumber() > 0)
info.SetPortNumber(registered_info->GetPortNumber());
}
if(info.GetPortNumber() == -1) // -> assign port number
{
info.SetPortNumber(ServiceManager::portNumberAssigner++);
}
// -- start check --
if(info.GetComponentName().empty()) throw Exception("ServiceManager::hostOnline(info): ComponentName is empty!");
// -- end check --
cout << "hostOnline(): " << info << endl;
mapAliveCheck[info.GetComponentName()] = timeCounter;
mapHostInformation[info.GetComponentName()] = info;
}
void ServiceManager::hostOffline(HostInformation &info)
{
removeHostInformation(info);
}
// --------------------------------------------------------
// private publisherOnline
// -> called from hostRegistration
// --------------------------------------------------------
void ServiceManager::publisherOnline(HostInformation &info)
{
HostInformation *registered_info = findHostInformation(info);
if(registered_info) // -- already registered
{
if(registered_info->GetOnlineStatus() == HostInformation::ONLINE)
cout << "hostOnline(): WARNING! " << info.GetComponentName() << " already registered ONLINE." << endl;
if(info.GetMulticastAddress().empty() && !registered_info->GetMulticastAddress().empty())
info.SetMulticastAddress(registered_info->GetMulticastAddress());
if(info.GetPortNumber() == -1 && registered_info->GetPortNumber() > 0)
info.SetPortNumber(registered_info->GetPortNumber());
}
if(info.GetMulticastAddress().empty()) // -> assign multicast address to publisher
{
info.SetMulticastAddress(multicastAddresses.front());
usedMulticastAddresses.push_back(multicastAddresses.front());
multicastAddresses.pop_front();
}
if(info.GetPortNumber() == -1) // -> assign port number to publisher
{
info.SetPortNumber(ServiceManager::portNumberAssigner++);
}
cout << "publisherOnline(): " << info << endl;
mapAliveCheck[info.GetComponentName()] = timeCounter;
mapHostInformation[info.GetComponentName()] = info;
}
// --------------------------------------------------------
// private publisherOffline
// -> called from hostRegistration
// --------------------------------------------------------
void ServiceManager::publisherOffline(HostInformation &info)
{
cout << "publisherOffline():" << info << endl;
recycleMulticastAddress(info.GetMulticastAddress()); // recycle multicast address
removeHostInformation(info);
}
// --------------------------------------------------------
// private findHostInformation/removeHostInformation/etc.
// --------------------------------------------------------
HostInformation* ServiceManager::findHostInformation(const HostInformation &info)
{
for(MapHostInformation::iterator it = mapHostInformation.begin(), it_end = mapHostInformation.end(); it != it_end; ++it)
{
if(it->first == info.GetComponentName())
{
//IDEBUG() << " Publisher " << info.GetComponentName() << " already registered! " ;
return &(it->second);
}
}
return NULL;
}
void ServiceManager::removeHostInformation(const HostInformation &info)
{
for(MapHostInformation::iterator it = mapHostInformation.begin(), it_end = mapHostInformation.end(); it != it_end; ++it)
{
if(it->first == info.GetComponentName())
{
mapHostInformation.erase(it);
break;
}
}
for(MapAliveCheck::iterator it = mapAliveCheck.begin(), it_end = mapAliveCheck.end(); it != it_end; ++it)
if(it->first == info.GetComponentName())
{
mapAliveCheck.erase(it);
break;
}
}
void ServiceManager::parseHostInformationFromDisc(string &filename)
{
vector<HostInformation> vectorHostInformation;
if(HostInformationSpace::ParseHostInformationFromDisc(filename, vectorHostInformation) == false)
return;
while(!vectorHostInformation.empty())
{
HostInformation info = vectorHostInformation.back();
vectorHostInformation.pop_back();
// -- start check --
if(info.GetComponentName().empty()) throw Exception("ServiceManager::parseHostInformationFromDisc(fileName): ComponentName is empty!");
// -- end check --
// Newer information already inserted? Drop current...
if(mapHostInformation.count(info.GetComponentName()) >= 1) continue;
// Insert current information to map
usedMulticastAddress(info.GetMulticastAddress()); // mark multicast address as in-use
// -- update port assignment usage --
if(info.GetPortNumber() > -1) ServiceManager::portNumberAssigner = info.GetPortNumber() + 1;
mapHostInformation[info.GetComponentName()] = info;
}
}
void ServiceManager::recycleMulticastAddress(const string &multicastAddress)
{
if(multicastAddress.empty()) return;
multicastAddresses.push_back(multicastAddress);
for(ListAddresses::iterator it = usedMulticastAddresses.begin(), it_end = usedMulticastAddresses.end(); it != it_end; ++it)
if(*it == multicastAddress)
{
usedMulticastAddresses.erase(it);
break;
}
}
void ServiceManager::usedMulticastAddress(const string &multicastAddress)
{
if(multicastAddress.empty()) return;
usedMulticastAddresses.push_back(multicastAddress);
for(ListAddresses::iterator it = multicastAddresses.begin(), it_end = multicastAddresses.end(); it != it_end; ++it)
if(*it == multicastAddress)
{
multicastAddresses.erase(it);
break;
}
}
void ServiceManager::cmdPrintHostInformation(ostream &ostr)
{
if(mapHostInformation.empty())
{
ostr << "---- No components in ServiceManager's database..." << endl;
return;
}
ostr << "---- OFFLINE Components in ServiceManager's database: " << endl;
for(MapHostInformation::iterator it = mapHostInformation.begin(), it_end = mapHostInformation.end(); it != it_end; ++it)
if(it->second.GetOnlineStatus() == HostInformation::OFFLINE)
ostr << it->second << endl;
ostr << endl;
ostr << "---- ONLINE Components in ServiceManager's database: " << endl;
for(MapHostInformation::iterator it = mapHostInformation.begin(), it_end = mapHostInformation.end(); it != it_end; ++it)
if(it->second.GetOnlineStatus() == HostInformation::ONLINE)
ostr << it->second << endl;
}
} // namespace ServiceManagerSpace