libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
exchange_connection.hpp
Go to the documentation of this file.
1 #ifndef ISIMUD_EXCHANGES_COMMON_EXCHANGE_CONNECTION_HPP
2 #define ISIMUD_EXCHANGES_COMMON_EXCHANGE_CONNECTION_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2016 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 
23 #include "iso_3166_country_codes.hpp"
24 #include "iso_4217_currency_codes.hpp"
25 #include "thread_traits.hpp"
26 
27 namespace isimud { namespace ISIMUD_VER_NAMESPACE { namespace exchanges { namespace common {
28 
29 /// A class that implements bi-directional client-to-exchange message flow.
30 /**
31  This class is implemented with two threads: one handles the client-to-exchange messages, the other that handles the exchange-to-client messages. This implies that for optimal performance two cores are required per instance of this class. i.e. optimally, a computer can run P/2 instances of this class, where P is the number of available cores.
32  Ideally the computer should have at least two physical processors and two physical PCI buses. Each processor should be connected to only one PCI bus with processor zero connected to all of the peripherals (SATA, etc buses). At a minimum taskset, numactl (e.g. "numactl --preferred=1 --cpunodebind=1") or CPU groups (cgroups) should be used to isolate the OS to processor zero (including suitable interrupts), and the process that instantiates this class should be isolated to all processors excluding zero. Onto the PCI buses connected to those processors the network cards should be attached. The network cards should be DMA-capable at the minimum. Consideration should be given to using Solarflare (or equivalent) network cards and <a href="http://www.openonload.org/">OpenOnload</a>, with suitable configuration. Co-location should be a consideration.
33 */
34 template<class ClientCxn, class EchgCxn>
35 class exchange_connection final {
36 public:
37  /// A single connection to a client.
38  using client_link_t=ClientCxn;
39  /// A type containing the details of the exchange-side messages that will be sent and received to and from the exchange.
40  using client_msg_details_t=typename client_link_t::proc_rules_t::src_msg_details_t;
41  /// A single connection to a specific exchange.
42  using exchg_link_t=exchange_to_client_processor<typename client_link_t::svr_mgr_t::session::ptr_type, EchgCxn>;
43  using exchg_to_client_proc_rules_t=typename exchg_link_t::proc_rules_t;
44  /// A type containing the details of the client-side messages that will be sent and received to and from the exchange.
45  using socket_t=typename client_link_t::socket_t;
46  using socket_priority=typename client_link_t::socket_priority;
47  using thread_traits=libjmmcg::ppd::api_threading_traits<libjmmcg::ppd::platform_api, libjmmcg::ppd::heavyweight_threading>;
48 
49  template<class LatencyTimestamps>
50  exchange_connection(typename client_link_t::ctor_args const &client_cxn_details, typename exchg_link_t::ctor_args const &exchange_cxn_details, socket_priority to_exchg_priority, socket_priority to_client_priority, exchg_to_client_proc_rules_t const &proc_rules, LatencyTimestamps &timestamps, char const *svr_name);
51  ~exchange_connection() noexcept(false);
52 
53  bool is_logged_on() const noexcept(true);
54 
55  std::sig_atomic_t signal_status() const noexcept(true);
56 
57  std::string to_string() const noexcept(false);
58 
59 private:
60  /**
61  Ensure that the exchange-to-client flow of messages shuts down as soon as possible.
62  */
63  std::atomic<bool> exit_{false};
64  /**
65  Ensure that we have a valid link to the exchange before we allow the client to connect. This is to stop the client sending messages before we have a valid link to the exchange.
66  */
67  exchg_link_t exchg_link_;
68  client_link_t client_link;
69 };
70 
71 template<class ClientCxn, class EchgCxn> inline std::ostream &
72 operator<<(std::ostream &os, exchange_connection<ClientCxn, EchgCxn> const &ec) noexcept(false);
73 
74 } } } }
75 
77 
78 #endif