-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessFactory.cpp
723 lines (609 loc) · 23.3 KB
/
ProcessFactory.cpp
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#include "ProcessFactory/ProcessFactory.h"
#include "ProcessFactory/FactoryLibrary.h"
#ifdef USE_GCC
#else
#include<process.h>
#include<fcntl.h>
#include<time.h>
#include<windows.h>
#include<tlhelp32.h>
#endif
using namespace std;
#define RESOURCE_MONITOR_INTERVAL_MS 3000
#ifdef USE_GCC
#endif
// -------------------------------------------------------
// initialize static struct ProcessFactoryConfig
// -------------------------------------------------------
int ProcessFactoryConfig::factoryPort = MicroMiddleware::MiddlewareSettings::GetProcessFactoryPort();
string ProcessFactoryConfig::display = "";
int ProcessFactoryConfig::debugLevel = 0;
string ProcessFactoryConfig::configurationFile = string("configurationFile.xml");
string ProcessFactoryConfig::systemManagerHost = string("");
int ProcessFactoryConfig::systemManagerPort = MicroMiddleware::MiddlewareSettings::GetSystemManagerPort();
string ProcessFactoryConfig::networkInterfaceIP = string("");
string ProcessFactoryConfig::debugTool = string("");
HostInformation ProcessFactoryConfig::factoryInfo = HostInformation();
#ifdef USE_GCC
string ProcessFactoryConfig::binDir = string("./");
string ProcessFactoryConfig::logDir = string("./");
#else
string ProcessFactoryConfig::binDir;
string ProcessFactoryConfig::logDir;
#endif
// -------------------------------------------------------
// ProcessFactory implementation starts
// -------------------------------------------------------
namespace ProcessFactorySpace
{
// -------------------------------------------------------
// class ProcessFactory
// -------------------------------------------------------
ProcessFactory::ProcessFactory(const string& name, int factoryPort, InterfaceHandle systemManager)
//: Component(name, false)
: privateInstanceId(0)
, systemManagerInterfaceHandle(systemManager)
, factoryInterfaceHandle(factoryPort, MicroMiddleware::MiddlewareSettings::GetHostName())
, resourceMonitor(MicroMiddleware::MiddlewareSettings::GetHostName(), RESOURCE_MONITOR_INTERVAL_MS)
, notifySystemManagerThread(NULL)
, componentFactoryCommand(NULL)
, runProcessFactory(true)
{
#ifdef USE_GCC
srand(Thread::currentThreadId()); //gettimeofday(NULL, NULL));
signal(SIGCHLD, SIG_IGN);
#else
srand(::GetCurrentTime());
#endif
IDEBUG() << "System manager handle " << systemManagerInterfaceHandle;
if(systemManagerInterfaceHandle.GetPortNumber() > 0 && !systemManagerInterfaceHandle.GetHostName().empty())
notifySystemManagerThread = new NotifySystemManagerThread(systemManagerInterfaceHandle);
resourceMonitor.SetNotifySystemManagerThread(notifySystemManagerThread);
resourceMonitor.AddProcess(ProcessFactoryConfig::factoryInfo);
componentFactoryCommand = new ProcessFactoryCommand(*this);
}
ProcessFactory::~ProcessFactory()
{
if(notifySystemManagerThread != NULL)
notifySystemManagerThread->stopThread(true);
}
// -------------------------------------------------------
// update running objects table
// - If a process has died, inform SystemManager
// -------------------------------------------------------
void ProcessFactory::run()
{
bool removedItem = false;
while(runProcessFactory)
{
if(!removedItem)
Thread::sleep(1);
if(!runProcessFactory) break;
removedItem = false;
MutexLocker lock(&guard);
#ifdef USE_GCC
reapZombieProcesses();
#endif
for(MapComponentInformation::iterator it = mapComponentInformation.begin(), it_end = mapComponentInformation.end(); it != it_end; ++it)
{
HostInformation &info = it->second;
if(info.GetProcessId() != -1)
{
int ret = 0; // Process state: Dead = 0, Alive = 1, Undefined State = all other values
#ifdef USE_GCC
int childStatus;
int childPid = waitpid(info.GetProcessId(), &childStatus, WNOHANG);
if(childPid == -1)
{
if(errno != ECHILD)
{
cout << "ERROR " << strerror(errno) << " waiting for child process: " << info << endl;
ret = -1;
}
// With popen this is always true because it uses sh command, which forks a new process (id)
else //if(errno == ECHILD)
ret = ProcessFactoryLibrary::isProcessRunning(info.GetProcessId());
}
else if(childPid == 0) // child not dead
ret = 1;
#else
ret = ProcessFactoryLibrary::isProcessRunning(info.GetProcessId());
#endif
// -- check return value 'ret' --
if(ret < 0 || ret > 1)
{
cout << "ProcessFactory::run(): ERROR while checking if process is running! Return value: " << ret << endl;
continue;
}
else if(ret == 1)// process still running
continue;
}
// -- Process is dead --
info.SetOnlineStatus(HostInformation::OFFLINE);
cout << "ProcessFactory::run(): DEAD! " << info << endl;
#ifdef USE_GCC
if(info.GetProcessId() > -1)
{
stringstream stream;
stream << "/var/run/sonarproc/" << info.GetProcessId() << "-" << info.GetExecutableName() << "-" << info.GetComponentName();
remove(stream.str().c_str());
}
#endif
resourceMonitor.RemoveProcess(info);
notifySystemManager(info, HostInformation::OFFLINE);
mapComponentInformation.erase(it);
removedItem = true;
break;
}
}
}
void ProcessFactory::notifySystemManager(HostInformation &info, HostInformation::OnlineStatus status)
{
if(notifySystemManagerThread == NULL) return;
info.SetOnlineStatus(status);
if(info.GetOnlineStatus() == HostInformation::OFFLINE)
info.SetProcessId(-1);
if(info.GetStartDescription() == HostInformation::START)
info.SetStartDescription(HostInformation::STOP);
notifySystemManagerThread->addAndWakeup(info);
}
#ifdef USE_GCC
void ProcessFactory::reapZombieProcesses()
{
int myProcessId = getpid();
vector<int> childZombies = ProcessFactoryLibrary::Linux::findChildZombies(myProcessId);
int childStatus;
for(vector<int>::iterator it = childZombies.begin(), it_end = childZombies.end(); it != it_end; ++it)
{
int childPid = *it;
int ret = waitpid(childPid, &childStatus, WNOHANG);
if(ret == -1 && errno != ECHILD)
IDEBUG() << "ERROR " << strerror(errno) << " waiting for zombie child process id " << childPid << " " << childStatus;
}
}
#endif
// -------------------------------------------------------
// Process/Node resources
// -------------------------------------------------------
HostResources ProcessFactory::GetProcessResources(string componentName)
{
MutexLocker lock(&guard);
if(mapComponentInformation.count(componentName) <= 0 || componentName.empty())
return HostResources();
const HostInformation &info = mapComponentInformation[componentName];
if(info.GetProcessId() < 0) return HostResources();
return resourceMonitor.GetResources(info.GetProcessId());
}
HostResources ProcessFactory::GetNodeResources()
{
HostResources res = resourceMonitor.GetResources();
res.SetHostName(factoryInterfaceHandle.GetHostName());
return res;
}
string ProcessFactory::GetAllComponentResources()
{
return resourceMonitor.GetStringAllResources();
}
map<int, HostResources> ProcessFactory::GetMapComponentResources()
{
return resourceMonitor.GetMapAllResources();
}
// -------------------------------------------------------
// Checks if the componentInfo.GetProcessId() is running
// -------------------------------------------------------
int ProcessFactory::IsComponentRunning(HostInformation componentInfo)
{
MutexLocker lock(&guard);
int running = ProcessFactoryLibrary::isProcessRunning(componentInfo.GetProcessId(), componentInfo.GetExecutableName());
if(running > 0)
{
cout << "Component " << componentInfo.GetComponentName() << " with process id " << componentInfo.GetProcessId() << " is running" << endl;
if(mapComponentInformation.count(componentInfo.GetComponentName()) <= 0)
{
componentInfo.SetOnlineStatus(HostInformation::ONLINE);
mapComponentInformation[componentInfo.GetComponentName()] = componentInfo;
resourceMonitor.AddProcess(componentInfo);
}
}
return running;
}
// -------------------------------------------------------
// create component
// -------------------------------------------------------
InterfaceHandle ProcessFactory::CreateComponent(const string& componentName, const string& instanceName)
{
if(componentName.empty() || instanceName.empty())
ExceptionMulticast::PostException("CreateComponent(componentName, instanceName): WARNING! Function called with empty strings! " + componentName + ", " + instanceName, MiddlewareException::CONFIGURATION_ERROR);
guard.lock();
// -- start is instanceName already running? --
if(mapComponentInformation.count(instanceName) >= 1)
{
const HostInformation ¤tInfo = mapComponentInformation[instanceName];
if(ProcessFactoryLibrary::isProcessRunning(currentInfo.GetProcessId()) == 1)
{
cerr << "CreateComponent(): WARNING! Component already started: " << currentInfo << endl;
InterfaceHandle handle(currentInfo.GetPortNumber(), currentInfo.GetHostIP());
guard.unlock();
return handle;
}
}
// -- end check if already running --
int componentsInstanceId = ++privateInstanceId;
guard.unlock();
// -- start arrange arguments --
stringstream executableName;
if(componentName.rfind(".exe") != string::npos)
executableName << componentName ;
else
executableName << componentName << ".exe" ;
stringstream args;
if(!instanceName.empty())
args << instanceName << " ";
else
args << componentName << " ";
args << componentsInstanceId << " " << factoryInterfaceHandle.GetPortNumber();
// -- end arrange arguments --
// -- new impl --
HostInformation info(instanceName, factoryInterfaceHandle.GetHostName(), -1, HostInformation::COMPONENT, HostInformation::OFFLINE);
info.SetExecutableName(executableName.str());
if(!ProcessFactoryConfig::networkInterfaceIP.empty())
info.SetHostIP(ProcessFactoryConfig::networkInterfaceIP);
else
info.SetHostIP(factoryInterfaceHandle.GetHostName());
info.SetArgumentList(args.str());
// -- new impl ends --
stringstream executableWithPath;
if(ProcessFactoryConfig::binDir.length() > 0)
executableWithPath << ProcessFactoryConfig::binDir;
executableWithPath << executableName.str();
try
{
stringstream errorMsg;
int processId = createProcess(executableWithPath.str(), instanceName, args.str(), errorMsg);
info.SetProcessId(processId);
// -- new impl starts --
if(processId > -1)
{
#ifdef USE_GCC
stringstream stream;
stream << "/var/run/sonarproc/" << processId << "-" << executableName.str() << "-" << instanceName;
ofstream ofstr(stream.str().c_str());
ofstr << processId << endl;
ofstr.close();
#endif
guard.lock();
info.SetOnlineStatus(HostInformation::ONLINE);
mapComponentInformation[info.GetComponentName()] = info;
resourceMonitor.AddProcess(info);
notifySystemManager(info, HostInformation::ONLINE);
guard.unlock();
cout << "CreateComponent(): SUCCESS! " << info << endl;
return getInterfaceHandle(5000, componentsInstanceId, processId, componentName, instanceName, factoryInterfaceHandle.GetHostName());
}
else
{
info.SetOnlineStatus(HostInformation::OFFLINE);
cout << "CreateComponent(): ERROR! " << info << endl;
}
// -- new impl ends --
}
catch(...)
{
info.SetProcessId(-1);
info.SetOnlineStatus(HostInformation::OFFLINE);
cout << "CreateComponent(): ERROR! " << info << endl;
}
return InterfaceHandle();
}
// -------------------------------------------------------
// create component, called from external components
// -------------------------------------------------------
string ProcessFactory::CreateExtComponent(HostInformation &info, HostInformation lookupServer)
{
info.SetHostDescription(HostInformation::COMPONENT);
info.SetHostName(factoryInterfaceHandle.GetHostName());
if(!ProcessFactoryConfig::networkInterfaceIP.empty())
info.SetHostIP(ProcessFactoryConfig::networkInterfaceIP);
else
info.SetHostIP(factoryInterfaceHandle.GetHostName());
info.SetStartDescription(HostInformation::NO_START_DESCRIPTION);
info.SetOnlineStatus(HostInformation::OFFLINE);
string errorMsg = CreateComponent(info, lookupServer);
MutexLocker lock(&guard);
notifySystemManager(info, info.GetOnlineStatus());
return errorMsg;
}
// -------------------------------------------------------
// create component, called from SystemManager
// -------------------------------------------------------
string ProcessFactory::CreateComponent(HostInformation &info, HostInformation lookupServer)
{
// -- start error checking --
if(info.GetComponentName().empty() || info.GetExecutableName().empty())
{
stringstream stream;
stream << "CreateComponent(info, lookupServer): ERROR! Function called with empty strings!" << endl;
stream << "HostInfo: " << info << endl << " LookupServer: " << lookupServer << endl;
ExceptionMulticast::PostException(stream.str(), MiddlewareException::CONFIGURATION_ERROR);
return stream.str();
}
if(info.GetStartDescription() == HostInformation::NO_START_DESCRIPTION)
cout << "CreateComponent(): WARNING! NO_START_DESCRIPTION is given!" << endl;
// -- end error checking --
guard.lock();
stringstream errorMsg;
// -- Is component already started by factory, and still running? --
if(mapComponentInformation.count(info.GetComponentName()) >= 1)
{
const HostInformation ¤tInfo = mapComponentInformation[info.GetComponentName()];
if(ProcessFactoryLibrary::isProcessRunning(currentInfo.GetProcessId()) == 1)
{
errorMsg << "CreateComponent(): WARNING! Component already started: " << info << endl;
cout << errorMsg.str();
info = currentInfo;
guard.unlock();
return errorMsg.str();
}
}
// -> factory's database is empty due to recent (re)start
// -- see if system manager's process id checks out --
else if(info.GetProcessId() != -1 && ProcessFactoryLibrary::isProcessRunning(info.GetProcessId(), info.GetExecutableName()) == 1) // true? -> component is running
{
info.SetOnlineStatus(HostInformation::ONLINE);
mapComponentInformation[info.GetComponentName()] = info;
errorMsg << "CreateComponent(): WARNING! Component already started: " << info << endl;
cout << errorMsg.str();
guard.unlock();
return errorMsg.str();
}
guard.unlock();
// -- create the new component process --
try
{
stringstream arguments;
arguments << PREFIX_HOST_INFO << " " << info << " "
<< PREFIX_INSTANCE_NAME << " " << info.GetComponentName() << " "
<< PREFIX_LOOKUP_HOST << " " << lookupServer.GetHostIP() << " "
<< PREFIX_LOOKUP_PORT << " " << lookupServer.GetPortNumber() << " ";
//cout << "CreateComponent(): " << info.GetExecutableName() << " " << arguments.str() << endl;
stringstream executableWithPath;
if(ProcessFactoryConfig::binDir.length() > 0)
executableWithPath << ProcessFactoryConfig::binDir;
executableWithPath << info.GetExecutableName();
int processId = createProcess(executableWithPath.str(), info.GetComponentName(), arguments.str(), errorMsg);
info.SetProcessId(processId);
if(processId == -1) // error
{
info.SetOnlineStatus(HostInformation::OFFLINE);
errorMsg << "CreateComponent(): ERROR! " << info << endl;
cout << errorMsg.str();
}
else // success
{
info.SetOnlineStatus(HostInformation::ONLINE);
cout << "CreateComponent(): SUCCESS! " << info << endl;
#ifdef USE_GCC
stringstream stream;
stream << "/var/run/sonarproc/" << processId << "-" << info.GetExecutableName() << "-" << info.GetComponentName();
ofstream ofstr(stream.str().c_str());
ofstr << processId << endl;
ofstr.close();
#endif
guard.lock();
mapComponentInformation[info.GetComponentName()] = info;
resourceMonitor.AddProcess(info);
guard.unlock();
}
}
catch(...)
{
info.SetProcessId(-1);
info.SetOnlineStatus(HostInformation::OFFLINE);
errorMsg << "CreateComponent(): ERROR! " << info << endl;
cout << errorMsg.str();
}
return errorMsg.str();
}
// -------------------------------------------------------
// create process
// -------------------------------------------------------
int ProcessFactory::createProcess(const string &executableName, const string &componentName, const string &args, stringstream &errorMsg)
{
int id = -1;
stringstream command;
#ifdef USE_GCC
// -- start check if executable exists --
ifstream ifile(executableName.c_str());
if(!ifile.is_open())
{
errorMsg << "ProcessFactory::createProcess(): ERROR! Couldn't find executable " << executableName << endl;
ifile.close();
return id;
}
ifile.close();
// -- end check if executable exists --
if(strcmp(ProcessFactoryConfig::display.c_str(), "noX") != 0)
{
id = ProcessFactoryLibrary::Linux::createProcessXtermForkExec(executableName, componentName, args);
}
else if(ProcessFactoryConfig::debugLevel > 0)
{
FILE *fd = NULL;
id = ProcessFactoryLibrary::Linux::createProcessPopenReadPid(executableName, componentName, args, fd);
if(id > 0 && fd != NULL) // NB! -> only need to close fd when I use signal(SIGCHLD, SIG_IGN)
pclose(fd);
// ClosePipeThread *cpt = new ClosePipeThread(fd); // self destructing
}
else
{
id = ProcessFactoryLibrary::Linux::createProcessForkExec(executableName, componentName, args);
}
#else
try
{
command << executableName << " " << args ;
id = ProcessFactoryLibrary::Windows::createProcess(command.str(), componentName);
}
catch(MiddlewareException ex)
{
errorMsg << "CreateProcess(): ERROR! Is " << executableName << " located in the path?" << endl;
}
catch(...)
{
errorMsg << "CreateProcess(): ERROR! Is " << executableName << " located in the path?" << endl;
}
#endif
return id;
}
int ProcessFactory::KillAll()
{
MutexLocker singleLock(&guard);
for(MapComponentInformation::iterator it = mapComponentInformation.begin(), it_end = mapComponentInformation.end(); it != it_end; ++it)
{
(it->second).SetStartDescription(HostInformation::STOP);
resourceMonitor.RemoveProcess((it->second));
try
{
ProcessFactoryLibrary::killProcess(it->second);
}
catch(...)
{
cout << "ProcessFactory::KillAll(): ERROR while Removing dead process : " << (it->second) << endl;
continue;
}
cout << "ProcessFactory::KillAll(): Removing dead process : " << (it->second) << endl;
}
mapComponentInformation.clear();
return 1;
}
int ProcessFactory::KillComponent(string componentName)
{
MutexLocker singleLock(&guard);
for(MapComponentInformation::iterator it = mapComponentInformation.begin(), it_end = mapComponentInformation.end(); it != it_end; ++it)
if((it->second).GetComponentName() == componentName)
{
(it->second).SetStartDescription(HostInformation::STOP);
ProcessFactoryLibrary::killProcess(it->second);
cout << "ProcessFactory::KillComponent(): Removing dead process : " << (it->second) << endl;
resourceMonitor.RemoveProcess((it->second));
mapComponentInformation.erase(it);
return 1;
}
return 0;
}
// -------------------------------------------------------
// get component's interfacehandle
// -------------------------------------------------------
InterfaceHandle ProcessFactory::GetComponent(const string& componentName, const string& instanceName)
{
MutexLocker singleLock(&guard);
for(MapComponentInformation::iterator it = mapComponentInformation.begin(), it_end = mapComponentInformation.end(); it != it_end; ++it)
if((it->second).GetComponentName() == componentName || (it->second).GetComponentName() == instanceName)
return InterfaceHandle((it->second).GetPortNumber(), (it->second).GetHostName());
throw MiddlewareException("Could not find " + instanceName);
return InterfaceHandle();
}
InterfaceHandle ProcessFactory::getInterfaceHandle(
int timeOut,
int instanceId,
int processId,
const string& componentName,
const string& instanceName,
const string& hostName)
{
int sleepInterval = 100; // milliseconds
int maxNumberOfTries = timeOut/sleepInterval;
int numberOfTries = 0;
while(numberOfTries < maxNumberOfTries)
{
guard.lock();
for(MapInstanceIdComponentName::iterator it = mapInstanceIdComponentName.begin(), it_end = mapInstanceIdComponentName.end(); it != it_end; ++it)
{
if((it->first) == instanceId)
{
if(mapComponentInformation.count(it->second) <= 0)
{
throw MiddlewareException("ProcessFactory::getInterfaceHandle(): Couldn't find the component " + it->second);
//cout << "ProcessFactory::getInterfaceHandle(): Couldnt find the component " << it->second << endl;
}
const HostInformation &info = mapComponentInformation[it->second];
InterfaceHandle handle = InterfaceHandle(info.GetPortNumber(), info.GetHostName());
cout << "ProcessFactory::getInterfaceHandle(): Returning InterfaceHandle(" << handle << ")" << endl;
mapInstanceIdComponentName.erase(it);
guard.unlock();
return handle;
}
}
guard.unlock();
int ret = ProcessFactoryLibrary::isProcessRunning(processId);
if(ret < 1)
{
cout << "ProcessFactory::getInterfaceHandle(): DEAD! Process " << componentName << " " << instanceName << " is not running! " << endl;
break;
}
numberOfTries++;
Thread::sleep(1); //sleepInterval);
}
stringstream ss;
ss << "ProcessFactory::getInterfaceHandle(): Could not create instance "
<< instanceName << " of type " << componentName << " on " << hostName
<< " within the given time limit (" << timeOut/1000.0 << " s).";
cout << ss.str() << endl;
throw MiddlewareException(ss.str());
}
// -------------------------------------------------------
// outdated function to report back that a component has been created
// -------------------------------------------------------
void ProcessFactory::ComponentCreated(
const string& hostName,
int mainStubPort,
int processId,
const string& componentName,
const string& processFileName,
int instanceId)
{
HostInformation info;
info.SetComponentName(componentName);
info.SetProcessId(processId);
info.SetExecutableName(processFileName);
info.SetHostName(hostName);
info.SetPortNumber(mainStubPort);
info.SetHostDescription(HostInformation::COMPONENT);
info.SetOnlineStatus(HostInformation::ONLINE);
if(!ProcessFactoryConfig::networkInterfaceIP.empty())
info.SetHostIP(ProcessFactoryConfig::networkInterfaceIP);
else
info.SetHostIP(hostName);
MutexLocker lock(&guard);
mapInstanceIdComponentName[instanceId] = componentName;
mapComponentInformation[componentName] = info;
cout << "ProcessFactory::ComponentCreated(): " << componentName << " (port=" << mainStubPort << ", pid=" << processId << ")" << endl;
}
// -------------------------------------------------------
// Functions used by ProcessFactory command UI
// -------------------------------------------------------
void ProcessFactory::cmdKillAll()
{
MapComponentInformation tempMap = mapComponentInformation;
this->KillAll();
for(MapComponentInformation::iterator it = tempMap.begin(), it_end = tempMap.end(); it != it_end; ++it)
this->notifySystemManager(it->second, HostInformation::OFFLINE);
}
void ProcessFactory::cmdQuit()
{
MutexLocker lock(&guard);
runProcessFactory = false;
}
// -------------------------------------------------------
// print component factory's data-base
// -------------------------------------------------------
void ProcessFactory::cmdPrintHostInformation(ostream& ostr)
{
MutexLocker Lock(&guard);
ostr << "------------- Host Resources for Components on Factory " << factoryInterfaceHandle.GetHostName() << " --------------" << endl;
ostr << resourceMonitor.GetStringAllResources();
ostr << "----------------------- Components On Factory " << factoryInterfaceHandle.GetHostName() << " -----------------------" << endl;
for(MapComponentInformation::iterator it = mapComponentInformation.begin(), it_end = mapComponentInformation.end(); it != it_end; ++it)
ostr << it->second << endl;
}
}