14#include "keyple/core/service/AbstractPluginAdapter.hpp"
22#include "keyple/core/service/AbstractReaderAdapter.hpp"
23#include "keyple/core/service/LocalConfigurableReaderAdapter.hpp"
24#include "keyple/core/service/ObservableLocalConfigurableReaderAdapter.hpp"
25#include "keyple/core/util/cpp/StringUtils.hpp"
26#include "keyple/core/util/cpp/exception/Exception.hpp"
27#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
28#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
34using keyple::core::util::cpp::StringUtils;
35using keyple::core::util::cpp::exception::Exception;
36using keyple::core::util::cpp::exception::IllegalArgumentException;
37using keyple::core::util::cpp::exception::IllegalStateException;
39AbstractPluginAdapter::AbstractPluginAdapter(
40 const std::string& pluginName,
41 std::shared_ptr<KeyplePluginExtension> pluginExtension)
42: mPluginName(pluginName)
43, mPluginExtension(pluginExtension)
48std::shared_ptr<LocalReaderAdapter>
49AbstractPluginAdapter::buildLocalReaderAdapter(
50 std::shared_ptr<ReaderSpi> readerSpi)
52 std::shared_ptr<LocalReaderAdapter> adapter =
nullptr;
54 const auto& observable
55 = std::dynamic_pointer_cast<ObservableReaderSpi>(readerSpi);
56 const auto& configurable
57 = std::dynamic_pointer_cast<ConfigurableReaderSpi>(readerSpi);
62 = std::make_shared<ObservableLocalConfigurableReaderAdapter>(
63 configurable, getName());
65 adapter = std::make_shared<ObservableLocalReaderAdapter>(
66 observable, getName());
68 }
else if (configurable) {
69 adapter = std::make_shared<LocalConfigurableReaderAdapter>(
70 configurable, getName());
72 adapter = std::make_shared<LocalReaderAdapter>(readerSpi, getName());
79AbstractPluginAdapter::checkStatus()
const
82 throw IllegalStateException(
83 "Plugin '" + mPluginName +
"' is not or no longer registered");
88AbstractPluginAdapter::doRegister()
94AbstractPluginAdapter::doUnregister()
96 mIsRegistered =
false;
98 for (
const auto& reader : mReaders) {
100 std::dynamic_pointer_cast<AbstractReaderAdapter>(reader.second)
102 }
catch (
const Exception& e) {
104 "[plugin=%] Failed to unregister reader [reader=%, reason=%]\n",
106 reader.second->getName(),
112 mIsRegistered =
false;
116AbstractPluginAdapter::getName()
const
121std::shared_ptr<KeyplePluginExtension>
122AbstractPluginAdapter::getExtension(
123 const std::type_info& pluginExtensionClass)
const
125 (void)pluginExtensionClass;
129 return mPluginExtension;
132std::shared_ptr<KeypleReaderExtension>
133AbstractPluginAdapter::getReaderExtension(
134 const std::type_info& readerExtensionClass,
135 const std::string& readerName)
const
139 const auto reader = std::dynamic_pointer_cast<AbstractReaderAdapter>(
140 getReader(readerName));
141 if (reader ==
nullptr) {
142 throw IllegalArgumentException(
143 "Reader '" + readerName +
"' is not found");
146 return reader->getExtension(readerExtensionClass);
149std::map<const std::string, std::shared_ptr<CardReader>>&
150AbstractPluginAdapter::getReadersMap()
155const std::vector<std::string>
156AbstractPluginAdapter::getReaderNames()
const
160 std::vector<std::string> readerNames;
161 for (
const auto& pair : mReaders) {
162 readerNames.push_back(pair.first);
168const std::vector<std::shared_ptr<CardReader>>
169AbstractPluginAdapter::getReaders()
const
173 std::vector<std::shared_ptr<CardReader>> readers;
174 for (
const auto& pair : mReaders) {
175 readers.push_back(pair.second);
181std::shared_ptr<CardReader>
182AbstractPluginAdapter::getReader(
const std::string& name)
const
186 if (mReaders.find(name) != mReaders.end()) {
187 return mReaders.at(name);
193std::shared_ptr<CardReader>
194AbstractPluginAdapter::findReader(
const std::string& readerNameRegex)
const
196 for (
const auto& reader : mReaders) {
198 if (StringUtils::matches(
199 reader.second->getName(), readerNameRegex)) {
200 return reader.second;
203 }
catch (
const std::regex_error& e) {
204 throw IllegalArgumentException(
205 "Parameter 'readerNameRegex' has an invalid regex syntax:"
206 + readerNameRegex +
": " + e.what());