mbed Connector Interface simplification API on top of mbed-client
Fork of mbedConnectorInterfaceV3 by
NOTE:
This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!
source/ThreadedResourceObserver.cpp@93:9ec5d0fa62dd, 2017-03-10 (annotated)
- Committer:
- ansond
- Date:
- Fri Mar 10 17:50:52 2017 +0000
- Revision:
- 93:9ec5d0fa62dd
- Parent:
- 70:d49128903181
- Child:
- 122:4072e03884e4
updates for mbedOS 5.4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:1f1f55e73248 | 1 | /** |
ansond | 0:1f1f55e73248 | 2 | * @file ThreadedResourceObserver.cpp |
ansond | 0:1f1f55e73248 | 3 | * @brief mbed CoAP DynamicResource Thread-based observer (implementation) |
ansond | 0:1f1f55e73248 | 4 | * @author Doug Anson/Chris Paola |
ansond | 0:1f1f55e73248 | 5 | * @version 1.0 |
ansond | 0:1f1f55e73248 | 6 | * @see |
ansond | 0:1f1f55e73248 | 7 | * |
ansond | 0:1f1f55e73248 | 8 | * Copyright (c) 2014 |
ansond | 0:1f1f55e73248 | 9 | * |
ansond | 0:1f1f55e73248 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 0:1f1f55e73248 | 11 | * you may not use this file except in compliance with the License. |
ansond | 0:1f1f55e73248 | 12 | * You may obtain a copy of the License at |
ansond | 0:1f1f55e73248 | 13 | * |
ansond | 0:1f1f55e73248 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 0:1f1f55e73248 | 15 | * |
ansond | 0:1f1f55e73248 | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 0:1f1f55e73248 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 0:1f1f55e73248 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 0:1f1f55e73248 | 19 | * See the License for the specific language governing permissions and |
ansond | 0:1f1f55e73248 | 20 | * limitations under the License. |
ansond | 0:1f1f55e73248 | 21 | */ |
ansond | 0:1f1f55e73248 | 22 | |
ansond | 31:2507e64fcc42 | 23 | // Class support |
ansond | 31:2507e64fcc42 | 24 | #include "mbed-connector-interface/ThreadedResourceObserver.h" |
ansond | 0:1f1f55e73248 | 25 | |
ansond | 34:a10d65907549 | 26 | #ifdef CONNECTOR_USING_THREADS |
ansond | 34:a10d65907549 | 27 | |
ansond | 0:1f1f55e73248 | 28 | // constructor |
ansond | 66:764ec3dd205a | 29 | ThreadedResourceObserver::ThreadedResourceObserver(DynamicResource *resource,int sleep_time) : ResourceObserver(resource,sleep_time), m_observation_thread() { |
ansond | 66:764ec3dd205a | 30 | // default is not observing... |
ansond | 66:764ec3dd205a | 31 | this->setObserving(false); |
ansond | 66:764ec3dd205a | 32 | |
ansond | 66:764ec3dd205a | 33 | // DEBUG |
ansond | 66:764ec3dd205a | 34 | if (sleep_time > 0) { |
ansond | 66:764ec3dd205a | 35 | this->logger()->log("ThreadedResourceObserver being used for %s (sleep_time: %d ms)",resource->getFullName().c_str(),sleep_time); |
ansond | 66:764ec3dd205a | 36 | } |
ansond | 66:764ec3dd205a | 37 | else { |
ansond | 66:764ec3dd205a | 38 | this->logger()->log("ThreadedResourceObserver being used for %s",resource->getFullName().c_str()); |
ansond | 66:764ec3dd205a | 39 | } |
ansond | 66:764ec3dd205a | 40 | |
ansond | 66:764ec3dd205a | 41 | // start the thread by invoking the thread task... |
ansond | 93:9ec5d0fa62dd | 42 | this->m_observation_thread.start(callback(this,&ThreadedResourceObserver::observation_task)); |
ansond | 0:1f1f55e73248 | 43 | } |
ansond | 0:1f1f55e73248 | 44 | |
ansond | 0:1f1f55e73248 | 45 | // destructor |
ansond | 0:1f1f55e73248 | 46 | ThreadedResourceObserver::~ThreadedResourceObserver() { |
ansond | 0:1f1f55e73248 | 47 | this->stopObservation(); |
ansond | 0:1f1f55e73248 | 48 | this->m_observation_thread.terminate(); |
ansond | 0:1f1f55e73248 | 49 | } |
ansond | 0:1f1f55e73248 | 50 | |
ansond | 66:764ec3dd205a | 51 | // thread task method |
ansond | 69:baa167cf771b | 52 | void ThreadedResourceObserver::observation_task() { |
ansond | 69:baa167cf771b | 53 | while(true) { |
ansond | 70:d49128903181 | 54 | Thread::wait(this->getSleepTime()); |
ansond | 70:d49128903181 | 55 | if (this->isObserving() == true && this->getResource() != NULL) { |
ansond | 70:d49128903181 | 56 | DynamicResource *res = this->getResource(); |
ansond | 70:d49128903181 | 57 | if (res != NULL && res->isRegistered() == true) { |
ansond | 69:baa167cf771b | 58 | res->observe(); |
ansond | 67:0c130dc6b489 | 59 | } |
ansond | 0:1f1f55e73248 | 60 | } |
ansond | 0:1f1f55e73248 | 61 | } |
ansond | 0:1f1f55e73248 | 62 | } |
ansond | 0:1f1f55e73248 | 63 | |
ansond | 0:1f1f55e73248 | 64 | // begin observing... |
ansond | 0:1f1f55e73248 | 65 | void ThreadedResourceObserver::beginObservation() { |
ansond | 0:1f1f55e73248 | 66 | this->setObserving(true); |
ansond | 0:1f1f55e73248 | 67 | } |
ansond | 0:1f1f55e73248 | 68 | |
ansond | 0:1f1f55e73248 | 69 | // stop observing... |
ansond | 0:1f1f55e73248 | 70 | void ThreadedResourceObserver::stopObservation() { |
ansond | 0:1f1f55e73248 | 71 | this->setObserving(false); |
ansond | 34:a10d65907549 | 72 | } |
ansond | 34:a10d65907549 | 73 | |
ansond | 54:dfee8691c83a | 74 | // halt the underlying observer mechanism |
ansond | 54:dfee8691c83a | 75 | void ThreadedResourceObserver::halt() { |
ansond | 54:dfee8691c83a | 76 | m_observation_thread.terminate(); |
ansond | 54:dfee8691c83a | 77 | } |
ansond | 54:dfee8691c83a | 78 | |
ansond | 34:a10d65907549 | 79 | #endif // CONNECTOR_USING_THREADS |