summaryrefslogtreecommitdiff
path: root/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp')
-rw-r--r--AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp257
1 files changed, 83 insertions, 174 deletions
diff --git a/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp b/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp
index 4a6accf..2aa8370 100644
--- a/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp
+++ b/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp
@@ -36,47 +36,21 @@ namespace am
static CAmCommonAPIWrapper* pSingleCommonAPIInstance = NULL;
-bool timeoutToTimespec(const int64_t & localTimeout, timespec & pollTimeout)
-{
- if(CommonAPI::TIMEOUT_INFINITE == localTimeout)//dispatch never
- {
- return false;
- }
- else
- {
- if(CommonAPI::TIMEOUT_NONE==localTimeout)//dispatch immediately
- {
- pollTimeout.tv_sec = 0;
- pollTimeout.tv_nsec = 5000000;//5 ms
- }
- else
- {
- pollTimeout.tv_sec = localTimeout / 1000;
- pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000;
- }
- return true;
- }
-}
-
-
CAmCommonAPIWrapper::CAmCommonAPIWrapper(CAmSocketHandler* socketHandler, const std::string & applicationName):
pCommonPrepareCallback(this,&CAmCommonAPIWrapper::commonPrepareCallback), //
+ pCommonDispatchCallback(this, &CAmCommonAPIWrapper::commonDispatchCallback), //
pCommonFireCallback(this, &CAmCommonAPIWrapper::commonFireCallback), //
pCommonCheckCallback(this, &CAmCommonAPIWrapper::commonCheckCallback), //
- pCommonDispatchCallback(this, &CAmCommonAPIWrapper::commonDispatchCallback), //
pCommonTimerCallback(this, &CAmCommonAPIWrapper::commonTimerCallback), //
- mpSocketHandler(socketHandler),
- mRegisteredDispatchSources(),
- mMapWatches(),
- mSourcesToDispatch(),
- mListTimerhandles()
+ mpSocketHandler(socketHandler), //
+ mWatchToCheck(NULL)
{
assert(NULL!=socketHandler);
//Get the runtime
mRuntime = CommonAPI::Runtime::get();
assert(NULL!=mRuntime);
-//Create the context
+ //Create the context
if(applicationName.size())
mContext = std::make_shared<CommonAPI::MainLoopContext>(applicationName);
else
@@ -101,11 +75,9 @@ CAmCommonAPIWrapper::~CAmCommonAPIWrapper()
mContext->unsubscribeForDispatchSources(mDispatchSourceListenerSubscription);
mContext->unsubscribeForWatches(mWatchListenerSubscription);
mContext->unsubscribeForTimeouts(mTimeoutSourceListenerSubscription);
- deregisterAllDispatchSource();
- deregisterAllTimeouts();
- deregisterAllWatches();
mContext.reset();
mpSocketHandler = NULL;
+ mWatchToCheck = NULL;
}
CAmCommonAPIWrapper* CAmCommonAPIWrapper::instantiateOnce(CAmSocketHandler* socketHandler, const std::string & applicationName)
@@ -143,214 +115,151 @@ CAmCommonAPIWrapper* CAmCommonAPIWrapper::getInstance()
return pSingleCommonAPIInstance;
}
-void CAmCommonAPIWrapper::commonPrepareCallback(const sh_pollHandle_t, void*)
-{
- for (auto dispatchSourceIterator = mRegisteredDispatchSources.begin();
- dispatchSourceIterator != mRegisteredDispatchSources.end();
- dispatchSourceIterator++)
- {
- int64_t dispatchTimeout(CommonAPI::TIMEOUT_INFINITE);
- if((*dispatchSourceIterator)->prepare(dispatchTimeout))
- {
- while ((*dispatchSourceIterator)->dispatch());
- }
- }
-}
-
-void CAmCommonAPIWrapper::commonFireCallback(const pollfd pollfd, const sh_pollHandle_t handle, void *)
+bool CAmCommonAPIWrapper::commonDispatchCallback(const sh_pollHandle_t handle, void *userData)
{
- CommonAPI::Watch* pWatchToCheck = watchWithHandle(handle);
- if( pWatchToCheck )
- pWatchToCheck->dispatch(pollfd.revents);
-}
+ (void) handle;
+ (void) userData;
-bool CAmCommonAPIWrapper::commonCheckCallback(const sh_pollHandle_t handle, void *)
-{
- CommonAPI::Watch* pWatchToCheck = watchWithHandle(handle);
- if( pWatchToCheck )
+ std::list<CommonAPI::DispatchSource*>::iterator iterator(mSourcesToDispatch.begin());
+ for(;iterator!=mSourcesToDispatch.end();)
{
- const ArrayDispatchSources & vecDispatch = pWatchToCheck->getDependentDispatchSources();
- if(vecDispatch.size()>0)
- {
- mSourcesToDispatch[handle].insert(mSourcesToDispatch[handle].end(), vecDispatch.begin(), vecDispatch.end());
- return true;
+ CommonAPI::DispatchSource* source = *iterator;
+ if (!source->dispatch()) {
+ iterator=mSourcesToDispatch.erase(iterator);
}
+ else
+ iterator++;
}
+ if (!mSourcesToDispatch.empty())
+ return (true);
+
return false;
}
-bool CAmCommonAPIWrapper::commonDispatchCallback(const sh_pollHandle_t handle, void *)
+bool CAmCommonAPIWrapper::commonCheckCallback(const sh_pollHandle_t, void *)
{
- CommonAPI::Watch* pWatchToCheck = watchWithHandle(handle);
- if( pWatchToCheck )
- {
- std::list<CommonAPI::DispatchSource*> & srcList = mSourcesToDispatch[handle];
- for(auto it = srcList.begin();it!=srcList.end();)
- {
- if (false==(*it)->dispatch())
- it=srcList.erase(it);
- else
- it++;
- }
- if (!srcList.empty())
- return (true);
- }
- mSourcesToDispatch.erase(handle);
- return false;
+ std::vector<CommonAPI::DispatchSource*> vecDispatch=mWatchToCheck->getDependentDispatchSources();
+ mSourcesToDispatch.insert(mSourcesToDispatch.end(), vecDispatch.begin(), vecDispatch.end());
+
+ return (mWatchToCheck || !mSourcesToDispatch.empty());
}
-void CAmCommonAPIWrapper::commonTimerCallback(sh_timerHandle_t handle, void *)
+void CAmCommonAPIWrapper::commonFireCallback(const pollfd pollfd, const sh_pollHandle_t, void *)
{
- CommonAPI::Timeout* pTimeout = timeoutWithHandle(handle);
-
- if( NULL==pTimeout )
+ mWatchToCheck=NULL;
+ try
{
- //erroneous call because deregisterTimeout has been called, so try to remove the timer from the sockethandler
- mpSocketHandler->removeTimer(handle);
+ mWatchToCheck=mMapWatches.at(pollfd.fd);
}
- else
- {
- if ( false==pTimeout->dispatch() ) //it should be removed
- {
- mpSocketHandler->removeTimer(handle);
- mListTimerhandles.erase(handle);
- }
- #ifndef WITH_TIMERFD
- else //the timeout should be rescheduled
- mpSocketHandler->restartTimer(handle);
- #endif
+ catch (const std::out_of_range& error) {
+ logInfo(__PRETTY_FUNCTION__,error.what());
+ return;
}
-}
-void CAmCommonAPIWrapper::registerDispatchSource(CommonAPI::DispatchSource* dispatchSource, const CommonAPI::DispatchPriority)
-{
- mRegisteredDispatchSources.push_back(dispatchSource);
+ mWatchToCheck->dispatch(pollfd.events);
}
-void CAmCommonAPIWrapper::deregisterDispatchSource(CommonAPI::DispatchSource* dispatchSource)
+void CAmCommonAPIWrapper::commonPrepareCallback(const sh_pollHandle_t, void*)
{
- for(IteratorArrayDispatchSources dispatchSourceIterator = mRegisteredDispatchSources.begin(); dispatchSourceIterator != mRegisteredDispatchSources.end(); dispatchSourceIterator++)
+ for (auto dispatchSourceIterator = mRegisteredDispatchSources.begin();
+ dispatchSourceIterator != mRegisteredDispatchSources.end();
+ dispatchSourceIterator++)
{
- if( *dispatchSourceIterator == dispatchSource )
+ int64_t dispatchTimeout(CommonAPI::TIMEOUT_INFINITE);
+ if(dispatchSourceIterator->second->prepare(dispatchTimeout))
{
- mRegisteredDispatchSources.erase(dispatchSourceIterator);
- break;
+ while (dispatchSourceIterator->second->dispatch());
}
}
}
-void CAmCommonAPIWrapper::deregisterAllDispatchSource()
+void CAmCommonAPIWrapper::registerDispatchSource(CommonAPI::DispatchSource* dispatchSource, const CommonAPI::DispatchPriority dispatchPriority)
{
- mRegisteredDispatchSources.clear();
+ mRegisteredDispatchSources.insert({dispatchPriority, dispatchSource});
}
-void CAmCommonAPIWrapper::registerWatch(CommonAPI::Watch* watch, const CommonAPI::DispatchPriority)
+void CAmCommonAPIWrapper::deregisterDispatchSource(CommonAPI::DispatchSource* dispatchSource)
{
- logInfo(__PRETTY_FUNCTION__);
- pollfd pollfd_ (watch->getAssociatedFileDescriptor());
- sh_pollHandle_t handle (0);
-
- am_Error_e error = mpSocketHandler->addFDPoll(pollfd_.fd, pollfd_.events, &pCommonPrepareCallback, &pCommonFireCallback, &pCommonCheckCallback, &pCommonDispatchCallback, watch, handle);
+ for(auto dispatchSourceIterator = mRegisteredDispatchSources.begin();
+ dispatchSourceIterator != mRegisteredDispatchSources.end();
+ dispatchSourceIterator++) {
- //if everything is alright, add the watch and the handle to our map so we know this relationship
- if (error != am_Error_e::E_OK || handle == 0)
- {
- logError(__func__,"entering watch failed");
+ if(dispatchSourceIterator->second == dispatchSource) {
+ mRegisteredDispatchSources.erase(dispatchSourceIterator);
+ break;
+ }
}
- else
- mMapWatches.insert(std::make_pair(handle,watch));
}
void CAmCommonAPIWrapper::deregisterWatch(CommonAPI::Watch* watch)
{
- for(IteratorMapWatches iter=mMapWatches.begin();iter!=mMapWatches.end();iter++)
+ for(std::map<int,CommonAPI::Watch*>::iterator iter(mMapWatches.begin());iter!=mMapWatches.end();iter++)
{
if (iter->second == watch)
{
- mpSocketHandler->removeFDPoll(iter->first);
mMapWatches.erase(iter);
break;
}
}
}
-void CAmCommonAPIWrapper::deregisterAllWatches()
-{
- for(IteratorMapWatches iter=mMapWatches.begin();iter!=mMapWatches.end();iter++)
- mpSocketHandler->removeFDPoll(iter->first);
- mMapWatches.clear();
-}
-
void CAmCommonAPIWrapper::registerTimeout(CommonAPI::Timeout* timeout, const CommonAPI::DispatchPriority)
{
timespec pollTimeout;
- if(timeoutToTimespec(timeout->getTimeoutInterval(), pollTimeout))
- {
- //prepare handle and callback. new is eval, but there is no other choice because we need the pointer!
- sh_timerHandle_t handle;
+ int64_t localTimeout = timeout->getTimeoutInterval();
- //add the timer to the pollLoop
- am_Error_e error = mpSocketHandler->addTimer(pollTimeout, &pCommonTimerCallback, handle, timeout, true);
- if (error != am_Error_e::E_OK || handle == 0)
- {
- logError(__func__,"adding timer failed");
- }
- else
- {
- mListTimerhandles.insert(std::make_pair(handle,timeout));
- }
- }
+ pollTimeout.tv_sec = localTimeout / 1000;
+ pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000;
+
+ //prepare handle and callback. new is eval, but there is no other choice because we need the pointer!
+ sh_timerHandle_t handle;
+
+ //add the timer to the pollLoop
+ mpSocketHandler->addTimer(pollTimeout, &pCommonTimerCallback, handle, timeout);
+
+ timerHandles myHandle({handle,timeout});
+ mpListTimerhandles.push_back(myHandle);
+
+ return;
}
void CAmCommonAPIWrapper::deregisterTimeout(CommonAPI::Timeout* timeout)
{
- for( IteratorMapTimeouts iter=mListTimerhandles.begin();iter!= mListTimerhandles.end();iter++)
+ for( std::vector<timerHandles>::iterator iter(mpListTimerhandles.begin());iter!=mpListTimerhandles.end();iter++)
{
- if(iter->second==timeout)
+ if(iter->timeout==timeout)
{
- mpSocketHandler->removeTimer(iter->first);
- mListTimerhandles.erase(iter->first);
- break;
+ mpSocketHandler->removeTimer(iter->handle);
}
}
}
-void CAmCommonAPIWrapper::deregisterAllTimeouts()
+void CAmCommonAPIWrapper::registerWatch(CommonAPI::Watch* watch, const CommonAPI::DispatchPriority)
{
- for( IteratorMapTimeouts iter=mListTimerhandles.begin();iter!= mListTimerhandles.end();iter++)
- mpSocketHandler->removeTimer(iter->first);
- mListTimerhandles.clear();
-}
+ logInfo(__PRETTY_FUNCTION__);
+ pollfd pollfd_ (watch->getAssociatedFileDescriptor());
+ sh_pollHandle_t handle (0);
-CommonAPI::Watch* CAmCommonAPIWrapper::watchWithHandle(const sh_pollHandle_t handle)
-{
- CommonAPI::Watch* pWatchToCheck = NULL;
- try
- {
- pWatchToCheck = mMapWatches.at(handle);
- }
- catch (const std::out_of_range& error)
- {
- logInfo(__PRETTY_FUNCTION__,error.what());
- }
- return pWatchToCheck;
+ am_Error_e error = mpSocketHandler->addFDPoll(pollfd_.fd, pollfd_.events, &pCommonPrepareCallback, &pCommonFireCallback, &pCommonCheckCallback, &pCommonDispatchCallback, watch, handle);
+
+ //if everything is alright, add the watch and the handle to our map so we know this relationship
+ if (error == !am_Error_e::E_OK || handle == 0)
+ logError(__func__,"entering watch failed");
+
+ mMapWatches.insert(std::make_pair(pollfd_.fd,watch));
}
-CommonAPI::Timeout* CAmCommonAPIWrapper::timeoutWithHandle(const sh_pollHandle_t handle)
+void CAmCommonAPIWrapper::commonTimerCallback(sh_timerHandle_t handle, void *)
{
- CommonAPI::Timeout* pTimeout = NULL;
- try
+ for( std::vector<timerHandles>::iterator iter(mpListTimerhandles.begin());iter!=mpListTimerhandles.end();iter++)
{
- pTimeout = mListTimerhandles.at(handle);
+ if(iter->handle==handle)
+ {
+ iter->timeout->dispatch();
+ }
}
- catch (const std::out_of_range& error)
- {
- logInfo(__PRETTY_FUNCTION__,error.what());
- }
- return pTimeout;
}
-
CAmCommonAPIWrapper* (*getCAPI)() = CAmCommonAPIWrapper::getInstance;
}