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