scheduler.cpp 11.1 KB
Newer Older
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
// Header include
#include <ITA/simulation_scheduler/room_acoustics/scheduler.h>

// simulation scheduler includes
#include <ITA/simulation_scheduler/room_acoustics/result_handler.h>
#include "../src/ITA/simulation_scheduler/room_acoustics/replacement_filter.h"

// Vista includes
#include <VistaBase/VistaTimeUtils.h>

// ITA include
#include <ITAException.h>

namespace ITA
{
	namespace simulation_scheduler
	{
		namespace room_acoustics
		{
			void CScheduler::SchedulerConfig::setWorkerConfig (
				const std::vector<IWorkerInterface::WorkerConfig>& vWorkerConfig )
			{
				if ( HasSubList ( "Scheduler" ) )
				{
					auto&& schedulerConfig = GetSubListRef ( "Scheduler" );

					auto config = VistaPropertyList ( );

					for ( auto workerConfig : vWorkerConfig )
					{
						// Check whether there is only one item in workerConfig.
						// If so, begin() points to this item.
						// So we check if it is a property list so we can use it as the config.
						if ( workerConfig.size ( ) == 1 &&
							 workerConfig.begin ( )->second.GetPropertyType ( ) == VistaProperty::ePropType::PROPT_PROPERTYLIST )
							config.MergeWith ( workerConfig );
						else
							ITA_EXCEPT_INVALID_PARAMETER ( "The given config should only have one sub list entry." );
					}

					schedulerConfig.SetPropertyListValue ( "Scheduler/Worker", config );
				}
				else
					ITA_EXCEPT_INVALID_PARAMETER ( "Config is not a Scheduler config." )
			}

			void CScheduler::SchedulerConfig::setAudibilityFilterConfig (
				const std::list<audibility_filter::IAudibilityFilter::AudibilityFilterConfig>& vFilterConfig )
			{
				if ( HasSubList ( "Scheduler" ) )
				{
					auto&& schedulerConfig = GetSubListRef ( "Scheduler" );

					auto config = VistaPropertyList ( );

					int counter = 0;

					for ( auto filterConfig : vFilterConfig )
					{
						// Check whether there is only one item in filterConfig.
						// If so, begin() points to this item.
						// So we check if it is a property list so we can use it as the config.
						if ( filterConfig.size ( ) == 1 &&
							 filterConfig.begin ( )->second.GetPropertyType ( ) == VistaProperty::ePropType::PROPT_PROPERTYLIST )
						{
							schedulerConfig.SetValueInSubList ( std::to_string ( counter ), "Scheduler/AudibilityFilterOrder", filterConfig.begin ( )->first );
							config.MergeWith ( filterConfig );
						}
						else
							ITA_EXCEPT_INVALID_PARAMETER ( "The given config should only have one sub list entry." );

						counter++;
					}

					schedulerConfig.SetPropertyListValue ( "Scheduler/AudibilityFilter", config );
				}
				else
					ITA_EXCEPT_INVALID_PARAMETER ( "Config is not a Scheduler config." )
			}

			std::vector<IWorkerInterface::WorkerConfig> CScheduler::SchedulerConfig::
				getWorkerConfig ( ) const
			{
				if ( HasSubList ( "Scheduler" ) )
				{
					const auto schedulerConfig = GetSubListConstRef ( "Scheduler" );

					if ( schedulerConfig.HasSubList ( "Scheduler/Worker" ) )
					{
						const auto workerConfigs = schedulerConfig.GetSubListConstRef ( "Scheduler/Worker" );

						std::vector<IWorkerInterface::WorkerConfig> configs;

						for ( auto iter = workerConfigs.begin ( ); iter != workerConfigs.end ( ); ++iter )
						{
							IWorkerInterface::WorkerConfig workerConfig;
							workerConfig.SetPropertyListValue ( iter->first, iter->second.GetPropertyListValue ( ) );
							configs.push_back ( workerConfig );
						}

						return configs;
					}
					else
						return std::vector<IWorkerInterface::WorkerConfig> ( );
				}
				else
					ITA_EXCEPT_INVALID_PARAMETER ( "Config is not a Scheduler config." )
			}

