2 * Copyright (c) 2012 BMW
4 * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
12 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
13 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 * For further information see http://www.genivi.org/.
18 #ifndef ROUTINGSENDER_H_
19 #define ROUTINGSENDER_H_
21 #include "routing/IAmRoutingSend.h"
22 #include "IAmRoutingReceiverShadow.h"
23 #include <semaphore.h>
29 class CAmWorkerThreadPool;
32 * Base class for workers implements everything that is needed to implement a workerthread
33 * inherit from this class when adding new workers
38 CAmWorker(CAmWorkerThreadPool* pool);
42 * needs to be overwritten, this function is called when the worker should start to work
44 void virtual start2work()=0;
47 * needs to be overwritten, this function is called when the worker thread is canceled. Should be used for
48 * clean up and sending important messages
50 void virtual cancelWork()=0;
52 * waits for a semaphore with a timeout. This leaves the Threadpool the chance to interrupt the processing
53 * You should call whenever waiting on some event, even with time=0 in order to make sure that cancel events are
55 * @param time time until timeout in timespec format
56 * @return true if thread is canceled. Then just return start2work function so that the thread is given back to the pool
58 bool timedWait(timespec time);
61 * the semaphore for cancellation is set by the thread automatically...
64 void setCancelSempaphore(sem_t* cancel);
65 CAmWorkerThreadPool* pPool;
67 sem_t* mCancelSem; //<! semaphore for cancellation
71 * This class handles the threadpool
73 class CAmWorkerThreadPool
77 * creates the pool. Give the max number of threads as argument
78 * @param numThreads max number of threads
80 CAmWorkerThreadPool(int numThreads);
81 virtual ~CAmWorkerThreadPool();
84 * assigns a thread to a worker class and starts working.
86 * @return the actual assigned workerID or -1 in case no thread is free
88 int16_t startWork(CAmWorker* worker);
91 * @param workerID thread to be canceled
92 * @return true if thread was found, false if not
94 bool cancelWork(int workerID);
97 * the workers call this function upon completion of their task
100 void finishedWork(pthread_t threadID);
103 static void* CAmWorkerThread(void* data);
114 std::vector<threadInfo_s> mListWorkers; //<! list of all workers
115 static pthread_mutex_t mBlockingMutex; //<! mutex to block the acces of the list
118 class CAmRoutingSenderAsync: public IAmRoutingSend
121 CAmRoutingSenderAsync();
122 virtual ~CAmRoutingSenderAsync();
123 am_Error_e startupInterface(IAmRoutingReceive* routingreceiveinterface) ;
124 void setRoutingReady(const uint16_t handle) ;
125 void setRoutingRundown(const uint16_t handle) ;
126 am_Error_e asyncAbort(const am_Handle_s handle) ;
127 am_Error_e asyncConnect(const am_Handle_s handle, const am_connectionID_t connectionID, const am_sourceID_t sourceID, const am_sinkID_t sinkID, const am_ConnectionFormat_e connectionFormat) ;
128 am_Error_e asyncDisconnect(const am_Handle_s handle, const am_connectionID_t connectionID) ;
129 am_Error_e asyncSetSinkVolume(const am_Handle_s handle, const am_sinkID_t sinkID, const am_volume_t volume, const am_RampType_e ramp, const am_time_t time) ;
130 am_Error_e asyncSetSourceVolume(const am_Handle_s handle, const am_sourceID_t sourceID, const am_volume_t volume, const am_RampType_e ramp, const am_time_t time) ;
131 am_Error_e asyncSetSourceState(const am_Handle_s handle, const am_sourceID_t sourceID, const am_SourceState_e state) ;
132 am_Error_e asyncSetSinkSoundProperties(const am_Handle_s handle, const am_sinkID_t sinkID, const std::vector<am_SoundProperty_s>& listSoundProperties) ;
133 am_Error_e asyncSetSinkSoundProperty(const am_Handle_s handle, const am_sinkID_t sinkID, const am_SoundProperty_s& soundProperty) ;
134 am_Error_e asyncSetSourceSoundProperties(const am_Handle_s handle, const am_sourceID_t sourceID, const std::vector<am_SoundProperty_s>& listSoundProperties) ;
135 am_Error_e asyncSetSourceSoundProperty(const am_Handle_s handle, const am_sourceID_t sourceID, const am_SoundProperty_s& soundProperty) ;
136 am_Error_e asyncCrossFade(const am_Handle_s handle, const am_crossfaderID_t crossfaderID, const am_HotSink_e hotSink, const am_RampType_e rampType, const am_time_t time) ;
137 am_Error_e setDomainState(const am_domainID_t domainID, const am_DomainState_e domainState) ;
138 am_Error_e returnBusName(std::string& BusName) const ;
139 void getInterfaceVersion(std::string& version) const ;
142 * threadafe insert of route and connectionID
143 * @param connectionID
146 void insertConnectionSafe(am_connectionID_t connectionID, am_RoutingElement_s route);
149 * threadsafe removal of a connection
152 void removeConnectionSafe(am_connectionID_t);
155 * threadsafe remove of a handle
158 void removeHandleSafe(uint16_t handle);
161 * threadsafe update of Sinkvolume
165 void updateSinkVolumeSafe(am_sinkID_t sinkID, am_volume_t volume);
168 * threadsafe update of SourceVolume
172 void updateSourceVolumeSafe(am_sourceID_t sourceID, am_volume_t volume);
175 * threadsafe update of sourceState
179 void updateSourceStateSafe(am_sourceID_t sourceID, am_SourceState_e state);
182 * threadsafe update of sinkSoundProperty
184 * @param soundProperty
186 void updateSinkSoundPropertySafe(am_sinkID_t sinkID, am_SoundProperty_s soundProperty);
189 * threadsafe update of sourceSoundProperty
191 * @param soundProperty
193 void updateSourceSoundPropertySafe(am_sourceID_t sourceID, am_SoundProperty_s soundProperty);
196 * threadsafe update of domainstate
200 void updateDomainstateSafe(am_domainID_t domainID, am_DomainState_e domainState);
202 void updateDomainListSafe(std::vector<am_Domain_s> listDomains);
204 void updateSourceListSafe(std::vector<am_Source_s> listSource);
206 void updateSinkListSafe(std::vector<am_Sink_s> listSinks);
210 * Extra thread that handles dbus stimulation for interrupt tests
211 * This is a very very very basic implementation of the dbus interface
212 * there is not failure handling, nothing.
213 * it is used just for testing, not intended to be used otherwise...
216 std::vector<am_Domain_s> createDomainTable();
217 std::vector<am_Sink_s> createSinkTable();
218 std::vector<am_Source_s> createSourceTable();
219 std::vector<am_Gateway_s> createGatewayTable();
220 IAmRoutingReceiverShadow* mShadow;
221 IAmRoutingReceive* mReceiveInterface;
222 CAmSocketHandler *mSocketHandler;
223 std::vector<am_Domain_s> mDomains;
224 std::vector<am_Sink_s> mSinks;
225 std::vector<am_Source_s> mSources;
226 std::vector<am_Gateway_s> mGateways;
227 std::map<uint16_t, int16_t> mMapHandleWorker;
228 std::map<am_connectionID_t, am_RoutingElement_s> mMapConnectionIDRoute;
229 CAmWorkerThreadPool mPool;
230 pthread_t mInterruptThread;
231 static pthread_mutex_t mMapConnectionMutex;
232 static pthread_mutex_t mMapHandleWorkerMutex;
233 static pthread_mutex_t mSinksMutex;
234 static pthread_mutex_t mSourcesMutex;
235 static pthread_mutex_t mDomainsMutex;
239 * worker to for connection
241 class asycConnectWorker: public CAmWorker
244 asycConnectWorker(CAmRoutingSenderAsync * asyncSender, CAmWorkerThreadPool* pool, IAmRoutingReceiverShadow* shadow, const am_Handle_s handle, const am_connectionID_t connectionID, const am_sourceID_t sourceID, const am_sinkID_t sinkID, const am_ConnectionFormat_e connectionFormat);
248 CAmRoutingSenderAsync * mAsyncSender;
249 IAmRoutingReceiverShadow *mShadow;
251 am_connectionID_t mConnectionID;
252 am_sourceID_t mSourceID;
254 am_ConnectionFormat_e mConnectionFormat;
258 * worker for disconnecting
260 class asycDisConnectWorker: public CAmWorker
263 asycDisConnectWorker(CAmRoutingSenderAsync * asyncSender, CAmWorkerThreadPool* pool, IAmRoutingReceiverShadow* shadow, const am_Handle_s handle, const am_connectionID_t connectionID);
267 CAmRoutingSenderAsync * mAsyncSender;
268 IAmRoutingReceiverShadow *mShadow;
270 am_connectionID_t mConnectionID;
271 am_ConnectionFormat_e mConnectionFormat;
275 * worker to for connection
278 #include <semaphore.h>
279 #include <sys/signalfd.h>
282 class asyncSetSinkVolumeWorker: public CAmWorker
285 asyncSetSinkVolumeWorker(CAmRoutingSenderAsync * asyncSender, CAmWorkerThreadPool* pool, IAmRoutingReceiverShadow* shadow, const am_volume_t currentVolume, const am_Handle_s handle, const am_sinkID_t sinkID, const am_volume_t volume, const am_RampType_e ramp, const am_time_t time);
289 CAmRoutingSenderAsync * mAsyncSender;
290 IAmRoutingReceiverShadow *mShadow;
291 am_volume_t mCurrentVolume;
299 class asyncSetSourceVolumeWorker: public CAmWorker
302 asyncSetSourceVolumeWorker(CAmRoutingSenderAsync * asyncSender, CAmWorkerThreadPool* pool, IAmRoutingReceiverShadow* shadow, const am_volume_t currentVolume, const am_Handle_s handle, const am_sourceID_t SourceID, const am_volume_t volume, const am_RampType_e ramp, const am_time_t time);
306 CAmRoutingSenderAsync * mAsyncSender;
307 IAmRoutingReceiverShadow *mShadow;
308 am_volume_t mCurrentVolume;
310 am_sourceID_t mSourceID;
316 class asyncSetSourceStateWorker: public CAmWorker
319 asyncSetSourceStateWorker(CAmRoutingSenderAsync * asyncSender, CAmWorkerThreadPool* pool, IAmRoutingReceiverShadow* shadow, const am_Handle_s handle, const am_sourceID_t sourceID, const am_SourceState_e state);
323 CAmRoutingSenderAsync * mAsyncSender;
324 IAmRoutingReceiverShadow *mShadow;
326 am_sourceID_t mSourceID;
327 am_SourceState_e mSourcestate;
330 class asyncSetSinkSoundPropertyWorker: public CAmWorker
333 asyncSetSinkSoundPropertyWorker(CAmRoutingSenderAsync * asyncSender, CAmWorkerThreadPool* pool, IAmRoutingReceiverShadow* shadow, const am_Handle_s handle, const am_SoundProperty_s soundProperty, const am_sinkID_t sinkID);
337 CAmRoutingSenderAsync * mAsyncSender;
338 IAmRoutingReceiverShadow *mShadow;
341 am_SoundProperty_s mSoundProperty;
344 class asyncSetSourceSoundPropertyWorker: public CAmWorker
347 asyncSetSourceSoundPropertyWorker(CAmRoutingSenderAsync * asyncSender, CAmWorkerThreadPool* pool, IAmRoutingReceiverShadow* shadow, const am_Handle_s handle, const am_SoundProperty_s soundProperty, const am_sourceID_t sourceID);
351 CAmRoutingSenderAsync * mAsyncSender;
352 IAmRoutingReceiverShadow *mShadow;
354 am_sourceID_t mSourceID;
355 am_SoundProperty_s mSoundProperty;
358 class asyncDomainStateChangeWorker: public CAmWorker
361 asyncDomainStateChangeWorker(CAmRoutingSenderAsync * asyncSender, CAmWorkerThreadPool* pool, IAmRoutingReceiverShadow* shadow, const am_domainID_t domainID, const am_DomainState_e domainState);
365 CAmRoutingSenderAsync * mAsyncSender;
366 IAmRoutingReceiverShadow *mShadow;
367 am_domainID_t mDomainID;
368 am_DomainState_e mDomainState;
371 class syncRegisterWorker: public CAmWorker
374 syncRegisterWorker(CAmRoutingSenderAsync * asyncSender, CAmWorkerThreadPool* pool, IAmRoutingReceiverShadow* shadow, const std::vector<am_Domain_s> domains, const std::vector<am_Sink_s> sinks, const std::vector<am_Source_s> sources, const uint16_t handle);
378 CAmRoutingSenderAsync * mAsyncSender;
379 IAmRoutingReceiverShadow *mShadow;
380 std::vector<am_Domain_s> mListDomains;
381 std::vector<am_Sink_s> mListSinks;
382 std::vector<am_Source_s> mListSources;
388 #endif /* ROUTINGSENDER_H_ */