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/EventQueueResourceObserver.cpp@125:4bf229bf14a3, 2017-07-13 (annotated)
- Committer:
- ansond
- Date:
- Thu Jul 13 19:21:28 2017 +0000
- Revision:
- 125:4bf229bf14a3
- Child:
- 126:f37e34daa100
rebased R1.2B to include EventQueue additions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 125:4bf229bf14a3 | 1 | /** |
ansond | 125:4bf229bf14a3 | 2 | * @file EventQueueResourceObserver.cpp |
ansond | 125:4bf229bf14a3 | 3 | * @brief mbed CoAP DynamicResource Thread-based observer (implementation) |
ansond | 125:4bf229bf14a3 | 4 | * @author Doug Anson/Chris Paola |
ansond | 125:4bf229bf14a3 | 5 | * @version 1.0 |
ansond | 125:4bf229bf14a3 | 6 | * @see |
ansond | 125:4bf229bf14a3 | 7 | * |
ansond | 125:4bf229bf14a3 | 8 | * Copyright (c) 2017 |
ansond | 125:4bf229bf14a3 | 9 | * |
ansond | 125:4bf229bf14a3 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 125:4bf229bf14a3 | 11 | * you may not use this file except in compliance with the License. |
ansond | 125:4bf229bf14a3 | 12 | * You may obtain a copy of the License at |
ansond | 125:4bf229bf14a3 | 13 | * |
ansond | 125:4bf229bf14a3 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 125:4bf229bf14a3 | 15 | * |
ansond | 125:4bf229bf14a3 | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 125:4bf229bf14a3 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 125:4bf229bf14a3 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 125:4bf229bf14a3 | 19 | * See the License for the specific language governing permissions and |
ansond | 125:4bf229bf14a3 | 20 | * limitations under the License. |
ansond | 125:4bf229bf14a3 | 21 | */ |
ansond | 125:4bf229bf14a3 | 22 | |
ansond | 125:4bf229bf14a3 | 23 | // Class support |
ansond | 125:4bf229bf14a3 | 24 | #include "mbed-connector-interface/EventQueueResourceObserver.h" |
ansond | 125:4bf229bf14a3 | 25 | |
ansond | 125:4bf229bf14a3 | 26 | #ifdef CONNECTOR_USING_EVENT_QUEUES |
ansond | 125:4bf229bf14a3 | 27 | |
ansond | 125:4bf229bf14a3 | 28 | // constructor |
ansond | 125:4bf229bf14a3 | 29 | EventQueueResourceObserver::EventQueueResourceObserver(DynamicResource *resource,int sleep_time) : ResourceObserver(resource,sleep_time), m_event_queue() { |
ansond | 125:4bf229bf14a3 | 30 | // default is not observing... |
ansond | 125:4bf229bf14a3 | 31 | this->setObserving(false); |
ansond | 125:4bf229bf14a3 | 32 | |
ansond | 125:4bf229bf14a3 | 33 | // DEBUG |
ansond | 125:4bf229bf14a3 | 34 | this->logger()->log("EventQueueResourceObserver being used for %s (sleep_time: %d ms)",resource->getFullName().c_str(),sleep_time); |
ansond | 125:4bf229bf14a3 | 35 | |
ansond | 125:4bf229bf14a3 | 36 | // start the thread by invoking the thread task... |
ansond | 125:4bf229bf14a3 | 37 | this->m_id = this->m_event_queue.call_every(sleep_time,callback(this,&EventQueueResourceObserver::observation_task)); |
ansond | 125:4bf229bf14a3 | 38 | } |
ansond | 125:4bf229bf14a3 | 39 | |
ansond | 125:4bf229bf14a3 | 40 | // destructor |
ansond | 125:4bf229bf14a3 | 41 | EventQueueResourceObserver::~EventQueueResourceObserver() { |
ansond | 125:4bf229bf14a3 | 42 | this->stopObservation(); |
ansond | 125:4bf229bf14a3 | 43 | this->m_event_queue.cancel(this->m_id); |
ansond | 125:4bf229bf14a3 | 44 | } |
ansond | 125:4bf229bf14a3 | 45 | |
ansond | 125:4bf229bf14a3 | 46 | // observation task method |
ansond | 125:4bf229bf14a3 | 47 | void EventQueueResourceObserver::observation_task() { |
ansond | 125:4bf229bf14a3 | 48 | if (this->isObserving() == true && this->getResource() != NULL) { |
ansond | 125:4bf229bf14a3 | 49 | DynamicResource *res = this->getResource(); |
ansond | 125:4bf229bf14a3 | 50 | if (res != NULL && res->isRegistered() == true) { |
ansond | 125:4bf229bf14a3 | 51 | res->observe(); |
ansond | 125:4bf229bf14a3 | 52 | } |
ansond | 125:4bf229bf14a3 | 53 | } |
ansond | 125:4bf229bf14a3 | 54 | } |
ansond | 125:4bf229bf14a3 | 55 | |
ansond | 125:4bf229bf14a3 | 56 | // begin observing... |
ansond | 125:4bf229bf14a3 | 57 | void EventQueueResourceObserver::beginObservation() { |
ansond | 125:4bf229bf14a3 | 58 | this->setObserving(true); |
ansond | 125:4bf229bf14a3 | 59 | } |
ansond | 125:4bf229bf14a3 | 60 | |
ansond | 125:4bf229bf14a3 | 61 | // stop observing... |
ansond | 125:4bf229bf14a3 | 62 | void EventQueueResourceObserver::stopObservation() { |
ansond | 125:4bf229bf14a3 | 63 | this->setObserving(false); |
ansond | 125:4bf229bf14a3 | 64 | } |
ansond | 125:4bf229bf14a3 | 65 | |
ansond | 125:4bf229bf14a3 | 66 | // halt the underlying observer mechanism |
ansond | 125:4bf229bf14a3 | 67 | void EventQueueResourceObserver::halt() { |
ansond | 125:4bf229bf14a3 | 68 | this->m_event_queue.cancel(this->m_id); |
ansond | 125:4bf229bf14a3 | 69 | } |
ansond | 125:4bf229bf14a3 | 70 | |
ansond | 125:4bf229bf14a3 | 71 | #endif // CONNECTOR_USING_EVENT_QUEUES |