NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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 
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 
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:
70 };
71 
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>())
86 {
87  this->construct(options);
88 }
89 
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>())
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 
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()) {
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());
233  this->getIoService().post([this, data] { this->receive(*data); });
234  });
235 }
236 
237 void
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
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
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
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
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) {
321  }
322  else {
323  this->Face::doProcessEvents(timeout, keepThread);
324  }
325 }
326 
327 } // namespace util
328 } // namespace ndn
void pause() final
Pause the transport, canceling all pending operations.
bool enableRegistrationReply
if true, prefix registration command will be automatically replied with a successful response ...
ControlParameters & setFaceId(uint64_t faceId)
std::vector< lp::Nack > sentNacks
Nacks sent out of this DummyClientFace.
Copyright (c) 2011-2015 Regents of the University of California.
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:219
Signal< DummyClientFace, Interest > onSendInterest
emits whenever an Interest is sent
represents parameters in a ControlCommand request or response
ndn security KeyChain
Definition: key-chain.cpp:70
std::vector< Interest > sentInterests
Interests sent out of this DummyClientFace.
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Definition: packet.hpp:148
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:300
size_t wireEncode(EncodingImpl< TAG > &encoder) const
void close() final
Close the connection.
DummyIoService & getIoService()
Definition: face.hpp:383
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
Represents an Interest packet.
Definition: interest.hpp:48
const Block & wireEncode() const
Use a SHA-256 digest only, no signer needs to be specified.
const NackHeader & getHeader() const
Definition: nack.hpp:63
std::vector< Data > sentData
Data sent out of this DummyClientFace.
ReceiveCallback m_receiveCallback
Definition: transport.hpp:109
provides a lightweight signal / event system
Definition: signal.hpp:52
void resume() final
Resume the transport.
Signing parameters passed to KeyChain.
std::function< void(time::milliseconds)> m_processEventsOverride
represents a Network Nack
Definition: nack.hpp:38
#define NDN_THROW(e)
Definition: exception.hpp:61
options for DummyClientFace
Declare a field.
Definition: field-decl.hpp:176
DummyClientFace(const Options &options=Options())
Create a dummy face with internal IO service.
Signal< DummyClientFace, lp::Nack > onSendNack
emits whenever a Nack is sent
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
Signal< DummyClientFace, Data > onSendData
emits whenever a Data packet is sent
void unlink()
unlink the broadcast media if previously linked
Represents an absolute name.
Definition: name.hpp:41
void receive(const Interest &interest)
cause the Face to receive an interest
shared_ptr< BroadcastLink > m_bcastLink
void linkTo(DummyClientFace &other)
link another DummyClientFace through a broadcast media
Provides TLV-block delivery service.
Definition: transport.hpp:35
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
Definition: interest.cpp:60
A client-side face for unit testing.
ControlParameters & setCost(uint64_t cost)
Represents a name component.
ControlResponse & setBody(const Block &body)
void encode()
Encode sub-elements into TLV-VALUE.
Definition: block.cpp:351
const Name & getName() const noexcept
Definition: interest.hpp:172
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder.
Definition: data.cpp:46
ControlParameters & setOrigin(RouteOrigin origin)
void send(const Block &block) final
Send a TLV block through the transport.
ControlCommand response.
virtual void connect(ReceiveCallback receiveCallback)
Asynchronously open the connection.
Definition: transport.cpp:32
uint64_t registrationReplyFaceId
FaceId used in prefix registration replies.
Represents a Data packet.
Definition: data.hpp:37
std::unique_ptr< KeyChain > m_internalKeyChain
ControlResponse & setCode(uint32_t code)
Signal< Transport, Block > onSendBlock
const Interest & getInterest() const
Definition: nack.hpp:51
bool enablePacketLogging
if true, packets sent out of DummyClientFace will be appended to a container
std::function< void(time::milliseconds)> processEventsOverride
if not empty, face.processEvents() will be overridden by this function
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48