NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017 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 
22 #include "dummy-client-face.hpp"
23 #include "../detail/lp-field-tag.hpp"
24 #include "../lp/packet.hpp"
25 #include "../lp/tags.hpp"
26 #include "../mgmt/nfd/controller.hpp"
27 #include "../mgmt/nfd/control-response.hpp"
28 #include "../transport/transport.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:
85  Signal<Transport, Block> onSendBlock;
86 };
87 
88 DummyClientFace::DummyClientFace(const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
89  : Face(make_shared<DummyClientFace::Transport>())
90  , m_internalKeyChain(new KeyChain)
91  , m_keyChain(*m_internalKeyChain)
92 {
93  this->construct(options);
94 }
95 
97  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
98  : Face(make_shared<DummyClientFace::Transport>(), keyChain)
99  , m_keyChain(keyChain)
100 {
101  this->construct(options);
102 }
103 
104 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService,
105  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
106  : Face(make_shared<DummyClientFace::Transport>(), ioService)
107  , m_internalKeyChain(new KeyChain)
108  , m_keyChain(*m_internalKeyChain)
109 {
110  this->construct(options);
111 }
112 
113 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
114  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
115  : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain)
116  , m_keyChain(keyChain)
117 {
118  this->construct(options);
119 }
120 
121 void
122 DummyClientFace::construct(const Options& options)
123 {
124  static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) {
125  Block packet(blockFromDaemon);
126  packet.encode();
127  lp::Packet lpPacket(packet);
128 
129  Buffer::const_iterator begin, end;
130  std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
131  Block block(&*begin, std::distance(begin, end));
132 
133  if (block.type() == tlv::Interest) {
134  shared_ptr<Interest> interest = make_shared<Interest>(block);
135  if (lpPacket.has<lp::NackField>()) {
136  shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest));
137  nack->setHeader(lpPacket.get<lp::NackField>());
138  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*nack, lpPacket);
139  onSendNack(*nack);
140  }
141  else {
142  addTagFromField<lp::NextHopFaceIdTag, lp::NextHopFaceIdField>(*interest, lpPacket);
143  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*interest, lpPacket);
144  onSendInterest(*interest);
145  }
146  }
147  else if (block.type() == tlv::Data) {
148  shared_ptr<Data> data = make_shared<Data>(block);
149  addTagFromField<lp::CachePolicyTag, lp::CachePolicyField>(*data, lpPacket);
150  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*data, lpPacket);
151  onSendData(*data);
152  }
153  });
154 
155  if (options.enablePacketLogging)
156  this->enablePacketLogging();
157 
158  if (options.enableRegistrationReply)
159  this->enableRegistrationReply();
160 
161  m_processEventsOverride = options.processEventsOverride;
162 }
163 
164 void
165 DummyClientFace::enablePacketLogging()
166 {
167  onSendInterest.connect([this] (const Interest& interest) {
168  this->sentInterests.push_back(interest);
169  });
170  onSendData.connect([this] (const Data& data) {
171  this->sentData.push_back(data);
172  });
173  onSendNack.connect([this] (const lp::Nack& nack) {
174  this->sentNacks.push_back(nack);
175  });
176 }
177 
178 void
179 DummyClientFace::enableRegistrationReply()
180 {
181  onSendInterest.connect([this] (const Interest& interest) {
182  static const Name localhostRegistration("/localhost/nfd/rib");
183  if (!localhostRegistration.isPrefixOf(interest.getName()))
184  return;
185 
186  nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
187  params.setFaceId(1);
189  if (interest.getName().get(3) == name::Component("register")) {
190  params.setCost(0);
191  }
192 
194  resp.setCode(200);
195  resp.setBody(params.wireEncode());
196 
197  shared_ptr<Data> data = make_shared<Data>(interest.getName());
198  data->setContent(resp.wireEncode());
199 
201 
202  this->getIoService().post([this, data] { this->receive(*data); });
203  });
204 }
205 
206 void
208 {
209  lp::Packet lpPacket(interest.wireEncode());
210 
211  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, interest);
212  addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest);
213  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest);
214 
215  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
216 }
217 
218 void
220 {
221  lp::Packet lpPacket(data.wireEncode());
222 
223  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, data);
224  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data);
225 
226  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
227 }
228 
229 void
231 {
232  lp::Packet lpPacket;
233  lpPacket.add<lp::NackField>(nack.getHeader());
234  Block interest = nack.getInterest().wireEncode();
235  lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
236 
237  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack);
238  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack);
239 
240  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
241 }
242 
243 void
244 DummyClientFace::doProcessEvents(time::milliseconds timeout, bool keepThread)
245 {
246  if (m_processEventsOverride != nullptr) {
247  m_processEventsOverride(timeout);
248  }
249  else {
250  this->Face::doProcessEvents(timeout, keepThread);
251  }
252 }
253 
254 } // namespace util
255 } // namespace ndn
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.
const Name & getName() const
Definition: interest.hpp:139
Copyright (c) 2011-2015 Regents of the University of California.
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:280
const NackHeader & getHeader() const
Definition: nack.hpp:65
Signal< DummyClientFace, Interest > onSendInterest
emits whenever an Interest is sent
represents parameters in a ControlCommand request or response
void resume() override
resume the transport
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:153
boost::posix_time::time_duration milliseconds(long duration)
Definition: asio.hpp:117
const Interest & getInterest() const
Definition: nack.hpp:53
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
represents an Interest packet
Definition: interest.hpp:42
use sha256 digest, no signer needs to be specified
const Block & wireEncode() const
std::vector< Data > sentData
Data sent out of this DummyClientFace.
ReceiveCallback m_receiveCallback
Definition: transport.hpp:124
Signing parameters passed to KeyChain.
boost::asio::io_service & getIoService()
represents a Network Nack
Definition: nack.hpp:40
options for DummyClientFace
size_t size() const
Get size of encoded wire, including Type-Length-Value.
Definition: block.cpp:299
void pause() override
pause the transport
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
Definition: data.cpp:48
DummyClientFace(const Options &options=Options())
Create a dummy face with internal IO service.
Signal< DummyClientFace, lp::Nack > onSendNack
emits whenever a Nack is sent
Block blockFromValue() const
Definition: block.cpp:323
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:95
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: interest.cpp:56
boost::asio::io_service * m_ioService
Definition: transport.hpp:121
Signal< DummyClientFace, Data > onSendData
emits whenever a Data packet is sent
Represents an absolute name.
Definition: name.hpp:42
void receive(const Interest &interest)
cause the Face to receive an interest
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:260
provides TLV-block delivery service
Definition: transport.hpp:40
boost::asio::io_service & getIoService()
Return nullptr (cannot use IoService in simulations), preserved for API compatibility.
Definition: face.hpp:468
size_t appendByteArray(const uint8_t *array, size_t length)
Append a byte array array of length length.
Definition: encoder.cpp:135
a client-side face for unit testing
ControlParameters & setCost(uint64_t cost)
Component holds a read-only name component value.
ControlResponse & setBody(const Block &body)
void send(const Block &wire) override
send a TLV block through the transport
void encode()
Encode sub elements into TLV-VALUE.
Definition: block.cpp:362
const uint8_t * wire() const
Get pointer to encoded wire.
Definition: block.cpp:290
ControlParameters & setOrigin(RouteOrigin origin)
void close() override
Close the connection.
ControlCommand response.
Represents a Data packet.
Definition: data.hpp:35
void send(const Block &header, const Block &payload) override
send two memory blocks through the transport
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:164
ControlResponse & setCode(uint32_t code)
EncodingImpl< EncoderTag > EncodingBuffer
ndn security v2 KeyChain
Definition: key-chain.cpp:76
Signal< Transport, Block > onSendBlock
virtual void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
asynchronously open the connection
Definition: transport.cpp:44
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