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 
35 class DummyClientFace::Transport : public ndn::Transport
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&
79  getIoService()
80  {
81  return *m_ioService;
82  }
83 
84 public:
85  Signal<Transport, Block> onSendBlock;
86 };
87 
88 struct DummyClientFace::BroadcastLink
89 {
90  std::vector<DummyClientFace*> faces;
91 };
92 
94  : Error("Face has already been linked to another face")
95 {
96 }
97 
98 DummyClientFace::DummyClientFace(const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
99  : Face(make_shared<DummyClientFace::Transport>())
100  , m_internalKeyChain(new KeyChain)
101  , m_keyChain(*m_internalKeyChain)
102 {
103  this->construct(options);
104 }
105 
107  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
108  : Face(make_shared<DummyClientFace::Transport>(), keyChain)
109  , m_keyChain(keyChain)
110 {
111  this->construct(options);
112 }
113 
114 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService,
115  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
116  : Face(make_shared<DummyClientFace::Transport>(), ioService)
117  , m_internalKeyChain(new KeyChain)
118  , m_keyChain(*m_internalKeyChain)
119 {
120  this->construct(options);
121 }
122 
123 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain,
124  const Options& options/* = DummyClientFace::DEFAULT_OPTIONS*/)
125  : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain)
126  , m_keyChain(keyChain)
127 {
128  this->construct(options);
129 }
130 
132 {
133  unlink();
134 }
135 
136 void
137 DummyClientFace::construct(const Options& options)
138 {
139  static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (const Block& blockFromDaemon) {
140  Block packet(blockFromDaemon);
141  packet.encode();
142  lp::Packet lpPacket(packet);
143 
144  Buffer::const_iterator begin, end;
145  std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
146  Block block(&*begin, std::distance(begin, end));
147 
148  if (block.type() == tlv::Interest) {
149  shared_ptr<Interest> interest = make_shared<Interest>(block);
150  if (lpPacket.has<lp::NackField>()) {
151  shared_ptr<lp::Nack> nack = make_shared<lp::Nack>(std::move(*interest));
152  nack->setHeader(lpPacket.get<lp::NackField>());
153  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*nack, lpPacket);
154  onSendNack(*nack);
155  }
156  else {
157  addTagFromField<lp::NextHopFaceIdTag, lp::NextHopFaceIdField>(*interest, lpPacket);
158  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*interest, lpPacket);
159  onSendInterest(*interest);
160  }
161  }
162  else if (block.type() == tlv::Data) {
163  shared_ptr<Data> data = make_shared<Data>(block);
164  addTagFromField<lp::CachePolicyTag, lp::CachePolicyField>(*data, lpPacket);
165  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*data, lpPacket);
166  onSendData(*data);
167  }
168  });
169 
170  if (options.enablePacketLogging)
171  this->enablePacketLogging();
172 
173  if (options.enableRegistrationReply)
174  this->enableRegistrationReply();
175 
176  m_processEventsOverride = options.processEventsOverride;
177 
178  enableBroadcastLink();
179 }
180 
181 void
182 DummyClientFace::enableBroadcastLink()
183 {
184  this->onSendInterest.connect([this] (const Interest& interest) {
185  if (m_bcastLink != nullptr) {
186  for (auto otherFace : m_bcastLink->faces) {
187  if (otherFace != this) {
188  otherFace->receive(interest);
189  }
190  }
191  }
192  });
193  this->onSendData.connect([this] (const Data& data) {
194  if (m_bcastLink != nullptr) {
195  for (auto otherFace : m_bcastLink->faces) {
196  if (otherFace != this) {
197  otherFace->receive(data);
198  }
199  }
200  }
201  });
202  this->onSendNack.connect([this] (const lp::Nack& nack) {
203  if (m_bcastLink != nullptr) {
204  for (auto otherFace : m_bcastLink->faces) {
205  if (otherFace != this) {
206  otherFace->receive(nack);
207  }
208  }
209  }
210  });
211 }
212 
213 void
214 DummyClientFace::enablePacketLogging()
215 {
216  onSendInterest.connect([this] (const Interest& interest) {
217  this->sentInterests.push_back(interest);
218  });
219  onSendData.connect([this] (const Data& data) {
220  this->sentData.push_back(data);
221  });
222  onSendNack.connect([this] (const lp::Nack& nack) {
223  this->sentNacks.push_back(nack);
224  });
225 }
226 
227 void
228 DummyClientFace::enableRegistrationReply()
229 {
230  onSendInterest.connect([this] (const Interest& interest) {
231  static const Name localhostRegistration("/localhost/nfd/rib");
232  if (!localhostRegistration.isPrefixOf(interest.getName()))
233  return;
234 
235  nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
236  params.setFaceId(1);
238  if (interest.getName().get(3) == name::Component("register")) {
239  params.setCost(0);
240  }
241 
243  resp.setCode(200);
244  resp.setBody(params.wireEncode());
245 
246  shared_ptr<Data> data = make_shared<Data>(interest.getName());
247  data->setContent(resp.wireEncode());
248 
250 
251  this->getIoService().post([this, data] { this->receive(*data); });
252  });
253 }
254 
255 void
257 {
258  lp::Packet lpPacket(interest.wireEncode());
259 
260  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, interest);
261  addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest);
262  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest);
263 
264  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
265 }
266 
267 void
269 {
270  lp::Packet lpPacket(data.wireEncode());
271 
272  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, data);
273  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data);
274 
275  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
276 }
277 
278 void
280 {
281  lp::Packet lpPacket;
282  lpPacket.add<lp::NackField>(nack.getHeader());
283  Block interest = nack.getInterest().wireEncode();
284  lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
285 
286  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack);
287  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack);
288 
289  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
290 }
291 
292 void
294 {
295  if (m_bcastLink != nullptr && other.m_bcastLink != nullptr) {
296  if (m_bcastLink != other.m_bcastLink) {
297  // already on different links
298  BOOST_THROW_EXCEPTION(AlreadyLinkedError());
299  }
300  }
301  else if (m_bcastLink == nullptr && other.m_bcastLink != nullptr) {
302  m_bcastLink = other.m_bcastLink;
303  m_bcastLink->faces.push_back(this);
304  }
305  else if (m_bcastLink != nullptr && other.m_bcastLink == nullptr) {
306  other.m_bcastLink = m_bcastLink;
307  m_bcastLink->faces.push_back(&other);
308  }
309  else {
310  m_bcastLink = other.m_bcastLink = make_shared<BroadcastLink>();
311  m_bcastLink->faces.push_back(this);
312  m_bcastLink->faces.push_back(&other);
313  }
314 }
315 
316 void
318 {
319  if (m_bcastLink == nullptr) {
320  return;
321  }
322 
323  auto it = std::find(m_bcastLink->faces.begin(), m_bcastLink->faces.end(), this);
324  BOOST_ASSERT(it != m_bcastLink->faces.end());
325  m_bcastLink->faces.erase(it);
326 
327  if (m_bcastLink->faces.size() == 1) {
328  m_bcastLink->faces[0]->m_bcastLink = nullptr;
329  m_bcastLink->faces.clear();
330  }
331  m_bcastLink = nullptr;
332 }
333 
334 void
335 DummyClientFace::doProcessEvents(time::milliseconds timeout, bool keepThread)
336 {
337  if (m_processEventsOverride != nullptr) {
338  m_processEventsOverride(timeout);
339  }
340  else {
341  this->Face::doProcessEvents(timeout, keepThread);
342  }
343 }
344 
345 } // namespace util
346 } // 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) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:311
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
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
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:119
Signing parameters passed to KeyChain.
represents a Network Nack
Definition: nack.hpp:40
options for DummyClientFace
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:324
shared_ptr< Transport > getTransport()
Definition: face.cpp:172
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
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:116
Signal< DummyClientFace, Data > onSendData
emits whenever a Data packet is sent
void unlink()
unlink the broadcast media if previously linked
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
void linkTo(DummyClientFace &other)
link another DummyClientFace through a broadcast media
provides TLV-block delivery service
Definition: transport.hpp:35
boost::asio::io_service & getIoService()
Definition: face.hpp:472
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)
ControlParameters & setOrigin(RouteOrigin origin)
ControlCommand response.
Represents a Data packet.
Definition: data.hpp:35
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
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