custom for >5 resources
Fork of mbedConnectorInterface by
Diff: api/ThreadedResourceObserver.cpp
- Revision:
- 31:bacc63106754
- Parent:
- 30:113c2a1d8db2
- Child:
- 36:1c6c45584c13
diff -r 113c2a1d8db2 -r bacc63106754 api/ThreadedResourceObserver.cpp --- a/api/ThreadedResourceObserver.cpp Tue Apr 07 21:30:03 2015 +0000 +++ b/api/ThreadedResourceObserver.cpp Wed Apr 08 15:49:56 2015 +0000 @@ -23,30 +23,41 @@ #include "ThreadedResourceObserver.h" // constructor - ThreadedResourceObserver::ThreadedResourceObserver(DynamicResource *resource,int sleep_time_ms) : - ResourceObserver(resource), - m_begin_observation(false), - m_observation_thread(&ThreadedResourceObserver::_observation_notifier,this), - m_sleep_time_ms(sleep_time_ms) { + ThreadedResourceObserver::ThreadedResourceObserver(DynamicResource *resource,int sleep_time) : + ResourceObserver(resource,sleep_time) + #ifdef CONNECTOR_USING_THREADS + ,m_observation_thread(&ThreadedResourceObserver::_observation_notifier,this) + #endif + { } // destructor ThreadedResourceObserver::~ThreadedResourceObserver() { + this->stopObservation(); + #ifdef CONNECTOR_USING_THREADS this->m_observation_thread.terminate(); + #endif } // notifier - void ThreadedResourceObserver::_observation_notifier(void const *args) { - ThreadedResourceObserver *me = (ThreadedResourceObserver *)args; + void ThreadedResourceObserver::_observation_notifier(void const *instance) { + #ifdef CONNECTOR_USING_THREADS + ThreadedResourceObserver *me = (ThreadedResourceObserver *)instance; while(true) { - Thread::wait(me->m_sleep_time_ms); - if (me->m_begin_observation == true && me->m_resource != NULL) { - me->m_resource->observe(); + Thread::wait(me->getSleepTime()); + if (me->isObserving() == true && me->getResource() != NULL) { + me->getResource()->observe(); } } + #endif + } + + // begin observing... + void ThreadedResourceObserver::beginObservation() { + this->setObserving(true); } - // begin observing... - void ThreadedResourceObserver::beginObservation() { - this->m_begin_observation = true; + // stop observing... + void ThreadedResourceObserver::stopObservation() { + this->setObserving(false); } \ No newline at end of file