blob: 2e8781d37669c17d709cc57971a7d86ad59af120 [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
Junxiao Shi15b12e72014-08-09 19:56:24 -070026#ifndef NFD_TESTS_DUMMY_CLIENT_FACE_HPP
27#define NFD_TESTS_DUMMY_CLIENT_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
Junxiao Shi15b12e72014-08-09 19:56:24 -070035class DummyClientTransport : public ndn::Transport
Vince Lehman72446ec2014-07-09 10:50:02 -050036{
37public:
38 void
39 receive(const Block& block)
40 {
Junxiao Shi15b12e72014-08-09 19:56:24 -070041 if (static_cast<bool>(m_receiveCallback))
42 m_receiveCallback(block);
Vince Lehman72446ec2014-07-09 10:50:02 -050043 }
44
45 virtual void
46 close()
47 {
48 }
49
50 virtual void
51 pause()
52 {
53 }
54
55 virtual void
56 resume()
57 {
58 }
59
60 virtual void
61 send(const Block& wire)
62 {
Vince Lehman5144f822014-07-23 15:12:56 -070063 if (wire.type() == tlv::Interest) {
Vince Lehman72446ec2014-07-09 10:50:02 -050064 m_sentInterests->push_back(Interest(wire));
65 }
Vince Lehman5144f822014-07-23 15:12:56 -070066 else if (wire.type() == tlv::Data) {
Vince Lehman72446ec2014-07-09 10:50:02 -050067 m_sentDatas->push_back(Data(wire));
68 }
69 }
70
71 virtual void
72 send(const Block& header, const Block& payload)
73 {
74 this->send(payload);
75 }
76
77public:
78 std::vector<Interest>* m_sentInterests;
79 std::vector<Data>* m_sentDatas;
80};
81
82
Junxiao Shi15b12e72014-08-09 19:56:24 -070083/** \brief a client-side face for unit testing
Vince Lehman72446ec2014-07-09 10:50:02 -050084 */
Junxiao Shi15b12e72014-08-09 19:56:24 -070085class DummyClientFace : public ndn::Face
Vince Lehman72446ec2014-07-09 10:50:02 -050086{
87public:
88 explicit
Junxiao Shi15b12e72014-08-09 19:56:24 -070089 DummyClientFace(shared_ptr<DummyClientTransport> transport)
Vince Lehman72446ec2014-07-09 10:50:02 -050090 : Face(transport)
91 , m_transport(transport)
92 {
93 m_transport->m_sentInterests = &m_sentInterests;
94 m_transport->m_sentDatas = &m_sentDatas;
95 }
96
97 /** \brief cause the Face to receive a packet
98 */
99 template<typename Packet>
100 void
101 receive(const Packet& packet)
102 {
103 m_transport->receive(packet.wireEncode());
104 }
105
106public:
Junxiao Shi15b12e72014-08-09 19:56:24 -0700107 /** \brief sent Interests
108 * \note After .expressInterest, .processEvents must be called before
109 * the Interest would show up here.
110 */
Vince Lehman72446ec2014-07-09 10:50:02 -0500111 std::vector<Interest> m_sentInterests;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700112 /** \brief sent Datas
113 * \note After .put, .processEvents must be called before
114 * the Interest would show up here.
115 */
Vince Lehman72446ec2014-07-09 10:50:02 -0500116 std::vector<Data> m_sentDatas;
117
118private:
Junxiao Shi15b12e72014-08-09 19:56:24 -0700119 shared_ptr<DummyClientTransport> m_transport;
Vince Lehman72446ec2014-07-09 10:50:02 -0500120};
121
Junxiao Shi15b12e72014-08-09 19:56:24 -0700122inline shared_ptr<DummyClientFace>
123makeDummyClientFace()
Vince Lehman72446ec2014-07-09 10:50:02 -0500124{
Junxiao Shi15b12e72014-08-09 19:56:24 -0700125 return make_shared<DummyClientFace>(make_shared<DummyClientTransport>());
Vince Lehman72446ec2014-07-09 10:50:02 -0500126}
127
Vince Lehman5144f822014-07-23 15:12:56 -0700128} // namespace tests
129} // namespace nfd
Vince Lehman72446ec2014-07-09 10:50:02 -0500130
Junxiao Shi15b12e72014-08-09 19:56:24 -0700131#endif // NFD_TESTS_DUMMY_CLIENT_FACE_HPP