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 namespace ndn {
28 namespace util {
29 
30 const DummyClientFace::Options DummyClientFace::DEFAULT_OPTIONS { true, false };
31 
33 {
34 public:
35  void
36  receive(const Block& block)
37  {
38  if (static_cast<bool>(m_receiveCallback))
39  m_receiveCallback(block);
40  }
41 
42  virtual void
44  {
45  }
46 
47  virtual void
49  {
50  }
51 
52  virtual void
54  {
55  }
56 
57  virtual void
58  send(const Block& wire)
59  {
60  onSendBlock(wire);
61  }
62 
63  virtual void
64  send(const Block& header, const Block& payload)
65  {
66  EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
67  encoder.appendByteArray(header.wire(), header.size());
68  encoder.appendByteArray(payload.wire(), payload.size());
69 
70  this->send(encoder.block());
71  }
72 
73  boost::asio::io_service&
75  {
76  return *m_ioService;
77  }
78 
79 public:
80  Signal<Transport, Block> onSendBlock;
81 };
82 
83 DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport)
84  : Face(transport)
85  , m_transport(transport)
86 {
87  this->construct(options);
88 }
89 
90 DummyClientFace::DummyClientFace(const Options& options, shared_ptr<Transport> transport,
91  boost::asio::io_service& ioService)
92  : Face(transport, ioService)
93  , m_transport(transport)
94 {
95  this->construct(options);
96 }
97 
98 void
99 DummyClientFace::construct(const Options& options)
100 {
101  m_transport->onSendBlock.connect([this] (const Block& blockFromDaemon) {
102  const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon);
103 
104  if (block.type() == tlv::Interest) {
105  shared_ptr<Interest> interest = make_shared<Interest>(block);
106  if (&block != &blockFromDaemon)
107  interest->getLocalControlHeader().wireDecode(blockFromDaemon);
108 
109  onSendInterest(*interest);
110  }
111  else if (block.type() == tlv::Data) {
112  shared_ptr<Data> data = make_shared<Data>(block);
113  if (&block != &blockFromDaemon)
114  data->getLocalControlHeader().wireDecode(blockFromDaemon);
115 
116  onSendData(*data);
117  }
118  });
119 
120  if (options.enablePacketLogging)
121  this->enablePacketLogging();
122 
123  if (options.enableRegistrationReply)
124  this->enableRegistrationReply();
125 }
126 
127 void
128 DummyClientFace::enablePacketLogging()
129 {
130  onSendInterest.connect([this] (const Interest& interest) {
131  this->sentInterests.push_back(interest);
132  });
133  onSendData.connect([this] (const Data& data) {
134  this->sentDatas.push_back(data);
135  });
136 }
137 
138 void
139 DummyClientFace::enableRegistrationReply()
140 {
141  onSendInterest.connect([this] (const Interest& interest) {
142  static const Name localhostRegistration("/localhost/nfd/rib");
143  if (!localhostRegistration.isPrefixOf(interest.getName()))
144  return;
145 
146  nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
147  params.setFaceId(1);
148  params.setOrigin(0);
149  if (interest.getName().get(3) == name::Component("register")) {
150  params.setCost(0);
151  }
152 
154  resp.setCode(200);
155  resp.setBody(params.wireEncode());
156 
157  shared_ptr<Data> data = make_shared<Data>(interest.getName());
158  data->setContent(resp.wireEncode());
159 
160  KeyChain keyChain;
162 
163  this->getIoService().post([this, data] { this->receive(*data); });
164  });
165 }
166 
167 template<typename Packet>
168 void
169 DummyClientFace::receive(const Packet& packet)
170 {
171  // do not restrict what injected control header can contain
172  if (!packet.getLocalControlHeader().empty(nfd::LocalControlHeader::ENCODE_ALL)) {
173 
174  Block header = packet.getLocalControlHeader().wireEncode(packet,
176  Block payload = packet.wireEncode();
177 
178  EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
179  encoder.appendByteArray(header.wire(), header.size());
180  encoder.appendByteArray(payload.wire(), payload.size());
181 
182  m_transport->receive(encoder.block());
183  }
184  else {
185  m_transport->receive(packet.wireEncode());
186  }
187 }
188 
189 template void
190 DummyClientFace::receive<Interest>(const Interest& packet);
191 
192 template void
193 DummyClientFace::receive<Data>(const Data& packet);
194 
195 
196 shared_ptr<DummyClientFace>
198 {
199  // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
200  return shared_ptr<DummyClientFace>(
201  new DummyClientFace(options, make_shared<DummyClientFace::Transport>()));
202 }
203 
204 shared_ptr<DummyClientFace>
205 makeDummyClientFace(boost::asio::io_service& ioService,
206  const DummyClientFace::Options& options)
207 {
208  // cannot use make_shared<DummyClientFace> because DummyClientFace constructor is private
209  return shared_ptr<DummyClientFace>(
210  new DummyClientFace(options, make_shared<DummyClientFace::Transport>(),
211  ref(ioService)));
212 }
213 
214 } // namespace util
215 } // namespace ndn
ControlParameters & setFaceId(uint64_t faceId)
const Name & getName() const
Definition: interest.hpp:216
Copyright (c) 2011-2015 Regents of the University of California.
Signal< DummyClientFace, Interest > onSendInterest
emits whenever an Interest is sent
represents parameters in a ControlCommand request or response
std::vector< Interest > sentInterests
Interests sent out of this DummyClientFace.
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents an Interest packet
Definition: interest.hpp:45
use sha256 digest, no signer needs to be specified
bool enablePacketLogging
if true, packets sent out of DummyClientFace will be appended to a container
const Block & wireEncode() const
friend shared_ptr< DummyClientFace > makeDummyClientFace(const DummyClientFace::Options &options)
ReceiveCallback m_receiveCallback
Definition: transport.hpp:103
Signing parameters passed to KeyChain.
boost::asio::io_service & getIoService()
Face()
Create a new Face using the default transport (UnixTransport)
Definition: face.cpp:40
virtual void send(const Block &wire)
Send block of data from.
static const Block & getPayload(const Block &wire)
size_t size() const
Definition: block.cpp:504
EncodingImpl< EncoderTag > EncodingBuffer
Block blockFromValue() const
Definition: block.cpp:437
options for DummyClientFace
virtual void close()
Close the connection.
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:100
std::vector< Data > sentDatas
Data sent out of this DummyClientFace.
boost::asio::io_service * m_ioService
Definition: transport.hpp:100
Signal< DummyClientFace, Data > onSendData
emits whenever a Data packet is sent
Name abstraction to represent an absolute name.
Definition: name.hpp:46
size_t appendByteArray(const uint8_t *array, size_t length)
Append a byte array array of length length.
Definition: encoder.cpp:135
uint32_t type() const
Definition: block.hpp:346
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)
const uint8_t * wire() const
Definition: block.cpp:495
ControlParameters & setOrigin(uint64_t origin)
static const Options DEFAULT_OPTIONS
default options
void receive(const Packet &packet)
cause the Face to receive a packet
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
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 ...
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:419
ControlResponse & setCode(uint32_t code)
Signal< Transport, Block > onSendBlock