HCE project C++ developers source code library  1.1.1
HCE project developer library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ComponentManager.cpp
Go to the documentation of this file.
1 #include <Poco/ScopedLock.h>
2 
3 #include "ComponentManager.hpp"
7 
8 #include <unistd.h>
9 
10 #define COMPONENT_WAIT_TIME 10
11 
12 namespace HCE
13 {
14  namespace component
15  {
16  Poco::SharedPtr<ComponentBase> ComponentManager::getFreeComponent()
17  {
18  Poco::SharedPtr<ComponentBase> retComponent;
19  insertComponentMutex.lock();
20  typename std::vector<Poco::SharedPtr<ComponentBase> >::iterator it;
21  while(retComponent.isNull())
22  {
23  for(it = componentsList.begin(); it != componentsList.end(); ++it)
24  {
25  if(!((*it)->getIsBusy()))
26  {
27  (*it)->setIsBusy(true);
28  retComponent = (*it);
29  break;
30  }
31  }
32  usleep(COMPONENT_WAIT_TIME);
33  }
34  insertComponentMutex.unlock();
35  return retComponent;
36  }
37 
38  void ComponentManager::addComponent(Poco::SharedPtr<ComponentBase> componentPtr)
39  {
40  if(!componentPtr.isNull())
41  {
42  insertComponentMutex.lock();
43  componentsList.push_back(componentPtr);
44  insertComponentMutex.unlock();
45  }
46  }
47 
48  void ComponentManager::setTaskManager(Poco::SharedPtr<Poco::TaskManager> taskManager)
49  {
50  if(_taskManager.isNull())
51  {
52  _taskManager = taskManager;
53  }
54  }
55 
56  void ComponentManager::addTask(Poco::SharedPtr<TaskWaitObject> task)
57  {
58  if(_taskManager.isNull())
59  {
60  throw TaskManagerEmptyException("TaskManagerEmpty");
61  }
62  else
63  {
64  Poco::ScopedLock<Poco::FastMutex> sLock(incomeDataMutex);
65  taskQueue.push(task);
66  conditionVar.signal();
67  }
68  }
69 
71  {
72  Poco::ScopedLock<Poco::FastMutex> sLock(incomeDataMutex);
73  while(!taskQueue.empty())
74  {
75  taskQueue.pop();
76  }
77  }
78 
80  {
81  if(!ownThread.isRunning())
82  {
83  ownThread.start(*this);
84  }
85  else
86  {
87  throw ThreadAlreadyRunException("ThreadAlreadyRun");
88  }
89  }
90 
92  {
93  exitFlag = true;
94  Poco::ScopedLock<Poco::FastMutex> sLock(incomeDataMutex);
95  conditionVar.signal();
96  }
97 
98 
100  {
101  Poco::SharedPtr<ComponentBase> localObjectPtr;
102  while(!exitFlag)
103  {
104  incomeDataMutex.lock();
105  while(!taskQueue.empty())
106  {
107  Poco::SharedPtr<TaskWaitObject> taskPtr = taskQueue.front();
108  taskQueue.pop();
109  incomeDataMutex.unlock();
110  localObjectPtr = getFreeComponent();
111 //------------------------ insert task ---------------------------------------------------------------------------
112  TaskWaitObjectWrapper *taskWrapper = new TaskWaitObjectWrapper(taskPtr, localObjectPtr, componentMutex);
113  while(true)
114  {
115  try
116  {
117  _taskManager->start(taskWrapper);
118  break;
119  }
120  catch(Poco::NoThreadAvailableException &excp)
121  {
122  usleep(COMPONENT_WAIT_TIME);
123  }
124  catch(Poco::Exception &exp)
125  {
126 // std::string what = exp.what();
127  usleep(COMPONENT_WAIT_TIME);
128  }
129  }
130 //------------------------ insert task END -----------------------------------------------------------------------
131  incomeDataMutex.lock();
132  }
133  if(exitFlag)
134  {
135  incomeDataMutex.unlock();
136  break;
137  }
139  incomeDataMutex.unlock();
140  }
141  if(!_threadPool.isNull())
142  {
143  _threadPool->stopAll();
144  _threadPool->joinAll();
145  }
146  }
147  }
148 }