Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2ed9e07 | 2017-08-13 16:45:48 +0000 | [diff] [blame] | 2 | /* |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 23 | #include "../detail/lp-field-tag.hpp" |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 24 | #include "../lp/packet.hpp" |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 25 | #include "../lp/tags.hpp" |
| 26 | #include "../mgmt/nfd/controller.hpp" |
| 27 | #include "../mgmt/nfd/control-response.hpp" |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 28 | #include "../transport/transport.hpp" |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 30 | #include <boost/asio/io_service.hpp> |
| 31 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace util { |
| 34 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 47 | 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 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 52 | 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 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 57 | 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 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 62 | 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 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 68 | 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 | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 90 | , m_internalKeyChain(new KeyChain) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 91 | , m_keyChain(*m_internalKeyChain) |
| 92 | { |
| 93 | this->construct(options); |
| 94 | } |
| 95 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 96 | DummyClientFace::DummyClientFace(KeyChain& keyChain, |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 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 | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 107 | , m_internalKeyChain(new KeyChain) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 108 | , m_keyChain(*m_internalKeyChain) |
| 109 | { |
| 110 | this->construct(options); |
| 111 | } |
| 112 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 113 | DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 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>()); |
Junxiao Shi | b38e6d4 | 2017-08-16 16:15:28 +0000 | [diff] [blame^] | 138 | addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*nack, lpPacket); |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 139 | onSendNack(*nack); |
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 | else { |
Junxiao Shi | b38e6d4 | 2017-08-16 16:15:28 +0000 | [diff] [blame^] | 142 | addTagFromField<lp::NextHopFaceIdTag, lp::NextHopFaceIdField>(*interest, lpPacket); |
| 143 | addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*interest, lpPacket); |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 144 | onSendInterest(*interest); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 145 | } |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 146 | } |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 147 | else if (block.type() == tlv::Data) { |
| 148 | shared_ptr<Data> data = make_shared<Data>(block); |
Junxiao Shi | b38e6d4 | 2017-08-16 16:15:28 +0000 | [diff] [blame^] | 149 | addTagFromField<lp::CachePolicyTag, lp::CachePolicyField>(*data, lpPacket); |
| 150 | addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*data, lpPacket); |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 151 | onSendData(*data); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 152 | } |
| 153 | }); |
| 154 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 155 | if (options.enablePacketLogging) |
| 156 | this->enablePacketLogging(); |
| 157 | |
| 158 | if (options.enableRegistrationReply) |
| 159 | this->enableRegistrationReply(); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 160 | |
| 161 | m_processEventsOverride = options.processEventsOverride; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void |
| 165 | DummyClientFace::enablePacketLogging() |
| 166 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 167 | onSendInterest.connect([this] (const Interest& interest) { |
| 168 | this->sentInterests.push_back(interest); |
| 169 | }); |
| 170 | onSendData.connect([this] (const Data& data) { |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 171 | this->sentData.push_back(data); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 172 | }); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 173 | onSendNack.connect([this] (const lp::Nack& nack) { |
| 174 | this->sentNacks.push_back(nack); |
| 175 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void |
| 179 | DummyClientFace::enableRegistrationReply() |
| 180 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 181 | onSendInterest.connect([this] (const Interest& interest) { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 182 | static const Name localhostRegistration("/localhost/nfd/rib"); |
| 183 | if (!localhostRegistration.isPrefixOf(interest.getName())) |
| 184 | return; |
| 185 | |
| 186 | nfd::ControlParameters params(interest.getName().get(-5).blockFromValue()); |
| 187 | params.setFaceId(1); |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 188 | params.setOrigin(nfd::ROUTE_ORIGIN_APP); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 189 | if (interest.getName().get(3) == name::Component("register")) { |
| 190 | params.setCost(0); |
| 191 | } |
| 192 | |
| 193 | nfd::ControlResponse resp; |
| 194 | resp.setCode(200); |
| 195 | resp.setBody(params.wireEncode()); |
| 196 | |
| 197 | shared_ptr<Data> data = make_shared<Data>(interest.getName()); |
| 198 | data->setContent(resp.wireEncode()); |
| 199 | |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 200 | m_keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256)); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 201 | |
| 202 | this->getIoService().post([this, data] { this->receive(*data); }); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 203 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 206 | void |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 207 | DummyClientFace::receive(const Interest& interest) |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 208 | { |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 209 | lp::Packet lpPacket(interest.wireEncode()); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 210 | |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 211 | addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, interest); |
| 212 | addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest); |
| 213 | addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 214 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 215 | static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode()); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 218 | void |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 219 | DummyClientFace::receive(const Data& data) |
| 220 | { |
| 221 | lp::Packet lpPacket(data.wireEncode()); |
| 222 | |
| 223 | addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, data); |
| 224 | addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data); |
| 225 | |
| 226 | static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode()); |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | DummyClientFace::receive(const lp::Nack& nack) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 231 | { |
| 232 | lp::Packet lpPacket; |
| 233 | lpPacket.add<lp::NackField>(nack.getHeader()); |
| 234 | Block interest = nack.getInterest().wireEncode(); |
| 235 | lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end())); |
| 236 | |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 237 | addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack); |
| 238 | addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 239 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 240 | static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode()); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 241 | } |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 242 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 243 | void |
Junxiao Shi | 2ed9e07 | 2017-08-13 16:45:48 +0000 | [diff] [blame] | 244 | DummyClientFace::doProcessEvents(time::milliseconds timeout, bool keepThread) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 245 | { |
| 246 | if (m_processEventsOverride != nullptr) { |
| 247 | m_processEventsOverride(timeout); |
| 248 | } |
| 249 | else { |
| 250 | this->Face::doProcessEvents(timeout, keepThread); |
| 251 | } |
| 252 | } |
| 253 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 254 | } // namespace util |
| 255 | } // namespace ndn |