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