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