			std::list<audibility_filter::IAudibilityFilter::AudibilityFilterConfig> CScheduler::SchedulerConfig::
				getAudibilityFilterConfig ( ) const
			{
				if ( HasSubList ( "Scheduler" ) )
				{
					const auto schedulerConfig = GetSubListConstRef ( "Scheduler" );

					if ( schedulerConfig.HasSubList ( "Scheduler/AudibilityFilter" ) && schedulerConfig.HasSubList ( "Scheduler/AudibilityFilterOrder" ) )
					{
						const auto filterConfigs = schedulerConfig.GetSubListConstRef ( "Scheduler/AudibilityFilter" );

						const auto filterOrder = schedulerConfig.GetSubListConstRef ( "Scheduler/AudibilityFilterOrder" );

						std::list<audibility_filter::IAudibilityFilter::AudibilityFilterConfig> configs;

						for ( int i = 0; i < filterOrder.size ( ); ++i )
						{
							auto filterName = filterOrder.GetValue<std::string> ( std::to_string ( i ) );

							audibility_filter::IAudibilityFilter::AudibilityFilterConfig filterConfig;

							filterConfig.SetPropertyListValue ( filterName, filterConfigs.GetSubListCopy ( filterName ) );

							configs.push_back ( filterConfig );
						}

						return configs;
					}
					else
						return std::list<audibility_filter::IAudibilityFilter::AudibilityFilterConfig> ( );
				}
				else
					ITA_EXCEPT_INVALID_PARAMETER ( "Config is not a Scheduler config." )
			}

			CScheduler::CScheduler ( const SchedulerConfig& pConfig )
			{
				if ( pConfig.HasSubList ( "Scheduler" ) )
				{
					m_bReplaceUpdates = pConfig.GetValueInSubList<bool> ( "ReplaceUpdates", "Scheduler" );

					m_eFieldOfDuty = static_cast< FieldOfDuty >( pConfig.GetValueInSubList<int> ( "FieldOfDuty", "Scheduler" ) );

					auto filterConfigs = pConfig.getAudibilityFilterConfig ( );

					auto workerConfigs = pConfig.getWorkerConfig ( );

					for ( const auto& filterConfig : filterConfigs )
					{
						m_lAudibilityFilters.push_back ( std::move ( audibility_filter::CAudibilityFilterFactory::createFilter ( filterConfig ) ) );
					}

					for ( const auto& workerConfig : workerConfigs )
					{
						m_vWorker.push_back ( std::move ( CWorkerFactory::createWorker ( workerConfig, this ) ) );
					}

					Run ( );
				}
				else
					ITA_EXCEPT_INVALID_PARAMETER ( "Config is not a Scheduler config." )
			}

			CScheduler::~CScheduler ( )
			{
				m_bStopIndicated = true;
				m_evTriggerLoop.SignalEvent ( );

				while ( m_bStopACK != true )
					VistaTimeUtils::Sleep ( 250 );

				m_evTriggerLoop.SignalEvent ( );

				VistaThreadLoop::StopGently ( true );
			}

			void CScheduler::postUpdate ( std::unique_ptr<IUpdateMessage> pUpdateMessage )
			{
				m_qUpdateQueue.push ( std::move ( pUpdateMessage ) );
				m_evTriggerLoop.SignalEvent ( );
			}

			void CScheduler::attachResultHandler ( IResultHandler* pResultHandler )
			{
				m_vpResultHandlers.push_back ( pResultHandler );
			}

			void CScheduler::detachResultHandler ( IResultHandler* pResultHandler )
			{
				const auto iterator = std::remove ( m_vpResultHandlers.begin ( ), m_vpResultHandlers.end ( ), pResultHandler );
				m_vpResultHandlers.erase ( iterator );
			}

			void CScheduler::handleSimulationFinished ( std::unique_ptr<CSimulationResult> pResult )
			{
				for ( auto handler : m_vpResultHandlers )
					handler->postResultReceived ( std::make_unique<CSimulationResult> ( *pResult ) );
			}

			void CScheduler::processUpdateScene ( std::unique_ptr <CUpdateScene> pUpdate )
			{
				auto bAudible = true;

				// Check if a previous update exists.
				if ( m_mPreviousStates.find ( pUpdate->getReferenceID ( ) ) != m_mPreviousStates.end ( ) )
				{
					// Test if the update is audible.
					// Note the extra condition in the for loop, this gains performance if a previous update indicates non audibility.
					for ( auto filterIter = m_lAudibilityFilters.begin ( );
						  bAudible && filterIter != m_lAudibilityFilters.end ( );
						  ++filterIter )
					{
222
						bAudible &= ( *filterIter )->changeIsAudible ( *m_mPreviousStates.at ( pUpdate->getReferenceID ( ) ), *pUpdate );
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
					}
				}

				if ( bAudible )
					m_lPendingUpdateList.push_back ( std::move ( pUpdate ) );
			}

