esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ping-client.hpp
Go to the documentation of this file.
1#ifndef NDNPH_APP_PING_CLIENT_HPP
2#define NDNPH_APP_PING_CLIENT_HPP
3
4#include "../face/packet-handler.hpp"
5#include "../port/clock/port.hpp"
6
7namespace ndnph {
8
16class PingClient : public PacketHandler {
17public:
24 explicit PingClient(Name prefix, Face& face, int interval = 1000)
25 : PacketHandler(face)
26 , m_prefix(std::move(prefix))
27 , m_interval(interval)
28 , m_next(port::Clock::add(port::Clock::now(), interval)) {
29 port::RandomSource::generate(reinterpret_cast<uint8_t*>(&m_seqNum), sizeof(m_seqNum));
30 }
31
32 struct Counters {
33 uint32_t nTxInterests = 0;
34 uint32_t nRxData = 0;
35 };
36
38 return m_cnt;
39 }
40
41private:
42 void loop() final {
43 auto now = port::Clock::now();
44 if (port::Clock::isBefore(now, m_next)) {
45 return;
46 }
47 sendInterest();
48 m_next = port::Clock::add(now, m_interval);
49 }
50
51 bool sendInterest() {
52 StaticRegion<1024> region;
53 Component seqNumComp = Component::from(region, TT::GenericNameComponent, tlv::NNI8(++m_seqNum));
54 NDNPH_ASSERT(!!seqNumComp);
55 Name name = m_prefix.append(region, seqNumComp);
56 NDNPH_ASSERT(!!name);
57
58 Interest interest = region.create<Interest>();
59 NDNPH_ASSERT(!!interest);
60 interest.setName(name);
61 interest.setMustBeFresh(true);
62
63 if (!send(interest)) {
64 return false;
65 }
66 ++m_cnt.nTxInterests;
67 return true;
68 }
69
70 bool processData(Data data) final {
71 const Name& dataName = data.getName();
72 if (!m_prefix.isPrefixOf(dataName) || m_prefix.size() + 1 != dataName.size()) {
73 return false;
74 }
75 Component lastComp = dataName[-1];
77 Decoder::readTlv(d, lastComp.tlv(), lastComp.tlv() + lastComp.size());
78 uint64_t seqNum = 0;
79 if (!tlv::NNI8::decode(d, seqNum)) {
80 return false;
81 }
82
83 if (m_seqNum == seqNum) {
84 ++m_cnt.nRxData;
85 }
86 return true;
87 }
88
89private:
90 Name m_prefix;
91 uint64_t m_seqNum = 0;
92 int m_interval = 1000;
93 port::Clock::Time m_next;
94 Counters m_cnt;
95};
96
97} // namespace ndnph
98
99#endif // NDNPH_APP_PING_CLIENT_HPP
static bool generate(uint8_t *output, size_t count)
Name component.
Definition component.hpp:16
size_t size() const
Definition component.hpp:157
static Component from(Region &region, uint16_t type, const Arg &... arg)
Construct from TLV-TYPE, and several arguments to be encoded to TLV-VALUE.
Definition component.hpp:75
const uint8_t * tlv() const
Definition component.hpp:153
Data packet.
Definition data.hpp:136
Decoded TLV.
Definition decoder.hpp:13
static bool readTlv(Tlv &d, const uint8_t *input, const uint8_t *end)
Definition decoder.hpp:46
Network layer face.
Definition face.hpp:12
Interest packet.
Definition interest.hpp:284
void setMustBeFresh(bool v)
Definition interest.hpp:296
void setName(const Name &v)
Definition interest.hpp:288
Name.
Definition name.hpp:14
size_t size() const
Get number of components.
Definition name.hpp:86
Name append(Region &region, const C &... comps) const
Append a sequence of components.
Definition name.hpp:183
bool isPrefixOf(const Name &other) const
Determine if this name is a prefix of other.
Definition name.hpp:239
Base class to receive packets from Face.
Definition packet-handler.hpp:10
bool send(Region &region, const Packet &packet, PacketInfo pi={})
Synchronously transmit a packet.
Definition packet-handler.hpp:49
Periodically transmit Interests to test reachability.
Definition ping-client.hpp:16
bool processData(Data data) final
Override to receive Data packets.
Definition ping-client.hpp:70
Counters readCounters() const
Definition ping-client.hpp:37
void loop() final
Override to be invoked periodically.
Definition ping-client.hpp:42
PingClient(Name prefix, Face &face, int interval=1000)
Constructor.
Definition ping-client.hpp:24
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
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
Definition nni.hpp:12
static bool decode(const Decoder::Tlv &d, T &value)
Definition nni.hpp:14
#define NDNPH_ASSERT(x)
Definition common.hpp:30
@ GenericNameComponent
Definition an.hpp:20
Definition fs.hpp:33
Definition ping-client.hpp:32
uint32_t nTxInterests
Definition ping-client.hpp:33
uint32_t nRxData
Definition ping-client.hpp:34