helloByeProto.hpp
Go to the documentation of this file.
1 #ifndef HELLOBYEPROTO_HPP
2 #define HELLOBYEPROTO_HPP
3 
4 #include <mutex>
5 
6 #include "common.hpp"
7 #include "stateMachine.hpp"
8 
9 namespace HelloBye {
10 
11 /*
12  * ===================================
13  * States
14  * ===================================
15  *
16  */
17 
19  static constexpr StateID Hello = 0;
20  static constexpr StateID Bye = 1;
21  static constexpr StateID Terminate = 2;
22 };
23 
25  static constexpr StateID Hello = 0;
26  static constexpr StateID Bye = 1;
27  static constexpr StateID RecvBye = 2;
28  static constexpr StateID Terminate = 3;
29 };
30 
31 /*
32  * ===================================
33  * Forward declarations
34  * ===================================
35  *
36  */
37 
38 template <class Identifier, class Packet> class HelloByeServerHello;
39 template <class Identifier, class Packet> class HelloByeServerBye;
40 template <class Identifier, class Packet> class HelloByeClientHello;
41 template <class Identifier, class Packet> class HelloByeClientBye;
42 template <class Identifier, class Packet> class HelloByeClientRecvBye;
43 
44 /*
45  * ===================================
46  * Client Config
47  * ===================================
48  *
49  */
50 
52 private:
53  uint32_t srcIp;
54  uint16_t dstPort;
55 
56  static HelloByeClientConfig *instance;
57  static std::mutex mtx;
58  HelloByeClientConfig() : srcIp(0), dstPort(0){};
59 
60 public:
61  auto getSrcIP() { return srcIp; }
62  auto getDstPort() { return dstPort; }
63 
64  void setSrcIP(uint32_t newIP) { srcIp = newIP; }
65  void setDstPort(uint16_t newPort) { dstPort = newPort; }
66 
67  static void createInstance() {
68  std::lock_guard<std::mutex> lock(mtx);
69  if (instance == nullptr) {
70  instance = new HelloByeClientConfig;
71  }
72  }
73 
74  static HelloByeClientConfig *getInstance() { return instance; }
75 };
76 
77 /*
78  * ===================================
79  * Server Hello
80  * ===================================
81  *
82  */
83 
84 template <class Identifier, class Packet> class HelloByeServerHello {
86  friend class HelloByeServerBye<Identifier, Packet>;
87 
88 private:
89  int clientCookie;
90  int serverCookie;
91 
92 public:
94 
95  static void *factory(typename Identifier::ConnectionID id) {
96  (void)id;
97  return new HelloByeServerHello();
98  }
99 
100  PROD_INLINE void fun(
101  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface);
102 
103  static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
104  HelloByeServerHello *t = reinterpret_cast<HelloByeServerHello *>(state.stateData);
105  t->fun(state, pkt, funIface);
106 
107  // Finish transision to other state
108  if (state.state == HelloByeServer::Bye) {
110  delete (t);
111  } else if (state.state == HelloByeServer::Terminate) {
112  delete (t);
113  }
114  }
115 };
116 
117 /*
118  * ===================================
119  * Server Bye
120  * ===================================
121  *
122  */
123 
124 template <class Identifier, class Packet> class HelloByeServerBye {
126 
127 private:
128  int clientCookie;
129  int serverCookie;
130 
131 public:
132  HelloByeServerBye(const HelloByeServerHello<Identifier, Packet> *in);
133 
134  PROD_INLINE void fun(
135  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface);
136 
137  static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
138  HelloByeServerBye *t = reinterpret_cast<HelloByeServerBye *>(state.stateData);
139  t->fun(state, pkt, funIface);
140  }
141 };
142 
143 /*
144  * ===================================
145  * Client Hello
146  * ===================================
147  *
148  */
149 
150 template <class Identifier, class Packet> class HelloByeClientHello {
152  friend class HelloByeClientBye<Identifier, Packet>;
153 
154 private:
155  int clientCookie;
156  uint32_t dstIp;
157  uint16_t srcPort;
158 
159 public:
160  HelloByeClientHello(uint32_t dstIp, uint16_t srcPort);
161 
162  PROD_INLINE void fun(
163  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface);
164 
165  static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
166  HelloByeClientHello *t = reinterpret_cast<HelloByeClientHello *>(state.stateData);
167  t->fun(state, pkt, funIface);
168  }
169 };
170 
171 /*
172  * ===================================
173  * Client Bye
174  * ===================================
175  *
176  */
177 
178 template <class Identifier, class Packet> class HelloByeClientBye {
180  friend class HelloByeClientRecvBye<Identifier, Packet>;
181 
182 private:
183  int clientCookie;
184  int serverCookie;
185 
186 public:
188 
189  PROD_INLINE void fun(
190  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface);
191 
192  static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
193  HelloByeClientBye *t = reinterpret_cast<HelloByeClientBye *>(state.stateData);
194  t->fun(state, pkt, funIface);
195  }
196 };
197 
198 /*
199  * ===================================
200  * Client RecvBye
201  * ===================================
202  *
203  */
204 
205 template <class Identifier, class Packet> class HelloByeClientRecvBye {
207 
208 private:
209  int clientCookie;
210  int serverCookie;
211 
212 public:
213  HelloByeClientRecvBye(const HelloByeClientBye<Identifier, Packet> *in);
214 
215  PROD_INLINE void fun(
216  typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface);
217 
218  static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface) {
219  HelloByeClientRecvBye *t = reinterpret_cast<HelloByeClientRecvBye *>(state.stateData);
220  t->fun(state, pkt, funIface);
221  }
222 };
223 
224 }; // namespace HelloBye
225 
226 #ifndef HELLOBYEPROTO_CPP
227 #include "../src/helloByeProto.cpp"
228 #endif
229 
230 #endif /* HELLOBYEPROTO_HPP */
#define PROD_INLINE
Definition: common.hpp:16
static constexpr StateID Hello
void setDstPort(uint16_t newPort)
static constexpr StateID Terminate
Main interface for the needs of a state function.
HelloByeServerBye(const HelloByeServerHello< Identifier, Packet > *in)
HelloByeClientRecvBye(const HelloByeClientBye< Identifier, Packet > *in)
static constexpr StateID Bye
static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
static HelloByeClientConfig * getInstance()
static constexpr StateID Hello
static constexpr StateID RecvBye
HelloByeClientHello(uint32_t dstIp, uint16_t srcPort)
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
static void * factory(typename Identifier::ConnectionID id)
uint16_t StateID
Definition: common.hpp:19
static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
PROD_INLINE void fun(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
static constexpr StateID Terminate
static void run(typename SM::State &state, Packet *pkt, typename SM::FunIface &funIface)
static constexpr StateID Bye
Represents one connection.
HelloByeClientBye(const HelloByeClientHello< Identifier, Packet > *in)
void setSrcIP(uint32_t newIP)