blob: 735702ea7f9c91d931f07e066d2af6a212f8e0de [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -07002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070011 */
12
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070013#ifndef NDN_TESTS_TRANSPORT_DUMMY_FACE_HPP
14#define NDN_TESTS_TRANSPORT_DUMMY_FACE_HPP
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070015
16#include "face.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070017#include "transport/transport.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070018
19namespace ndn {
20
21class DummyTransport : public Transport
22{
23public:
24 void
25 receive(const Block& block)
26 {
27 m_receiveCallback(block);
28 }
29
30 virtual void
31 close()
32 {
33 }
34
35 virtual void
36 pause()
37 {
38 }
39
40 virtual void
41 resume()
42 {
43 }
44
45 virtual void
46 send(const Block& wire)
47 {
48 if (wire.type() == Tlv::Interest) {
49 m_sentInterests->push_back(Interest(wire));
50 }
51 else if (wire.type() == Tlv::Data) {
52 m_sentDatas->push_back(Data(wire));
53 }
54 }
55
56 virtual void
57 send(const Block& header, const Block& payload)
58 {
59 this->send(payload);
60 }
61
62public:
63 std::vector<Interest>* m_sentInterests;
64 std::vector<Data>* m_sentDatas;
65};
66
67
68/** \brief a Face for unit testing
69 */
70class DummyFace : public Face
71{
72public:
73 explicit
74 DummyFace(shared_ptr<DummyTransport> transport)
75 : Face(transport)
76 , m_transport(transport)
77 {
78 m_transport->m_sentInterests = &m_sentInterests;
79 m_transport->m_sentDatas = &m_sentDatas;
80 }
81
82 /** \brief cause the Face to receive a packet
83 */
84 template<typename Packet>
85 void
86 receive(const Packet& packet)
87 {
88 m_transport->receive(packet.wireEncode());
89 }
90
91public:
92 std::vector<Interest> m_sentInterests;
93 std::vector<Data> m_sentDatas;
94
95private:
96 shared_ptr<DummyTransport> m_transport;
97};
98
99inline shared_ptr<DummyFace>
100makeDummyFace()
101{
102 return make_shared<DummyFace>(make_shared<DummyTransport>());
103}
104
105} // namespace ndn
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700106
107#endif // NDN_TESTS_TRANSPORT_DUMMY_FACE_HPP