Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: IoT_LED_demo ServoTest uWater_Project hackathon ... more
ThreadedResourceObserver.cpp
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 #ifdef CONNECTOR_USING_THREADS 00026 // DEBUG 00027 //DigitalOut __threaded_led(LED2); 00028 #endif 00029 00030 // constructor 00031 ThreadedResourceObserver::ThreadedResourceObserver(DynamicResource *resource,int sleep_time) : 00032 ResourceObserver(resource,sleep_time) 00033 #ifdef CONNECTOR_USING_THREADS 00034 ,m_observation_thread(&ThreadedResourceObserver::_observation_notifier,this) 00035 #endif 00036 { 00037 this->setObserving(false); 00038 // DEBUG 00039 std::printf("ThreadedResourceObserver being used for %s (sleep_time=%d)\r\n",resource->getName().c_str(),sleep_time); 00040 } 00041 00042 // destructor 00043 ThreadedResourceObserver::~ThreadedResourceObserver() { 00044 this->stopObservation(); 00045 #ifdef CONNECTOR_USING_THREADS 00046 this->m_observation_thread.terminate(); 00047 #endif 00048 } 00049 00050 // notifier 00051 void ThreadedResourceObserver::_observation_notifier(void const *instance) { 00052 #ifdef CONNECTOR_USING_THREADS 00053 ThreadedResourceObserver *me = (ThreadedResourceObserver *)instance; 00054 while(true) { 00055 Thread::wait(me->getSleepTime()); 00056 if (me->isObserving() == true && me->getResource() != NULL && nsdl_endpoint_is_registered() == true) { 00057 // 00058 // implementsObservation(): This switch denotes whether this threaded resource generates its own observations or relies on the heartbeat (time-based) mechanism. 00059 // -- if resource itself doesn't implement it, then we can call notify() with the get() on each heartbeat of the threaded resource (see note below) 00060 // -- otherwise, we let the resource itself call observe() as part of its observation implementation 00061 // 00062 // note: this is independent of whether the resource, when declared, is defined as an observable resource or not. If not, then notify() wont be called 00063 // as there will be no observations for this resource regardless of this switch value. 00064 // 00065 bool do_notify = !(me->getResource()->implementsObservation()); 00066 me->getResource()->observe(do_notify); 00067 //__threaded_led = !__threaded_led; 00068 } 00069 } 00070 #endif 00071 } 00072 00073 // begin observing... 00074 void ThreadedResourceObserver::beginObservation() { 00075 this->setObserving(true); 00076 } 00077 00078 // stop observing... 00079 void ThreadedResourceObserver::stopObservation() { 00080 this->setObserving(false); 00081 }
Generated on Tue Jul 12 2022 15:15:35 by
1.7.2