Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * 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. |
| 20 | */ |
| 21 | |
| 22 | #include "dummy-client-face.hpp" |
| 23 | #include "../transport/transport.hpp" |
| 24 | #include "../management/nfd-controller.hpp" |
| 25 | #include "../management/nfd-control-response.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace util { |
| 29 | |
| 30 | const DummyClientFace::Options DummyClientFace::DEFAULT_OPTIONS { true, false }; |
| 31 | |
| 32 | class DummyClientFace::Transport : public ndn::Transport |
| 33 | { |
| 34 | public: |
| 35 | void |
| 36 | receive(const Block& block) |
| 37 | { |
| 38 | if (static_cast<bool>(m_receiveCallback)) |
| 39 | m_receiveCallback(block); |
| 40 | } |
| 41 | |
| 42 | virtual void |
| 43 | close() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | virtual void |
| 48 | pause() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | virtual void |
| 53 | resume() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | virtual void |
| 58 | send(const Block& wire) |
| 59 | { |
| 60 | if (wire.type() == tlv::Interest) { |
| 61 | shared_ptr<Interest> interest = make_shared<Interest>(wire); |
| 62 | (*m_onInterest)(*interest); |
| 63 | } |
| 64 | else if (wire.type() == tlv::Data) { |
| 65 | shared_ptr<Data> data = make_shared<Data>(wire); |
| 66 | (*m_onData)(*data); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | virtual void |
| 71 | send(const Block& header, const Block& payload) |
| 72 | { |
| 73 | this->send(payload); |
| 74 | } |
| 75 | |
| 76 | boost::asio::io_service& |
| 77 | getIoService() |
| 78 | { |
| 79 | return *m_ioService; |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | friend class DummyClientFace; |
| 84 | EventEmitter<Interest>* m_onInterest; |
| 85 | EventEmitter<Data>* m_onData; |
| 86 | }; |
| 87 | |
| 88 | DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport) |
| 89 | : Face(transport) |
| 90 | , m_transport(transport) |
| 91 | { |
| 92 | this->construct(options); |
| 93 | } |
| 94 | |
| 95 | DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport, |
| 96 | boost::asio::io_service& ioService) |
| 97 | : Face(transport, ioService) |
| 98 | , m_transport(transport) |
| 99 | { |
| 100 | this->construct(options); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | DummyClientFace::construct(const Options& options) |
| 105 | { |
| 106 | m_transport->m_onInterest = &onInterest; |
| 107 | m_transport->m_onData = &onData; |
| 108 | |
| 109 | if (options.enablePacketLogging) |
| 110 | this->enablePacketLogging(); |
| 111 | |
| 112 | if (options.enableRegistrationReply) |
| 113 | this->enableRegistrationReply(); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | DummyClientFace::enablePacketLogging() |
| 118 | { |
| 119 | onInterest += [this] (const Interest& interest) { this->sentInterests.push_back(interest); }; |
| 120 | onData += [this] (const Data& data) { this->sentDatas.push_back(data); }; |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | DummyClientFace::enableRegistrationReply() |
| 125 | { |
| 126 | onInterest += [this] (const Interest& interest) { |
| 127 | static const Name localhostRegistration("/localhost/nfd/rib"); |
| 128 | if (!localhostRegistration.isPrefixOf(interest.getName())) |
| 129 | return; |
| 130 | |
| 131 | nfd::ControlParameters params(interest.getName().get(-5).blockFromValue()); |
| 132 | params.setFaceId(1); |
| 133 | params.setOrigin(0); |
| 134 | if (interest.getName().get(3) == name::Component("register")) { |
| 135 | params.setCost(0); |
| 136 | } |
| 137 | |
| 138 | nfd::ControlResponse resp; |
| 139 | resp.setCode(200); |
| 140 | resp.setBody(params.wireEncode()); |
| 141 | |
| 142 | shared_ptr<Data> data = make_shared<Data>(interest.getName()); |
| 143 | data->setContent(resp.wireEncode()); |
| 144 | |
| 145 | KeyChain keyChain; |
| 146 | keyChain.signWithSha256(*data); |
| 147 | |
| 148 | this->getIoService().post([this, data] { this->receive(*data); }); |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | template<typename Packet> |
| 153 | void |
| 154 | DummyClientFace::receive(const Packet& packet) |
| 155 | { |
| 156 | m_transport->receive(packet.wireEncode()); |
| 157 | } |
| 158 | |
| 159 | template void |
| 160 | DummyClientFace::receive<Interest>(const Interest& packet); |
| 161 | |
| 162 | template void |
| 163 | DummyClientFace::receive<Data>(const Data& packet); |
| 164 | |
| 165 | |
| 166 | shared_ptr<DummyClientFace> |
| 167 | makeDummyClientFace(const DummyClientFace::Options& options) |
| 168 | { |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame^] | 169 | // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 170 | return shared_ptr<DummyClientFace>( |
| 171 | new DummyClientFace(options, make_shared<DummyClientFace::Transport>())); |
| 172 | } |
| 173 | |
| 174 | shared_ptr<DummyClientFace> |
| 175 | makeDummyClientFace(boost::asio::io_service& ioService, |
| 176 | const DummyClientFace::Options& options) |
| 177 | { |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame^] | 178 | // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 179 | return shared_ptr<DummyClientFace>( |
| 180 | new DummyClientFace(options, make_shared<DummyClientFace::Transport>(), |
| 181 | ref(ioService))); |
| 182 | } |
| 183 | |
| 184 | } // namespace util |
| 185 | } // namespace ndn |