blob: 58b4e81acdc260a88331b0679aee1da7b4c168d6 [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"
17
18namespace ndn {
19
20class DummyTransport : public Transport
21{
22public:
23 void
24 receive(const Block& block)
25 {
26 m_receiveCallback(block);
27 }
28
29 virtual void
30 close()
31 {
32 }
33
34 virtual void
35 pause()
36 {
37 }
38
39 virtual void
40 resume()
41 {
42 }
43
44 virtual void
45 send(const Block& wire)
46 {
47 if (wire.type() == Tlv::Interest) {
48 m_sentInterests->push_back(Interest(wire));
49 }
50 else if (wire.type() == Tlv::Data) {
51 m_sentDatas->push_back(Data(wire));
52 }
53 }
54
55 virtual void
56 send(const Block& header, const Block& payload)
57 {
58 this->send(payload);
59 }
60
61public:
62 std::vector<Interest>* m_sentInterests;
63 std::vector<Data>* m_sentDatas;
64};
65
66
67/** \brief a Face for unit testing
68 */
69class DummyFace : public Face
70{
71public:
72 explicit
73 DummyFace(shared_ptr<DummyTransport> transport)
74 : Face(transport)
75 , m_transport(transport)
76 {
77 m_transport->m_sentInterests = &m_sentInterests;
78 m_transport->m_sentDatas = &m_sentDatas;
79 }
80
81 /** \brief cause the Face to receive a packet
82 */
83 template<typename Packet>
84 void
85 receive(const Packet& packet)
86 {
87 m_transport->receive(packet.wireEncode());
88 }
89
90public:
91 std::vector<Interest> m_sentInterests;
92 std::vector<Data> m_sentDatas;
93
94private:
95 shared_ptr<DummyTransport> m_transport;
96};
97
98inline shared_ptr<DummyFace>
99makeDummyFace()
100{
101 return make_shared<DummyFace>(make_shared<DummyTransport>());
102}
103
104} // namespace ndn
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700105
106#endif // NDN_TESTS_TRANSPORT_DUMMY_FACE_HPP