blob: 1594df8bdfdf31197bde4fa32131fb304c258537 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryb49313d2017-12-24 20:22:27 -07002/*
Davide Pesavento16916ae2019-03-29 23:53:26 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#ifndef NFD_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP
27#define NFD_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP
28
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/common.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070030
Davide Pesavento16916ae2019-03-29 23:53:26 -040031#include "face/null-transport.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070032
33namespace nfd {
34namespace face {
35namespace tests {
36
Davide Pesavento16916ae2019-03-29 23:53:26 -040037/** \brief Dummy Transport type used in unit tests.
38 *
39 * All packets sent through this transport are stored in `sentPackets`.
40 * Reception of a packet can be simulated by invoking `receivePacket()`.
41 * All persistency changes are recorded in `persistencyHistory`.
Eric Newberrya98bf932015-09-21 00:58:47 -070042 */
Davide Pesavento16916ae2019-03-29 23:53:26 -040043template<bool CAN_CHANGE_PERSISTENCY>
44class DummyTransportBase : public NullTransport
Eric Newberrya98bf932015-09-21 00:58:47 -070045{
46public:
Davide Pesavento16916ae2019-03-29 23:53:26 -040047 explicit
48 DummyTransportBase(const std::string& localUri = "dummy://",
49 const std::string& remoteUri = "dummy://",
50 ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_NON_LOCAL,
51 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
52 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT,
53 ssize_t mtu = MTU_UNLIMITED,
54 ssize_t sendQueueCapacity = QUEUE_UNSUPPORTED)
55 : NullTransport(FaceUri(localUri), FaceUri(remoteUri), scope, persistency)
Eric Newberrya98bf932015-09-21 00:58:47 -070056 {
Eric Newberrya98bf932015-09-21 00:58:47 -070057 this->setLinkType(linkType);
Junxiao Shi57df2882015-11-11 06:12:35 -070058 this->setMtu(mtu);
Eric Newberryb49313d2017-12-24 20:22:27 -070059 this->setSendQueueCapacity(sendQueueCapacity);
Eric Newberrya98bf932015-09-21 00:58:47 -070060 }
61
Davide Pesavento16916ae2019-03-29 23:53:26 -040062 using NullTransport::setMtu;
63 using NullTransport::setState;
Eric Newberrya98bf932015-09-21 00:58:47 -070064
Eric Newberryb49313d2017-12-24 20:22:27 -070065 ssize_t
66 getSendQueueLength() override
67 {
68 return m_sendQueueLength;
69 }
70
71 void
72 setSendQueueLength(ssize_t sendQueueLength)
73 {
74 m_sendQueueLength = sendQueueLength;
75 }
76
Eric Newberrya98bf932015-09-21 00:58:47 -070077 void
Eric Newberrya98bf932015-09-21 00:58:47 -070078 receivePacket(Block block)
79 {
Davide Pesavento16916ae2019-03-29 23:53:26 -040080 receive(Packet(std::move(block)));
Eric Newberrya98bf932015-09-21 00:58:47 -070081 }
82
Davide Pesavento8728a252015-11-06 04:01:22 +010083protected:
Yanbiao Li32dab972016-11-27 12:26:09 +080084 bool
Davide Pesavento16916ae2019-03-29 23:53:26 -040085 canChangePersistencyToImpl(ndn::nfd::FacePersistency) const override
Davide Pesavento8728a252015-11-06 04:01:22 +010086 {
Davide Pesavento16916ae2019-03-29 23:53:26 -040087 return CAN_CHANGE_PERSISTENCY;
88 }
89
90 void
91 afterChangePersistency(ndn::nfd::FacePersistency old) override
92 {
93 persistencyHistory.push_back(old);
Davide Pesavento8728a252015-11-06 04:01:22 +010094 }
95
Eric Newberrya98bf932015-09-21 00:58:47 -070096private:
Eric Newberryb49313d2017-12-24 20:22:27 -070097 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020098 doSend(Packet&& packet) override
Eric Newberrya98bf932015-09-21 00:58:47 -070099 {
100 sentPackets.push_back(std::move(packet));
101 }
102
103public:
Davide Pesavento16916ae2019-03-29 23:53:26 -0400104 std::vector<ndn::nfd::FacePersistency> persistencyHistory;
Eric Newberrya98bf932015-09-21 00:58:47 -0700105 std::vector<Packet> sentPackets;
Eric Newberryb49313d2017-12-24 20:22:27 -0700106
107private:
Davide Pesavento16916ae2019-03-29 23:53:26 -0400108 ssize_t m_sendQueueLength = 0;
Eric Newberrya98bf932015-09-21 00:58:47 -0700109};
110
Davide Pesavento16916ae2019-03-29 23:53:26 -0400111using DummyTransport = DummyTransportBase<true>;
112
Eric Newberrya98bf932015-09-21 00:58:47 -0700113} // namespace tests
114} // namespace face
115} // namespace nfd
116
117#endif // NFD_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP