Keyple Service C++ Library - 3.4.1
Component of the Keyple C++ middleware
CardInsertionActiveMonitoringJobAdapter.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/CardInsertionActiveMonitoringJobAdapter.hpp"
15
16#include <memory>
17#include <string>
18
19#include "keyple/core/util/cpp/exception/InterruptedException.hpp"
20#include "keyple/core/util/cpp/exception/RuntimeException.hpp"
21
22namespace keyple {
23namespace core {
24namespace service {
25
26using keyple::core::util::cpp::exception::InterruptedException;
27using keyple::core::util::cpp::exception::RuntimeException;
28
30
31const std::string CardInsertionActiveMonitoringJobAdapter::JOB_ID
32 = "INSERTION_ACTIVE";
33
34/* CARD INSERTION ACTIVE MONITORING JOB
35 * --------------------------------------------------------- */
36
37CardInsertionActiveMonitoringJobAdapter::CardInsertionActiveMonitoringJob ::
38 CardInsertionActiveMonitoringJob(
39 std::shared_ptr<AbstractObservableStateAdapter> monitoringState,
40 CardInsertionActiveMonitoringJobAdapter* parent)
41: Job("CardInsertionActiveMonitoringJobAdapter - " + parent->mReader->getName())
42, mMonitoringState(monitoringState)
43, mParent(parent)
44{
45}
46
47void
48CardInsertionActiveMonitoringJobAdapter::CardInsertionActiveMonitoringJob::
49 execute()
50{
51 try {
52 mParent->mLogger->trace(
53 "[fsmJob=%, reader=%] Starting monitoring job process "
54 "[mode=Polling using 'isCardPresent()']\n",
55 JOB_ID,
56 mParent->mReader->getName());
57
58 /* Re-init loop value to true */
59 mParent->mLoop = true;
60
61 /*
62 * C++: jobs are queued asynchronously by the ExecutorService, so
63 * stop() may already have been called (and cancel() set) before this
64 * method starts running, in which case the reset above would
65 * otherwise silently revive a job that was already told to stop and
66 * loop forever. Checking isCancelled() here and on every iteration
67 * closes that race.
68 */
69 while (mParent->mLoop && !isCancelled()) {
70 /* Polls for CARD_INSERTED */
71 if (mParent->mMonitorInsertion
72 && mParent->mReader->isCardPresent()) {
73 mParent->mLogger->trace(
74 "[fsmJob=%, reader=%] Card present\n",
75 JOB_ID,
76 mParent->mReader->getName());
77 mMonitoringState->onEvent(InternalEvent::CARD_INSERTED);
78 return;
79 }
80
81 /* Polls for CARD_REMOVED */
82 if (!mParent->mMonitorInsertion
83 && !mParent->mReader->isCardPresent()) {
84 mParent->mLogger->trace(
85 "[fsmJob=%, reader=%] Card not present\n",
86 JOB_ID,
87 mParent->mReader->getName());
88 mParent->mLoop = false;
89 mMonitoringState->onEvent(InternalEvent::CARD_REMOVED);
90 return;
91 }
92
93 /* Wait a bit */
94 try {
95 Thread::sleep(mParent->mSleepDurationMillis);
96 } catch (const InterruptedException& ignored) {
97 (void)ignored;
98 /* Restore interrupted state... */
99 interrupt();
100 mParent->mLoop = false;
101 }
102 }
103 mParent->mLogger->trace(
104 "[fsmJob=%, reader=%] Monitoring job polling process stopped\n",
105 JOB_ID,
106 mParent->mReader->getName());
107
108 } catch (const RuntimeException& e) {
109 dynamic_cast<ObservableLocalReaderAdapter*>(mParent->mReader)
110 ->getObservationExceptionHandler()
111 ->onReaderObservationError(
112 dynamic_cast<ObservableLocalReaderAdapter*>(mParent->mReader)
113 ->getPluginName(),
114 mParent->mReader->getName(),
115 std::make_shared<RuntimeException>(e));
116 }
117}
118
119/* CARD INSERTION ACTIVE MONITORING JOB ADAPTER
120 * ------------------------------------------------- */
121
122CardInsertionActiveMonitoringJobAdapter::
123 CardInsertionActiveMonitoringJobAdapter(
124 ObservableLocalReaderAdapter* reader,
125 const int64_t sleepDurationMillis,
126 const bool monitorInsertion)
127: AbstractMonitoringJobAdapter(reader)
128, mSleepDurationMillis(sleepDurationMillis)
129, mMonitorInsertion(monitorInsertion)
130, mReader(reader)
131{
132}
133
134std::shared_ptr<Job>
135CardInsertionActiveMonitoringJobAdapter::getMonitoringJob(
136 const std::shared_ptr<AbstractObservableStateAdapter> monitoringState)
137{
138 return std::make_shared<CardInsertionActiveMonitoringJob>(
139 monitoringState, this);
140}
141
142void
143CardInsertionActiveMonitoringJobAdapter::stop()
144{
145 mLoop = false;
146}
147
148} /* namespace service */
149} /* namespace core */
150} /* namespace keyple */
ObservableLocalReaderAdapter::InternalEvent InternalEvent