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 | 48dbab6 | 2023-08-12 16:06:52 -0400 | [diff] [blame] | 3 | * Copyright (c) 2012-2023 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 | |
| 25 | namespace ndn { |
| 26 | namespace chronosync { |
| 27 | |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 28 | DummyForwarder::DummyForwarder(boost::asio::io_context& io, KeyChain& keyChain) |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 29 | : m_io(io) |
| 30 | , m_keyChain(keyChain) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | Face& |
| 35 | DummyForwarder::addFace() |
| 36 | { |
Davide Pesavento | 48dbab6 | 2023-08-12 16:06:52 -0400 | [diff] [blame] | 37 | auto face = std::make_shared<DummyClientFace>(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 38 | DummyClientFace* self = &*face; // to prevent memory leak |
| 39 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 40 | face->onSendInterest.connect([this, self] (const Interest& interest) { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 41 | Interest i(interest); |
| 42 | for (auto& otherFace : m_faces) { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 43 | if (self == &*otherFace) { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 44 | continue; |
| 45 | } |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 46 | boost::asio::post(m_io, [=] { otherFace->receive(i); }); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 47 | } |
| 48 | }); |
Davide Pesavento | 48dbab6 | 2023-08-12 16:06:52 -0400 | [diff] [blame] | 49 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 50 | face->onSendData.connect([this, self] (const Data& data) { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 51 | Data d(data); |
| 52 | for (auto& otherFace : m_faces) { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 53 | if (self == &*otherFace) { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 54 | continue; |
| 55 | } |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 56 | boost::asio::post(m_io, [=] { otherFace->receive(d); }); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 57 | } |
| 58 | }); |
| 59 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 60 | face->onSendNack.connect([this, self] (const lp::Nack& nack) { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 61 | lp::Nack n(nack); |
| 62 | for (auto& otherFace : m_faces) { |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 63 | if (self == &*otherFace) { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 64 | continue; |
| 65 | } |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 66 | boost::asio::post(m_io, [=] { otherFace->receive(n); }); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 67 | } |
| 68 | }); |
| 69 | |
| 70 | m_faces.push_back(face); |
| 71 | return *face; |
| 72 | } |
| 73 | |
Nick Gordon | 0b3beab | 2018-03-02 13:03:28 -0600 | [diff] [blame] | 74 | void |
| 75 | DummyForwarder::removeFaces() |
| 76 | { |
| 77 | m_faces.clear(); |
| 78 | } |
| 79 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 80 | } // namespace chronosync |
| 81 | } // namespace ndn |