			void CScheduler::processUpdateConfig ( std::unique_ptr <CUpdateConfig> pUpdate )
			{
				switch ( pUpdate->getType ( ) )
				{
					case CUpdateConfig::ConfigChangeType::changeDirectivity:
						ITA_EXCEPT_NOT_IMPLEMENTED;
						break;
					case CUpdateConfig::ConfigChangeType::changeHRTF:
						ITA_EXCEPT_NOT_IMPLEMENTED;
						break;
					case CUpdateConfig::ConfigChangeType::resetAll:
						reset ( );
						break;
					default:
						ITA_EXCEPT_INVALID_PARAMETER ( "Unkown type of config change" );
				}
			}

			void CScheduler::reset ( )
			{
				// signal the reset to the workers
				for ( auto& worker : m_vWorker )
				{
					while ( worker->isBusy ( ) )
						VistaTimeUtils::Sleep ( 250 );

					worker->reset ( );
				}

				// The loop was on hold, clear all update lists.
				m_qUpdateQueue.clear ( );
				m_lUpdateList.clear ( );
				m_lPendingUpdateList.clear ( );

				// start the loop again.
				m_bResetIndicated = true;
				m_evTriggerLoop.SignalEvent ( );
			}

			bool CScheduler::LoopBody ( )
			{
				// Wait for trigger 
				m_evTriggerLoop.WaitForEvent ( true );
				m_evTriggerLoop.ResetThisEvent ( );

				if ( m_bResetIndicated )
				{
					m_bResetIndicated = false;
				}

				// Handle the stop of the loop
				if ( m_bStopIndicated )
				{
					IndicateLoopEnd ( );
					m_bStopACK = true;
					return false;
				}

				// get the updates from the queue to a list.
				std::unique_ptr<IUpdateMessage> updateMessage;

				while ( m_qUpdateQueue.try_pop ( updateMessage ) )
				{
					m_lUpdateList.push_back ( std::move ( updateMessage ) );
				}

				// process the updates
				for ( auto& update : m_lUpdateList )
				{
					const auto rawUpdate = update.release ( );
					if ( const auto updateConfig = dynamic_cast< CUpdateConfig* >( rawUpdate ) )
					{
						processUpdateConfig ( std::unique_ptr<CUpdateConfig> ( updateConfig ) );

						if ( m_bResetIndicated )
						{
							return true;
						}
					}
					else if ( const auto updateScene = dynamic_cast< CUpdateScene* >( rawUpdate ) )
						processUpdateScene ( std::unique_ptr <CUpdateScene> ( updateScene ) );
					else
						ITA_EXCEPT_INVALID_PARAMETER ( "Unkown type of UpdateMessage" );
				}

				// Now, the list only contains non valid unique_ptr, we can clear it.
				m_lUpdateList.clear ( );

				// replace old updates
				if ( m_bReplaceUpdates )
					CReplacementFilter::filterReplace ( m_lPendingUpdateList );

				// distribute updates to the worker
				for ( auto workerIter = m_vWorker.begin ( );
					  !m_lPendingUpdateList.empty ( ) && workerIter != m_vWorker.end ( );
					  ++workerIter )
				{
					auto& worker = *workerIter;
					if ( !worker->isBusy ( ) )
					{
						// get the update out of the list.
						auto update = std::move ( m_lPendingUpdateList.front ( ) );
						m_lPendingUpdateList.pop_front ( );

						// save a copy for the filters.
						m_mPreviousStates [update->getReferenceID ( )] = new CUpdateScene ( *update );

						// post the update to the worker.
						worker->postUpdate ( std::move ( update ) );
					}
				}

				return true;
			}

			std::vector<IWorkerInterface*> CScheduler::getWorker ( )
			{
				std::vector<IWorkerInterface*> vec;

				for ( const auto& worker : m_vWorker )
				{
					vec.push_back ( worker.get ( ) );
				}

				return vec;
			}

			std::vector<audibility_filter::IAudibilityFilter*> CScheduler::getFilter ( )
			{
				std::vector<audibility_filter::IAudibilityFilter*> vec;

				for ( const auto& filter : m_lAudibilityFilters )
				{
					vec.push_back ( filter.get ( ) );
				}

				return vec;
			}
		} // namespace room_acoustics
	} // namespace simulation_scheduler
} // namespace ITA