root/isimud/exchanges/FIX/common/connectivity_policy_impl.hpp

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. connection_failed
  2. thr
  3. to_string
  4. process

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/isimud/exchanges/FIX/common/connectivity_policy_impl.hpp 2177 2017-10-11 21:29:22Z jmmcg $
   3 **
   4 ** Copyright (C) 2016 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 namespace isimud { namespace exchanges { namespace FIX { namespace common {
  22 
  23 template<class LogonT> inline
  24 connectivity_policy<LogonT>::connectivity_policy(gateways_t const &gws) noexcept(true)
  25 : gateways(gws) {}
  26 
  27 template<class LogonT>
  28 template<class ConnectFn> inline void
  29 connectivity_policy<LogonT>::operator()(ConnectFn const &cnx) const {
  30         std::exception_ptr cnx_failed(connection_failed(cnx, gateways.primary_gateway));
  31         if (cnx_failed) {
  32                 std::rethrow_exception(cnx_failed);
  33         }
  34 }
  35 
  36 template<class LogonT>
  37 template<class ConnectFn> inline std::exception_ptr
  38 connectivity_policy<LogonT>::connection_failed(ConnectFn const &cnx, endpoint_t const &endpoint) {
  39         std::exception_ptr cnx_failed;
  40         unsigned i=0;
  41         for (; i<max_attempts; ++i) {
  42                 try {
  43                         cnx(boost::asio::ip::tcp::endpoint(endpoint.first, endpoint.second));
  44                         break;
  45                 } catch (std::exception const &e) {
  46                         cnx_failed=std::make_exception_ptr(e);
  47                         std::this_thread::sleep_for(min_timeout);
  48                 }
  49         }
  50         return (i>=max_attempts) ? cnx_failed : std::exception_ptr();
  51 }
  52 
  53 template<class LogonT>
  54 constexpr std::chrono::seconds connectivity_policy<LogonT>::min_timeout;
  55 
  56 template<class MsgT>
  57 constexpr std::chrono::seconds server_hb_t<MsgT>::heartbeat_interval;
  58 
  59 template<class MsgT>
  60 template<class ClientCxn>
  61 inline
  62 server_hb_t<MsgT>::server_hb_t(ClientCxn &cxn)
  63 : exit_(false), thr(&server_hb_t::template process<ClientCxn>, this, std::ref(cxn)) {
  64 }
  65 
  66 template<class MsgT>
  67 inline
  68 server_hb_t<MsgT>::~server_hb_t() noexcept(true) {
  69         exit_=true;
  70         if (thr.joinable()) {
  71                 thr.join();
  72         }
  73 }
  74 
  75 template<class MsgT>
  76 inline std::string
  77 server_hb_t<MsgT>::to_string() const noexcept(false) {
  78         std::ostringstream os;
  79         os
  80                 <<"max missed heartbeats="<<max_missed_heartbeats
  81                 <<", heartbeat interval="<<heartbeat_interval.count()<<" sec"
  82                 <<", client processing-error: '"<<ex<<"'";
  83         return os.str();
  84 }
  85 
  86 template<class MsgT>
  87 template<class ClientCxn>
  88 inline void
  89 server_hb_t<MsgT>::process(ClientCxn &cxn) noexcept(true) {
  90         try {
  91                 const hb_t hb{};
  92                 while (LIKELY(!static_cast<bool>(exit_))) {
  93                         std::this_thread::sleep_for(heartbeat_interval);
  94                         cxn.write(hb);
  95                 }
  96         } catch (std::exception const &e) {
  97                 ex=std::make_exception_ptr(e);
  98         }
  99 }
 100 
 101 } } } }

/* [<][>][^][v][top][bottom][index][help] */