1 #ifndef libjmmcg_isimud_exchanges_MIT_common_connectivity_policy_hpp
2 #define libjmmcg_isimud_exchanges_MIT_common_connectivity_policy_hpp
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #include "messages.hpp"
25
26 #include <boost/asio/ip/address.hpp>
27 #include <boost/asio/ip/tcp.hpp>
28
29 #include <atomic>
30 #include <chrono>
31 #include <thread>
32
33 namespace isimud { namespace exchanges { namespace MIT { namespace common {
34
35
36
37
38
39
40
41 template<class LogonT>
42 class connectivity_policy {
43 public:
44 using logon_args_t=LogonT;
45 using endpoint_t=std::pair<boost::asio::ip::address, unsigned short>;
46 struct gateways_t {
47 endpoint_t primary_gateway;
48 endpoint_t secondary_gateway;
49
50 gateways_t(endpoint_t const &primary, endpoint_t const &secondary) noexcept(true)
51 : primary_gateway(primary), secondary_gateway(secondary) {
52 }
53 gateways_t(gateways_t const &gws) noexcept(true)
54 : primary_gateway(gws.primary_gateway), secondary_gateway(gws.secondary_gateway) {
55 }
56 };
57 enum : unsigned {
58 max_attempts=3
59 };
60 static constexpr std::chrono::seconds min_timeout{5};
61 const gateways_t gateways;
62 const logon_args_t logon_args;
63 const logoff_args_t logoff_args;
64
65 connectivity_policy(gateways_t const &gws, logon_args_t const &logon, logoff_args_t const &logoff) noexcept(true);
66
67 template<class ConnectFn>
68 void operator()(ConnectFn const &cnx) const;
69
70 private:
71 template<class ConnectFn> static
72 std::exception_ptr connection_failed(ConnectFn const &cnx, endpoint_t const &endpoint);
73 };
74
75
76
77
78
79 template<class MsgT>
80 class server_hb_t {
81 public:
82
83 using hb_t=MsgT;
84
85 enum : unsigned {
86 max_missed_heartbeats=5
87 };
88 static constexpr std::chrono::seconds heartbeat_interval{3};
89
90 template<class ClientCxn>
91 explicit server_hb_t(ClientCxn &cxn);
92 ~server_hb_t() noexcept(true);
93
94 std::string to_string() const noexcept(false);
95
96 friend std::ostream &operator<<(std::ostream &os, server_hb_t const &s) noexcept(false) {
97 os<<s.to_string();
98 return os;
99 }
100
101 private:
102 std::atomic<bool> exit_;
103
104
105
106 std::exception_ptr ex=nullptr;
107
108
109
110 std::thread thr;
111
112 template<class ClientCxn>
113 void process(ClientCxn &cxn) noexcept(true);
114 };
115
116 } } } }
117
118 #include "connectivity_policy_impl.hpp"
119
120 #endif