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-2019 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  close() override
49  {
50  }
51 
52  void
53  pause() override
54  {
55  }
56 
57  void
58  resume() override
59  {
60  }
61 
62  void
63  send(const Block& wire) override
64  {
65  onSendBlock(wire);
66  }
67 
68  void
69  send(const Block& header, const Block& payload) override
70  {
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());
76  }
77 
78  boost::asio::io_service&
80  {
81  return *m_ioService;
82  }
83 
84 public:
86 };
87 
89 {
90  std::vector<DummyClientFace*> faces;
91 };
92 
94  : Error("Face has already been linked to another face")
95 {
96 }
97 
99  : Face(make_shared<DummyClientFace::Transport>())
100  , m_internalKeyChain(make_unique<KeyChain>())
102 {
103  this->construct(options);
104 }
105 
107  : Face(make_shared<DummyClientFace::Transport>(), keyChain)
108  , m_keyChain(keyChain)
109 {
110  this->construct(options);
111 }
112 
113 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, const Options& options)
114  : Face(make_shared<DummyClientFace::Transport>(), ioService)
115  , m_internalKeyChain(make_unique<KeyChain>())
116  , m_keyChain(*m_internalKeyChain)
117 {
118  this->construct(options);
119 }
120 
121 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, const Options& options)
122  : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain)
123  , m_keyChain(keyChain)
124 {
125  this->construct(options);
126 }
127 
129 {
130  unlink();
131 }
132 
133 void
134 DummyClientFace::construct(const Options& options)
135 {
136  static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) {
137  Block packet(blockFromDaemon);
138  packet.encode();
139  lp::Packet lpPacket(packet);
140 
141  Buffer::const_iterator begin, end;
142  std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
143  Block block(&*begin, std::distance(begin, end));
144 
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>());
150  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*nack, lpPacket);
151  onSendNack(*nack);
152  }
153  else {
154  addTagFromField<lp::NextHopFaceIdTag, lp::NextHopFaceIdField>(*interest, lpPacket);
155  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*interest, lpPacket);
156  onSendInterest(*interest);
157  }
158  }
159  else if (block.type() == tlv::Data) {
160  shared_ptr<Data> data = make_shared<Data>(block);
161  addTagFromField<lp::CachePolicyTag, lp::CachePolicyField>(*data, lpPacket);
162  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*data, lpPacket);
163  onSendData(*data);
164  }
165  });
166 
167  if (options.enablePacketLogging)
168  this->enablePacketLogging();
169 
170  if (options.enableRegistrationReply)
171  this->enableRegistrationReply();
172 
173  m_processEventsOverride = options.processEventsOverride;
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  });
208 }
209 
210 void
211 DummyClientFace::enablePacketLogging()
212 {
213  onSendInterest.connect([this] (const Interest& interest) {
214  this->sentInterests.push_back(interest);
215  });
216  onSendData.connect([this] (const Data& data) {
217  this->sentData.push_back(data);
218  });
219  onSendNack.connect([this] (const lp::Nack& nack) {
220  this->sentNacks.push_back(nack);
221  });
222 }
223 
224 void
225 DummyClientFace::enableRegistrationReply()
226 {
227  onSendInterest.connect([this] (const Interest& interest) {
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);
234  params.setOrigin(nfd::ROUTE_ORIGIN_APP);
235  if (interest.getName().get(3) == name::Component("register")) {
236  params.setCost(0);
237  }
238 
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 
246  m_keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
247 
248  this->getIoService().post([this, data] { this->receive(*data); });
249  });
250 }
251 
252 void
253 DummyClientFace::receive(const Interest& interest)
254 {
255  lp::Packet lpPacket(interest.wireEncode());
256 
257  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, interest);
258  addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest);
259  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest);
260 
261  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
262 }
263 
264 void
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)
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 
283  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack);
284  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack);
285 
286  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
287 }
288 
289 void
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
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
332 DummyClientFace::doProcessEvents(time::milliseconds timeout, bool keepThread)
333 {
334  if (m_processEventsOverride != nullptr) {
335  m_processEventsOverride(timeout);
336  }
337  else {
338  this->Face::doProcessEvents(timeout, keepThread);
339  }
340 }
341 
342 } // namespace util
343 } // namespace ndn
ndn::lp::Packet
Definition: packet.hpp:31
ndn::util::DummyClientFace::unlink
void unlink()
unlink the broadcast media if previously linked
Definition: dummy-client-face.cpp:314
ndn::util::DummyClientFace::Options
options for DummyClientFace
Definition: dummy-client-face.hpp:40
ndn::util::DummyClientFace::onSendData
Signal< DummyClientFace, Data > onSendData
emits whenever a Data packet is sent
Definition: dummy-client-face.hpp:181
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::util::DummyClientFace::onSendInterest
Signal< DummyClientFace, Interest > onSendInterest
emits whenever an Interest is sent
Definition: dummy-client-face.hpp:175
ndn::tlv::Interest
@ Interest
Definition: tlv.hpp:65
ndn::mgmt::ControlResponse::wireEncode
const Block & wireEncode() const
Definition: control-response.cpp:52
ndn::nfd::ROUTE_ORIGIN_APP
@ ROUTE_ORIGIN_APP
Definition: nfd-constants.hpp:101
ndn::util::DummyClientFace::m_processEventsOverride
std::function< void(time::milliseconds)> m_processEventsOverride
Definition: dummy-client-face.hpp:194
ndn::lp::Packet::add
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Definition: packet.hpp:148
ndn::util::DummyClientFace::Transport::resume
void resume() override
resume the transport
Definition: dummy-client-face.cpp:58
ndn::util::DummyClientFace::m_internalKeyChain
std::unique_ptr< KeyChain > m_internalKeyChain
Definition: dummy-client-face.hpp:192
ndn::util::DummyClientFace::Transport::send
void send(const Block &wire) override
send a TLV block through the transport
Definition: dummy-client-face.cpp:63
controller.hpp
ndn::util::DummyClientFace::Transport::receive
void receive(Block block) const
Definition: dummy-client-face.cpp:39
ndn::Data::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder in NDN Packet Format v0.2.
Definition: data.cpp:48
ndn::util::DummyClientFace::Transport::getIoService
boost::asio::io_service & getIoService()
Definition: dummy-client-face.cpp:79
packet.hpp
transport.hpp
ndn::Face
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
ndn::util::DummyClientFace::~DummyClientFace
~DummyClientFace()
Definition: dummy-client-face.cpp:128
ndn::Transport::m_receiveCallback
ReceiveCallback m_receiveCallback
Definition: transport.hpp:112
ndn::mgmt::ControlResponse::setCode
ControlResponse & setCode(uint32_t code)
Definition: control-response.hpp:87
ndn::util::DummyClientFace::DummyClientFace
DummyClientFace(const Options &options=Options())
Create a dummy face with internal IO service.
Definition: dummy-client-face.cpp:98
ndn::util::signal::Signal
provides a lightweight signal / event system
Definition: signal.hpp:52
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
ndn::util::DummyClientFace
a client-side face for unit testing
Definition: dummy-client-face.hpp:35
ndn::security::v2::KeyChain
The interface of signing key management.
Definition: key-chain.hpp:47
ndn::lp::Nack::setHeader
Nack & setHeader(const NackHeader &header)
Definition: nack.hpp:75
ndn::util::DummyClientFace::AlreadyLinkedError::AlreadyLinkedError
AlreadyLinkedError()
Definition: dummy-client-face.cpp:93
ndn::util::DummyClientFace::m_bcastLink
shared_ptr< BroadcastLink > m_bcastLink
Definition: dummy-client-face.hpp:191
dummy-client-face.hpp
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
control-response.hpp
ndn::lp::FieldDecl
Declare a field.
Definition: field-decl.hpp:180
ndn::mgmt::ControlResponse::setBody
ControlResponse & setBody(const Block &body)
Definition: control-response.hpp:115
lp-field-tag.hpp
ndn::util::DummyClientFace::Transport::send
void send(const Block &header, const Block &payload) override
send two memory blocks through the transport
Definition: dummy-client-face.cpp:69
ndn::mgmt::ControlResponse
ControlCommand response.
Definition: control-response.hpp:33
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::util::DummyClientFace::Transport::pause
void pause() override
pause the transport
Definition: dummy-client-face.cpp:53
ndn::lp::Packet::wireEncode
Block wireEncode() const
encode packet into wire format
Definition: packet.cpp:119
ndn::util::DummyClientFace::m_keyChain
KeyChain & m_keyChain
Definition: dummy-client-face.hpp:193
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::tlv::Data
@ Data
Definition: tlv.hpp:66
ndn::nfd::ControlParameters
represents parameters in a ControlCommand request or response
Definition: control-parameters.hpp:82
ndn::lp::Nack::getHeader
const NackHeader & getHeader() const
Definition: nack.hpp:63
ndn::util::DummyClientFace::Transport
Definition: dummy-client-face.cpp:36
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::lp::Nack
represents a Network Nack
Definition: nack.hpp:39
ndn::Block::size
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:290
ndn::util::DummyClientFace::Transport::close
void close() override
Close the connection.
Definition: dummy-client-face.cpp:48
ndn::Face::Error
Definition: face.hpp:93
tags.hpp
ndn::util::DummyClientFace::Transport::onSendBlock
Signal< Transport, Block > onSendBlock
Definition: dummy-client-face.cpp:85
ndn::util::DummyClientFace::AlreadyLinkedError
Definition: dummy-client-face.hpp:76
ndn::Interest::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder according to NDN Packet Format v0.3.
Definition: interest.cpp:87
ndn::Transport
Provides TLV-block delivery service.
Definition: transport.hpp:36
ndn::Block::wire
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition: block.cpp:281
ndn::Block::encode
void encode()
Encode sub-elements into TLV-VALUE.
Definition: block.cpp:353
ndn::lp::Nack::getInterest
const Interest & getInterest() const
Definition: nack.hpp:51
ndn::util::DummyClientFace::onSendNack
Signal< DummyClientFace, lp::Nack > onSendNack
emits whenever a Nack is sent
Definition: dummy-client-face.hpp:187
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38