Doug Anson / mbedConnectorInterfaceWithDM

Fork of mbedConnectorInterfaceV3 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         this->logger()->log("ThreadedResourceObserver being used for %s (sleep_time: %d ms)",resource->getFullName().c_str(),sleep_time);
00035         
00036         // start the thread by invoking the thread task...
00037         this->m_observation_thread.start(callback(this,&ThreadedResourceObserver::observation_task));
00038  }
00039  
00040  // destructor
00041  ThreadedResourceObserver::~ThreadedResourceObserver() {
00042      this->stopObservation();
00043      this->m_observation_thread.terminate();
00044  }
00045  
00046  // observation task method
00047  void ThreadedResourceObserver::observation_task() {
00048      while(true) {
00049          Thread::wait(this->getSleepTime());
00050          if (this->isObserving() == true && this->getResource() != NULL) {
00051              DynamicResource *res = this->getResource();
00052              if (res != NULL && res->isRegistered() == true) {
00053                  res->observe();
00054              }
00055          }
00056      }
00057  }
00058 
00059  // begin observing...
00060  void ThreadedResourceObserver::beginObservation() {
00061      this->setObserving(true);
00062  }
00063  
00064  // stop observing...
00065  void ThreadedResourceObserver::stopObservation() {
00066      this->setObserving(false);
00067  }
00068  
00069  // halt the underlying observer mechanism
00070  void ThreadedResourceObserver::halt() {
00071      this->m_observation_thread.terminate();
00072  }
00073  
00074  #endif // CONNECTOR_USING_THREADS