Keyple Service C++ Library - 3.3.5
Component of the Keyple C++ middleware
CardInsertionPassiveMonitoringJobAdapter.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/CardInsertionPassiveMonitoringJobAdapter.hpp"
15
16#include <memory>
17
18#include "keyple/core/plugin/ReaderIOException.hpp"
19#include "keyple/core/plugin/TaskCanceledException.hpp"
20#include "keyple/core/plugin/spi/reader/observable/state/insertion/CardInsertionWaiterBlockingSpi.hpp"
21#include "keyple/core/plugin/spi/reader/observable/state/insertion/WaitForCardInsertionBlockingSpi.hpp"
22#include "keyple/core/util/cpp/exception/RuntimeException.hpp"
23
24namespace keyple {
25namespace core {
26namespace service {
27
28using keyple::core::plugin::ReaderIOException;
29using keyple::core::plugin::TaskCanceledException;
30using keyple::core::plugin::spi::reader::observable::state::insertion::
31 CardInsertionWaiterBlockingSpi;
32using keyple::core::plugin::spi::reader::observable::state::insertion::
33 WaitForCardInsertionBlockingSpi;
34using keyple::core::util::cpp::exception::RuntimeException;
35
37
38/* CARD INSERTION PASSIVE MONITORING JOB
39 * -------------------------------------------------------- */
40
41CardInsertionPassiveMonitoringJobAdapter::CardInsertionPassiveMonitoringJob ::
42 CardInsertionPassiveMonitoringJob(
43 std::shared_ptr<AbstractObservableStateAdapter> monitoringState,
44 CardInsertionPassiveMonitoringJobAdapter* parent)
45: Job("CardInsertionPassiveMonitoringJob")
46, mMonitoringState(monitoringState)
47, mParent(parent)
48{
49}
50
51void
52CardInsertionPassiveMonitoringJobAdapter::CardInsertionPassiveMonitoringJob::
53 execute()
54{
55 try {
56 mParent->mLogger->trace(
57 "Start monitoring job process on reader [%]\n",
58 mParent->getReader()->getName());
59
60 const auto& cardInsertion
61 = std::dynamic_pointer_cast<CardInsertionWaiterBlockingSpi>(
62 mParent->mReaderSpi);
63 const auto& waitForCard
64 = std::dynamic_pointer_cast<WaitForCardInsertionBlockingSpi>(
65 mParent->mReaderSpi);
66 if (cardInsertion != nullptr) {
67 cardInsertion->waitForCardInsertion();
68 } else if (waitForCard != nullptr) {
69 waitForCard->waitForCardInsertion();
70 }
71 mMonitoringState->onEvent(InternalEvent::CARD_INSERTED);
72
73 } catch (const ReaderIOException& e) {
74 /* Just warn as it can be a disconnection of the reader. */
75 mParent->mLogger->warn(
76 "Monitoring job error while processing card insertion event on "
77 "reader [%]: %\n",
78 mParent->getReader()->getName(),
79 e.getMessage());
80
81 } catch (const TaskCanceledException& e) {
82 mParent->mLogger->warn(
83 "Monitoring job process cancelled: %\n", e.getMessage());
84
85 } catch (const RuntimeException& e) {
86 mParent->getReader()
87 ->getObservationExceptionHandler()
88 ->onReaderObservationError(
89 mParent->getReader()->getPluginName(),
90 mParent->getReader()->getName(),
91 std::make_shared<RuntimeException>(e));
92 }
93}
94
95/* CARD INSERTION PASSIVE MONITORING JOB ADAPTER
96 * ------------------------------------------------ */
97
98CardInsertionPassiveMonitoringJobAdapter::
99 CardInsertionPassiveMonitoringJobAdapter(
100 ObservableLocalReaderAdapter* reader)
101: AbstractMonitoringJobAdapter(reader)
102, mReaderSpi(reader->getObservableReaderSpi())
103{
104}
105
106std::shared_ptr<Job>
107CardInsertionPassiveMonitoringJobAdapter::getMonitoringJob(
108 std::shared_ptr<AbstractObservableStateAdapter> monitoringState)
109{
110 return std::make_shared<CardInsertionPassiveMonitoringJob>(
111 monitoringState, this);
112}
113
114void
115CardInsertionPassiveMonitoringJobAdapter::stop()
116{
117 mLogger->trace("Stop monitoring job process\n");
118
119 const auto& cardInsertion
120 = std::dynamic_pointer_cast<CardInsertionWaiterBlockingSpi>(mReaderSpi);
121 const auto& waitForCard
122 = std::dynamic_pointer_cast<WaitForCardInsertionBlockingSpi>(
123 mReaderSpi);
124 if (cardInsertion != nullptr) {
125 cardInsertion->stopWaitForCardInsertion();
126 } else if (waitForCard != nullptr) {
127 waitForCard->stopWaitForCardInsertion();
128 }
129
130 mLogger->trace("Monitoring job process stopped\n");
131}
132
133} /* namespace service */
134} /* namespace core */
135} /* namespace keyple */
ObservableLocalReaderAdapter::InternalEvent InternalEvent