esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
face.hpp
Go to the documentation of this file.
1 #ifndef NDNPH_FACE_FACE_HPP
2 #define NDNPH_FACE_FACE_HPP
3 
4 #include "../packet/lp.hpp"
5 #include "transport.hpp"
6 
7 namespace ndnph {
8 
9 class PacketHandler;
10 
12 class Face : public WithRegion {
13 public:
14  struct PacketInfo {
15  uint64_t endpointId = 0;
17  };
18 
30  explicit Face(Region& region, Transport& transport)
32  , m_transport(transport) {
33  m_transport.setRxCallback(transportRx, this);
34  }
35 
36  explicit Face(Transport& transport)
37  : Face(*new OwnRegion(), transport) {
38  m_ownRegion.reset(static_cast<OwnRegion*>(&region));
39  }
40 
43  return m_transport;
44  }
45 
54  m_frag = &frag;
55  }
56 
65  m_reass = &reass;
66  }
67 
72  bool addHandler(PacketHandler& h, int8_t prio = 0);
73 
76 
82  void loop();
83 
85  return m_currentPacketInfo;
86  }
87 
92  template<typename Packet>
93  bool send(Region& region, const Packet& packet, PacketInfo pi);
94 
95 private:
96  class ScopedCurrentPacketInfo {
97  public:
98  explicit ScopedCurrentPacketInfo(Face& face, PacketInfo& pi)
99  : m_face(face) {
100  m_face.m_currentPacketInfo = &pi;
101  }
102 
103  ~ScopedCurrentPacketInfo() {
104  m_face.m_currentPacketInfo = nullptr;
105  }
106 
107  private:
108  Face& m_face;
109  };
110 
111  static void transportRx(void* self, const uint8_t* pkt, size_t pktLen, uint64_t endpointId) {
112  reinterpret_cast<Face*>(self)->transportRx(pkt, pktLen, endpointId);
113  }
114 
115  void transportRx(const uint8_t* pkt, size_t pktLen, uint64_t endpointId);
116 
117  template<typename Packet, typename H = bool (PacketHandler::*)(Packet)>
118  bool process(H processPacket, Packet packet);
119 
120 private:
121  using OwnRegion = StaticRegion<2048>;
122  std::unique_ptr<OwnRegion> m_ownRegion;
123  Transport& m_transport;
124  lp::Fragmenter* m_frag = nullptr;
125  lp::Reassembler* m_reass = nullptr;
126  PacketHandler* m_handler = nullptr;
127  const PacketInfo* m_currentPacketInfo = nullptr;
128 };
129 
130 } // namespace ndnph
131 
132 #define NDNPH_FACE_FACE_HPP_END
133 #include "face-impl.inc"
134 #include "packet-handler.hpp"
135 
136 #endif // NDNPH_FACE_FACE_HPP
Network layer face.
Definition: face.hpp:12
void setReassembler(lp::Reassembler &reass)
Enable NDNLPv2 reassembly.
Definition: face.hpp:64
void setFragmenter(lp::Fragmenter &frag)
Enable NDNLPv2 fragmentation.
Definition: face.hpp:53
Transport & getTransport() const
Access the underlying transport.
Definition: face.hpp:42
Face(Region &region, Transport &transport)
Constructor.
Definition: face.hpp:30
const PacketInfo * getCurrentPacketInfo() const
Definition: face.hpp:84
void loop()
Process periodical events.
bool removeHandler(PacketHandler &h)
Remove a packet handler.
bool addHandler(PacketHandler &h, int8_t prio=0)
Add a packet handler.
Face(Transport &transport)
Definition: face.hpp:36
bool send(Region &region, const Packet &packet, PacketInfo pi)
Synchronously transmit a packet.
Base class to receive packets from Face.
Definition: packet-handler.hpp:10
Region-based memory allocator thats owns memory of NDNph objects.
Definition: region.hpp:9
Base class of an object associated with a Region.
Definition: region.hpp:182
Region & region
Definition: region.hpp:194
NDNLPv2 fragmenter.
Definition: lp.hpp:196
PIT token field.
Definition: lp.hpp:35
NDNLPv2 fragmenter.
Definition: lp.hpp:471
Base class of low-level transport.
Definition: transport.hpp:10
void setRxCallback(RxCallback cb, void *ctx)
Set incoming packet callback.
Definition: transport.hpp:27
Definition: fs.hpp:33
transport::Transport Transport
Definition: transport.hpp:95
Definition: face.hpp:14
lp::PitToken pitToken
Definition: face.hpp:16
uint64_t endpointId
Definition: face.hpp:15