Keyple Service C++ Library - 3.3.5
Component of the Keyple C++ middleware
LocalPoolPluginAdapter.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/LocalPoolPluginAdapter.hpp"
15
16#include <memory>
17#include <string>
18#include <typeinfo>
19#include <utility>
20#include <vector>
21
22#include "keyple/core/common/KeypleReaderExtension.hpp"
23#include "keyple/core/plugin/PluginIOException.hpp"
24#include "keyple/core/plugin/spi/reader/PoolReaderSpi.hpp"
25#include "keyple/core/service/KeyplePluginException.hpp"
26#include "keyple/core/util/KeypleAssert.hpp"
27#include "keyple/core/util/cpp/exception/Exception.hpp"
28
29namespace keyple {
30namespace core {
31namespace service {
32
33using keyple::core::common::KeypleReaderExtension;
34using keyple::core::plugin::PluginIOException;
35using keyple::core::plugin::spi::reader::PoolReaderSpi;
36using keyple::core::util::Assert;
37using keyple::core::util::cpp::exception::Exception;
38
39LocalPoolPluginAdapter::LocalPoolPluginAdapter(
40 std::shared_ptr<PoolPluginSpi> poolPluginSpi)
41: AbstractPluginAdapter(
42 poolPluginSpi->getName(),
43 std::dynamic_pointer_cast<KeyplePluginExtension>(poolPluginSpi))
44, mPoolPluginSpi(poolPluginSpi)
45{
46}
47
48void
49LocalPoolPluginAdapter::doUnregister()
50{
51 try {
52 mPoolPluginSpi->onUnregister();
53 } catch (const Exception& e) {
54 mLogger->error(
55 "Error unregistering plugin extension [%]: %\n",
56 getName(),
57 e.getMessage());
58 }
59
60 AbstractPluginAdapter::doUnregister();
61}
62
63const std::vector<std::string>
64LocalPoolPluginAdapter::getReaderGroupReferences() const
65{
66 checkStatus();
67
68 try {
69 return mPoolPluginSpi->getReaderGroupReferences();
70 } catch (const PluginIOException& e) {
71 throw KeyplePluginException(
72 std::string("Pool plugin [") + getName()
73 + "] is unable to get reader group references: "
74 + e.getMessage(),
75 e);
76 }
77}
78
79std::shared_ptr<CardReader>
80LocalPoolPluginAdapter::allocateReader(const std::string& readerGroupReference)
81{
82 checkStatus();
83
84 mLogger->debug(
85 "Pool plugin [%] allocates reader of group reference [%]\n",
86 getName(),
87 readerGroupReference);
88
89 Assert::getInstance().notEmpty(
90 readerGroupReference, "readerGroupReference");
91
92 std::shared_ptr<ReaderSpi> readerSpi = nullptr;
93
94 try {
95 readerSpi = mPoolPluginSpi->allocateReader(readerGroupReference);
96 } catch (const PluginIOException& e) {
97 throw KeyplePluginException(
98 std::string("Pool plugin [") + getName()
99 + "] unable to allocate reader of reader group reference ["
100 + readerGroupReference + "]: " + e.getMessage(),
101 e);
102 }
103
104 std::shared_ptr<LocalReaderAdapter> localReaderAdapter
105 = buildLocalReaderAdapter(readerSpi);
106 getReadersMap().insert({localReaderAdapter->getName(), localReaderAdapter});
107 localReaderAdapter->doRegister();
108
109 return localReaderAdapter;
110}
111
112void
113LocalPoolPluginAdapter::releaseReader(std::shared_ptr<CardReader> reader)
114{
115 checkStatus();
116
117 mLogger->debug(
118 "Pool plugin [%] releases reader [%]\n",
119 getName(),
120 reader != nullptr ? reader->getName() : "null");
121
122 Assert::getInstance().notNull(reader, "reader");
123
124 try {
125 mPoolPluginSpi->releaseReader(
126 std::dynamic_pointer_cast<LocalReaderAdapter>(reader)
127 ->getReaderSpi());
128
129 /* Java 'finally' code moved here */
130 getReadersMap().erase(reader->getName());
131 std::dynamic_pointer_cast<LocalReaderAdapter>(reader)->doUnregister();
132 } catch (const PluginIOException& e) {
133 /* Java 'finally' code moved here */
134 getReadersMap().erase(reader->getName());
135 std::dynamic_pointer_cast<LocalReaderAdapter>(reader)->doUnregister();
136 throw KeyplePluginException(
137 std::string("Pool plugin [") + getName()
138 + "] unable to release reader [" + reader->getName()
139 + "]: " + e.getMessage(),
140 e);
141 }
142}
143
144std::shared_ptr<SmartCard>
145LocalPoolPluginAdapter::getSelectedSmartCard(
146 const std::shared_ptr<CardReader> reader)
147{
148 Assert::getInstance().notNull(reader, "reader");
149
150 auto poolReaderSpi = std::dynamic_pointer_cast<PoolReaderSpi>(
151 getReaderExtension(typeid(KeypleReaderExtension), reader->getName()));
152
153 return std::dynamic_pointer_cast<SmartCard>(
154 poolReaderSpi->getSelectedSmartCard());
155}
156
157} /* namespace service */
158} /* namespace core */
159} /* namespace keyple */