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