18 #include "audiomanagerconfig.h" 36 static CAmCommonAPIWrapper* pSingleCommonAPIInstance = NULL;
45 mpSocketHandler(socketHandler),
48 assert(NULL!=socketHandler);
50 mRuntime = CommonAPI::Runtime::get();
51 assert(NULL!=mRuntime);
54 if(applicationName.size())
55 mContext = std::make_shared<CommonAPI::MainLoopContext>(applicationName);
57 mContext = std::make_shared<CommonAPI::MainLoopContext>();
58 assert(NULL!=mContext);
59 logInfo(__func__,
"CommonAPI main loop context with name '", mContext->getName(),
"' has been created!");
62 mDispatchSourceListenerSubscription = mContext->subscribeForDispatchSources(
63 std::bind(&CAmCommonAPIWrapper::registerDispatchSource,
this, std::placeholders::_1, std::placeholders::_2),
64 std::bind(&CAmCommonAPIWrapper::deregisterDispatchSource,
this, std::placeholders::_1));
65 mWatchListenerSubscription = mContext->subscribeForWatches(
66 std::bind(&CAmCommonAPIWrapper::registerWatch,
this, std::placeholders::_1, std::placeholders::_2),
67 std::bind(&CAmCommonAPIWrapper::deregisterWatch,
this, std::placeholders::_1));
68 mTimeoutSourceListenerSubscription = mContext->subscribeForTimeouts(
69 std::bind(&CAmCommonAPIWrapper::registerTimeout,
this, std::placeholders::_1, std::placeholders::_2),
70 std::bind(&CAmCommonAPIWrapper::deregisterTimeout,
this, std::placeholders::_1));
75 mContext->unsubscribeForDispatchSources(mDispatchSourceListenerSubscription);
76 mContext->unsubscribeForWatches(mWatchListenerSubscription);
77 mContext->unsubscribeForTimeouts(mTimeoutSourceListenerSubscription);
79 mpSocketHandler = NULL;
85 if(NULL==pSingleCommonAPIInstance)
87 if(NULL==socketHandler)
88 throw std::runtime_error(std::string(
"Expected a valid socket handler. The socket handler pointer must not be NULL."));
93 throw std::logic_error(std::string(
"The singleton instance has been already instantiated. This method should be called only once."));
94 return pSingleCommonAPIInstance;
101 if (pSingleCommonAPIInstance!=NULL)
102 delete pSingleCommonAPIInstance;
104 pSingleCommonAPIInstance=NULL;
108 logError(__func__,
"error while deleting CAPIWrapper instance");
114 assert(NULL!=pSingleCommonAPIInstance);
115 return pSingleCommonAPIInstance;
118 bool CAmCommonAPIWrapper::commonDispatchCallback(
const sh_pollHandle_t handle,
void *userData)
123 std::list<CommonAPI::DispatchSource*>::iterator iterator(mSourcesToDispatch.begin());
124 for(;iterator!=mSourcesToDispatch.end();)
126 CommonAPI::DispatchSource* source = *iterator;
127 if (!source->dispatch()) {
128 iterator=mSourcesToDispatch.erase(iterator);
133 if (!mSourcesToDispatch.empty())
139 bool CAmCommonAPIWrapper::commonCheckCallback(
const sh_pollHandle_t,
void *)
141 std::vector<CommonAPI::DispatchSource*> vecDispatch=mWatchToCheck->getDependentDispatchSources();
142 mSourcesToDispatch.insert(mSourcesToDispatch.end(), vecDispatch.begin(), vecDispatch.end());
144 return (mWatchToCheck || !mSourcesToDispatch.empty());
147 void CAmCommonAPIWrapper::commonFireCallback(
const pollfd pollfd,
const sh_pollHandle_t,
void *)
152 mWatchToCheck=mMapWatches.at(pollfd.fd);
154 catch (
const std::out_of_range& error) {
155 logInfo(__PRETTY_FUNCTION__,error.what());
159 mWatchToCheck->dispatch(pollfd.events);
162 void CAmCommonAPIWrapper::commonPrepareCallback(
const sh_pollHandle_t,
void*)
164 for (
auto dispatchSourceIterator = mRegisteredDispatchSources.begin();
165 dispatchSourceIterator != mRegisteredDispatchSources.end();
166 dispatchSourceIterator++)
168 int64_t dispatchTimeout(CommonAPI::TIMEOUT_INFINITE);
169 if(dispatchSourceIterator->second->prepare(dispatchTimeout))
171 while (dispatchSourceIterator->second->dispatch());
176 void CAmCommonAPIWrapper::registerDispatchSource(CommonAPI::DispatchSource* dispatchSource,
const CommonAPI::DispatchPriority dispatchPriority)
178 mRegisteredDispatchSources.insert({dispatchPriority, dispatchSource});
181 void CAmCommonAPIWrapper::deregisterDispatchSource(CommonAPI::DispatchSource* dispatchSource)
183 for(
auto dispatchSourceIterator = mRegisteredDispatchSources.begin();
184 dispatchSourceIterator != mRegisteredDispatchSources.end();
185 dispatchSourceIterator++) {
187 if(dispatchSourceIterator->second == dispatchSource) {
188 mRegisteredDispatchSources.erase(dispatchSourceIterator);
194 void CAmCommonAPIWrapper::deregisterWatch(CommonAPI::Watch* watch)
196 for(std::map<int,CommonAPI::Watch*>::iterator iter(mMapWatches.begin());iter!=mMapWatches.end();iter++)
198 if (iter->second == watch)
200 mMapWatches.erase(iter);
206 void CAmCommonAPIWrapper::registerTimeout(CommonAPI::Timeout* timeout,
const CommonAPI::DispatchPriority)
208 timespec pollTimeout;
209 int64_t localTimeout = timeout->getTimeoutInterval();
211 pollTimeout.tv_sec = localTimeout / 1000;
212 pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000;
218 mpSocketHandler->
addTimer(pollTimeout, &pCommonTimerCallback, handle, timeout);
220 timerHandles myHandle({handle,timeout});
221 mpListTimerhandles.push_back(myHandle);
226 void CAmCommonAPIWrapper::deregisterTimeout(CommonAPI::Timeout* timeout)
228 for( std::vector<timerHandles>::iterator iter(mpListTimerhandles.begin());iter!=mpListTimerhandles.end();iter++)
230 if(iter->timeout==timeout)
237 void CAmCommonAPIWrapper::registerWatch(CommonAPI::Watch* watch,
const CommonAPI::DispatchPriority)
240 pollfd pollfd_ (watch->getAssociatedFileDescriptor());
243 am_Error_e error = mpSocketHandler->
addFDPoll(pollfd_.fd, pollfd_.events, &pCommonPrepareCallback, &pCommonFireCallback, &pCommonCheckCallback, &pCommonDispatchCallback, watch, handle);
247 logError(__func__,
"entering watch failed");
249 mMapWatches.insert(std::make_pair(pollfd_.fd,watch));
252 void CAmCommonAPIWrapper::commonTimerCallback(
sh_timerHandle_t handle,
void *)
254 for( std::vector<timerHandles>::iterator iter(mpListTimerhandles.begin());iter!=mpListTimerhandles.end();iter++)
256 if(iter->handle==handle)
258 iter->timeout->dispatch();
CAmCommonAPIWrapper(CAmSocketHandler *socketHandler, const std::string &applicationName="")
SPDX license identifier: MPL-2.0.
A Common-API wrapper class, which loads the common-api runtime and instantiates all necessary objects...
am_Error_e
the errors of the audiomanager.
void logInfo(T value, TArgs...args)
logs given values with infolevel with the default context
static CAmCommonAPIWrapper * instantiateOnce(CAmSocketHandler *socketHandler, const std::string &applicationName="")
Creates a singleton instance attached to the provided socket handler object.
The am::CAmSocketHandler implements a mainloop for the AudioManager.
uint16_t sh_pollHandle_t
this is a handle for a filedescriptor to be used with the SocketHandler
SPDX license identifier: MPL-2.0.
am_Error_e removeTimer(const sh_timerHandle_t handle)
removes a timer from the list of timers
static void deleteInstance()
Deletes the instanciated object.
virtual ~CAmCommonAPIWrapper()
static CAmCommonAPIWrapper * getInstance()
Returns an already instantiated object.
am_Error_e addFDPoll(const int fd, const short event, std::function< void(const sh_pollHandle_t handle, void *userData)> prepare, std::function< void(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)> fired, std::function< bool(const sh_pollHandle_t handle, void *userData)> check, std::function< bool(const sh_pollHandle_t handle, void *userData)> dispatch, void *userData, sh_pollHandle_t &handle)
Adds a filedescriptor to the polling loop.
Copyright (C) 2012 - 2014, BMW AG.
SPDX license identifier: MPL-2.0.
void logError(T value, TArgs...args)
logs given values with errorlevel with the default context
no error - positive reply
sh_pollHandle_t sh_timerHandle_t
this is a handle for a timer to be used with the SocketHandler
am_Error_e addTimer(const timespec &timeouts, IAmShTimerCallBack *callback, sh_timerHandle_t &handle, void *userData, const bool __attribute__((__unused__)) repeats=false)