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 |
| 36 | receive(const Block& block) |
| 37 | { |
| 38 | if (static_cast<bool>(m_receiveCallback)) |
| 39 | m_receiveCallback(block); |
| 40 | } |
| 41 | |
| 42 | virtual void |
| 43 | close() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | virtual void |
| 48 | pause() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | virtual void |
| 53 | resume() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | virtual void |
| 58 | send(const Block& wire) |
| 59 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 60 | onSendBlock(wire); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | virtual void |
| 64 | send(const Block& header, const Block& payload) |
| 65 | { |
| 66 | this->send(payload); |
| 67 | } |
| 68 | |
| 69 | boost::asio::io_service& |
| 70 | getIoService() |
| 71 | { |
| 72 | return *m_ioService; |
| 73 | } |
| 74 | |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 75 | public: |
| 76 | Signal<Transport, Block> onSendBlock; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport) |
| 80 | : Face(transport) |
| 81 | , m_transport(transport) |
| 82 | { |
| 83 | this->construct(options); |
| 84 | } |
| 85 | |
| 86 | DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport, |
| 87 | boost::asio::io_service& ioService) |
| 88 | : Face(transport, ioService) |
| 89 | , m_transport(transport) |
| 90 | { |
| 91 | this->construct(options); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | DummyClientFace::construct(const Options& options) |
| 96 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 97 | m_transport->onSendBlock.connect([this] (const Block& wire) { |
| 98 | if (wire.type() == tlv::Interest) { |
| 99 | shared_ptr<Interest> interest = make_shared<Interest>(wire); |
| 100 | onSendInterest(*interest); |
| 101 | } |
| 102 | else if (wire.type() == tlv::Data) { |
| 103 | shared_ptr<Data> data = make_shared<Data>(wire); |
| 104 | onSendData(*data); |
| 105 | } |
| 106 | }); |
| 107 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 108 | if (options.enablePacketLogging) |
| 109 | this->enablePacketLogging(); |
| 110 | |
| 111 | if (options.enableRegistrationReply) |
| 112 | this->enableRegistrationReply(); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | DummyClientFace::enablePacketLogging() |
| 117 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 118 | onSendInterest.connect([this] (const Interest& interest) { |
| 119 | this->sentInterests.push_back(interest); |
| 120 | }); |
| 121 | onSendData.connect([this] (const Data& data) { |
| 122 | this->sentDatas.push_back(data); |
| 123 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void |
| 127 | DummyClientFace::enableRegistrationReply() |
| 128 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 129 | onSendInterest.connect([this] (const Interest& interest) { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 130 | static const Name localhostRegistration("/localhost/nfd/rib"); |
| 131 | if (!localhostRegistration.isPrefixOf(interest.getName())) |
| 132 | return; |
| 133 | |
| 134 | nfd::ControlParameters params(interest.getName().get(-5).blockFromValue()); |
| 135 | params.setFaceId(1); |
| 136 | params.setOrigin(0); |
| 137 | if (interest.getName().get(3) == name::Component("register")) { |
| 138 | params.setCost(0); |
| 139 | } |
| 140 | |
| 141 | nfd::ControlResponse resp; |
| 142 | resp.setCode(200); |
| 143 | resp.setBody(params.wireEncode()); |
| 144 | |
| 145 | shared_ptr<Data> data = make_shared<Data>(interest.getName()); |
| 146 | data->setContent(resp.wireEncode()); |
| 147 | |
| 148 | KeyChain keyChain; |
| 149 | keyChain.signWithSha256(*data); |
| 150 | |
| 151 | this->getIoService().post([this, data] { this->receive(*data); }); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 152 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | template<typename Packet> |
| 156 | void |
| 157 | DummyClientFace::receive(const Packet& packet) |
| 158 | { |
| 159 | m_transport->receive(packet.wireEncode()); |
| 160 | } |
| 161 | |
| 162 | template void |
| 163 | DummyClientFace::receive<Interest>(const Interest& packet); |
| 164 | |
| 165 | template void |
| 166 | DummyClientFace::receive<Data>(const Data& packet); |
| 167 | |
| 168 | |
| 169 | shared_ptr<DummyClientFace> |
| 170 | makeDummyClientFace(const DummyClientFace::Options& options) |
| 171 | { |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 172 | // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 173 | return shared_ptr<DummyClientFace>( |
| 174 | new DummyClientFace(options, make_shared<DummyClientFace::Transport>())); |
| 175 | } |
| 176 | |
| 177 | shared_ptr<DummyClientFace> |
| 178 | makeDummyClientFace(boost::asio::io_service& ioService, |
| 179 | const DummyClientFace::Options& options) |
| 180 | { |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 181 | // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 182 | return shared_ptr<DummyClientFace>( |
| 183 | new DummyClientFace(options, make_shared<DummyClientFace::Transport>(), |
| 184 | ref(ioService))); |
| 185 | } |
| 186 | |
| 187 | } // namespace util |
| 188 | } // namespace ndn |