fix8  version 1.4.0
Open Source C++ FIX Framework
myfix.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_MYFIX_HPP_
38 #define FIX8_MYFIX_HPP_
39 
40 //-----------------------------------------------------------------------------------------
42 
44 
47 {
49 
50 public:
53  tex_router_client(myfix_session_client& session) : _session(session) {}
54 
58  virtual bool operator() (const FIX8::TEX::ExecutionReport *msg) const;
59 };
60 
62 
65 {
67 
68 public:
76  FIX8::Logger *logger=nullptr, FIX8::Logger *plogger=nullptr) : Session(ctx, sid, persist, logger, plogger), _router(*this) {}
77 
84  bool handle_application(const unsigned seqnum, const FIX8::Message *&msg);
85 
90 };
91 
92 //-----------------------------------------------------------------------------------------
94 
96 
99 {
101 
102 public:
105  tex_router_server(myfix_session_server& session) : _session(session) {}
106 
110  virtual bool operator() (const FIX8::TEX::NewOrderSingle *msg) const;
111 };
112 
114 
117 {
119 
120 public:
128  FIX8::Logger *logger=nullptr, FIX8::Logger *plogger=nullptr) : Session(ctx, sci, persist, logger, plogger),
129  _router(*this) {}
130 
136  bool handle_application(const unsigned seqnum, const FIX8::Message *&msg);
137 
141 
146 };
147 
148 //-------------------------------------------------------------------------------------------------
150 class MyMenu
151 {
155 
157  struct MenuItem
158  {
159  const char _key = 0;
160  const std::string _help;
161 
162  MenuItem(char key, const std::string help=std::string()) : _key(key), _help(help) {}
163  MenuItem() = default;
164  bool operator() (const MenuItem& a, const MenuItem& b) const { return a._key < b._key; }
165  };
166 
168  std::istream _istr;
169  std::ostream& _ostr;
170 
171  using Handlers = std::map<const MenuItem, bool (MyMenu::*)(), MenuItem>;
172  static const Handlers _handlers;
173 
174 public:
175  MyMenu(FIX8::Session& session, int infd, std::ostream& ostr, FIX8::ConsoleMenu *cm=nullptr)
176  : _tty(infd), _cm(cm), _session(static_cast<myfix_session_client&>(session)),
177  _istr(new FIX8::fdinbuf(infd)), _ostr(ostr) {}
178  virtual ~MyMenu() {}
179 
180  std::istream& get_istr() { return _istr; }
181  std::ostream& get_ostr() { return _ostr; }
182  bool process(char ch)
183  {
184  auto itr(_handlers.find({ch}));
185  if (itr == _handlers.end())
186  {
187  _ostr << "Command not found";
188  return true;
189  }
190  return (this->*itr->second)();
191  }
192 
193  bool new_order_single();
194  bool new_order_single_50();
195  bool new_order_single_1000();
196  bool resend_request();
197  bool help();
198  bool do_exit() { return false; }
199  bool do_logout();
200  bool create_msgs();
201  bool version_info();
202  bool edit_msgs();
203  bool delete_msg();
204  bool delete_msgs();
205  bool print_msgs();
206  bool send_msgs();
207  bool send_msg();
208  bool write_msgs();
209  bool read_msgs();
210  bool set_lpp();
211  bool toggle_heartbeats();
212  bool static_probe();
215 
216  bool load_msgs(const std::string& fname);
219  void send_lst();
220  bool save_msg(const std::string& fname, FIX8::Message *msg);
221  unsigned get_msg_cnt() const { return _lst.size(); }
222 
224 
226  {
227  char buff[128] {};
228  _tty.unset_raw_mode();
229  _istr.getline(buff, sizeof(buff));
230  _tty.set_raw_mode();
231  return to = buff;
232  }
233 };
234 
235 //-----------------------------------------------------------------------------------------
237 struct RandDev
238 {
239  static void init()
240  {
241  time_t tval(time(0));
242 #ifdef _MSC_VER
243  srand (static_cast<unsigned>(((tval % _getpid()) * tval)));
244 #else
245  srandom (static_cast<unsigned>(((tval % getpid()) * tval)));
246 #endif
247  }
248 
249  template<typename T>
250  static T getrandom(const T range=0)
251  {
252 #ifdef _MSC_VER
253  T target(rand());
254 #else
255  T target(random());
256 #endif
257  return range ? target / (RAND_MAX / range + 1) : target;
258  }
259 };
260 
261 #endif // FIX8_MYFIX_HPP_
262 
bool write_msgs()
static void init()
Definition: myfix.hpp:239
bool new_order_single_alternate()
Definition: myfix.cpp:600
bool do_logout()
Definition: harness.cpp:367
std::ostream & _ostr
Definition: hftest.hpp:168
myfix_session_server & _session
Definition: myfix.hpp:100
f8_thread delegated async logging class
Definition: logger.hpp:153
unsigned get_msg_cnt() const
Definition: myfix.hpp:221
static const Handlers _handlers
Definition: hftest.hpp:171
FIX8::tty_save_state & get_tty()
Definition: myfix.hpp:223
virtual bool operator()(const FIX8::TEX::ExecutionReport *msg)
Definition: hftest.cpp:655
FIX8::Message * generate_new_order_single_alternate()
Definition: myfix.cpp:454
Fix8 Base Session. User sessions are derived from this class.
Definition: session.hpp:394
MyMenu(FIX8::Session &session, int infd, std::ostream &ostr, FIX8::ConsoleMenu *cm=nullptr)
Definition: myfix.hpp:175
const std::string _help
Definition: hftest.hpp:159
tex_router_client(myfix_session_client &session)
Definition: myfix.hpp:53
bool new_order_single_recycled()
Definition: myfix.cpp:614
static T getrandom(const T range=0)
Definition: myfix.hpp:250
Example server message router. Derives from fix8 generated router class.
Definition: hftest.hpp:108
Base (ABC) Persister class.
Definition: persist.hpp:55
ExecutionReport (8), application, 326 fields, 16 groups.
bool process(char ch)
Definition: myfix.hpp:182
FIX8::tty_save_state _tty
Definition: hftest.hpp:153
Quickfix style sessionid.
Definition: session.hpp:46
Example server session. Derives from FIX8::Session.
Definition: myfix.hpp:116
bool new_order_single()
Definition: hftest.cpp:419
bool delete_msgs()
Definition: harness.cpp:440
bool operator()(const MenuItem &a, const MenuItem &b) const
Definition: hftest.hpp:163
bool load_msgs(const std::string &fname)
Definition: harness.cpp:533
std::deque< FIX8::Message * > MsgList
Definition: consolemenu.hpp:44
bool save_msg(const std::string &fname, FIX8::Message *msg)
Definition: harness.cpp:496
FIX8::Message * generate_new_order_single()
Definition: myfix.cpp:527
bool toggle_heartbeats()
Definition: harness.cpp:515
void state_change(const FIX8::States::SessionStates before, const FIX8::States::SessionStates after)
Definition: harness.cpp:327
bool print_msgs()
Definition: harness.cpp:463
std::istream & get_istr()
Definition: myfix.hpp:180
A random number generator wrapper.
Definition: hftest.hpp:202
virtual ~MyMenu()
Definition: myfix.hpp:178
tex_router_server _router
Definition: myfix.hpp:118
tex_router_client _router
Definition: myfix.hpp:66
bool set_lpp()
Definition: harness.cpp:357
const F8MetaCntx & ctx()
Compiler generated metadata object, accessed through this function.
NewOrderSingle (D), application, 243 fields, 11 groups.
bool delete_msg()
Definition: harness.cpp:433
bool do_exit()
Definition: myfix.hpp:198
bool handle_application(const unsigned seqnum, const FIX8::Message *&msg)
Definition: harness.cpp:321
Console test harness menu.
Definition: consolemenu.hpp:48
bool version_info()
Definition: harness.cpp:417
std::map< const MenuItem, bool(MyMenu::*)(), MenuItem > Handlers
Definition: hftest.hpp:170
bool static_probe()
Definition: myfix.cpp:643
bool read_msgs()
Definition: harness.cpp:576
virtual bool operator()(const FIX8::TEX::NewOrderSingle *msg)
Definition: hftest.cpp:585
Example client session. Derives from FIX8::Session.
Definition: myfix.hpp:64
Simple menu system that will work with most term types.
Definition: hftest.hpp:151
tex_router_server(myfix_session_server &session)
Definition: myfix.hpp:105
A complete Fix message with header, body and trailer.
Definition: message.hpp:1058
void send_lst()
Definition: harness.cpp:447
const char _key
Definition: hftest.hpp:158
void state_change(const FIX8::States::SessionStates before, const FIX8::States::SessionStates after)
Definition: harness.cpp:339
F8API Session(const F8MetaCntx &ctx, const SessionID &sid, Persister *persist=nullptr, Logger *logger=nullptr, Logger *plogger=nullptr)
Definition: session.cpp:105
myfix_session_client(const FIX8::F8MetaCntx &ctx, const FIX8::SessionID &sid, FIX8::Persister *persist=nullptr, FIX8::Logger *logger=nullptr, FIX8::Logger *plogger=nullptr)
Definition: myfix.hpp:75
FIX8::MsgList _lst
Definition: myfix.hpp:153
bool handle_application(const unsigned seqnum, const FIX8::Message *&msg)
Definition: harness.cpp:333
bool new_order_single_1000()
Definition: myfix.cpp:692
MenuItem()=default
bool create_msgs()
Definition: harness.cpp:410
MenuItem(char key, const std::string help=std::string())
Definition: myfix.hpp:162
std::ostream & get_ostr()
Definition: myfix.hpp:181
FIX8::ConsoleMenu * _cm
Definition: myfix.hpp:154
FIX8::f8String & get_string(FIX8::f8String &to)
Definition: myfix.hpp:225
myfix_session_client & _session
Definition: myfix.hpp:167
std::istream _istr
Definition: hftest.hpp:167
myfix_session_client & _session
Definition: myfix.hpp:48
bool help()
Definition: harness.cpp:345
bool edit_msgs()
Definition: harness.cpp:426
Individual menu item.
Definition: hftest.hpp:156
bool resend_request()
Definition: myfix.cpp:727
Static metadata context class - one per FIX xml schema.
Definition: message.hpp:210
Example client message router. Derives from fix8 generated router class.
Definition: hftest.hpp:46
bool send_msgs()
Definition: harness.cpp:455
bool send_msg()
Definition: harness.cpp:471
std::string f8String
Definition: f8types.hpp:47
myfix_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: myfix.hpp:127
bool sample_scheduler_callback()
Definition: myfix.cpp:447
bool new_order_single_50()
Definition: myfix.cpp:681