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