blob: fd724b021ec89681efdb118c9d51878203d38159 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070020 */
21
Alexander Afanasyev8b1674a2014-05-15 00:58:43 -070022#ifndef NDN_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP
23#define NDN_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070024
25#include "face.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070026#include "transport/transport.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070027
28namespace ndn {
29
30class DummyTransport : public Transport
31{
32public:
33 void
34 receive(const Block& block)
35 {
36 m_receiveCallback(block);
37 }
38
39 virtual void
40 close()
41 {
42 }
43
44 virtual void
45 pause()
46 {
47 }
48
49 virtual void
50 resume()
51 {
52 }
53
54 virtual void
55 send(const Block& wire)
56 {
57 if (wire.type() == Tlv::Interest) {
58 m_sentInterests->push_back(Interest(wire));
59 }
60 else if (wire.type() == Tlv::Data) {
61 m_sentDatas->push_back(Data(wire));
62 }
63 }
64
65 virtual void
66 send(const Block& header, const Block& payload)
67 {
68 this->send(payload);
69 }
70
71public:
72 std::vector<Interest>* m_sentInterests;
73 std::vector<Data>* m_sentDatas;
74};
75
76
77/** \brief a Face for unit testing
78 */
79class DummyFace : public Face
80{
81public:
82 explicit
83 DummyFace(shared_ptr<DummyTransport> transport)
84 : Face(transport)
85 , m_transport(transport)
86 {
87 m_transport->m_sentInterests = &m_sentInterests;
88 m_transport->m_sentDatas = &m_sentDatas;
89 }
90
91 /** \brief cause the Face to receive a packet
92 */
93 template<typename Packet>
94 void
95 receive(const Packet& packet)
96 {
97 m_transport->receive(packet.wireEncode());
98 }
99
100public:
101 std::vector<Interest> m_sentInterests;
102 std::vector<Data> m_sentDatas;
103
104private:
105 shared_ptr<DummyTransport> m_transport;
106};
107
108inline shared_ptr<DummyFace>
109makeDummyFace()
110{
111 return make_shared<DummyFace>(make_shared<DummyTransport>());
112}
113
114} // namespace ndn
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700115
Alexander Afanasyev8b1674a2014-05-15 00:58:43 -0700116#endif // NDN_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP