libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
processing_rules.hpp
Go to the documentation of this file.
1 #ifndef ISIMUD_EXCHANGES_COMMON_MIT_PROCESSING_RULES_HPP
2 #define ISIMUD_EXCHANGES_COMMON_MIT_PROCESSING_RULES_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2015 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 
22 #include "../../common/processing_rules.hpp"
23 
24 #include "core/msm.hpp"
25 
26 #include <chrono>
27 #include <functional>
28 #include <thread>
29 
30 namespace isimud { namespace ISIMUD_VER_NAMESPACE { namespace exchanges { namespace MIT { namespace common {
31 
32 /// A client-to-MIT-protocol message handler.
33 /**
34  The behaviour of this handler is derived from the specification in [1].
35  [1] "MIT203 - MILLENNIUM EXCHANGE Native Trading Gateway"
36 */
37 template<class SrcMsgDetails, class DestMsgDetails>
38 class client_to_exchange_transformations final
39 : public exchanges::common::message_responses<
40  SrcMsgDetails,
41  DestMsgDetails,
42  client_to_exchange_transformations<SrcMsgDetails, DestMsgDetails>,
43  typename SrcMsgDetails::client_to_exchange_messages_t
44 > {
45 public:
46  using base_t=exchanges::common::message_responses<
47  SrcMsgDetails,
48  DestMsgDetails,
49  client_to_exchange_transformations<SrcMsgDetails, DestMsgDetails>,
50  typename SrcMsgDetails::client_to_exchange_messages_t
51  >;
52  using src_msg_details_t=typename base_t::src_msg_details_t;
53  using dest_msg_details_t=typename base_t::dest_msg_details_t;
54  using ref_data=typename dest_msg_details_t::ref_data; ///< The object containing the reference data that is used to convert the input symbol information to exchange-specific instrument identifiers.
55 
56  explicit constexpr client_to_exchange_transformations(ref_data const &rd) noexcept(true)
57  : ref_data_(rd), msm(ref_data_, ref_data_, ref_data_, ref_data_) {
58  }
59  constexpr client_to_exchange_transformations(client_to_exchange_transformations const &v) noexcept(true)
60  : base_t(v), ref_data_(v.ref_data_), msm(v.msm) {
61  }
62 
63  /**
64  \return False to continue processing messages, true otherwise.
65  */
66  template<class SktT>
67  bool process_msg(typename src_msg_details_t::msg_buffer_t const &buff, SktT &exchg_skt, SktT &dest_skt);
68 
69  std::string to_string() const noexcept(false);
70 
71 private:
72  struct state_machine_t;
73  using machine=libjmmcg::msm::unroll::machine<state_machine_t>;
74 
75  ref_data const &ref_data_;
76  const machine msm;
77 };
78 
79 template<class SrcMsgDetails, class DestMsgDetails> inline std::ostream &
80 operator<<(std::ostream &os, client_to_exchange_transformations<SrcMsgDetails, DestMsgDetails> const &ec) noexcept(false);
81 
82 /// A MIT-protocol-to-client message handler.
83 /**
84  The behaviour of this handler is derived from the specification in [1].
85  [1] "MIT203 - MILLENNIUM EXCHANGE Native Trading Gateway"
86 */
87 template<class SrcMsgDetails, class DestMsgDetails>
88 class exchange_to_client_transformations final
89 : public exchanges::common::message_responses<
90  SrcMsgDetails,
91  DestMsgDetails,
92  exchange_to_client_transformations<SrcMsgDetails, DestMsgDetails>,
93  typename SrcMsgDetails::exchange_to_client_messages_t
94 > {
95 public:
96  using base_t=exchanges::common::message_responses<
97  SrcMsgDetails,
98  DestMsgDetails,
99  exchange_to_client_transformations<SrcMsgDetails, DestMsgDetails>,
100  typename SrcMsgDetails::exchange_to_client_messages_t
101  >;
102  using src_msg_details_t=typename base_t::src_msg_details_t;
103  using dest_msg_details_t=typename base_t::dest_msg_details_t;
104  using ref_data=typename src_msg_details_t::ref_data; ///< The object containing the reference data that is used to convert the input symbol information to exchange-specific instrument identifiers.
105 
106  explicit constexpr exchange_to_client_transformations(ref_data const &rd) noexcept(true)
107  : ref_data_(rd),
108  business_msm(ref_data_, ref_data_, ref_data_, ref_data_),
109  admin_msm(business_msm, business_msm, business_msm, business_msm, business_msm) {
110  }
111  constexpr exchange_to_client_transformations(exchange_to_client_transformations const &v) noexcept(true)
112  : base_t(v), ref_data_(v.ref_data_), business_msm(v.business_msm), admin_msm(v.admin_msm) {
113  }
114 
115  /**
116  \return False to continue processing messages, true otherwise.
117  */
118  template<class SktT, class ClientCxn>
119  bool process_msg(typename src_msg_details_t::msg_buffer_t const &buff, SktT &exchg_skt, ClientCxn &client_skt);
120 
121  std::string to_string() const noexcept(false);
122 
123 private:
124  struct business_state_machine_t;
125  struct admin_state_machine_t;
126  using business_machine=libjmmcg::msm::unroll::machine<business_state_machine_t>;
127  using admin_machine=libjmmcg::msm::unroll::machine<admin_state_machine_t>;
128 
129  ref_data const &ref_data_;
130  const business_machine business_msm;
131  const admin_machine admin_msm;
132 };
133 
134 template<class SrcMsgDetails, class DestMsgDetails> inline std::ostream &
135 operator<<(std::ostream &os, exchange_to_client_transformations<SrcMsgDetails, DestMsgDetails> const &ec) noexcept(false);
136 
137 /// A simple, MIT-protocol exchange simulator.
138 /**
139  The behaviour of this simulator is a simplification derived from the specification in [1].
140  [1] "MIT203 - MILLENNIUM EXCHANGE Native Trading Gateway"
141 */
142 template<class SrcMsgDetails>
143 class simulator_responses final : public exchanges::common::simulator_responses<SrcMsgDetails, simulator_responses<SrcMsgDetails>> {
144 public:
145  using base_t=exchanges::common::simulator_responses<SrcMsgDetails, simulator_responses<SrcMsgDetails>>;
146  using msg_details_t=typename base_t::msg_details_t;
147 
148  static inline constexpr Reason_t logout_reason{"snafu\0"};
149  /// The only valid instrument, all others will be rejected.
150  static inline constexpr typename msg_details_t::SecurityID_t instrumentID=133215; ///< VODAFONE.
151  /// An invalid instrument, that will be rejected.
152  static inline constexpr typename msg_details_t::SecurityID_t invalidInstrumentID=1;
153  static inline constexpr std::chrono::seconds logout_timeout{3};
154 
155  simulator_responses() noexcept(true)
156  : base_t(), msm{std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this)} {
157  }
158  constexpr simulator_responses(simulator_responses const &sr) noexcept(true)
159  : base_t(sr), msm{std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this)} {
160  }
161 
162  /**
163  \return False to continue processing messages, true otherwise.
164  */
165  template<class SktT, class ClientCxn>
166  bool process_msg(typename msg_details_t::msg_buffer_t const &buff, SktT &exchg_skt, ClientCxn &client_cxn);
167 
168  std::string to_string() const noexcept(false);
169 
170 private:
171  struct state_machine_t;
172  using machine=libjmmcg::msm::unroll::machine<state_machine_t>;
173 
174  const machine msm;
175 };
176 
177 template<class SrcMsgDetails> inline std::ostream &
178 operator<<(std::ostream &os, simulator_responses<SrcMsgDetails> const &ec) noexcept(false);
179 
180 } } } } }
181 
183 
184 #endif