fix8  version 1.4.0
Open Source C++ FIX Framework
hftest.hpp
Go to the documentation of this file.
1 //-------------------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
20 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
21 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
22 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
24 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
25 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
26 
27 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
28 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
29 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
30 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
31 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
32 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
33 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 
35 */
36 //-------------------------------------------------------------------------------------------------
37 #ifndef FIX8_HFTEST_HPP_
38 #define FIX8_HFTEST_HPP_
39 
40 //-----------------------------------------------------------------------------------------
41 class hf_session_client;
42 
44 
47 {
49 
50 public:
53  tex_router_client(hf_session_client& session) : _session(session) {}
54 
58  virtual bool operator() (const FIX8::TEX::ExecutionReport *msg);
59 };
60 
62 
65 {
67  using Nos_queue = std::queue<FIX8::TEX::NewOrderSingle *>;
69 
70 public:
78  FIX8::Logger *logger=nullptr, FIX8::Logger *plogger=nullptr) : Session(ctx, sid, persist, logger, plogger), _router(*this) {}
79 
86  bool handle_application(const unsigned seqnum, const FIX8::Message *&msg);
87 
88  void push(FIX8::TEX::NewOrderSingle *nos) { _nos_queue.push(nos); }
90  {
91  if (_nos_queue.empty())
92  return 0;
93  FIX8::TEX::NewOrderSingle *nos(_nos_queue.front());
94  _nos_queue.pop();
95  return nos;
96  }
97 
98  bool cached() const { return !_nos_queue.empty(); }
99  int size() const { return static_cast<int>(_nos_queue.size()); }
100 };
101 
102 //-----------------------------------------------------------------------------------------
103 class hf_session_server;
104 
106 
109 {
111 
112 public:
115  tex_router_server(hf_session_server& session) : _session(session) {}
116 
120  virtual bool operator() (const FIX8::TEX::NewOrderSingle *msg);
121 };
122 
124 
127 {
129 
130 public:
138  FIX8::Logger *logger=nullptr, FIX8::Logger *plogger=nullptr) : Session(ctx, sci, persist, logger, plogger),
139  _router(*this) {}
140 
146  bool handle_application(const unsigned seqnum, const FIX8::Message *&msg);
147 };
148 
149 //-------------------------------------------------------------------------------------------------
151 class MyMenu
152 {
154 
156  struct MenuItem
157  {
158  const char _key = 0;
159  const std::string _help;
160 
161  MenuItem(char key, const std::string help=std::string()) : _key(key), _help(help) {}
162  MenuItem() = default;
163  bool operator() (const MenuItem& a, const MenuItem& b) const { return a._key < b._key; }
164  };
165 
167  std::istream _istr;
168  std::ostream& _ostr;
169 
170  using Handlers = std::map<const MenuItem, bool (MyMenu::*)(), MenuItem>;
171  static const Handlers _handlers;
172 
173 public:
174  MyMenu(FIX8::Session& session, int infd, std::ostream& ostr)
175  : _tty(infd), _session(static_cast<hf_session_client&>(session)),
176  _istr(new FIX8::fdinbuf(infd)), _ostr(ostr) {}
177  virtual ~MyMenu() {}
178 
179  std::istream& get_istr() { return _istr; }
180  std::ostream& get_ostr() { return _ostr; }
181  bool process(char ch)
182  {
183  auto itr(_handlers.find({ch}));
184  return itr == _handlers.end() ? true : (this->*itr->second)();
185  }
186 
187  bool new_order_single();
190  bool multi_new_order_single();
191  bool send_all_preloaded();
192  bool send_all_preloaded(coroutine& coro, FIX8::Session *ses);
193  bool help();
194  bool do_exit() { return false; }
195  bool do_logout();
196 
198 };
199 
200 //-----------------------------------------------------------------------------------------
202 struct RandDev
203 {
205  static void init()
206  {
207 #ifdef _MSC_VER
208  srand (static_cast<unsigned>(time(0) % _getpid()));
209 #else
210  srandom (static_cast<unsigned>(time(0) % getpid()));
211 #endif
212  }
213 
218  template<typename T>
219  static T getrandom(const T range=0)
220  {
221 #ifdef _MSC_VER
222  T target(rand());
223 #else
224  T target(random());
225 #endif
226  return range ? target / (RAND_MAX / range + 1) : target;
227  }
228 };
229 
230 #endif // FIX8_HFTEST_HPP_
231 
static void init()
Initialise the random number generator.
Definition: hftest.hpp:205
bool handle_application(const unsigned seqnum, const FIX8::Message *&msg)
Definition: hftest.cpp:579
bool do_logout()
Definition: harness.cpp:367
std::ostream & _ostr
Definition: hftest.hpp:168
f8_thread delegated async logging class
Definition: logger.hpp:153
static const Handlers _handlers
Definition: hftest.hpp:171
FIX8::tty_save_state & get_tty()
Definition: hftest.hpp:197
virtual bool operator()(const FIX8::TEX::ExecutionReport *msg)
Definition: hftest.cpp:655
Fix8 Base Session. User sessions are derived from this class.
Definition: session.hpp:394
const std::string _help
Definition: hftest.hpp:159
bool preload_new_order_single()
Definition: hftest.cpp:491
Example server session. Derives from FIX8::Session.
Definition: hftest.hpp:126
static T getrandom(const T range=0)
Definition: hftest.hpp:219
bool send_all_preloaded()
Definition: hftest.cpp:442
Example server message router. Derives from fix8 generated router class.
Definition: hftest.hpp:108
std::queue< FIX8::TEX::NewOrderSingle * > Nos_queue
Definition: hftest.hpp:67
Base (ABC) Persister class.
Definition: persist.hpp:55
ExecutionReport (8), application, 326 fields, 16 groups.
bool process(char ch)
Definition: hftest.hpp:181
FIX8::tty_save_state _tty
Definition: hftest.hpp:153
Quickfix style sessionid.
Definition: session.hpp:46
bool new_order_single()
Definition: hftest.cpp:419
bool operator()(const MenuItem &a, const MenuItem &b) const
Definition: hftest.hpp:163
std::istream & get_istr()
Definition: hftest.hpp:179
A random number generator wrapper.
Definition: hftest.hpp:202
virtual ~MyMenu()
Definition: hftest.hpp:177
bool handle_application(const unsigned seqnum, const FIX8::Message *&msg)
Definition: hftest.cpp:667
hf_session_server & _session
Definition: hftest.hpp:110
const F8MetaCntx & ctx()
Compiler generated metadata object, accessed through this function.
NewOrderSingle (D), application, 243 fields, 11 groups.
bool do_exit()
Definition: hftest.hpp:194
int size() const
Definition: hftest.hpp:99
hf_session_client & _session
Definition: hftest.hpp:166
std::map< const MenuItem, bool(MyMenu::*)(), MenuItem > Handlers
Definition: hftest.hpp:170
virtual bool operator()(const FIX8::TEX::NewOrderSingle *msg)
Definition: hftest.cpp:585
bool multi_new_order_single()
Definition: hftest.cpp:403
Simple menu system that will work with most term types.
Definition: hftest.hpp:151
hf_session_client & _session
Definition: hftest.hpp:48
A complete Fix message with header, body and trailer.
Definition: message.hpp:1058
const char _key
Definition: hftest.hpp:158
FIX8::TEX::NewOrderSingle * pop()
Definition: hftest.hpp:89
F8API Session(const F8MetaCntx &ctx, const SessionID &sid, Persister *persist=nullptr, Logger *logger=nullptr, Logger *plogger=nullptr)
Definition: session.cpp:105
MenuItem()=default
MenuItem(char key, const std::string help=std::string())
Definition: hftest.hpp:161
std::ostream & get_ostr()
Definition: hftest.hpp:180
tex_router_server _router
Definition: hftest.hpp:128
MyMenu(FIX8::Session &session, int infd, std::ostream &ostr)
Definition: hftest.hpp:174
Example client session. Derives from FIX8::Session.
Definition: hftest.hpp:64
tex_router_server(hf_session_server &session)
Definition: hftest.hpp:115
std::istream _istr
Definition: hftest.hpp:167
bool help()
Definition: harness.cpp:345
Individual menu item.
Definition: hftest.hpp:156
bool batch_preload_new_order_single()
Definition: hftest.cpp:358
bool cached() const
Definition: hftest.hpp:98
Static metadata context class - one per FIX xml schema.
Definition: message.hpp:210
hf_session_server(const FIX8::F8MetaCntx &ctx, const FIX8::sender_comp_id &sci, FIX8::Persister *persist=nullptr, FIX8::Logger *logger=nullptr, FIX8::Logger *plogger=nullptr)
Definition: hftest.hpp:137
Example client message router. Derives from fix8 generated router class.
Definition: hftest.hpp:46
void push(FIX8::TEX::NewOrderSingle *nos)
Definition: hftest.hpp:88
tex_router_client _router
Definition: hftest.hpp:66
tex_router_client(hf_session_client &session)
Definition: hftest.hpp:53
Nos_queue _nos_queue
Definition: hftest.hpp:68
hf_session_client(const FIX8::F8MetaCntx &ctx, const FIX8::SessionID &sid, FIX8::Persister *persist=nullptr, FIX8::Logger *logger=nullptr, FIX8::Logger *plogger=nullptr)
Definition: hftest.hpp:77