blob: b7448820ebf374887961826d41d12c2091b120eb [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
Alexander Afanasyev370d2602014-08-20 10:21:34 -050093 DummyClientFace(shared_ptr<DummyClientTransport> transport, boost::asio::io_service& ioService)
94 : Face(transport, ioService)
95 , m_transport(transport)
96 {
97 m_transport->m_sentInterests = &m_sentInterests;
98 m_transport->m_sentDatas = &m_sentDatas;
99 }
100
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700101 /** \brief cause the Face to receive a packet
102 */
103 template<typename Packet>
104 void
105 receive(const Packet& packet)
106 {
107 m_transport->receive(packet.wireEncode());
108 }
109
110public:
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700111 /** \brief sent Interests
112 * \note After .expressInterest, .processEvents must be called before
113 * the Interest would show up here.
114 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700115 std::vector<Interest> m_sentInterests;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700116 /** \brief sent Datas
117 * \note After .put, .processEvents must be called before
118 * the Interest would show up here.
119 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700120 std::vector<Data> m_sentDatas;
121
122private:
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700123 shared_ptr<DummyClientTransport> m_transport;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700124};
125
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700126inline shared_ptr<DummyClientFace>
127makeDummyClientFace()
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700128{
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700129 return make_shared<DummyClientFace>(make_shared<DummyClientTransport>());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700130}
131
Alexander Afanasyev370d2602014-08-20 10:21:34 -0500132inline shared_ptr<DummyClientFace>
133makeDummyClientFace(boost::asio::io_service& ioService)
134{
135 return make_shared<DummyClientFace>(make_shared<DummyClientTransport>(), ref(ioService));
136}
137
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700138} // namespace tests
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700139} // namespace ndn
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700140
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700141#endif // NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP