Keyple Service C++ Library - 3.3.5
Component of the Keyple C++ middleware
AbstractObservableStateAdapter.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/core/service/AbstractObservableStateAdapter.hpp"
15
16#include <memory>
17#include <typeinfo>
18
19#include "keyple/core/service/AbstractMonitoringJobAdapter.hpp"
20
21namespace keyple {
22namespace core {
23namespace service {
24
25AbstractObservableStateAdapter::AbstractObservableStateAdapter(
26 const MonitoringState monitoringState,
27 ObservableLocalReaderAdapter* reader,
28 std::shared_ptr<AbstractMonitoringJobAdapter> monitoringJob,
29 std::shared_ptr<ExecutorService> executorService)
30: mMonitoringState(monitoringState)
31, mReader(reader)
32, mMonitoringJob(monitoringJob)
33, mExecutorService(executorService)
34{
35}
36
37MonitoringState
38AbstractObservableStateAdapter::getMonitoringState() const
39{
40 return mMonitoringState;
41}
42
43ObservableLocalReaderAdapter*
44AbstractObservableStateAdapter::getReader() const
45{
46 return mReader;
47}
48
49void
50AbstractObservableStateAdapter::switchState(const MonitoringState stateId)
51{
52 mReader->switchState(stateId);
53}
54
55void
56AbstractObservableStateAdapter::onActivate()
57{
58 /* Launch the monitoringJob if necessary */
59 if (mMonitoringJob != nullptr) {
60 if (mExecutorService == nullptr) {
61 throw IllegalStateException("ExecutorService must be set");
62 }
63
64 mMonitoringEvent = mExecutorService->submit(
65 mMonitoringJob->getMonitoringJob(shared_from_this()));
66 }
67}
68
69void
70AbstractObservableStateAdapter::onDeactivate()
71{
72 /* Cancel the monitoringJob if necessary */
73 if (mMonitoringEvent != nullptr && !mMonitoringEvent->isDone()) {
74 mMonitoringJob->stop();
75 mMonitoringEvent->cancel(false);
76
77 /*
78 * Make sure pointer is set to nullptr to force Job/Thread destruction.
79 */
80 mMonitoringEvent = nullptr;
81 }
82}
83
84} /* namespace service */
85} /* namespace core */
86} /* namespace keyple */