Keyple Service C++ Library - 3.4.1
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(
62 "ExecutorService is not set. Cannot launch monitoring job");
63 }
64
65 mMonitoringEvent = mExecutorService->submit(
66 mMonitoringJob->getMonitoringJob(shared_from_this()));
67 }
68}
69
70void
71AbstractObservableStateAdapter::onDeactivate()
72{
73 /* Cancel the monitoringJob if necessary */
74 if (mMonitoringEvent != nullptr && !mMonitoringEvent->isDone()) {
75 mMonitoringJob->stop();
76 mMonitoringEvent->cancel(false);
77
78 /*
79 * Make sure pointer is set to nullptr to force Job/Thread destruction.
80 */
81 mMonitoringEvent = nullptr;
82 }
83}
84
85} /* namespace service */
86} /* namespace core */
87} /* namespace keyple */