Satellite-Gateway
GatewayClientCreate.h
Go to the documentation of this file.
1 
24 #ifndef GATEWAY_CLIENT_CREATE_H
25 #define GATEWAY_CLIENT_CREATE_H
26 
27 #include "ConfigFrame.h"
28 #include <memory>
29 #include <string>
30 
32 public:
33  static GatewayClientCreate Create(uint16_t a_ReferenceNbr, std::string a_RemoteAddress, uint16_t a_RemotePortNbr) {
34  GatewayClientCreate l_GatewayClientCreate;
35  l_GatewayClientCreate.m_ReferenceNbr = a_ReferenceNbr;
36  l_GatewayClientCreate.m_RemoteAddress = a_RemoteAddress;
37  l_GatewayClientCreate.m_RemotePortNbr = a_RemotePortNbr;
38  return l_GatewayClientCreate;
39  }
40 
41  static std::shared_ptr<GatewayClientCreate> CreateDeserializedFrame() {
42  auto l_GatewayClientCreate(std::shared_ptr<GatewayClientCreate>(new GatewayClientCreate));
43  l_GatewayClientCreate->m_eDeserialize = DESERIALIZE_HEADER;
44  l_GatewayClientCreate->m_BytesRemaining = 6; // Next: read the header including the frame type byte
45  return l_GatewayClientCreate;
46  }
47 
48  // Getter
49  uint16_t GetReferenceNbr() const {
50  assert(m_eDeserialize == DESERIALIZE_FULL);
51  return m_ReferenceNbr;
52  }
53 
54  std::string GetRemoteAddress() const {
55  assert(m_eDeserialize == DESERIALIZE_FULL);
56  return m_RemoteAddress;
57  }
58 
59  uint16_t GetRemotePortNbr() const {
60  assert(m_eDeserialize == DESERIALIZE_FULL);
61  return m_RemotePortNbr;
62  }
63 
64 private:
65  // Private CTOR
66  GatewayClientCreate(): m_ReferenceNbr(0), m_RemotePortNbr(0), m_eDeserialize(DESERIALIZE_FULL) {
67  }
68 
69  // Methods
70  E_CONFIG_FRAME GetConfigFrameType() const { return CONFIG_FRAME_GATEWAY_CLIENT_CREATE; }
71 
72  // Serializer
73  const std::vector<unsigned char> Serialize() const {
74  assert(m_eDeserialize == DESERIALIZE_FULL);
75  std::vector<unsigned char> l_Buffer;
76  l_Buffer.emplace_back(CONFIG_FRAME_GATEWAY_CLIENT_CREATE);
77  l_Buffer.emplace_back((m_ReferenceNbr >> 8) & 0xFF);
78  l_Buffer.emplace_back((m_ReferenceNbr >> 0) & 0xFF);
79  l_Buffer.emplace_back((m_RemotePortNbr >> 8) & 0xFF);
80  l_Buffer.emplace_back((m_RemotePortNbr >> 0) & 0xFF);
81  l_Buffer.emplace_back((m_RemoteAddress.size()) & 0xFF);
82  l_Buffer.insert(l_Buffer.end(), m_RemoteAddress.data(), (m_RemoteAddress.data() + m_RemoteAddress.size()));
83  return l_Buffer;
84  }
85 
86  // Deserializer
87  bool Deserialize() {
88  // All requested bytes are available
89  switch (m_eDeserialize) {
90  case DESERIALIZE_HEADER: {
91  // Deserialize the body including the frame type byte
92  assert(m_Buffer.size() == 6);
94  m_ReferenceNbr = ntohs(*(reinterpret_cast<const uint16_t*>(&m_Buffer[1])));
95  m_RemotePortNbr = ntohs(*(reinterpret_cast<const uint16_t*>(&m_Buffer[3])));
97  m_Buffer.clear();
98  if (m_BytesRemaining) {
99  m_eDeserialize = DESERIALIZE_BODY;
100  } else {
101  m_eDeserialize = DESERIALIZE_FULL;
102  } // else
103 
104  break;
105  }
106  case DESERIALIZE_BODY: {
107  m_RemoteAddress.append(m_Buffer.begin(), m_Buffer.end());
108  m_eDeserialize = DESERIALIZE_FULL;
109  break;
110  }
111  case DESERIALIZE_ERROR:
112  case DESERIALIZE_FULL:
113  default:
114  assert(false);
115  } // switch
116 
117  // No error
118  return true;
119  }
120 
121  // Members
122  uint16_t m_ReferenceNbr;
123  std::string m_RemoteAddress;
124  uint16_t m_RemotePortNbr;
125  typedef enum {
126  DESERIALIZE_ERROR = 0,
127  DESERIALIZE_HEADER = 1,
128  DESERIALIZE_BODY = 2,
129  DESERIALIZE_FULL = 3
130  } E_DESERIALIZE;
131  E_DESERIALIZE m_eDeserialize;
132 };
133 
134 #endif // GATEWAY_CLIENT_CREATE_H
E_CONFIG_FRAME
Definition: ConfigFrame.h:29
uint16_t GetRemotePortNbr() const
static std::shared_ptr< GatewayClientCreate > CreateDeserializedFrame()
static GatewayClientCreate Create(uint16_t a_ReferenceNbr, std::string a_RemoteAddress, uint16_t a_RemotePortNbr)
uint16_t GetReferenceNbr() const
std::vector< unsigned char > m_Buffer
The buffer containing partly received frames or higher-layer payload.
Definition: Frame.h:154
std::string GetRemoteAddress() const
size_t m_BytesRemaining
The amount of bytes required to finalize frame assembly during reception.
Definition: Frame.h:155