Maggie Mei / mbedConnectorInterfaceWithDM

Fork of mbedConnectorInterfaceWithDM by Doug Anson

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  // Class support
00024  #include "mbed-connector-interface/ThreadedResourceObserver.h"
00025  
00026  #ifdef CONNECTOR_USING_THREADS
00027  
00028  // constructor
00029  ThreadedResourceObserver::ThreadedResourceObserver(DynamicResource *resource,int sleep_time) : ResourceObserver(resource,sleep_time), m_observation_thread() {
00030         // default is not observing...
00031         this->setObserving(false);
00032         
00033         // DEBUG
00034         if (sleep_time > 0) {
00035             this->logger()->log("ThreadedResourceObserver being used for %s (sleep_time: %d ms)",resource->getFullName().c_str(),sleep_time);
00036         }
00037         else {
00038             this->logger()->log("ThreadedResourceObserver being used for %s",resource->getFullName().c_str());
00039         }
00040         
00041         // start the thread by invoking the thread task...
00042         this->m_observation_thread.start(this,&ThreadedResourceObserver::observation_task);
00043  }
00044  
00045  // destructor
00046  ThreadedResourceObserver::~ThreadedResourceObserver() {
00047      this->stopObservation();
00048      this->m_observation_thread.terminate();
00049  }
00050  
00051  // thread task method
00052  void ThreadedResourceObserver::observation_task() {
00053      while(true) {
00054          Thread::wait(this->getSleepTime());
00055          if (this->isObserving() == true && this->getResource() != NULL) {
00056              DynamicResource *res = this->getResource();
00057              if (res != NULL && res->isRegistered() == true) {
00058                  res->observe();
00059              }
00060          }
00061      }
00062  }
00063 
00064  // begin observing...
00065  void ThreadedResourceObserver::beginObservation() {
00066      this->setObserving(true);
00067  }
00068  
00069  // stop observing...
00070  void ThreadedResourceObserver::stopObservation() {
00071      this->setObserving(false);
00072  }
00073  
00074  // halt the underlying observer mechanism
00075  void ThreadedResourceObserver::halt() {
00076      m_observation_thread.terminate();
00077  }
00078  
00079  #endif // CONNECTOR_USING_THREADS