-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMrcpTaskProcessor.cpp
260 lines (232 loc) · 7.51 KB
/
CMrcpTaskProcessor.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
///////////////////////////////////////////////////////////////////////////////
// File Name: CMrcpTaskProcessor.cpp
//
// Description: CMrcpTaskProcessor class implementation
// Provides the command and callback queue capabilities
// Marshalls commands sent and events received
//
// Declared in this file:
// none
//
// Implemented in this file:
// CMrcpTaskProcessor::~CMrcpTaskProcessor();
// int CMrcpTaskProcessor::Start();
// int CMrcpTaskProcessor::Stop();
// int CMrcpTaskProcessor::QueueTask( MrcpTasks* a_mrcpTasks);
// static CMrcpTaskProcessor* CMrcpTaskProcessor::Instance(std::string a_idString = "TASK");
//
///////////////////////////////////////////////////////////////////////////////
// Revisions:
// Date Initial Description
// ---- ------- -----------
//
///////////////////////////////////////////////////////////////////////////////
#include "CMrcpTaskProcessor.h"
#include "MrcpUtils.h"
#include "MrcpTasks.h"
#include "MrcpClientEvents.h"
#include <queue>
#include <map>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/bind.hpp>
namespace MrcpV2RefLib
{
CMrcpTaskProcessor::CMrcpTaskProcessorByStringMap CMrcpTaskProcessor::m_taskProcessors;
boost::mutex CMrcpTaskProcessor::m_singleton;
//////////////////////////////////////////////////////////////////////////
//
// Description - Constructor
// Input - string containing ID for instance
//
// Output - None
// string identifies instance as either callback or task processor
///////////////////////////////////////////////////////////////////////////
CMrcpTaskProcessor::CMrcpTaskProcessor(std::string a_idString)
:m_eventProcessorRunning(false)
,m_idString(a_idString)
,m_name("CMrcpTaskProcessor")
{
ClassName("CMrcpTaskProcessor");
}
//////////////////////////////////////////////////////////////////////////
//
// Description - Destructor
// Input - None
//
// Output - None
//
///////////////////////////////////////////////////////////////////////////
CMrcpTaskProcessor::~CMrcpTaskProcessor()
{
CMrcpTaskProcessorByStringMap::iterator l_pos;
if ((l_pos = m_taskProcessors.find(m_idString)) != m_taskProcessors.end())
{
boost::mutex::scoped_lock l_lock( m_singleton);
m_taskProcessors.erase(m_idString);
}
}
//////////////////////////////////////////////////////////////////////////
//
// Description - Instance
// Input - string - id string identifies the instance
//
// Output - returns pointer to task processor instance
// Instantiates a task processir class by ID or returns pointer
// if object already instantiated
///////////////////////////////////////////////////////////////////////////
CMrcpTaskProcessor* CMrcpTaskProcessor::Instance(std::string a_idString)
{
CMrcpTaskProcessorByStringMap::iterator l_pos;
CMrcpTaskProcessor* l_taskProcessor;
if ((l_pos = m_taskProcessors.find(a_idString)) == m_taskProcessors.end())
{
boost::mutex::scoped_lock l_lock( m_singleton);
if ((l_pos = m_taskProcessors.find(a_idString)) == m_taskProcessors.end())
{
l_taskProcessor = new CMrcpTaskProcessor(a_idString);
m_taskProcessors.insert(std::make_pair( a_idString, l_taskProcessor));
if ((l_pos = m_taskProcessors.find(a_idString)) == m_taskProcessors.end())
{
std::string l_errorInfo = "Error creating Event Processor for: " + a_idString;
CLogger::Instance()->Log( LOG_LEVEL_ERROR, l_errorInfo);
return NULL;
}
}
}
return l_pos->second;
}
//////////////////////////////////////////////////////////////////////////
//
// Description - Start
// Input - None
//
// Output - success or failure
// starts a thread that will process entries added to the queue
///////////////////////////////////////////////////////////////////////////
int CMrcpTaskProcessor::Start()
{
Name("Start");
{ // scope for lock
boost::mutex::scoped_lock l_controlLock( m_controlMutex);
if ( EventProcessorRunning())
{
std::string l_statInfo = "Error starting task processor: " + m_idString;
CLogger::Instance()->Log( LOG_LEVEL_ERROR, *this, l_statInfo);
return -1;
}
EventProcessorRunning(true);
m_thread = boost::shared_ptr<boost::thread> (new boost::thread( boost::bind( &CMrcpTaskProcessor::ProcessTasks, this)));
}
while (!EventProcessorRunning())
{
MrcpUtils::MsSleep(100);
}
return MrcpSuccess;
}
//////////////////////////////////////////////////////////////////////////
//
// Description - Stop
// Input - None
//
// Output - Success or failure
// signals the processing thread to shutdown
///////////////////////////////////////////////////////////////////////////
int CMrcpTaskProcessor::Stop()
{
Name("Stop");
{ // scope for lock
boost::mutex::scoped_lock l_controlLock( m_controlMutex);
if ( !EventProcessorRunning() )
{
std::string l_statusInfo = "Error Stopping Task Processor: " + m_idString;
CLogger::Instance()->Log( LOG_LEVEL_ERROR, *this, l_statusInfo);
return -1;
}
EventProcessorRunning(false);
m_semaphore.notify_one();
m_thread->join();
}
return MrcpSuccess;
}
//////////////////////////////////////////////////////////////////////////
//
// Description - QueueTask
// Input - task pointer
//
// Output - success or fail
// places a command or event in the queue and signals the processing
// thread.
///////////////////////////////////////////////////////////////////////////
int CMrcpTaskProcessor::QueueTask( MrcpTasks* a_mrcpTask)
{
Name("QueueTask");
if ( EventProcessorRunning())
{
{ // scope for lock
boost::mutex::scoped_lock l_lock( m_queueMutex);
m_queue.push( a_mrcpTask);
}
m_semaphore.notify_one();
return MrcpSuccess;
}
else
{
CLogger::Instance()->Log( LOG_LEVEL_ERROR, *this, "Processor not running");
return -1;
}
}
//////////////////////////////////////////////////////////////////////////
//
// Description - ProcessTasks
// Input - None
//
// Output - None
// processes all command or event objects queued. Calls their execute
// methods for object processing
///////////////////////////////////////////////////////////////////////////
void CMrcpTaskProcessor::ProcessTasks()
{
Name("ProcessTasks");
MrcpTasks* l_task;
boost::mutex::scoped_lock semaphoreLock( m_semaphoreMutex);
std::string l_statusInfo = "Entering Processing Thread: " + m_idString;
CLogger::Instance()->Log( LOG_LEVEL_INFO, *this, l_statusInfo);
{ // scope for lock
boost::mutex::scoped_lock l_controlLock( m_controlMutex);
EventProcessorRunning(true);
}
while( EventProcessorRunning())
{
if ( m_queue.empty())
{
m_semaphore.wait(semaphoreLock);
}
{ // scope for lock
boost::mutex::scoped_lock l_queueLock( m_queueMutex);
if ( !m_queue.empty ())
{
l_task = m_queue.front();
m_queue.pop();
}
else
{
continue;
}
}
try
{
l_task->Execute();
}
catch(...)
{
std::string l_debugInfo = "Caught Exception! debug info: " + MrcpUtils::itos((int)l_task->m_signalObj);
CLogger::Instance()->Log( LOG_LEVEL_ERROR, *this, "ProcessTasks", l_debugInfo);
}
delete l_task;
}
std::string l_stat2Info = "Exiting Processing Thread: " + m_idString;
CLogger::Instance()->Log( LOG_LEVEL_INFO, *this, l_stat2Info);
}
}//end namespace