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; -*- */
22 #include "dummy-client-face.hpp"
23 #include "../lp/packet.hpp"
24 #include "../lp/tags.hpp"
25 #include "../mgmt/nfd/controller.hpp"
26 #include "../mgmt/nfd/control-response.hpp"
27 #include "../transport/transport.hpp"
28 
29 #include <boost/asio/io_service.hpp>
30 
31 namespace ndn {
32 namespace util {
33 
35 {
36 public:
37  void
38  receive(Block block) const
39  {
40  block.encode();
41  if (m_receiveCallback) {
42  m_receiveCallback(block);
43  }
44  }
45 
46  virtual void
47  close() override
48  {
49  }
50 
51  virtual void
52  pause() override
53  {
54  }
55 
56  virtual void
57  resume() override
58  {
59  }
60 
61  virtual void
62  send(const Block& wire) override
63  {
64  onSendBlock(wire);
65  }
66 
67  virtual void
68  send(const Block& header, const Block& payload) override
69  {
70  EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
71  encoder.appendByteArray(header.wire(), header.size());
72  encoder.appendByteArray(payload.wire(), payload.size());
73 
74  this->send(encoder.block());
75  }
76 
77  boost::asio::io_service&
79  {
80  return *m_ioService;
81  }
82 
83 public:
85 };
86 
87 DummyClientFace::DummyClientFace(const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
88  : Face(make_shared<DummyClientFace::Transport>())
89  , m_internalKeyChain(new KeyChain)
90  , m_keyChain(*m_internalKeyChain)
91 {
92  this->construct(options);
93 }
94 
96  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
97  : Face(make_shared<DummyClientFace::Transport>(), keyChain)
98  , m_keyChain(keyChain)
99 {
100  this->construct(options);
101 }
102 
103 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService,
104  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
105  : Face(make_shared<DummyClientFace::Transport>(), ioService)
106  , m_internalKeyChain(new KeyChain)
107  , m_keyChain(*m_internalKeyChain)
108 {
109  this->construct(options);
110 }
111 
112 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
113  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
114  : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain)
115  , m_keyChain(keyChain)
116 {
117  this->construct(options);
118 }
119 
120 void
121 DummyClientFace::construct(const Options& options)
122 {
123  static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) {
124  Block packet(blockFromDaemon);
125  packet.encode();
126  lp::Packet lpPacket(packet);
127 
128  Buffer::const_iterator begin, end;
129  std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
130  Block block(&*begin, std::distance(begin, end));
131 
132  if (block.type() == tlv::Interest) {
133  shared_ptr<Interest> interest = make_shared<Interest>(block);
134  if (lpPacket.has<lp::NackField>()) {
135  shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest));
136  nack->setHeader(lpPacket.get<lp::NackField>());
137  if (lpPacket.has<lp::NextHopFaceIdField>()) {
138  nack->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>()));
139  }
140  if (lpPacket.has<lp::CongestionMarkField>()) {
141  nack->setTag(make_shared<lp::CongestionMarkTag>(lpPacket.get<lp::CongestionMarkField>()));
142  }
143  onSendNack(*nack);
144  }
145  else {
146  if (lpPacket.has<lp::NextHopFaceIdField>()) {
147  interest->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>()));
148  }
149  if (lpPacket.has<lp::CongestionMarkField>()) {
150  interest->setTag(make_shared<lp::CongestionMarkTag>(lpPacket.get<lp::CongestionMarkField>()));
151  }
152  onSendInterest(*interest);
153  }
154  }
155  else if (block.type() == tlv::Data) {
156  shared_ptr<Data> data = make_shared<Data>(block);
157 
158  if (lpPacket.has<lp::CachePolicyField>()) {
159  data->setTag(make_shared<lp::CachePolicyTag>(lpPacket.get<lp::CachePolicyField>()));
160  }
161  if (lpPacket.has<lp::CongestionMarkField>()) {
162  data->setTag(make_shared<lp::CongestionMarkTag>(lpPacket.get<lp::CongestionMarkField>()));
163  }
164 
165  onSendData(*data);
166  }
167  });
168 
169  if (options.enablePacketLogging)
170  this->enablePacketLogging();
171 
172  if (options.enableRegistrationReply)
173  this->enableRegistrationReply();
174 
175  m_processEventsOverride = options.processEventsOverride;
176 }
177 
178 void
179 DummyClientFace::enablePacketLogging()
180 {
181  onSendInterest.connect([this] (const Interest& interest) {
182  this->sentInterests.push_back(interest);
183  });
184  onSendData.connect([this] (const Data& data) {
185  this->sentData.push_back(data);
186  });
187  onSendNack.connect([this] (const lp::Nack& nack) {
188  this->sentNacks.push_back(nack);
189  });
190 }
191 
192 void
193 DummyClientFace::enableRegistrationReply()
194 {
195  onSendInterest.connect([this] (const Interest& interest) {
196  static const Name localhostRegistration("/localhost/nfd/rib");
197  if (!localhostRegistration.isPrefixOf(interest.getName()))
198  return;
199 
200  nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
201  params.setFaceId(1);
202  params.setOrigin(0);
203  if (interest.getName().get(3) == name::Component("register")) {
204  params.setCost(0);
205  }
206 
208  resp.setCode(200);
209  resp.setBody(params.wireEncode());
210 
211  shared_ptr<Data> data = make_shared<Data>(interest.getName());
212  data->setContent(resp.wireEncode());
213 
215 
216  this->getIoService().post([this, data] { this->receive(*data); });
217  });
218 }
219 
220 template<typename Packet, typename Field, typename Tag>
221 static void
222 addFieldFromTag(lp::Packet& lpPacket, const Packet& packet)
223 {
224  shared_ptr<Tag> tag = static_cast<const TagHost&>(packet).getTag<Tag>();
225  if (tag != nullptr) {
226  lpPacket.add<Field>(*tag);
227  }
228 }
229 
230 template<typename Packet>
231 void
232 DummyClientFace::receive(const Packet& packet)
233 {
234  lp::Packet lpPacket(packet.wireEncode());
235 
236  addFieldFromTag<Packet, lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, packet);
237  addFieldFromTag<Packet, lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, packet);
238  addFieldFromTag<Packet, lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, packet);
239 
240  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
241 }
242 
243 template void
244 DummyClientFace::receive<Interest>(const Interest& packet);
245 
246 template void
247 DummyClientFace::receive<Data>(const Data& packet);
248 
249 template<>
250 void
251 DummyClientFace::receive<lp::Nack>(const lp::Nack& nack)
252 {
253  lp::Packet lpPacket;
254  lpPacket.add<lp::NackField>(nack.getHeader());
255  Block interest = nack.getInterest().wireEncode();
256  lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
257 
258  addFieldFromTag<lp::Nack, lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack);
259  addFieldFromTag<lp::Nack, lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack);
260 
261  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
262 }
263 
264 void
265 DummyClientFace::doProcessEvents(const time::milliseconds& timeout, bool keepThread)
266 {
267  if (m_processEventsOverride != nullptr) {
268  m_processEventsOverride(timeout);
269  }
270  else {
271  this->Face::doProcessEvents(timeout, keepThread);
272  }
273 }
274 
275 } // namespace util
276 } // 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.
Copyright (c) 2011-2015 Regents of the University of California.
static void addFieldFromTag(lp::Packet &lpPacket, const Packet &packet)
Signal< DummyClientFace, Interest > onSendInterest
emits whenever an Interest is sent
represents parameters in a ControlCommand request or response
virtual void pause() override
pause the transport
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:411
Base class to store tag information (e.g., inside Interest and Data packets)
Definition: tag-host.hpp:34
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:156
The packet signing interface.
Definition: key-chain.hpp:47
const uint8_t * wire() const
Definition: block.cpp:495
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents an Interest packet
Definition: interest.hpp:42
const Block & wireEncode() const
use sha256 digest, no signer needs to be specified
std::vector< Data > sentData
Data sent out of this DummyClientFace.
void sign(Data &data, const SigningInfo &params=DEFAULT_SIGNING_INFO)
Sign data according to the supplied signing information.
Definition: key-chain.cpp:517
ReceiveCallback m_receiveCallback
Definition: transport.hpp:124
provides a lightweight signal / event system
virtual void send(const Block &header, const Block &payload) override
send two memory blocks through the transport
virtual void doProcessEvents(const time::milliseconds &timeout, bool keepThread)
Definition: face.cpp:424
virtual void send(const Block &wire) override
send a TLV block through the transport
Signing parameters passed to KeyChain.
boost::asio::io_service & getIoService()
represents a Network Nack
Definition: nack.hpp:40
options for DummyClientFace
virtual void close() override
Close the connection.
size_t size() const
Definition: block.cpp:504
EncodingImpl< EncoderTag > EncodingBuffer
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:125
boost::asio::io_service * m_ioService
Definition: transport.hpp:121
Signal< DummyClientFace, Data > onSendData
emits whenever a Data packet is sent
Name abstraction to represent an absolute name.
Definition: name.hpp:46
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:308
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:690
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 encode()
Encode subblocks into wire buffer.
Definition: block.cpp:355
Block blockFromValue() const
Definition: block.cpp:437
ControlParameters & setOrigin(uint64_t origin)
void receive(const Packet &packet)
cause the Face to receive a packet
ControlCommand response.
represents a Data packet
Definition: data.hpp:37
ControlResponse & setCode(uint32_t code)
virtual void resume() override
resume the transport
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
const Name & getName() const
Definition: interest.hpp:215