blob: d8789bd0473704cf6c182f7ba7d8a029684b8884 [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 Afanasyev4abdbf12014-08-11 12:48:54 -070022#ifndef NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP
23#define NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_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 {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070029namespace tests {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070030
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070031class DummyClientTransport : public ndn::Transport
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070032{
33public:
34 void
35 receive(const Block& block)
36 {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070037 if (static_cast<bool>(m_receiveCallback))
38 m_receiveCallback(block);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070039 }
40
41 virtual void
42 close()
43 {
44 }
45
46 virtual void
47 pause()
48 {
49 }
50
51 virtual void
52 resume()
53 {
54 }
55
56 virtual void
57 send(const Block& wire)
58 {
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060059 if (wire.type() == tlv::Interest) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070060 m_sentInterests->push_back(Interest(wire));
61 }
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060062 else if (wire.type() == tlv::Data) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070063 m_sentDatas->push_back(Data(wire));
64 }
65 }
66
67 virtual void
68 send(const Block& header, const Block& payload)
69 {
70 this->send(payload);
71 }
72
73public:
74 std::vector<Interest>* m_sentInterests;
75 std::vector<Data>* m_sentDatas;
76};
77
78
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070079/** \brief a client-side face for unit testing
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070080 */
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070081class DummyClientFace : public ndn::Face
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070082{
83public:
84 explicit
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070085 DummyClientFace(shared_ptr<DummyClientTransport> transport)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070086 : Face(transport)
87 , m_transport(transport)
88 {
89 m_transport->m_sentInterests = &m_sentInterests;
90 m_transport->m_sentDatas = &m_sentDatas;
91 }
92
93 /** \brief cause the Face to receive a packet
94 */
95 template<typename Packet>
96 void
97 receive(const Packet& packet)
98 {
99 m_transport->receive(packet.wireEncode());
100 }
101
102public:
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700103 /** \brief sent Interests
104 * \note After .expressInterest, .processEvents must be called before
105 * the Interest would show up here.
106 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700107 std::vector<Interest> m_sentInterests;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700108 /** \brief sent Datas
109 * \note After .put, .processEvents must be called before
110 * the Interest would show up here.
111 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700112 std::vector<Data> m_sentDatas;
113
114private:
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700115 shared_ptr<DummyClientTransport> m_transport;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700116};
117
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700118inline shared_ptr<DummyClientFace>
119makeDummyClientFace()
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700120{
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700121 return make_shared<DummyClientFace>(make_shared<DummyClientTransport>());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700122}
123
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700124} // namespace tests
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700125} // namespace ndn
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700126
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700127#endif // NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP