s-net-Tools
StreamTestEntity.h
Go to the documentation of this file.
1 
22 #ifndef STREAM_TEST_ENTITY_H
23 #define STREAM_TEST_ENTITY_H
24 
25 #include "HdlcdClient.h"
26 #include "Statistic.h"
27 #include "SnetProbeRequest.h"
28 #include "SnetProbeReply.h"
29 #include "SnetPeerStatistic.h"
30 
32 public:
33  StreamTestEntity(HdlcdClient& a_HdlcdClient, uint16_t a_SrcSSA, uint16_t a_DstSSA): m_SrcSSA(a_SrcSSA), m_DstSSA(a_DstSSA), m_HdlcdClient(a_HdlcdClient), m_LocalSeqNbr(0), m_RemoteStatistic("remote "), m_LocalStatistic("local ") {
34  // Init
35  srand(::time(NULL));
36  m_LocalSeed = rand();
37  m_RemoteSeed = 0;
38  m_TotalBytesWritten = 0;
39  m_HdlcdClient.SetOnDataCallback([this](const HdlcdPacketData& a_PacketData) {
40  PacketReceived(a_PacketData);
41  });
42  }
43 
44  void Start() {
45  // Trigger activity
46  SendNextProbeRequest();
47  }
48 
49 private:
50  // Helpers
51  void PacketReceived(const HdlcdPacketData& a_PacketData) {
52  SnetAppMessage l_AppMessage;
53  if (l_AppMessage.Deserialize(a_PacketData.GetData())) {
54  // Checks: Unicast SSAs must match, but do not try to distinguish streams if non-unicast addresses were used!
55  if (((l_AppMessage.GetSrcSSA() == m_DstSSA) || (m_DstSSA >= 0x3FF0)) && (l_AppMessage.GetSrcServiceId() == 0x22) && (l_AppMessage.GetDstServiceId() == 0x22)) {
56  if (l_AppMessage.GetToken() == 0x01) {
57  HandleProbeReply(a_PacketData);
58  } else if (l_AppMessage.GetToken() == 0x02) {
59  HandlePeerStatistics(a_PacketData);
60  } // else if
61  } // if
62  } // if
63  }
64 
65  void HandleProbeReply(const HdlcdPacketData& a_PacketData) {
66  SnetProbeReply l_ProbeReply;
67  if (l_ProbeReply.Deserialize(a_PacketData.GetData())) {
68  UpdateStatistics(l_ProbeReply.GetRemoteSeed(), l_ProbeReply.GetLocalSeed(), l_ProbeReply.GetRemoteSeqNbr(), l_ProbeReply.GetLocalSeqNbr());
69  } // if
70  }
71 
72  void HandlePeerStatistics(const HdlcdPacketData& a_PacketData) {
73  SnetPeerStatistic l_PeerStatistic;
74  if (l_PeerStatistic.Deserialize(a_PacketData.GetData())) {
75  // Print
76  std::cout << "New peer statistics, Gen = " << l_PeerStatistic.GetLocalGeneration() << ", Amount = " << l_PeerStatistic.GetAmountOfPackets();
77  if (l_PeerStatistic.GetAmountOfPackets() == 256) {
78  std::cout << std::endl;
79  } else {
80  std::cout << " (!!!)" << std::endl;
81  } // else
82  } // if
83  }
84 
85  void UpdateStatistics(uint32_t a_RemoteSeed, uint32_t a_LocalSeed, uint32_t a_RemoteSeqNbr, uint32_t a_LocalSeqNbr) {
86  // Update statistics
87  if (m_LocalSeed != a_LocalSeed) {
88  return;
89  } // if
90 
91  if (m_RemoteSeed != a_RemoteSeed) {
92  m_RemoteSeed = a_RemoteSeed;
93  m_RemoteStatistic.Reset();
94  m_LocalStatistic.Reset();
95  } // if
96 
97  m_RemoteStatistic.Update(a_RemoteSeqNbr);
98  m_LocalStatistic.Update(a_LocalSeqNbr);
99  }
100 
101  void SendNextProbeRequest() {
102  // Create probe request
103  SnetProbeRequest l_ProbeRequest(m_SrcSSA, m_DstSSA);
104  l_ProbeRequest.SetLocalSeed (m_LocalSeed);
105  l_ProbeRequest.SetLocalSeqNbr(m_LocalSeqNbr);
106 
107  // Send one probe request, and if done, call this method again via a provided lambda-callback
108  if (m_HdlcdClient.Send(std::move(HdlcdPacketData::CreatePacket(l_ProbeRequest.Serialize(), true)), [this](){ SendNextProbeRequest(); })) {
109  // One packet is on its way
110  m_TotalBytesWritten += l_ProbeRequest.GetSize();
111  if (((++m_LocalSeqNbr) % 0xFF) == 0) {
112  std::cout << "256 Packets written to the TCP socket (" << std::dec << m_TotalBytesWritten << " bytes total)" << std::endl;
113  } // if
114  } // if
115  }
116 
117  // Members
118  uint16_t m_SrcSSA;
119  uint16_t m_DstSSA;
120  uint32_t m_RemoteSeed;
121  uint32_t m_LocalSeed;
122  HdlcdClient& m_HdlcdClient;
123  uint32_t m_LocalSeqNbr;
124  Statistic m_RemoteStatistic;
125  Statistic m_LocalStatistic;
126  unsigned long m_TotalBytesWritten;
127 };
128 
129 #endif // STREAM_TEST_ENTITY_H
void SetLocalSeed(uint32_t a_LocalSeed=0)
uint32_t GetRemoteSeed() const
bool Send(const HdlcdPacketData &a_PacketData, std::function< void()> a_OnSendDoneCallback=nullptr)
Send a single data packet to the peer entity.
Definition: HdlcdClient.h:203
uint32_t GetRemoteSeqNbr() const
static HdlcdPacketData CreatePacket(const std::vector< unsigned char > a_Payload, bool a_bReliable, bool a_bInvalid=false, bool a_bWasSent=false)
uint8_t GetDstServiceId() const
A set of tools to exchange and handle packets of s-net(r) devices via the HDLC Daemon. Copyright (C) 2016 Florian Evers, florian-evers@gmx.de.
uint16_t GetToken() const
uint8_t GetSrcServiceId() const
void SetLocalSeqNbr(uint32_t a_LocalSeqNbr=0)
size_t Deserialize(const std::vector< unsigned char > &a_Buffer)
uint16_t GetSrcSSA() const
Definition: SnetPacket.h:53
size_t Deserialize(const std::vector< unsigned char > &a_Buffer)
Class HdlcdClient.
Definition: HdlcdClient.h:54
void SetOnDataCallback(std::function< void(const HdlcdPacketData &a_PacketData)> a_OnDataCallback)
Provide a callback method to be called for received data packets.
Definition: HdlcdClient.h:172
A set of tools to exchange and handle packets of s-net(r) devices via the HDLC Daemon. Copyright (C) 2016 Florian Evers, florian-evers@gmx.de.
uint32_t GetLocalSeqNbr() const
const std::vector< unsigned char > & GetData() const
void Update(uint32_t a_SeqNbr)
Definition: Statistic.h:43
size_t Deserialize(const std::vector< unsigned char > &a_Buffer)
void Reset()
Definition: Statistic.h:33
uint32_t GetLocalSeed() const
Copyright (c) 2016, Florian Evers, florian-evers@gmx.de All rights reserved.
uint16_t GetAmountOfPackets() const
A set of tools to exchange and handle packets of s-net(r) devices via the HDLC Daemon. Copyright (C) 2016 Florian Evers, florian-evers@gmx.de.
A set of tools to exchange and handle packets of s-net(r) devices via the HDLC Daemon. Copyright (C) 2016 Florian Evers, florian-evers@gmx.de.
std::vector< unsigned char > Serialize() const
StreamTestEntity(HdlcdClient &a_HdlcdClient, uint16_t a_SrcSSA, uint16_t a_DstSSA)
size_t GetSize() const
uint32_t GetLocalGeneration() const