Keyple Service C++ Library - 3.4.1
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&) {
40 mLogger->trace(
41 "[plugin=%] Method 'setCallback(...)' unavailable for legacy "
42 "plugin\n",
43 getName());
44 autonomousObservablePluginSpi->connect(this);
45 }
46}
47
48void
49AutonomousObservableLocalPluginAdapter::onReaderConnected(
50 const std::vector<std::shared_ptr<ReaderSpi>>& readers)
51{
52 Assert::getInstance().notEmpty(readers, "readers");
53 std::vector<std::string> notifyReaders;
54
55 for (const auto& readerSpi : readers) {
56 /* Add reader to plugin */
57 addReader(readerSpi);
58 notifyReaders.push_back(readerSpi->getName());
59 }
60
61 notifyObservers(
62 std::make_shared<PluginEventAdapter>(
63 getName(), notifyReaders, PluginEvent::Type::READER_CONNECTED));
64}
65
66void
67AutonomousObservableLocalPluginAdapter::onReaderDisconnected(
68 const std::vector<std::string>& readerNames)
69{
70 Assert::getInstance().notEmpty(readerNames, "readerNames");
71 std::vector<std::string> notifyReaders;
72
73 for (const auto& readerName : readerNames) {
74 const std::shared_ptr<CardReader> reader = getReader(readerName);
75 if (reader == nullptr) {
76 mLogger->warn(
77 "[plugin=%] Unable to remove unknown reader [reader=%]\n",
78 getName(),
79 readerName);
80 } else {
81 /* Unregister and remove reader */
82 std::dynamic_pointer_cast<LocalReaderAdapter>(reader)
83 ->doUnregister();
84 getReadersMap().erase(reader->getName());
85 mLogger->info(
86 "[plugin=%] Reader removed from readers list [reader=%]\n",
87 getName(),
88 reader->getName());
89
90 notifyReaders.push_back(readerName);
91 }
92 }
93
94 notifyObservers(
95 std::make_shared<PluginEventAdapter>(
96 getName(), notifyReaders, PluginEvent::Type::READER_DISCONNECTED));
97}
98
99void
100AutonomousObservableLocalPluginAdapter::addReader(
101 std::shared_ptr<ReaderSpi> readerSpi)
102{
103 std::shared_ptr<LocalReaderAdapter> reader
104 = buildLocalReaderAdapter(readerSpi);
105 reader->doRegister();
106 getReadersMap().insert({reader->getName(), reader});
107
108 mLogger->info(
109 "[plugin=%] Reader added to readers list [reader=%]\n",
110 getName(),
111 readerSpi->getName());
112}
113
114} /* namespace service */
115} /* namespace core */
116} /* namespace keyple */