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

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

INCLUDED FROM


   1 #ifndef libjmmcg_isimud_exchanges_MIT_common_connectivity_policy_hpp
   2 #define libjmmcg_isimud_exchanges_MIT_common_connectivity_policy_hpp
   3 
   4 /******************************************************************************
   5 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/isimud/exchanges/MIT/common/connectivity_policy.hpp 2177 2017-10-11 21:29:22Z jmmcg $
   6 **
   7 ** Copyright (C) 2015 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 "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 /// An implementation of the connectivity policy for the MIT protocol.
  36 /**
  37         From section 4.4 "Connectivity Policy" of [1].
  38         Note that BIT is the same, but OLSO & TRQ are unclear regarding this policy, so we'll assume it is the same.
  39         [1] "MIT203 - MILLENNIUM EXCHANGE Native Trading Gateway" Issue 11.6, 17 August 2015.
  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 /// Section 5.2.2 "Heartbeats" of [1]. Generate heartbeats from the containing simulator.
  76 /**
  77         The simulator generates heartbeats to which the client responds.
  78 */
  79 template<class MsgT>
  80 class server_hb_t {
  81 public:
  82         /// The specific heartbeat message type.
  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                 \todo Need to check this for errors.
 105         */
 106         std::exception_ptr ex=nullptr;
 107         /**
 108                 \todo Ideally this should run at low priority.
 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

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