Doug Anson / mbedConnectorInterfaceV3
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ThreadedResourceObserver.cpp Source File

ThreadedResourceObserver.cpp

Go to the documentation of this file.
00001 /**
00002  * @file    ThreadedResourceObserver.cpp
00003  * @brief   mbed CoAP DynamicResource Thread-based observer (implementation)
00004  * @author  Doug Anson/Chris Paola
00005  * @version 1.0
00006  * @see
00007  *
00008  * Copyright (c) 2014
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022  
00023  #include "ThreadedResourceObserver.h"
00024  
00025  // constructor
00026  ThreadedResourceObserver::ThreadedResourceObserver(DynamicResource *resource,int sleep_time) : 
00027                                                             ResourceObserver(resource,sleep_time) 
00028  #ifdef CONNECTOR_USING_THREADS
00029                                                             ,m_observation_thread(&ThreadedResourceObserver::_observation_notifier,this) 
00030  #endif 
00031                                                             {
00032         this->setObserving(false);
00033         // DEBUG
00034         std::printf("ThreadedResourceObserver being used for %s (sleep_time=%d)\r\n",resource->getFullName().c_str(),sleep_time);
00035  }
00036  
00037  // destructor
00038  ThreadedResourceObserver::~ThreadedResourceObserver() {
00039      this->stopObservation();
00040  #ifdef CONNECTOR_USING_THREADS
00041      this->m_observation_thread.terminate();
00042  #endif
00043  }
00044  
00045  // notifier
00046  void ThreadedResourceObserver::_observation_notifier(void const *instance) {
00047  #ifdef CONNECTOR_USING_THREADS
00048      ThreadedResourceObserver *me = (ThreadedResourceObserver *)instance;
00049      while(true) {
00050          Thread::wait(me->getSleepTime());
00051          if (me->isObserving() == true && me->getResource() != NULL && me->getResource()->isRegistered() == true) {
00052              me->getResource()->observe();
00053          }
00054      }
00055  #endif
00056  }
00057 
00058  // begin observing...
00059  void ThreadedResourceObserver::beginObservation() {
00060      this->setObserving(true);
00061  }
00062  
00063  // stop observing...
00064  void ThreadedResourceObserver::stopObservation() {
00065      this->setObserving(false);
00066  }