libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
server_heartbeats_impl.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2015 by J.M.McGuiness, isimud@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 namespace isimud { namespace ISIMUD_VER_NAMESPACE { namespace exchanges { namespace common {
20 
21 template<class MsgT, unsigned MissedHbs, unsigned HbInterval>
22 template<class ClientCxn, class MakeHB>
23 inline
24 server_hb_t<MsgT, MissedHbs, HbInterval>::server_hb_t(ClientCxn &cxn, MakeHB &&make_hb)
25 : exit_(false),
26  hb_proc_ex(),
27  make_hb_msg(std::move(make_hb)),
29  heartbeat_sender_thr.kernel_priority(thread_traits::heartbeat_thread.priority);
30  heartbeat_sender_thr.kernel_affinity(
31  typename thread_t::thread_traits::api_params_type::processor_mask_type(thread_traits::heartbeat_thread.core)
32  );
33  heartbeat_sender_thr.set_name("heartbeats");
34 }
35 
36 template<class MsgT, unsigned MissedHbs, unsigned HbInterval>
37 inline void
38 server_hb_t<MsgT, MissedHbs, HbInterval>::stop() noexcept(true) {
39  exit_=true;
40 }
41 
42 template<class MsgT, unsigned MissedHbs, unsigned HbInterval>
43 inline
44 server_hb_t<MsgT, MissedHbs, HbInterval>::~server_hb_t() noexcept(true) {
45  stop();
46 }
47 
48 template<class MsgT, unsigned MissedHbs, unsigned HbInterval>
49 inline std::string
50 server_hb_t<MsgT, MissedHbs, HbInterval>::to_string() const noexcept(false) {
51  std::ostringstream os;
52  os
53  <<"max missed heartbeats="<<max_missed_heartbeats
54  <<", heartbeat interval="<<heartbeat_interval.count()<<" sec"
55  <<", heartbeat core="<<thread_traits::heartbeat_thread
56  <<", heartbeat processing-error: '"<<hb_proc_ex<<"'";
57  return os.str();
58 }
59 
60 template<class MsgT, unsigned MissedHbs, unsigned HbInterval>
61 template<class ClientCxn>
62 inline void
63 server_hb_t<MsgT, MissedHbs, HbInterval>::process(ClientCxn &cxn) noexcept {
64  try {
65  const hb_t hb{make_hb_msg()};
66  while (LIKELY(!static_cast<bool>(exit_))) {
67  std::this_thread::sleep_for(heartbeat_interval);
68  if (LIKELY(!static_cast<bool>(exit_))) {
69  cxn.write(hb);
70  }
71  }
72  } catch (...) {
73  if (!static_cast<bool>(exit_)) {
74  hb_proc_ex=boost::current_exception();
75  }
76  }
77 }
78 
79 } } } }