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 | /* |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 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 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 88 | struct DummyClientFace::BroadcastLink |
| 89 | { |
| 90 | std::vector<DummyClientFace*> faces; |
| 91 | }; |
| 92 | |
| 93 | DummyClientFace::AlreadyLinkedError::AlreadyLinkedError() |
| 94 | : Error("Face has already been linked to another face") |
| 95 | { |
| 96 | } |
| 97 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame^] | 98 | DummyClientFace::DummyClientFace(const Options& options) |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 99 | : Face(make_shared<DummyClientFace::Transport>()) |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame^] | 100 | , m_internalKeyChain(make_unique<KeyChain>()) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 101 | , m_keyChain(*m_internalKeyChain) |
| 102 | { |
| 103 | this->construct(options); |
| 104 | } |
| 105 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame^] | 106 | DummyClientFace::DummyClientFace(KeyChain& keyChain, const Options& options) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 107 | : Face(make_shared<DummyClientFace::Transport>(), keyChain) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 108 | , m_keyChain(keyChain) |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 109 | { |
| 110 | this->construct(options); |
| 111 | } |
| 112 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame^] | 113 | DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, const Options& options) |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 114 | : Face(make_shared<DummyClientFace::Transport>(), ioService) |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame^] | 115 | , m_internalKeyChain(make_unique<KeyChain>()) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 116 | , m_keyChain(*m_internalKeyChain) |
| 117 | { |
| 118 | this->construct(options); |
| 119 | } |
| 120 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame^] | 121 | DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, const Options& options) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 122 | : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 123 | , m_keyChain(keyChain) |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 124 | { |
| 125 | this->construct(options); |
| 126 | } |
| 127 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 128 | DummyClientFace::~DummyClientFace() |
| 129 | { |
| 130 | unlink(); |
| 131 | } |
| 132 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 133 | void |
| 134 | DummyClientFace::construct(const Options& options) |
| 135 | { |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 136 | static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) { |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 137 | Block packet(blockFromDaemon); |
| 138 | packet.encode(); |
| 139 | lp::Packet lpPacket(packet); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 140 | |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 141 | Buffer::const_iterator begin, end; |
| 142 | std::tie(begin, end) = lpPacket.get<lp::FragmentField>(); |
| 143 | Block block(&*begin, std::distance(begin, end)); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 144 | |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 145 | if (block.type() == tlv::Interest) { |
| 146 | shared_ptr<Interest> interest = make_shared<Interest>(block); |
| 147 | if (lpPacket.has<lp::NackField>()) { |
| 148 | shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest)); |
| 149 | nack->setHeader(lpPacket.get<lp::NackField>()); |
Junxiao Shi | b38e6d4 | 2017-08-16 16:15:28 +0000 | [diff] [blame] | 150 | addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*nack, lpPacket); |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 151 | onSendNack(*nack); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 152 | } |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 153 | else { |
Junxiao Shi | b38e6d4 | 2017-08-16 16:15:28 +0000 | [diff] [blame] | 154 | addTagFromField<lp::NextHopFaceIdTag, lp::NextHopFaceIdField>(*interest, lpPacket); |
| 155 | addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*interest, lpPacket); |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 156 | onSendInterest(*interest); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 157 | } |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 158 | } |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 159 | else if (block.type() == tlv::Data) { |
| 160 | shared_ptr<Data> data = make_shared<Data>(block); |
Junxiao Shi | b38e6d4 | 2017-08-16 16:15:28 +0000 | [diff] [blame] | 161 | addTagFromField<lp::CachePolicyTag, lp::CachePolicyField>(*data, lpPacket); |
| 162 | addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*data, lpPacket); |
Eric Newberry | e3e2505 | 2015-11-28 20:37:08 -0700 | [diff] [blame] | 163 | onSendData(*data); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 164 | } |
| 165 | }); |
| 166 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 167 | if (options.enablePacketLogging) |
| 168 | this->enablePacketLogging(); |
| 169 | |
| 170 | if (options.enableRegistrationReply) |
| 171 | this->enableRegistrationReply(); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 172 | |
| 173 | m_processEventsOverride = options.processEventsOverride; |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 174 | |
| 175 | enableBroadcastLink(); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | DummyClientFace::enableBroadcastLink() |
| 180 | { |
| 181 | this->onSendInterest.connect([this] (const Interest& interest) { |
| 182 | if (m_bcastLink != nullptr) { |
| 183 | for (auto otherFace : m_bcastLink->faces) { |
| 184 | if (otherFace != this) { |
| 185 | otherFace->receive(interest); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | }); |
| 190 | this->onSendData.connect([this] (const Data& data) { |
| 191 | if (m_bcastLink != nullptr) { |
| 192 | for (auto otherFace : m_bcastLink->faces) { |
| 193 | if (otherFace != this) { |
| 194 | otherFace->receive(data); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | }); |
| 199 | this->onSendNack.connect([this] (const lp::Nack& nack) { |
| 200 | if (m_bcastLink != nullptr) { |
| 201 | for (auto otherFace : m_bcastLink->faces) { |
| 202 | if (otherFace != this) { |
| 203 | otherFace->receive(nack); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void |
| 211 | DummyClientFace::enablePacketLogging() |
| 212 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 213 | onSendInterest.connect([this] (const Interest& interest) { |
| 214 | this->sentInterests.push_back(interest); |
| 215 | }); |
| 216 | onSendData.connect([this] (const Data& data) { |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 217 | this->sentData.push_back(data); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 218 | }); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 219 | onSendNack.connect([this] (const lp::Nack& nack) { |
| 220 | this->sentNacks.push_back(nack); |
| 221 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void |
| 225 | DummyClientFace::enableRegistrationReply() |
| 226 | { |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 227 | onSendInterest.connect([this] (const Interest& interest) { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 228 | static const Name localhostRegistration("/localhost/nfd/rib"); |
| 229 | if (!localhostRegistration.isPrefixOf(interest.getName())) |
| 230 | return; |
| 231 | |
| 232 | nfd::ControlParameters params(interest.getName().get(-5).blockFromValue()); |
| 233 | params.setFaceId(1); |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 234 | params.setOrigin(nfd::ROUTE_ORIGIN_APP); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 235 | if (interest.getName().get(3) == name::Component("register")) { |
| 236 | params.setCost(0); |
| 237 | } |
| 238 | |
| 239 | nfd::ControlResponse resp; |
| 240 | resp.setCode(200); |
| 241 | resp.setBody(params.wireEncode()); |
| 242 | |
| 243 | shared_ptr<Data> data = make_shared<Data>(interest.getName()); |
| 244 | data->setContent(resp.wireEncode()); |
| 245 | |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 246 | m_keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256)); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 247 | |
| 248 | this->getIoService().post([this, data] { this->receive(*data); }); |
Junxiao Shi | 27913b4 | 2014-12-23 14:49:38 -0700 | [diff] [blame] | 249 | }); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 252 | void |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 253 | DummyClientFace::receive(const Interest& interest) |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 254 | { |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 255 | lp::Packet lpPacket(interest.wireEncode()); |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 256 | |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 257 | addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, interest); |
| 258 | addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest); |
| 259 | addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 260 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 261 | static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode()); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 264 | void |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 265 | DummyClientFace::receive(const Data& data) |
| 266 | { |
| 267 | lp::Packet lpPacket(data.wireEncode()); |
| 268 | |
| 269 | addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, data); |
| 270 | addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data); |
| 271 | |
| 272 | static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode()); |
| 273 | } |
| 274 | |
| 275 | void |
| 276 | DummyClientFace::receive(const lp::Nack& nack) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 277 | { |
| 278 | lp::Packet lpPacket; |
| 279 | lpPacket.add<lp::NackField>(nack.getHeader()); |
| 280 | Block interest = nack.getInterest().wireEncode(); |
| 281 | lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end())); |
| 282 | |
Zhiyi Zhang | 9cbd010 | 2017-01-03 11:03:01 -0800 | [diff] [blame] | 283 | addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack); |
| 284 | addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 285 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 286 | static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode()); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 287 | } |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 288 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 289 | void |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 290 | DummyClientFace::linkTo(DummyClientFace& other) |
| 291 | { |
| 292 | if (m_bcastLink != nullptr && other.m_bcastLink != nullptr) { |
| 293 | if (m_bcastLink != other.m_bcastLink) { |
| 294 | // already on different links |
| 295 | BOOST_THROW_EXCEPTION(AlreadyLinkedError()); |
| 296 | } |
| 297 | } |
| 298 | else if (m_bcastLink == nullptr && other.m_bcastLink != nullptr) { |
| 299 | m_bcastLink = other.m_bcastLink; |
| 300 | m_bcastLink->faces.push_back(this); |
| 301 | } |
| 302 | else if (m_bcastLink != nullptr && other.m_bcastLink == nullptr) { |
| 303 | other.m_bcastLink = m_bcastLink; |
| 304 | m_bcastLink->faces.push_back(&other); |
| 305 | } |
| 306 | else { |
| 307 | m_bcastLink = other.m_bcastLink = make_shared<BroadcastLink>(); |
| 308 | m_bcastLink->faces.push_back(this); |
| 309 | m_bcastLink->faces.push_back(&other); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void |
| 314 | DummyClientFace::unlink() |
| 315 | { |
| 316 | if (m_bcastLink == nullptr) { |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | auto it = std::find(m_bcastLink->faces.begin(), m_bcastLink->faces.end(), this); |
| 321 | BOOST_ASSERT(it != m_bcastLink->faces.end()); |
| 322 | m_bcastLink->faces.erase(it); |
| 323 | |
| 324 | if (m_bcastLink->faces.size() == 1) { |
| 325 | m_bcastLink->faces[0]->m_bcastLink = nullptr; |
| 326 | m_bcastLink->faces.clear(); |
| 327 | } |
| 328 | m_bcastLink = nullptr; |
| 329 | } |
| 330 | |
| 331 | void |
Junxiao Shi | 2ed9e07 | 2017-08-13 16:45:48 +0000 | [diff] [blame] | 332 | DummyClientFace::doProcessEvents(time::milliseconds timeout, bool keepThread) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 333 | { |
| 334 | if (m_processEventsOverride != nullptr) { |
| 335 | m_processEventsOverride(timeout); |
| 336 | } |
| 337 | else { |
| 338 | this->Face::doProcessEvents(timeout, keepThread); |
| 339 | } |
| 340 | } |
| 341 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 342 | } // namespace util |
| 343 | } // namespace ndn |