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 namespace isimud { namespace ISIMUD_VER_NAMESPACE { namespace exchanges { namespace BATSBOE { namespace common {
27 
28 /// A client-to-BATSBOE-protocol message handler.
29 /**
30  The behaviour of this handler is derived from the specification in [1, 2].
31  [1] "BATS Chi-X Europe Binary Order Entry Specification Version 1.44"
32  [2] "BZX Exchange US Equities BOE Specification Version 1.8.6"
33 */
34 template<class SrcMsgDetails, class DestMsgDetails>
35 class client_to_exchange_transformations final
36 : public exchanges::common::message_responses<
37  SrcMsgDetails,
38  DestMsgDetails,
39  client_to_exchange_transformations<SrcMsgDetails, DestMsgDetails>,
40  typename SrcMsgDetails::client_to_exchange_messages_t
41 > {
42 public:
43  using base_t=exchanges::common::message_responses<
44  SrcMsgDetails,
45  DestMsgDetails,
46  client_to_exchange_transformations<SrcMsgDetails, DestMsgDetails>,
47  typename SrcMsgDetails::client_to_exchange_messages_t
48  >;
49  using src_msg_details_t=typename base_t::src_msg_details_t;
50  using dest_msg_details_t=typename base_t::dest_msg_details_t;
51 
52  explicit client_to_exchange_transformations(typename dest_msg_details_t::SeqNum_t sn) noexcept(true)
53  : sequenceNumber(sn) {
54  }
55 
56  /**
57  \return False to continue processing messages, true otherwise.
58  */
59  template<class SktT>
60  bool process_msg(typename src_msg_details_t::msg_buffer_t const &buff, SktT &exchg_skt, SktT &dest_skt);
61 
62  std::string to_string() const noexcept(false);
63 
64 private:
65  struct state_machine_t;
66  using machine=libjmmcg::msm::unroll::machine<state_machine_t>;
67 
68  typename dest_msg_details_t::SeqNum_t sequenceNumber;
69  const machine msm{std::ref(sequenceNumber), std::ref(sequenceNumber), std::ref(sequenceNumber), std::ref(sequenceNumber)};
70 };
71 
72 template<class SrcMsgDetails, class DestMsgDetails> inline std::ostream &
73 operator<<(std::ostream &os, client_to_exchange_transformations<SrcMsgDetails, DestMsgDetails> const &ec) noexcept(false);
74 
75 /// A BATSBOE-protocol-to-client message handler.
76 /**
77  The behaviour of this handler is derived from the specification in [1, 2].
78  [1] "BATS Chi-X Europe Binary Order Entry Specification Version 1.44"
79  [2] "BZX Exchange US Equities BOE Specification Version 1.8.6"
80 */
81 template<class SrcMsgDetails, class DestMsgDetails>
82 class exchange_to_client_transformations final
83 : public exchanges::common::message_responses<
84  SrcMsgDetails,
85  DestMsgDetails,
86  exchange_to_client_transformations<SrcMsgDetails, DestMsgDetails>,
87  typename SrcMsgDetails::exchange_to_client_messages_t
88 > {
89 public:
90  using base_t=exchanges::common::message_responses<
91  SrcMsgDetails,
92  DestMsgDetails,
93  exchange_to_client_transformations<SrcMsgDetails, DestMsgDetails>,
94  typename SrcMsgDetails::exchange_to_client_messages_t
95  >;
96  using src_msg_details_t=typename base_t::src_msg_details_t;
97  using dest_msg_details_t=typename base_t::dest_msg_details_t;
98 
99  explicit exchange_to_client_transformations(typename dest_msg_details_t::SeqNum_t seq_num) noexcept(true)
100  : sequenceNumber(seq_num) {
101  }
102 
103  /**
104  \return False to continue processing messages, true otherwise.
105  */
106  template<class SktT, class ClientCxn>
107  bool process_msg(typename src_msg_details_t::msg_buffer_t const &buff, SktT &exchg_skt, ClientCxn &client_skt);
108 
109  std::string to_string() const noexcept(false);
110 
111 private:
112  struct business_state_machine_t;
113  struct admin_state_machine_t;
114  using business_machine=libjmmcg::msm::unroll::machine<business_state_machine_t>;
115  using admin_machine=libjmmcg::msm::unroll::machine<admin_state_machine_t>;
116 
117  typename dest_msg_details_t::SeqNum_t sequenceNumber;
118  const business_machine business_msm{std::ref(sequenceNumber), std::ref(sequenceNumber), std::ref(sequenceNumber), std::ref(sequenceNumber), std::ref(sequenceNumber), std::ref(sequenceNumber)};
119  const admin_machine admin_msm{std::make_tuple(std::ref(sequenceNumber), business_msm), std::make_tuple(std::ref(sequenceNumber), business_msm), std::make_tuple(std::ref(sequenceNumber), business_msm)};
120 };
121 
122 template<class SrcMsgDetails, class DestMsgDetails> inline std::ostream &
123 operator<<(std::ostream &os, exchange_to_client_transformations<SrcMsgDetails, DestMsgDetails> const &ec) noexcept(false);
124 
125 /// A simple, BATSBOE-protocol exchange simulator.
126 /**
127  The behaviour of this simulator is a simplification derived from the specification in [1, 2].
128  [1] "BATS Chi-X Europe Binary Order Entry Specification Version 1.44"
129  [2] "BZX Exchange US Equities BOE Specification Version 1.8.6"
130 */
131 template<class SrcMsgDetails>
132 class simulator_responses final : public exchanges::common::simulator_responses<SrcMsgDetails, simulator_responses<SrcMsgDetails>> {
133 public:
134  using base_t=exchanges::common::simulator_responses<SrcMsgDetails, simulator_responses<SrcMsgDetails>>;
135  using msg_details_t=typename base_t::msg_details_t;
136 
137  /// The only valid instrument, all others will be rejected.
138  static inline constexpr typename msg_details_t::SecurityID_t instrumentID{"GB00BH4HKS39"};
139  /// An invalid instrument, that will be rejected.
140  static inline constexpr typename msg_details_t::SecurityID_t invalidInstrumentID{"GB0000595859"};
141 
142  simulator_responses() noexcept(true)
143  : base_t(), msm{std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this), std::ref(*this)} {
144  }
145  simulator_responses(simulator_responses const &sr) noexcept(true)
146  : 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)} {
147  }
148 
149  /**
150  \return False to continue processing messages, true otherwise.
151  */
152  template<class SktT, class ClientCxn>
153  bool process_msg(typename msg_details_t::msg_buffer_t const &buff, SktT &exchg_skt, ClientCxn &client_skt);
154 
155  std::string to_string() const noexcept(false);
156 
157 private:
158  struct state_machine_t;
159  using machine=libjmmcg::msm::unroll::machine<state_machine_t>;
160 
161  const machine msm;
162 };
163 
164 template<class SrcMsgDetails> inline std::ostream &
165 operator<<(std::ostream &os, simulator_responses<SrcMsgDetails> const &ec) noexcept(false);
166 
167 } } } } }
168 
170 
171 #endif