blob: 84da29f60427065fad50c4251199fe026089f375 [file] [log] [blame]
Vince Lehman72446ec2014-07-09 10:50:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California,
4 * 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
Vince Lehman5144f822014-07-23 15:12:56 -070026#ifndef NFD_TESTS_DUMMY_FACE_HPP
27#define NFD_TESTS_DUMMY_FACE_HPP
Vince Lehman72446ec2014-07-09 10:50:02 -050028
29#include <ndn-cxx/face.hpp>
30#include <ndn-cxx/transport/transport.hpp>
31
Vince Lehman5144f822014-07-23 15:12:56 -070032namespace nfd {
33namespace tests {
Vince Lehman72446ec2014-07-09 10:50:02 -050034
Vince Lehman5144f822014-07-23 15:12:56 -070035class DummyTransport : public ndn::Transport
Vince Lehman72446ec2014-07-09 10:50:02 -050036{
37public:
38 void
39 receive(const Block& block)
40 {
41 m_receiveCallback(block);
42 }
43
44 virtual void
45 close()
46 {
47 }
48
49 virtual void
50 pause()
51 {
52 }
53
54 virtual void
55 resume()
56 {
57 }
58
59 virtual void
60 send(const Block& wire)
61 {
Vince Lehman5144f822014-07-23 15:12:56 -070062 if (wire.type() == tlv::Interest) {
Vince Lehman72446ec2014-07-09 10:50:02 -050063 m_sentInterests->push_back(Interest(wire));
64 }
Vince Lehman5144f822014-07-23 15:12:56 -070065 else if (wire.type() == tlv::Data) {
Vince Lehman72446ec2014-07-09 10:50:02 -050066 m_sentDatas->push_back(Data(wire));
67 }
68 }
69
70 virtual void
71 send(const Block& header, const Block& payload)
72 {
73 this->send(payload);
74 }
75
76public:
77 std::vector<Interest>* m_sentInterests;
78 std::vector<Data>* m_sentDatas;
79};
80
81
82/** \brief a Face for unit testing
83 */
Vince Lehman5144f822014-07-23 15:12:56 -070084class DummyFace : public ndn::Face
Vince Lehman72446ec2014-07-09 10:50:02 -050085{
86public:
87 explicit
88 DummyFace(shared_ptr<DummyTransport> transport)
89 : Face(transport)
90 , m_transport(transport)
91 {
92 m_transport->m_sentInterests = &m_sentInterests;
93 m_transport->m_sentDatas = &m_sentDatas;
94 }
95
96 /** \brief cause the Face to receive a packet
97 */
98 template<typename Packet>
99 void
100 receive(const Packet& packet)
101 {
102 m_transport->receive(packet.wireEncode());
103 }
104
105public:
106 std::vector<Interest> m_sentInterests;
107 std::vector<Data> m_sentDatas;
108
109private:
110 shared_ptr<DummyTransport> m_transport;
111};
112
113inline shared_ptr<DummyFace>
114makeDummyFace()
115{
116 return make_shared<DummyFace>(make_shared<DummyTransport>());
117}
118
Vince Lehman5144f822014-07-23 15:12:56 -0700119} // namespace tests
120} // namespace nfd
Vince Lehman72446ec2014-07-09 10:50:02 -0500121
Vince Lehman5144f822014-07-23 15:12:56 -0700122#endif // NFD_TESTS_DUMMY_FACE_HPP