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

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

INCLUDED FROM


   1 #ifndef libjmmcg_isimud_exchanges_FIX_common_connectivity_policy_hpp
   2 #define libjmmcg_isimud_exchanges_FIX_common_connectivity_policy_hpp
   3 
   4 /******************************************************************************
   5 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/isimud/exchanges/FIX/common/connectivity_policy.hpp 2177 2017-10-11 21:29:22Z jmmcg $
   6 **
   7 ** Copyright (C) 2016 by J.M.McGuiness, coder@hussar.me.uk
   8 **
   9 ** This library is free software; you can redistribute it and/or
  10 ** modify it under the terms of the GNU Lesser General Public
  11 ** License as published by the Free Software Foundation; either
  12 ** version 2.1 of the License, or (at your option) any later version.
  13 **
  14 ** This library is distributed in the hope that it will be useful,
  15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17 ** Lesser General Public License for more details.
  18 **
  19 ** You should have received a copy of the GNU Lesser General Public
  20 ** License along with this library; if not, write to the Free Software
  21 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22 */
  23 
  24 #include <boost/asio/ip/address.hpp>
  25 #include <boost/asio/ip/tcp.hpp>
  26 
  27 #include <atomic>
  28 #include <chrono>
  29 #include <thread>
  30 
  31 namespace isimud { namespace exchanges { namespace FIX { namespace common {
  32 
  33 /// An implementation of the connectivity policy for the FIX protocol.
  34 template<class LogonT>
  35 class connectivity_policy {
  36 public:
  37         using logon_args_t=LogonT;
  38         using endpoint_t=std::pair<boost::asio::ip::address, unsigned short>;
  39         struct gateways_t {
  40                 const endpoint_t primary_gateway;
  41                 
  42                 explicit gateways_t(endpoint_t const &primary) noexcept(true)
  43                 : primary_gateway(primary) {
  44                 }
  45                 gateways_t(gateways_t const &gws) noexcept(true)
  46                 : primary_gateway(gws.primary_gateway) {
  47                 }
  48         };
  49         enum : unsigned {
  50                 max_attempts=1
  51         };
  52         static constexpr std::chrono::seconds min_timeout{5};
  53         const gateways_t gateways;
  54         const logon_args_t logon_args{};
  55         const logoff_args_t logoff_args{};
  56 
  57         explicit connectivity_policy(gateways_t const &gws) noexcept(true);
  58 
  59         template<class ConnectFn>
  60         void operator()(ConnectFn const &cnx) const;
  61 
  62 private:
  63         template<class ConnectFn> static
  64         std::exception_ptr connection_failed(ConnectFn const &cnx, endpoint_t const &endpoint);
  65 };
  66 
  67 /// Generate heartbeats from the containing simulator.
  68 /**
  69         The simulator generates heartbeats to which the client responds.
  70 */
  71 template<class MsgT>
  72 class server_hb_t {
  73 public:
  74         /// The specific heartbeat message type.
  75         using hb_t=MsgT;
  76 
  77         enum : unsigned {
  78                 max_missed_heartbeats=5
  79         };
  80         static constexpr std::chrono::seconds heartbeat_interval{1};
  81 
  82         template<class ClientCxn>
  83         explicit server_hb_t(ClientCxn &cxn);
  84         ~server_hb_t() noexcept(true);
  85 
  86         std::string to_string() const noexcept(false);
  87 
  88         friend std::ostream &operator<<(std::ostream &os, server_hb_t const &s) noexcept(false) {
  89                 os<<s.to_string();
  90                 return os;
  91         }
  92 
  93 private:
  94         std::atomic<bool> exit_;
  95         /**
  96                 \todo Need to check this for errors.
  97         */
  98         std::exception_ptr ex=nullptr;
  99         /**
 100                 \todo Ideally this should run at low priority.
 101         */
 102         std::thread thr;
 103 
 104         template<class ClientCxn>
 105         void process(ClientCxn &cxn) noexcept(true);
 106 };
 107 
 108 } } } }
 109 
 110 #include "connectivity_policy_impl.hpp"
 111 
 112 #endif

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