libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
server_heartbeats.hpp
Go to the documentation of this file.
1 #ifndef ISIMUD_EXCHANGES_COMMON_SERVER_HEARTBEATS_HPP
2 #define ISIMUD_EXCHANGES_COMMON_SERVER_HEARTBEATS_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2018 by J.M.McGuiness, isimud@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 #include "thread_traits.hpp"
23 
24 #include "core/jthread.hpp"
25 
26 #include <boost/exception_ptr.hpp>
27 
28 #include <atomic>
29 #include <chrono>
30 #include <functional>
31 
32 namespace isimud { namespace ISIMUD_VER_NAMESPACE { namespace exchanges { namespace common {
33 
34 /// Section 5.2.2 "Heartbeats" of [1]. Generate heartbeats from the containing simulator.
35 /**
36  The simulator generates heartbeats to which the client responds.
37 */
38 template<class MsgT, unsigned MissedHbs, unsigned HbInterval>
39 class server_hb_t {
40 public:
41  /// The specific heartbeat message type.
42  using hb_t=MsgT;
43 
44  enum : unsigned {
45  max_missed_heartbeats=MissedHbs
46  };
47  static inline constexpr std::chrono::seconds heartbeat_interval{HbInterval};
48 
49  template<class ClientCxn, class MakeHB>
50  server_hb_t(ClientCxn &cxn, MakeHB &&make_hb);
51  ~server_hb_t() noexcept(true);
52 
53  void stop() noexcept(true);
54 
55  std::string to_string() const noexcept(false);
56 
57  friend std::ostream &operator<<(std::ostream &os, server_hb_t const &s) noexcept(false) {
58  os<<s.to_string();
59  return os;
60  }
61 
62  template<class ClientCxn>
63  void process(ClientCxn &cxn) noexcept;
64 
65 private:
66  using thread_t=libjmmcg::ppd::jthread;
67 
68  std::atomic<bool> exit_;
69  /**
70  \todo Need to report errors correctly....
71  */
72  boost::exception_ptr hb_proc_ex;
73  const std::function<hb_t()> make_hb_msg;
74  thread_t heartbeat_sender_thr;
75 };
76 
77 } } } }
78 
80 
81 #endif