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