Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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" |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 23 | #include "../lp/tags.hpp" |
| 24 | #include "../mgmt/nfd/controller.hpp" |
| 25 | #include "../mgmt/nfd/control-response.hpp" |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 26 | #include "../transport/transport.hpp" |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 28 | #include <boost/asio/io_service.hpp> |
| 29 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace util { |
| 32 | |
Junxiao Shi | a1478db | 2016-09-09 04:13:15 +0000 | [diff] [blame^] | 33 | const DummyClientFace::Options DummyClientFace::DEFAULT_OPTIONS{true, false}; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 34 | |
| 35 | class DummyClientFace::Transport : public ndn::Transport |
| 36 | { |
| 37 | public: |
| 38 | void |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 39 | receive(Block block) const |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 40 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 41 | block.encode(); |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 42 | if (m_receiveCallback) { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 43 | m_receiveCallback(block); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 44 | } |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | virtual void |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 48 | close() override |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
| 52 | virtual void |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 53 | pause() override |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
| 57 | virtual void |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 58 | resume() override |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | virtual void |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 63 | send(const Block& wire) override |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 64 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 65 | onSendBlock(wire); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | virtual void |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 69 | send(const Block& header, const Block& payload) override |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 70 | { |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 71 | EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size()); |
| 72 | encoder.appendByteArray(header.wire(), header.size()); |
| 73 | encoder.appendByteArray(payload.wire(), payload.size()); |
| 74 | |
| 75 | this->send(encoder.block()); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | boost::asio::io_service& |
| 79 | getIoService() |
| 80 | { |
| 81 | return *m_ioService; |
| 82 | } |
| 83 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 84 | public: |
| 85 | Signal<Transport, Block> onSendBlock; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 88 | DummyClientFace::DummyClientFace(const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/) |
| 89 | : Face(make_shared<DummyClientFace::Transport>()) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 90 | , m_internalKeyChain(new KeyChain) |
| 91 | , m_keyChain(*m_internalKeyChain) |
| 92 | { |
| 93 | this->construct(options); |
| 94 | } |
| 95 | |
| 96 | DummyClientFace::DummyClientFace(KeyChain& keyChain, |
| 97 | const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/) |
| 98 | : Face(make_shared<DummyClientFace::Transport>(), keyChain) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 99 | , m_keyChain(keyChain) |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 100 | { |
| 101 | this->construct(options); |
| 102 | } |
| 103 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 104 | DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, |
| 105 | const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/) |
| 106 | : Face(make_shared<DummyClientFace::Transport>(), ioService) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 107 | , m_internalKeyChain(new KeyChain) |
| 108 | , m_keyChain(*m_internalKeyChain) |
| 109 | { |
| 110 | this->construct(options); |
| 111 | } |
| 112 | |
| 113 | DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, |
| 114 | const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/) |
| 115 | : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 116 | , m_keyChain(keyChain) |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 117 | { |
| 118 | this->construct(options); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | DummyClientFace::construct(const Options& options) |
| 123 | { |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 124 | static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) { |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 125 | Block packet(blockFromDaemon); |
| 126 | packet.encode(); |
| 127 | lp::Packet lpPacket(packet); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 128 | |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 129 | Buffer::const_iterator begin, end; |
| 130 | std::tie(begin, end) = lpPacket.get<lp::FragmentField>(); |
| 131 | Block block(&*begin, std::distance(begin, end)); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 132 | |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 133 | if (block.type() == tlv::Interest) { |
| 134 | shared_ptr<Interest> interest = make_shared<Interest>(block); |
| 135 | if (lpPacket.has<lp::NackField>()) { |
| 136 | shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest)); |
| 137 | nack->setHeader(lpPacket.get<lp::NackField>()); |
| 138 | if (lpPacket.has<lp::NextHopFaceIdField>()) { |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 139 | nack->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>())); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 140 | } |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 141 | onSendNack(*nack); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 142 | } |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 143 | else { |
| 144 | if (lpPacket.has<lp::NextHopFaceIdField>()) { |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 145 | interest->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>())); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 146 | } |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 147 | onSendInterest(*interest); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 148 | } |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 149 | } |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 150 | else if (block.type() == tlv::Data) { |
| 151 | shared_ptr<Data> data = make_shared<Data>(block); |
| 152 | |
| 153 | if (lpPacket.has<lp::CachePolicyField>()) { |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 154 | data->setTag(make_shared<lp::CachePolicyTag>(lpPacket.get<lp::CachePolicyField>())); |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | onSendData(*data); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 158 | } |
| 159 | }); |
| 160 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 161 | if (options.enablePacketLogging) |
| 162 | this->enablePacketLogging(); |
| 163 | |
| 164 | if (options.enableRegistrationReply) |
| 165 | this->enableRegistrationReply(); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | DummyClientFace::enablePacketLogging() |
| 170 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 171 | onSendInterest.connect([this] (const Interest& interest) { |
| 172 | this->sentInterests.push_back(interest); |
| 173 | }); |
| 174 | onSendData.connect([this] (const Data& data) { |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 175 | this->sentData.push_back(data); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 176 | }); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 177 | onSendNack.connect([this] (const lp::Nack& nack) { |
| 178 | this->sentNacks.push_back(nack); |
| 179 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void |
| 183 | DummyClientFace::enableRegistrationReply() |
| 184 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 185 | onSendInterest.connect([this] (const Interest& interest) { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 186 | static const Name localhostRegistration("/localhost/nfd/rib"); |
| 187 | if (!localhostRegistration.isPrefixOf(interest.getName())) |
| 188 | return; |
| 189 | |
| 190 | nfd::ControlParameters params(interest.getName().get(-5).blockFromValue()); |
| 191 | params.setFaceId(1); |
| 192 | params.setOrigin(0); |
| 193 | if (interest.getName().get(3) == name::Component("register")) { |
| 194 | params.setCost(0); |
| 195 | } |
| 196 | |
| 197 | nfd::ControlResponse resp; |
| 198 | resp.setCode(200); |
| 199 | resp.setBody(params.wireEncode()); |
| 200 | |
| 201 | shared_ptr<Data> data = make_shared<Data>(interest.getName()); |
| 202 | data->setContent(resp.wireEncode()); |
| 203 | |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 204 | m_keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256)); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 205 | |
| 206 | this->getIoService().post([this, data] { this->receive(*data); }); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 207 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | template<typename Packet> |
| 211 | void |
| 212 | DummyClientFace::receive(const Packet& packet) |
| 213 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 214 | lp::Packet lpPacket(packet.wireEncode()); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 215 | |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 216 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = |
| 217 | static_cast<const TagHost&>(packet).getTag<lp::IncomingFaceIdTag>(); |
| 218 | if (incomingFaceIdTag != nullptr) { |
| 219 | lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 220 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 221 | |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 222 | shared_ptr<lp::NextHopFaceIdTag> nextHopFaceIdTag = |
| 223 | static_cast<const TagHost&>(packet).getTag<lp::NextHopFaceIdTag>(); |
| 224 | if (nextHopFaceIdTag != nullptr) { |
| 225 | lpPacket.add<lp::NextHopFaceIdField>(*nextHopFaceIdTag); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 226 | } |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 227 | static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode()); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | template void |
| 231 | DummyClientFace::receive<Interest>(const Interest& packet); |
| 232 | |
| 233 | template void |
| 234 | DummyClientFace::receive<Data>(const Data& packet); |
| 235 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 236 | template<> |
| 237 | void |
| 238 | DummyClientFace::receive<lp::Nack>(const lp::Nack& nack) |
| 239 | { |
| 240 | lp::Packet lpPacket; |
| 241 | lpPacket.add<lp::NackField>(nack.getHeader()); |
| 242 | Block interest = nack.getInterest().wireEncode(); |
| 243 | lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end())); |
| 244 | |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 245 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = nack.getTag<lp::IncomingFaceIdTag>(); |
| 246 | if (incomingFaceIdTag != nullptr) { |
| 247 | lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 250 | static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode()); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 251 | } |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 252 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 253 | } // namespace util |
| 254 | } // namespace ndn |