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-2022 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 
23 #include "ndn-cxx/impl/lp-field-tag.hpp"
24 #include "ndn-cxx/lp/packet.hpp"
25 #include "ndn-cxx/lp/tags.hpp"
29 
30 #include <boost/asio/io_service.hpp>
31 
32 namespace ndn {
33 namespace util {
34 
35 class DummyClientFace::Transport final : 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  send(const Block& block) final
49  {
50  onSendBlock(block);
51  }
52 
53  void
54  close() final
55  {
56  }
57 
58  void
59  pause() final
60  {
61  }
62 
63  void
64  resume() final
65  {
66  }
67 
68 public:
69  Signal<Transport, Block> onSendBlock;
70 };
71 
72 struct DummyClientFace::BroadcastLink
73 {
74  std::vector<DummyClientFace*> faces;
75 };
76 
78  : Error("Face has already been linked to another face")
79 {
80 }
81 
83  : Face(make_shared<DummyClientFace::Transport>())
84  , m_internalKeyChain(make_unique<KeyChain>())
85  , m_keyChain(*m_internalKeyChain)
86 {
87  this->construct(options);
88 }
89 
90 DummyClientFace::DummyClientFace(KeyChain& keyChain, const Options& options)
91  : Face(make_shared<DummyClientFace::Transport>(), keyChain)
92  , m_keyChain(keyChain)
93 {
94  this->construct(options);
95 }
96 
97 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, const Options& options)
98  : Face(make_shared<DummyClientFace::Transport>(), ioService)
99  , m_internalKeyChain(make_unique<KeyChain>())
100  , m_keyChain(*m_internalKeyChain)
101 {
102  this->construct(options);
103 }
104 
105 DummyClientFace::DummyClientFace(boost::asio::io_service& ioService, KeyChain& keyChain, const Options& options)
106  : Face(make_shared<DummyClientFace::Transport>(), ioService, keyChain)
107  , m_keyChain(keyChain)
108 {
109  this->construct(options);
110 }
111 
113 {
114  unlink();
115 }
116 
117 void
118 DummyClientFace::construct(const Options& options)
119 {
120  static_pointer_cast<Transport>(getTransport())->onSendBlock.connect([this] (Block packet) {
121  packet.encode();
122  lp::Packet lpPacket(packet);
123  auto frag = lpPacket.get<lp::FragmentField>();
124  Block block({frag.first, frag.second});
125 
126  if (block.type() == tlv::Interest) {
127  auto interest = make_shared<Interest>(block);
128  if (lpPacket.has<lp::NackField>()) {
129  auto nack = make_shared<lp::Nack>(std::move(*interest));
130  nack->setHeader(lpPacket.get<lp::NackField>());
131  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*nack, lpPacket);
132  onSendNack(*nack);
133  }
134  else {
135  addTagFromField<lp::NextHopFaceIdTag, lp::NextHopFaceIdField>(*interest, lpPacket);
136  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*interest, lpPacket);
137  onSendInterest(*interest);
138  }
139  }
140  else if (block.type() == tlv::Data) {
141  auto data = make_shared<Data>(block);
142  addTagFromField<lp::CachePolicyTag, lp::CachePolicyField>(*data, lpPacket);
143  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(*data, lpPacket);
144  onSendData(*data);
145  }
146  });
147 
148  if (options.enablePacketLogging)
149  this->enablePacketLogging();
150 
151  if (options.enableRegistrationReply)
152  this->enableRegistrationReply();
153 
154  m_processEventsOverride = options.processEventsOverride;
155 
156  enableBroadcastLink();
157 }
158 
159 void
160 DummyClientFace::enableBroadcastLink()
161 {
162  this->onSendInterest.connect([this] (const Interest& interest) {
163  if (m_bcastLink != nullptr) {
164  for (auto otherFace : m_bcastLink->faces) {
165  if (otherFace != this) {
166  otherFace->receive(interest);
167  }
168  }
169  }
170  });
171  this->onSendData.connect([this] (const Data& data) {
172  if (m_bcastLink != nullptr) {
173  for (auto otherFace : m_bcastLink->faces) {
174  if (otherFace != this) {
175  otherFace->receive(data);
176  }
177  }
178  }
179  });
180  this->onSendNack.connect([this] (const lp::Nack& nack) {
181  if (m_bcastLink != nullptr) {
182  for (auto otherFace : m_bcastLink->faces) {
183  if (otherFace != this) {
184  otherFace->receive(nack);
185  }
186  }
187  }
188  });
189 }
190 
191 void
192 DummyClientFace::enablePacketLogging()
193 {
194  onSendInterest.connect([this] (const Interest& interest) {
195  this->sentInterests.push_back(interest);
196  });
197  onSendData.connect([this] (const Data& data) {
198  this->sentData.push_back(data);
199  });
200  onSendNack.connect([this] (const lp::Nack& nack) {
201  this->sentNacks.push_back(nack);
202  });
203 }
204 
205 void
206 DummyClientFace::enableRegistrationReply()
207 {
208  onSendInterest.connect([this] (const Interest& interest) {
209  static const Name localhostRegistration("/localhost/nfd/rib");
210  if (!localhostRegistration.isPrefixOf(interest.getName()))
211  return;
212 
213  nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
214  params.setFaceId(1);
215  params.setOrigin(nfd::ROUTE_ORIGIN_APP);
216  if (interest.getName().get(3) == name::Component("register")) {
217  params.setCost(0);
218  }
219 
221  resp.setCode(200);
222  resp.setBody(params.wireEncode());
223 
224  shared_ptr<Data> data = make_shared<Data>(interest.getName());
225  data->setContent(resp.wireEncode());
226 
227  m_keyChain.sign(*data, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_SHA256));
228 
229  this->getIoService().post([this, data] { this->receive(*data); });
230  });
231 }
232 
233 void
234 DummyClientFace::receive(const Interest& interest)
235 {
236  lp::Packet lpPacket(interest.wireEncode());
237 
238  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, interest);
239  addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest);
240  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest);
241 
242  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
243 }
244 
245 void
246 DummyClientFace::receive(const Data& data)
247 {
248  lp::Packet lpPacket(data.wireEncode());
249 
250  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, data);
251  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data);
252 
253  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
254 }
255 
256 void
257 DummyClientFace::receive(const lp::Nack& nack)
258 {
259  lp::Packet lpPacket;
260  lpPacket.add<lp::NackField>(nack.getHeader());
261  Block interest = nack.getInterest().wireEncode();
262  lpPacket.add<lp::FragmentField>(make_pair(interest.begin(), interest.end()));
263 
264  addFieldFromTag<lp::IncomingFaceIdField, lp::IncomingFaceIdTag>(lpPacket, nack);
265  addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, nack);
266 
267  static_pointer_cast<Transport>(getTransport())->receive(lpPacket.wireEncode());
268 }
269 
270 void
271 DummyClientFace::linkTo(DummyClientFace& other)
272 {
273  if (m_bcastLink != nullptr && other.m_bcastLink != nullptr) {
274  if (m_bcastLink != other.m_bcastLink) {
275  // already on different links
277  }
278  }
279  else if (m_bcastLink == nullptr && other.m_bcastLink != nullptr) {
280  m_bcastLink = other.m_bcastLink;
281  m_bcastLink->faces.push_back(this);
282  }
283  else if (m_bcastLink != nullptr && other.m_bcastLink == nullptr) {
284  other.m_bcastLink = m_bcastLink;
285  m_bcastLink->faces.push_back(&other);
286  }
287  else {
288  m_bcastLink = other.m_bcastLink = make_shared<BroadcastLink>();
289  m_bcastLink->faces.push_back(this);
290  m_bcastLink->faces.push_back(&other);
291  }
292 }
293 
294 void
295 DummyClientFace::unlink()
296 {
297  if (m_bcastLink == nullptr) {
298  return;
299  }
300 
301  auto it = std::find(m_bcastLink->faces.begin(), m_bcastLink->faces.end(), this);
302  BOOST_ASSERT(it != m_bcastLink->faces.end());
303  m_bcastLink->faces.erase(it);
304 
305  if (m_bcastLink->faces.size() == 1) {
306  m_bcastLink->faces[0]->m_bcastLink = nullptr;
307  m_bcastLink->faces.clear();
308  }
309  m_bcastLink = nullptr;
310 }
311 
312 void
313 DummyClientFace::doProcessEvents(time::milliseconds timeout, bool keepThread)
314 {
315  if (m_processEventsOverride != nullptr) {
316  m_processEventsOverride(timeout);
317  }
318  else {
319  this->Face::doProcessEvents(timeout, keepThread);
320  }
321 }
322 
323 } // namespace util
324 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
void encode()
Encode sub-elements into TLV-VALUE.
Definition: block.cpp:368
Represents a Data packet.
Definition: data.hpp:38
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder.
Definition: data.cpp:46
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:91
shared_ptr< Transport > getTransport() const
Returns the underlying transport.
Definition: face.hpp:433
Represents an Interest packet.
Definition: interest.hpp:50
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
Definition: interest.cpp:65
Provides TLV-block delivery service.
Definition: transport.hpp:36
Declare a field.
Definition: field-decl.hpp:177
represents a Network Nack
Definition: nack.hpp:39
const Interest & getInterest() const
Definition: nack.hpp:51
const NackHeader & getHeader() const
Definition: nack.hpp:63
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Definition: packet.hpp:148
Block wireEncode() const
encode packet into wire format
Definition: packet.cpp:119
ControlResponse & setBody(const Block &body)
ControlResponse & setCode(uint32_t code)
A client-side face for unit testing.
void receive(const Interest &interest)
cause the Face to receive an interest
Signal< DummyClientFace, Interest > onSendInterest
emits whenever an Interest is sent
Signal< DummyClientFace, lp::Nack > onSendNack
emits whenever a Nack is sent
void unlink()
unlink the broadcast media if previously linked
Signal< DummyClientFace, Data > onSendData
emits whenever a Data packet is sent
DummyClientFace(const Options &options=Options())
Create a dummy face with internal IO service.
provides a lightweight signal / event system
Definition: signal.hpp:53
#define NDN_THROW(e)
Definition: exception.hpp:61
mgmt::ControlResponse ControlResponse
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
@ ControlParameters
Definition: tlv-nfd.hpp:35
@ Name
Definition: tlv.hpp:67
@ Data
Definition: tlv.hpp:66
@ Interest
Definition: tlv.hpp:65
Definition: data.cpp:25