Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 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 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 60 | onSendBlock(wire); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | virtual void |
| 64 | send(const Block& header, const Block& payload) |
| 65 | { |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 66 | EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size()); |
| 67 | encoder.appendByteArray(header.wire(), header.size()); |
| 68 | encoder.appendByteArray(payload.wire(), payload.size()); |
| 69 | |
| 70 | this->send(encoder.block()); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | boost::asio::io_service& |
| 74 | getIoService() |
| 75 | { |
| 76 | return *m_ioService; |
| 77 | } |
| 78 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 79 | public: |
| 80 | Signal<Transport, Block> onSendBlock; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport) |
| 84 | : Face(transport) |
| 85 | , m_transport(transport) |
| 86 | { |
| 87 | this->construct(options); |
| 88 | } |
| 89 | |
| 90 | DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport, |
| 91 | boost::asio::io_service& ioService) |
| 92 | : Face(transport, ioService) |
| 93 | , m_transport(transport) |
| 94 | { |
| 95 | this->construct(options); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | DummyClientFace::construct(const Options& options) |
| 100 | { |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 101 | m_transport->onSendBlock.connect([this] (const Block& blockFromDaemon) { |
| 102 | const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon); |
| 103 | |
| 104 | if (block.type() == tlv::Interest) { |
| 105 | shared_ptr<Interest> interest = make_shared<Interest>(block); |
| 106 | if (&block != &blockFromDaemon) |
| 107 | interest->getLocalControlHeader().wireDecode(blockFromDaemon); |
| 108 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 109 | onSendInterest(*interest); |
| 110 | } |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 111 | else if (block.type() == tlv::Data) { |
| 112 | shared_ptr<Data> data = make_shared<Data>(block); |
| 113 | if (&block != &blockFromDaemon) |
| 114 | data->getLocalControlHeader().wireDecode(blockFromDaemon); |
| 115 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 116 | onSendData(*data); |
| 117 | } |
| 118 | }); |
| 119 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 120 | if (options.enablePacketLogging) |
| 121 | this->enablePacketLogging(); |
| 122 | |
| 123 | if (options.enableRegistrationReply) |
| 124 | this->enableRegistrationReply(); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | DummyClientFace::enablePacketLogging() |
| 129 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 130 | onSendInterest.connect([this] (const Interest& interest) { |
| 131 | this->sentInterests.push_back(interest); |
| 132 | }); |
| 133 | onSendData.connect([this] (const Data& data) { |
| 134 | this->sentDatas.push_back(data); |
| 135 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void |
| 139 | DummyClientFace::enableRegistrationReply() |
| 140 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 141 | onSendInterest.connect([this] (const Interest& interest) { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 142 | static const Name localhostRegistration("/localhost/nfd/rib"); |
| 143 | if (!localhostRegistration.isPrefixOf(interest.getName())) |
| 144 | return; |
| 145 | |
| 146 | nfd::ControlParameters params(interest.getName().get(-5).blockFromValue()); |
| 147 | params.setFaceId(1); |
| 148 | params.setOrigin(0); |
| 149 | if (interest.getName().get(3) == name::Component("register")) { |
| 150 | params.setCost(0); |
| 151 | } |
| 152 | |
| 153 | nfd::ControlResponse resp; |
| 154 | resp.setCode(200); |
| 155 | resp.setBody(params.wireEncode()); |
| 156 | |
| 157 | shared_ptr<Data> data = make_shared<Data>(interest.getName()); |
| 158 | data->setContent(resp.wireEncode()); |
| 159 | |
| 160 | KeyChain keyChain; |
| 161 | keyChain.signWithSha256(*data); |
| 162 | |
| 163 | this->getIoService().post([this, data] { this->receive(*data); }); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 164 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | template<typename Packet> |
| 168 | void |
| 169 | DummyClientFace::receive(const Packet& packet) |
| 170 | { |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 171 | // do not restrict what injected control header can contain |
Jiewen Tan | c759a20 | 2015-01-29 23:31:09 -0800 | [diff] [blame] | 172 | if (!packet.getLocalControlHeader().empty(nfd::LocalControlHeader::ENCODE_ALL)) { |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 173 | |
Jiewen Tan | c759a20 | 2015-01-29 23:31:09 -0800 | [diff] [blame] | 174 | Block header = packet.getLocalControlHeader().wireEncode(packet, |
| 175 | nfd::LocalControlHeader::ENCODE_ALL); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 176 | Block payload = packet.wireEncode(); |
| 177 | |
| 178 | EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size()); |
| 179 | encoder.appendByteArray(header.wire(), header.size()); |
| 180 | encoder.appendByteArray(payload.wire(), payload.size()); |
| 181 | |
| 182 | m_transport->receive(encoder.block()); |
| 183 | } |
| 184 | else { |
| 185 | m_transport->receive(packet.wireEncode()); |
| 186 | } |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | template void |
| 190 | DummyClientFace::receive<Interest>(const Interest& packet); |
| 191 | |
| 192 | template void |
| 193 | DummyClientFace::receive<Data>(const Data& packet); |
| 194 | |
| 195 | |
| 196 | shared_ptr<DummyClientFace> |
| 197 | makeDummyClientFace(const DummyClientFace::Options& options) |
| 198 | { |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 199 | // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 200 | return shared_ptr<DummyClientFace>( |
| 201 | new DummyClientFace(options, make_shared<DummyClientFace::Transport>())); |
| 202 | } |
| 203 | |
| 204 | shared_ptr<DummyClientFace> |
| 205 | makeDummyClientFace(boost::asio::io_service& ioService, |
| 206 | const DummyClientFace::Options& options) |
| 207 | { |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 208 | // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 209 | return shared_ptr<DummyClientFace>( |
| 210 | new DummyClientFace(options, make_shared<DummyClientFace::Transport>(), |
| 211 | ref(ioService))); |
| 212 | } |
| 213 | |
| 214 | } // namespace util |
| 215 | } // namespace ndn |