blob: cefc08a1832636b91bf599dcd3ab2414e9eedbd8 [file] [log] [blame]
Ashlesh Gawande08784d42017-09-06 23:40:21 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento30c41ec2024-02-12 17:36:35 -05003 * Copyright (c) 2012-2024 University of California, Los Angeles
Ashlesh Gawande08784d42017-09-06 23:40:21 -05004 *
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 Pesavento1af79492023-09-22 15:45:21 -040022#include <boost/asio/io_context.hpp>
23#include <boost/asio/post.hpp>
Ashlesh Gawande08784d42017-09-06 23:40:21 -050024
Davide Pesavento30c41ec2024-02-12 17:36:35 -050025namespace chronosync::tests {
Ashlesh Gawande08784d42017-09-06 23:40:21 -050026
Davide Pesavento30c41ec2024-02-12 17:36:35 -050027using ndn::DummyClientFace;
28
29DummyForwarder::DummyForwarder(boost::asio::io_context& io, ndn::KeyChain& keyChain)
Ashlesh Gawande08784d42017-09-06 23:40:21 -050030 : m_io(io)
31 , m_keyChain(keyChain)
32{
33}
34
Davide Pesavento30c41ec2024-02-12 17:36:35 -050035ndn::Face&
Ashlesh Gawande08784d42017-09-06 23:40:21 -050036DummyForwarder::addFace()
37{
Davide Pesavento48dbab62023-08-12 16:06:52 -040038 auto face = std::make_shared<DummyClientFace>(m_io, m_keyChain, DummyClientFace::Options{true, true});
39 DummyClientFace* self = &*face; // to prevent memory leak
40
Davide Pesavento30c41ec2024-02-12 17:36:35 -050041 face->onSendInterest.connect([this, self] (const auto& interest) {
42 ndn::Interest i(interest);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050043 for (auto& otherFace : m_faces) {
Nick Gordon0b3beab2018-03-02 13:03:28 -060044 if (self == &*otherFace) {
Ashlesh Gawande08784d42017-09-06 23:40:21 -050045 continue;
46 }
Davide Pesavento1af79492023-09-22 15:45:21 -040047 boost::asio::post(m_io, [=] { otherFace->receive(i); });
Ashlesh Gawande08784d42017-09-06 23:40:21 -050048 }
49 });
Davide Pesavento48dbab62023-08-12 16:06:52 -040050
Davide Pesavento30c41ec2024-02-12 17:36:35 -050051 face->onSendData.connect([this, self] (const auto& data) {
52 ndn::Data d(data);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050053 for (auto& otherFace : m_faces) {
Nick Gordon0b3beab2018-03-02 13:03:28 -060054 if (self == &*otherFace) {
Ashlesh Gawande08784d42017-09-06 23:40:21 -050055 continue;
56 }
Davide Pesavento1af79492023-09-22 15:45:21 -040057 boost::asio::post(m_io, [=] { otherFace->receive(d); });
Ashlesh Gawande08784d42017-09-06 23:40:21 -050058 }
59 });
60
Davide Pesavento30c41ec2024-02-12 17:36:35 -050061 face->onSendNack.connect([this, self] (const auto& nack) {
62 ndn::lp::Nack n(nack);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050063 for (auto& otherFace : m_faces) {
Nick Gordon0b3beab2018-03-02 13:03:28 -060064 if (self == &*otherFace) {
Ashlesh Gawande08784d42017-09-06 23:40:21 -050065 continue;
66 }
Davide Pesavento1af79492023-09-22 15:45:21 -040067 boost::asio::post(m_io, [=] { otherFace->receive(n); });
Ashlesh Gawande08784d42017-09-06 23:40:21 -050068 }
69 });
70
71 m_faces.push_back(face);
72 return *face;
73}
74
Nick Gordon0b3beab2018-03-02 13:03:28 -060075void
76DummyForwarder::removeFaces()
77{
78 m_faces.clear();
79}
80
Davide Pesavento30c41ec2024-02-12 17:36:35 -050081} // namespace chronosync::tests