dummy-client-face.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 Regents of the University of California.
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 
23 #include "ndn-cxx/impl/lp-field-tag.hpp"
24 #include "ndn-cxx/lp/packet.hpp"
25 #include "ndn-cxx/lp/tags.hpp"
29 
30 #include <boost/asio/io_service.hpp>
31 
32 namespace ndn {
33 namespace util {
34 
35 class DummyClientFace::Transport final : public ndn::Transport
36 {
37 public:
38  void
39  receive(Block block) const
40  {
41  block.encode();
42  if (m_receiveCallback) {
43  m_receiveCallback(block);
44  }
45  }
46 
47  void
48  send(const Block& block) final
49  {
50  onSendBlock(block);
51  }
52 
53  void
54  close() final
55  {
56  }
57 
58  void
59  pause() final
60  {
61  }
62 
63  void
64  resume() final
65  {
66  }
67 
68 public:
69  Signal<Transport, Block> onSendBlock;
70 };
71 
72 struct DummyClientFace::BroadcastLink
73 {
74  std::vector<DummyClientFace*> faces;
75 };
76 
78  : Error("Face has already been linked to another face")
79 {
80 }
81 
83  : Face(make_shared<DummyClientFace::Transport>())
84  , m_internalKeyChain(make_unique<KeyChain>())
85  , m_keyChain(*m_internalKeyChain)
86 {
87  this->construct(options);
88 }
89 
90 DummyClientFace::DummyClientFace(KeyChain& keyChain, const Options& options)
91  : Face(make_shared<DummyClientFace::Transport>(), keyChain)
92  , m_keyChain(keyChain)
93 {
94  this->construct(options);
95 }
96 
97 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, const Options& options)
98  : Face(make_shared<DummyClientFace::Transport>(), ioService)
99  , m_internalKeyChain(make_unique<KeyChain>())
100  , m_keyChain(*m_internalKeyChain)
101 {
102  this->construct(options);
103 }
104 
105 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, const Options& options)
106  : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain)
107  , m_keyChain(keyChain)
108 {
109  this->construct(options);
110 }
111 
113 {
114  unlink();
115 }
116 
117 void
118 DummyClientFace::construct(const Options& options)
119 {
120  static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (Block packet) {
121  packet.encode();
122  lp::Packet lpPacket(packet);
123  auto frag = lpPacket.get<lp::FragmentField>();
124  Block block({frag.first, frag.second});
125 
126  if (block.type() == tlv::Interest) {
127  auto interest = make_shared<Interest>(block);
128  if (lpPacket.has<lp::NackField>()) {
129  auto nack = make_shared<lp::Nack>(std::move(*interest));
130  nack->setHeader(lpPacket.get<lp::NackField>());
131  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*nack, lpPacket);
132  onSendNack(*nack);
133  }
134  else {
135  addTagFromField<lp::NextHopFaceIdTag, lp::NextHopFaceIdField>(*interest, lpPacket);
136  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*interest, lpPacket);
137  onSendInterest(*interest);
138  }
139  }
140  else if (block.type() == tlv::Data) {
141  auto data = make_shared<Data>(block);
142  addTagFromField<lp::CachePolicyTag, lp::CachePolicyField>(*data, lpPacket);
143  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*data, lpPacket);
144  onSendData(*data);
145  }
146  });
147 
148  if (options.enablePacketLogging)
149  this->enablePacketLogging();
150 
151  if (options.enableRegistrationReply)
152  this->enableRegistrationReply(options.registrationReplyFaceId);
153 
154  m_processEventsOverride = options.processEventsOverride;
155 
156  enableBroadcastLink();
157 }
158 
159 void
160 DummyClientFace::enableBroadcastLink()
161 {
162  this->onSendInterest.connect([this] (const Interest& interest) {
163  if (m_bcastLink != nullptr) {
164  for (auto otherFace : m_bcastLink->faces) {
165  if (otherFace != this) {
166  otherFace->receive(interest);
167  }
168  }
169  }
170  });
171  this->onSendData.connect([this] (const Data& data) {
172  if (m_bcastLink != nullptr) {
173  for (auto otherFace : m_bcastLink->faces) {
174  if (otherFace != this) {
175  otherFace->receive(data);
176  }
177  }
178  }
179  });
180  this->onSendNack.connect([this] (const lp::Nack& nack) {
181  if (m_bcastLink != nullptr) {
182  for (auto otherFace : m_bcastLink->faces) {
183  if (otherFace != this) {
184  otherFace->receive(nack);
185  }
186  }
187  }
188  });
189 }
190 
191 void
192 DummyClientFace::enablePacketLogging()
193 {
194  onSendInterest.connect([this] (const Interest& interest) {
195  this->sentInterests.push_back(interest);
196  });
197  onSendData.connect([this] (const Data& data) {
198  this->sentData.push_back(data);
199  });
200  onSendNack.connect([this] (const lp::Nack& nack) {
201  this->sentNacks.push_back(nack);
202  });
203 }
204 
205 void
206 DummyClientFace::enableRegistrationReply(uint64_t faceId)
207 {
208  onSendInterest.connect([=] (const Interest& interest) {
209  static const Name localhostRibPrefix("/localhost/nfd/rib");
210  static const name::Component registerVerb("register");
211  const auto& name = interest.getName();
212  if (name.size() <= 4 || !localhostRibPrefix.isPrefixOf(name))
213  return;
214 
215  nfd::ControlParameters params(name[4].blockFromValue());
216  if (!params.hasFaceId()) {
217  params.setFaceId(faceId);
218  }
219  if (!params.hasOrigin()) {
220  params.setOrigin(nfd::ROUTE_ORIGIN_APP);
221  }
222  if (!params.hasCost() && name[3] == registerVerb) {
223  params.setCost(0);
224  }
225 
227  resp.setCode(200);
228  resp.setBody(params.wireEncode());
229 
230  shared_ptr<Data> data = make_shared<Data>(name);
231  data->setContent(resp.wireEncode());
232  m_keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
233  this->getIoService().post([this, data] { this->receive(*data); });
234  });
235 }
236 
237 void
238 DummyClientFace::receive(const Interest& interest)
239 {
240  lp::Packet lpPacket(interest.wireEncode());
241 
242  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, interest);
243  addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest);
244  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest);
245 
246  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
247 }
248 
249 void
250 DummyClientFace::receive(const Data& data)
251 {
252  lp::Packet lpPacket(data.wireEncode());
253 
254  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, data);
255  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data);
256 
257  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
258 }
259 
260 void
261 DummyClientFace::receive(const lp::Nack& nack)
262 {
263  lp::Packet lpPacket;
264  lpPacket.add<lp::NackField>(nack.getHeader());
265  Block interest = nack.getInterest().wireEncode();
266  lpPacket.add<lp::FragmentField>({interest.begin(), interest.end()});
267 
268  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack);
269  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack);
270 
271  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
272 }
273 
274 void
275 DummyClientFace::linkTo(DummyClientFace& other)
276 {
277  if (m_bcastLink != nullptr && other.m_bcastLink != nullptr) {
278  if (m_bcastLink != other.m_bcastLink) {
279  // already on different links
281  }
282  }
283  else if (m_bcastLink == nullptr && other.m_bcastLink != nullptr) {
284  m_bcastLink = other.m_bcastLink;
285  m_bcastLink->faces.push_back(this);
286  }
287  else if (m_bcastLink != nullptr && other.m_bcastLink == nullptr) {
288  other.m_bcastLink = m_bcastLink;
289  m_bcastLink->faces.push_back(&other);
290  }
291  else {
292  m_bcastLink = other.m_bcastLink = make_shared<BroadcastLink>();
293  m_bcastLink->faces.push_back(this);
294  m_bcastLink->faces.push_back(&other);
295  }
296 }
297 
298 void
299 DummyClientFace::unlink()
300 {
301  if (m_bcastLink == nullptr) {
302  return;
303  }
304 
305  auto it = std::find(m_bcastLink->faces.begin(), m_bcastLink->faces.end(), this);
306  BOOST_ASSERT(it != m_bcastLink->faces.end());
307  m_bcastLink->faces.erase(it);
308 
309  if (m_bcastLink->faces.size() == 1) {
310  m_bcastLink->faces[0]->m_bcastLink = nullptr;
311  m_bcastLink->faces.clear();
312  }
313  m_bcastLink = nullptr;
314 }
315 
316 void
317 DummyClientFace::doProcessEvents(time::milliseconds timeout, bool keepThread)
318 {
319  if (m_processEventsOverride != nullptr) {
320  m_processEventsOverride(timeout);
321  }
322  else {
323  this->Face::doProcessEvents(timeout, keepThread);
324  }
325 }
326 
327 } // namespace util
328 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
void encode()
Encode sub-elements into TLV-VALUE.
Definition: block.cpp:351
Represents a Data packet.
Definition: data.hpp:39
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder.
Definition: data.cpp:46
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:91
shared_ptr< Transport > getTransport() const
Returns the underlying transport.
Definition: face.hpp:433
Represents an Interest packet.
Definition: interest.hpp:50
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
Definition: interest.cpp:60
Provides a "TLV-oriented" delivery service.
Definition: transport.hpp:37
Declare a field.
Definition: field-decl.hpp:177
Represents a Network Nack.
Definition: nack.hpp:40
const Interest & getInterest() const
Definition: nack.hpp:52
const NackHeader & getHeader() const
Definition: nack.hpp:64
Packet & add(const typename FIELD::ValueType &value)
Add a FIELD with value.
Definition: packet.hpp:151
Block wireEncode() const
Encode packet into wire format.
Definition: packet.cpp:119
ControlResponse & setBody(const Block &body)
ControlResponse & setCode(uint32_t code)
Options for DummyClientFace.
A client-side face for unit testing.
void receive(const Interest &interest)
Cause the Face to receive an Interest packet.
Signal< DummyClientFace, Interest > onSendInterest
Emits whenever an Interest is sent.
Signal< DummyClientFace, lp::Nack > onSendNack
Emits whenever a Nack is sent.
void unlink()
Unlink the broadcast media if previously linked.
Signal< DummyClientFace, Data > onSendData
Emits whenever a Data packet is sent.
DummyClientFace(const Options &options=Options())
Create a dummy face with internal IO service.
Provides a lightweight signal / event system.
Definition: signal.hpp:53
#define NDN_THROW(e)
Definition: exception.hpp:61
mgmt::ControlResponse ControlResponse
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
@ ControlParameters
Definition: tlv-nfd.hpp:35
@ Name
Definition: tlv.hpp:71
@ Data
Definition: tlv.hpp:69
@ Interest
Definition: tlv.hpp:68
Definition: data.cpp:25