Keyple Service C++ Library - 3.3.5
Component of the Keyple C++ middleware
AutonomousObservableLocalPluginAdapter.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/AutonomousObservableLocalPluginAdapter.hpp"
15
16#include <memory>
17#include <string>
18#include <vector>
19
20#include "keyple/core/service/PluginEventAdapter.hpp"
21#include "keyple/core/util/KeypleAssert.hpp"
22#include "keyple/core/util/cpp/exception/Exception.hpp"
23#include "keypop/reader/CardReader.hpp"
24
25namespace keyple {
26namespace core {
27namespace service {
28
29using keyple::core::util::cpp::exception::Exception;
30using keypop::reader::CardReader;
31
32AutonomousObservableLocalPluginAdapter::AutonomousObservableLocalPluginAdapter(
33 std::shared_ptr<AutonomousObservablePluginSpi>
34 autonomousObservablePluginSpi)
35: AbstractObservableLocalPluginAdapter(autonomousObservablePluginSpi)
36{
37 try {
38 autonomousObservablePluginSpi->setCallback(this);
39 } catch (const Exception& e) {
40 mLogger->trace(
41 "Method 'setCallback(...)' unavailable for legacy plugin: %\n",
42 e.getMessage());
43 autonomousObservablePluginSpi->connect(this);
44 }
45}
46
47void
48AutonomousObservableLocalPluginAdapter::onReaderConnected(
49 const std::vector<std::shared_ptr<ReaderSpi>>& readers)
50{
51 Assert::getInstance().notEmpty(readers, "readers");
52 std::vector<std::string> notifyReaders;
53
54 for (const auto& readerSpi : readers) {
55 /* Add reader to plugin */
56 addReader(readerSpi);
57 notifyReaders.push_back(readerSpi->getName());
58 }
59
60 notifyObservers(std::make_shared<PluginEventAdapter>(
61 getName(), notifyReaders, PluginEvent::Type::READER_CONNECTED));
62}
63
64void
65AutonomousObservableLocalPluginAdapter::onReaderDisconnected(
66 const std::vector<std::string>& readerNames)
67{
68 Assert::getInstance().notEmpty(readerNames, "readerNames");
69 std::vector<std::string> notifyReaders;
70
71 for (const auto& readerName : readerNames) {
72 const std::shared_ptr<CardReader> reader = getReader(readerName);
73 if (reader == nullptr) {
74 mLogger->warn(
75 "Plugin [%] unable to remove unknown reader [%]\n",
76 getName(),
77 readerName);
78 } else {
79 /* Unregister and remove reader */
80 std::dynamic_pointer_cast<LocalReaderAdapter>(reader)
81 ->doUnregister();
82 getReadersMap().erase(reader->getName());
83 mLogger->info(
84 "Plugin [%] removes reader [%] from readers list\n",
85 getName(),
86 reader->getName());
87
88 notifyReaders.push_back(readerName);
89 }
90 }
91
92 notifyObservers(std::make_shared<PluginEventAdapter>(
93 getName(), notifyReaders, PluginEvent::Type::READER_DISCONNECTED));
94}
95
96void
97AutonomousObservableLocalPluginAdapter::addReader(
98 std::shared_ptr<ReaderSpi> readerSpi)
99{
100 std::shared_ptr<LocalReaderAdapter> reader
101 = buildLocalReaderAdapter(readerSpi);
102 reader->doRegister();
103 getReadersMap().insert({reader->getName(), reader});
104
105 mLogger->info(
106 "Plugin [%] adds reader [%] to readers list\n",
107 getName(),
108 readerSpi->getName());
109}
110
111} /* namespace service */
112} /* namespace core */
113} /* namespace keyple */