Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 3 | * Copyright (c) 2012-2024 University of California, Los Angeles |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
| 7 | * |
| 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "dummy-forwarder.hpp" |
| 21 | |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame] | 22 | #include <boost/asio/io_context.hpp> |
| 23 | #include <boost/asio/post.hpp> |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 24 | |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 25 | namespace chronosync::tests { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 26 | |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 27 | using ndn::DummyClientFace; |
| 28 | |
| 29 | DummyForwarder::DummyForwarder(boost::asio::io_context& io, ndn::KeyChain& keyChain) |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 30 | : m_io(io) |
| 31 | , m_keyChain(keyChain) |
| 32 | { |
| 33 | } |
| 34 | |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 35 | ndn::Face& |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 36 | DummyForwarder::addFace() |
| 37 | { |
Davide Pesavento | 48dbab6 | 2023-08-12 16:06:52 -0400 | [diff] [blame] | 38 | auto face = std::make_shared<DummyClientFace>(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 39 | DummyClientFace* self = &*face; // to prevent memory leak |
| 40 | |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 41 | face->onSendInterest.connect([this, self] (const auto& interest) { |
| 42 | ndn::Interest i(interest); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 43 | for (auto& otherFace : m_faces) { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 44 | if (self == &*otherFace) { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 45 | continue; |
| 46 | } |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame] | 47 | boost::asio::post(m_io, [=] { otherFace->receive(i); }); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 48 | } |
| 49 | }); |
Davide Pesavento | 48dbab6 | 2023-08-12 16:06:52 -0400 | [diff] [blame] | 50 | |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 51 | face->onSendData.connect([this, self] (const auto& data) { |
| 52 | ndn::Data d(data); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 53 | for (auto& otherFace : m_faces) { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 54 | if (self == &*otherFace) { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 55 | continue; |
| 56 | } |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame] | 57 | boost::asio::post(m_io, [=] { otherFace->receive(d); }); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 58 | } |
| 59 | }); |
| 60 | |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 61 | face->onSendNack.connect([this, self] (const auto& nack) { |
| 62 | ndn::lp::Nack n(nack); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 63 | for (auto& otherFace : m_faces) { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 64 | if (self == &*otherFace) { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 65 | continue; |
| 66 | } |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame] | 67 | boost::asio::post(m_io, [=] { otherFace->receive(n); }); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 68 | } |
| 69 | }); |
| 70 | |
| 71 | m_faces.push_back(face); |
| 72 | return *face; |
| 73 | } |
| 74 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 75 | void |
| 76 | DummyForwarder::removeFaces() |
| 77 | { |
| 78 | m_faces.clear(); |
| 79 | } |
| 80 | |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 81 | } // namespace chronosync::tests |