esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
packet-handler.hpp
Go to the documentation of this file.
1 #ifndef NDNPH_FACE_PACKET_HANDLER_HPP
2 #define NDNPH_FACE_PACKET_HANDLER_HPP
3 
4 #include "../port/clock/port.hpp"
5 #include "face.hpp"
6 
7 namespace ndnph {
8 
11 public:
13 
15  explicit PacketHandler() = default;
16 
18  explicit PacketHandler(Face& face, int8_t prio = 0) {
19  face.addHandler(*this, prio);
20  }
21 
22 protected:
24  virtual ~PacketHandler() {
25  if (m_face != nullptr) {
26  m_face->removeHandler(*this);
27  }
28  }
29 
30  Face* getFace() const {
31  return m_face;
32  }
33 
39  return m_face == nullptr ? nullptr : m_face->getCurrentPacketInfo();
40  }
41 
48  template<typename Packet>
49  bool send(Region& region, const Packet& packet, PacketInfo pi = {}) {
50  return m_face != nullptr && m_face->send(region, packet, pi);
51  }
52 
55  public:
56  explicit WithEndpointId(uint64_t endpointId)
58 
59  void operator()(PacketInfo& pi) const {
61  }
62 
63  public:
64  uint64_t endpointId = 0;
65  };
66 
68  class WithPitToken {
69  public:
71  : pitToken(pitToken) {}
72 
73  void operator()(PacketInfo& pi) const {
74  pi.pitToken = pitToken;
75  }
76 
77  public:
79  };
80 
86  template<typename Packet, typename... PacketInfoModifier>
87  bool send(Region& region, const Packet& packet, const PacketInfoModifier&... pim) {
88  return send(region, packet, PacketInfo(), pim...);
89  }
90 
103  template<typename Packet, typename... Arg,
104  typename = typename std::enable_if<
105  !std::is_base_of<Region, typename std::decay<Packet>::type>::value>::type>
106  bool send(const Packet& packet, Arg&&... arg) {
107  return send(regionOf(packet), packet, std::forward<Arg>(arg)...);
108  }
109 
118  template<typename... Arg>
119  bool reply(Arg&&... arg) {
120  const PacketInfo* pi = getCurrentPacketInfo();
121  return pi != nullptr && send(std::forward<Arg>(arg)..., *pi);
122  }
123 
126  public:
128  : m_ph(*ph) {
129  port::RandomSource::generate(reinterpret_cast<uint8_t*>(&m_pitToken), sizeof(m_pitToken));
130  expireNow();
131  }
132 
140  template<typename Packet, typename... Arg>
141  bool send(const Packet& interest, int timeout, Arg&&... arg) {
142  m_expire = ndnph::port::Clock::add(ndnph::port::Clock::now(), timeout);
143 
144  do {
145  ++m_pitToken;
146  } while (m_pitToken == 0);
147  auto token = lp::PitToken::from4(m_pitToken);
148  return m_ph.send(interest, WithPitToken(token), std::forward<Arg>(arg)...);
149  }
150 
151  template<typename Packet>
152  bool send(const Packet& interest) {
153  return send(interest, interest.getLifetime());
154  }
155 
156  template<typename Packet, typename ArgFirst, typename... Arg,
157  typename = typename std::enable_if<!std::is_integral<ArgFirst>::value>::type>
158  bool send(const Packet& interest, ArgFirst&& arg1, Arg&&... arg) {
159  return send(interest, interest.getLifetime(), std::forward<ArgFirst>(arg1),
160  std::forward<Arg>(arg)...);
161  }
162 
171  bool matchPitToken() const {
172  auto pi = m_ph.getCurrentPacketInfo();
173  return pi != nullptr && pi->pitToken.to4() == m_pitToken;
174  }
175 
182  bool match(const Data& data, const Interest& interest) const {
183  return matchPitToken() && data.canSatisfy(interest);
184  }
185 
193  bool match(const Data& data, const Name& name, bool canBePrefix = true) const {
194  StaticRegion<128> region;
195  auto interest = region.create<Interest>();
196  NDNPH_ASSERT(!!interest);
197  interest.setName(name);
198  interest.setCanBePrefix(canBePrefix);
199  return match(data, interest);
200  }
201 
203  void expireNow() {
204  m_expire = port::Clock::now();
205  }
206 
208  bool expired() const {
210  }
211 
212  private:
213  PacketHandler& m_ph;
214  uint32_t m_pitToken = 0;
215  port::Clock::Time m_expire;
216  };
217 
218 private:
220  virtual void loop() {}
221 
227  virtual bool processInterest(Interest) {
228  return false;
229  }
230 
236  virtual bool processData(Data) {
237  return false;
238  }
239 
245  virtual bool processNack(Nack) {
246  return false;
247  }
248 
249  template<typename Packet, typename PimFirst, typename... PimRest>
250  bool send(Region& region, const Packet& packet, PacketInfo pi, const PimFirst& pimFirst,
251  const PimRest&... pimRest) {
252  pimFirst(pi);
253  return send(region, packet, pi, pimRest...);
254  }
255 
256 private:
257  Face* m_face = nullptr;
258  PacketHandler* m_next = nullptr;
259  int8_t m_prio = 0;
260  friend Face;
261 };
262 
263 } // namespace ndnph
264 
265 #define NDNPH_FACE_PACKET_HANDLER_HPP_END
266 #include "face-impl.inc"
267 
268 #endif // NDNPH_FACE_PACKET_HANDLER_HPP
static bool generate(uint8_t *output, size_t count)
Data packet.
Definition: data.hpp:136
bool canSatisfy(const InterestT &interest, CanSatisfyOptions opts=CanSatisfyNone) const
Determine whether Data can satisfy Interest.
Definition: data.hpp:309
Network layer face.
Definition: face.hpp:12
const PacketInfo * getCurrentPacketInfo() const
Definition: face.hpp:84
bool removeHandler(PacketHandler &h)
Remove a packet handler.
bool addHandler(PacketHandler &h, int8_t prio=0)
Add a packet handler.
bool send(Region &region, const Packet &packet, PacketInfo pi)
Synchronously transmit a packet.
Interest packet.
Definition: interest.hpp:284
Nack packet.
Definition: nack.hpp:73
Name.
Definition: name.hpp:14
Helper to keep track an outgoing pending Interest.
Definition: packet-handler.hpp:125
bool expired() const
Determine if the pending Interest has expired / timed out.
Definition: packet-handler.hpp:208
bool match(const Data &data, const Name &name, bool canBePrefix=true) const
Check Interest-Data match.
Definition: packet-handler.hpp:193
bool match(const Data &data, const Interest &interest) const
Check Interest-Data match.
Definition: packet-handler.hpp:182
void expireNow()
Set expire time to now.
Definition: packet-handler.hpp:203
bool send(const Packet &interest, ArgFirst &&arg1, Arg &&... arg)
Definition: packet-handler.hpp:158
OutgoingPendingInterest(PacketHandler *ph)
Definition: packet-handler.hpp:127
bool matchPitToken() const
Compare PIT token of current incoming packet against last outgoing Interest.
Definition: packet-handler.hpp:171
bool send(const Packet &interest, int timeout, Arg &&... arg)
Send an Interest.
Definition: packet-handler.hpp:141
bool send(const Packet &interest)
Definition: packet-handler.hpp:152
Set EndpointId of PacketInfo.
Definition: packet-handler.hpp:54
WithEndpointId(uint64_t endpointId)
Definition: packet-handler.hpp:56
uint64_t endpointId
Definition: packet-handler.hpp:64
void operator()(PacketInfo &pi) const
Definition: packet-handler.hpp:59
Set PIT token of PacketInfo.
Definition: packet-handler.hpp:68
WithPitToken(const lp::PitToken &pitToken)
Definition: packet-handler.hpp:70
lp::PitToken pitToken
Definition: packet-handler.hpp:78
void operator()(PacketInfo &pi) const
Definition: packet-handler.hpp:73
Base class to receive packets from Face.
Definition: packet-handler.hpp:10
virtual bool processNack(Nack)
Override to receive Nack packets.
Definition: packet-handler.hpp:245
PacketHandler(Face &face, int8_t prio=0)
Construct and add handler to Face.
Definition: packet-handler.hpp:18
virtual bool processData(Data)
Override to receive Data packets.
Definition: packet-handler.hpp:236
bool send(const Packet &packet, Arg &&... arg)
Synchronously transmit a packet.
Definition: packet-handler.hpp:106
bool send(Region &region, const Packet &packet, PacketInfo pi={})
Synchronously transmit a packet.
Definition: packet-handler.hpp:49
Face::PacketInfo PacketInfo
Definition: packet-handler.hpp:12
Face * getFace() const
Definition: packet-handler.hpp:30
const PacketInfo * getCurrentPacketInfo() const
Retrieve information about current processing packet.
Definition: packet-handler.hpp:38
bool send(Region &region, const Packet &packet, const PacketInfoModifier &... pim)
Synchronously transmit a packet.
Definition: packet-handler.hpp:87
virtual bool processInterest(Interest)
Override to receive Interest packets.
Definition: packet-handler.hpp:227
virtual void loop()
Override to be invoked periodically.
Definition: packet-handler.hpp:220
virtual ~PacketHandler()
Remove handler from Face.
Definition: packet-handler.hpp:24
bool reply(Arg &&... arg)
Synchronously transmit a packet in reply to current processing packet.
Definition: packet-handler.hpp:119
PacketHandler()=default
Construct without adding to Face.
Region-based memory allocator thats owns memory of NDNph objects.
Definition: region.hpp:9
RefType create(Arg &&... arg)
Allocate and create an object, and return its reference.
Definition: region.hpp:90
Region with statically allocated memory.
Definition: region.hpp:143
PIT token field.
Definition: lp.hpp:35
static PitToken from4(uint32_t n)
Construct 4-octet PIT token from uint32.
Definition: lp.hpp:38
uint32_t to4() const
Interpret 4-octet PIT token as uint32.
Definition: lp.hpp:60
static bool isBefore(Time a, Time b)
Definition: ino.hpp:42
static Time now()
Definition: ino.hpp:21
static Time add(Time t, int ms)
Definition: ino.hpp:27
#define NDNPH_ASSERT(x)
Definition: common.hpp:30
Definition: fs.hpp:33
Definition: face.hpp:14
lp::PitToken pitToken
Definition: face.hpp:16
uint64_t endpointId
Definition: face.hpp:15