Keyple Service C++ Library - 3.3.5
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
18#include "keyple/core/plugin/ReaderIOException.hpp"
19#include "keyple/core/plugin/TaskCanceledException.hpp"
20#include "keyple/core/service/ObservableLocalReaderAdapter.hpp"
21#include "keyple/core/util/cpp/Thread.hpp"
22#include "keyple/core/util/cpp/exception/InterruptedException.hpp"
23#include "keyple/core/util/cpp/exception/RuntimeException.hpp"
24
25namespace keyple {
26namespace core {
27namespace service {
28
30using keyple::core::util::cpp::exception::InterruptedException;
31using keyple::core::util::cpp::exception::RuntimeException;
32
33/* CARD REMOVAL ACTIVE MONITORING JOB
34 * ----------------------------------------------------------- */
35
36CardRemovalActiveMonitoringJobAdapter::CardRemovalActiveMonitoringJob ::
37 CardRemovalActiveMonitoringJob(
38 std::shared_ptr<AbstractObservableStateAdapter> monitoringState,
39 CardRemovalActiveMonitoringJobAdapter* parent)
40: Job("CardRemovalActiveMonitoringJobAdapter")
41, mMonitoringState(monitoringState)
42, mParent(parent)
43{
44}
45
46void
47CardRemovalActiveMonitoringJobAdapter::CardRemovalActiveMonitoringJob::execute()
48{
49 try {
50 mParent->mLogger->trace(
51 "Start monitoring job polling process using 'isCardPresentPing()'"
52 "method on reader [%]\n",
53 mParent->getReader()->getName());
54
55 /* Re-init loop value to true */
56 mParent->mLoop = true;
57
58 while (mParent->mLoop) {
59 if (!mParent->getReader()->isCardPresentPing()) {
60 mParent->mLogger->trace("Card stop responding\n");
61 break;
62 }
63
64 /* Wait a bit */
65 try {
66 Thread::sleep(mParent->mSleepDurationMillis);
67 } catch (const InterruptedException& ignored) {
68 (void)ignored;
69 /* Restore interrupted state... */
70 interrupt();
71 mParent->mLoop = false;
72 }
73 }
74
75 mParent->mLogger->trace("Monitoring job polling process stopped\n");
76 } catch (const RuntimeException& e) {
77 mParent->getReader()
78 ->getObservationExceptionHandler()
79 ->onReaderObservationError(
80 mParent->getReader()->getPluginName(),
81 mParent->getReader()->getName(),
82 std::make_shared<RuntimeException>(e));
83 }
84
85 /* Finally block */
86 mMonitoringState->onEvent(InternalEvent::CARD_REMOVED);
87}
88
89/* CARD REMOVAL ACTIVE MONITORING JOB ADAPTER
90 * --------------------------------------------------- */
91
92CardRemovalActiveMonitoringJobAdapter::CardRemovalActiveMonitoringJobAdapter(
93 ObservableLocalReaderAdapter* reader, const int64_t sleepDurationMillis)
94: AbstractMonitoringJobAdapter(reader)
95, mReaderSpi(std::dynamic_pointer_cast<WaitForCardRemovalBlockingSpi>(
96 reader->getObservableReaderSpi()))
97, mSleepDurationMillis(sleepDurationMillis)
98{
99}
100
101std::shared_ptr<Job>
102CardRemovalActiveMonitoringJobAdapter::getMonitoringJob(
103 std::shared_ptr<AbstractObservableStateAdapter> monitoringState)
104{
105 return std::make_shared<CardRemovalActiveMonitoringJob>(
106 monitoringState, this);
107}
108
109void
110CardRemovalActiveMonitoringJobAdapter::stop()
111{
112 mLoop = false;
113}
114
115} /* namespace service */
116} /* namespace core */
117} /* namespace keyple */
ObservableLocalReaderAdapter::InternalEvent InternalEvent