NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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 "../transport/transport.hpp"
24 #include "../management/nfd-controller.hpp"
25 #include "../management/nfd-control-response.hpp"
26 
27 #include <boost/asio/io_service.hpp>
28 
29 namespace ndn {
30 namespace util {
31 
32 const DummyClientFace::Options DummyClientFace::DEFAULT_OPTIONS { true, false };
33 
35 {
36 public:
37  void
38  receive(Block block)
39  {
40  block.encode();
41  if (static_cast<bool>(m_receiveCallback)) {
42  m_receiveCallback(block);
43  }
44  }
45 
46  virtual void
48  {
49  }
50 
51  virtual void
53  {
54  }
55 
56  virtual void
58  {
59  }
60 
61  virtual void
62  send(const Block& wire)
63  {
64  onSendBlock(wire);
65  }
66 
67  virtual void
68  send(const Block& header, const Block& payload)
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>())
91 #endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
92 {
93  this->construct(options);
94 }
95 
96 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService,
97  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
98  : Face(make_shared<DummyClientFace::Transport>(), ioService)
101 #endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
102 {
103  this->construct(options);
104 }
105 
106 void
107 DummyClientFace::construct(const Options& options)
108 {
109  static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) {
110  Block packet(blockFromDaemon);
111  packet.encode();
112  lp::Packet lpPacket(packet);
113 
114  Buffer::const_iterator begin, end;
115  std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
116  Block block(&*begin, std::distance(begin, end));
117 
118  if (block.type() == tlv::Interest) {
119  shared_ptr<Interest> interest = make_shared<Interest>(block);
120  if (lpPacket.has<lp::NackField>()) {
121  shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest));
122  nack->setHeader(lpPacket.get<lp::NackField>());
123  if (lpPacket.has<lp::NextHopFaceIdField>()) {
124  nack->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>()));
125  }
126  onSendNack(*nack);
127  }
128  else {
129  if (lpPacket.has<lp::NextHopFaceIdField>()) {
130  interest->setTag(make_shared<lp::NextHopFaceIdTag>(lpPacket.get<lp::NextHopFaceIdField>()));
131  }
132  onSendInterest(*interest);
133  }
134  }
135  else if (block.type() == tlv::Data) {
136  shared_ptr<Data> data = make_shared<Data>(block);
137 
138  if (lpPacket.has<lp::CachePolicyField>()) {
139  data->setTag(make_shared<lp::CachePolicyTag>(lpPacket.get<lp::CachePolicyField>()));
140  }
141 
142  onSendData(*data);
143  }
144  });
145 
146  if (options.enablePacketLogging)
147  this->enablePacketLogging();
148 
149  if (options.enableRegistrationReply)
150  this->enableRegistrationReply();
151 }
152 
153 void
154 DummyClientFace::enablePacketLogging()
155 {
156  onSendInterest.connect([this] (const Interest& interest) {
157  this->sentInterests.push_back(interest);
158  });
159  onSendData.connect([this] (const Data& data) {
160  this->sentData.push_back(data);
161  });
162  onSendNack.connect([this] (const lp::Nack& nack) {
163  this->sentNacks.push_back(nack);
164  });
165 }
166 
167 void
168 DummyClientFace::enableRegistrationReply()
169 {
170  onSendInterest.connect([this] (const Interest& interest) {
171  static const Name localhostRegistration("/localhost/nfd/rib");
172  if (!localhostRegistration.isPrefixOf(interest.getName()))
173  return;
174 
175  nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
176  params.setFaceId(1);
177  params.setOrigin(0);
178  if (interest.getName().get(3) == name::Component("register")) {
179  params.setCost(0);
180  }
181 
183  resp.setCode(200);
184  resp.setBody(params.wireEncode());
185 
186  shared_ptr<Data> data = make_shared<Data>(interest.getName());
187  data->setContent(resp.wireEncode());
188 
189  KeyChain keyChain;
191 
192  this->getIoService().post([this, data] { this->receive(*data); });
193  });
194 }
195 
196 template<typename Packet>
197 void
198 DummyClientFace::receive(const Packet& packet)
199 {
200  lp::Packet lpPacket(packet.wireEncode());
201 
202  shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag =
203  static_cast<const TagHost&>(packet).getTag<lp::IncomingFaceIdTag>();
204  if (incomingFaceIdTag != nullptr) {
205  lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
206  }
207 
208  shared_ptr<lp::NextHopFaceIdTag> nextHopFaceIdTag =
209  static_cast<const TagHost&>(packet).getTag<lp::NextHopFaceIdTag>();
210  if (nextHopFaceIdTag != nullptr) {
211  lpPacket.add<lp::NextHopFaceIdField>(*nextHopFaceIdTag);
212  }
213  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
214 }
215 
216 template void
217 DummyClientFace::receive<Interest>(const Interest& packet);
218 
219 template void
220 DummyClientFace::receive<Data>(const Data& packet);
221 
222 template<>
223 void
224 DummyClientFace::receive<lp::Nack>(const lp::Nack& nack)
225 {
226  lp::Packet lpPacket;
227  lpPacket.add<lp::NackField>(nack.getHeader());
228  Block interest = nack.getInterest().wireEncode();
229  lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
230 
231  shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = nack.getTag<lp::IncomingFaceIdTag>();
232  if (incomingFaceIdTag != nullptr) {
233  lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
234  }
235 
236  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
237 }
238 
239 #ifdef NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
240 
241 shared_ptr<DummyClientFace>
243 {
244  // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
245  return shared_ptr<DummyClientFace>(new DummyClientFace(options));
246 }
247 
248 shared_ptr<DummyClientFace>
249 makeDummyClientFace(boost::asio::io_service& ioService,
250  const DummyClientFace::Options& options)
251 {
252  // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
253  return shared_ptr<DummyClientFace>(new DummyClientFace(ref(ioService), options));
254 }
255 
256 #endif // NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
257 
258 } // namespace util
259 } // namespace ndn
virtual void connect(boost::asio::io_service &io_service, const ReceiveCallback &receiveCallback)
Connect transport.
Definition: transport.hpp:139
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.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
append packet to encoder
Definition: packet.cpp:42
Signal< DummyClientFace, Interest > onSendInterest
emits whenever an Interest is sent
represents parameters in a ControlCommand request or response
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:419
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:48
const uint8_t * wire() const
Definition: block.cpp:495
std::vector< Data > & sentDatas
deprecated alias to sentData
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents an Interest packet
Definition: interest.hpp:45
const Block & wireEncode() const
use sha256 digest, no signer needs to be specified
bool enablePacketLogging
if true, packets sent out of DummyClientFace will be appended to a container
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:501
ReceiveCallback m_receiveCallback
Definition: transport.hpp:110
provides a lightweight signal / event system
Signing parameters passed to KeyChain.
boost::asio::io_service & getIoService()
represents a Network Nack
Definition: nack.hpp:40
provides a tag type for simple types
Definition: tag.hpp:58
virtual void send(const Block &wire)
Send block of data from wire through the transport.
size_t size() const
Definition: block.cpp:504
EncodingImpl< EncoderTag > EncodingBuffer
Signal< DummyClientFace, lp::Nack > onSendNack
emits whenever a NACK is sent
options for DummyClientFace
virtual void close()
Close the connection.
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:119
boost::asio::io_service * m_ioService
Definition: transport.hpp:107
shared_ptr< DummyClientFace > makeDummyClientFace(const DummyClientFace::Options &options)
Create a dummy face with internal IO service.
Signal< DummyClientFace, Data > onSendData
emits whenever a Data packet is sent
DummyClientFace(const Options &options=DummyClientFace::DEFAULT_OPTIONS)
Create a dummy face with internal IO service.
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:320
boost::asio::io_service & getIoService()
Return nullptr (cannot use IoService in simulations), preserved for API compatibility.
Definition: face.hpp:664
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
#define NDN_UTIL_DUMMY_FACE_KEEP_DEPRECATED
Copyright (c) 2013-2015 Regents of the University of California.
ControlParameters & setOrigin(uint64_t origin)
static const Options DEFAULT_OPTIONS
default options
void receive(const Packet &packet)
cause the Face to receive a packet
ControlCommand response.
virtual void send(const Block &header, const Block &payload)
Alternative version of sending data, applying scatter/gather I/O concept.
represents a Data packet
Definition: data.hpp:39
bool enableRegistrationReply
if true, prefix registration command will be automatically replied with a successful response ...
ControlResponse & setCode(uint32_t code)
Signal< Transport, Block > onSendBlock
const Name & getName() const
Definition: interest.hpp:218