Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
TSAPlatformSocket.h
1 /* ===================================================================
2 *
3 * T R A D I N G S Y S T E M A P I ™
4 * Copyright © 1999 - 2014 by Peter Ritter ( TradingSystemAPI.com )
5 * A L L R I G H T S R E S E R V E D
6 *
7 * Consult your license regarding permissions and restrictions.
8 * You may obtain a copy of the License at:
9 * http://www.TradingSystemAPI.com/licenses/LICENSE-TSAPI-3.0.html
10 *
11 * ====================================================================
12 */
13 
14 #ifndef TSA_PLATFORM_SOCKET__INCLUDED
15 #define TSA_PLATFORM_SOCKET__INCLUDED
16 
17 #include <map>
18 #include <vector>
19 #include <chrono>
20 #include <thread>
21 #include <atomic>
22 
23 #include "TSATypeDef.h"
24 #include "TSAStreams.h"
25 #include "TSAOrder.h"
26 #include "TSAIntraday.h"
27 #include "TSAPlatformMsg.h"
28 #include "TSAJSON.h"
29 
30 namespace tsa {
31 
32  class transaction;
33  class strategy;
34  class platform_proxy;
35 
37  namespace net {
38 
39  /*
40  ** ====================================================
41  ** >>> class socket <<<
42  ** ====================================================
43  */
44 
46  class dll_export socket_base : public object {
47  tsa_declare_testable;
48  protected:
49  bool test__no_dispatch_to_strategy = false;
50  bool test__no_thread = false;
51  public:
52  static std::atomic<int> s_signal_value;
53  protected:
54  struct header {
55  int process_id;
56  int strategy_id;
57  message::type message_type;
58  };
59  std::vector<strategy*> m_strategies;
60  mutable std::mutex m_mutex;
61  mutable std::mutex m_mutex_handle_exception;
62  public:
63  const std::string& id(void)const;
64  void id(const std::string& id);
65  private:
66  std::string m_idendity;
67  bool m_has_already_run = false;
68  bool m_message_pump_is_running = false;
69  bool m_is_connected = false;
70  bool m_strategies_are_running = false;
71  private:
72  mutable std::mutex m_shutdown_request_mutex;
73  bool m_shutdown_requested = false;
74  std::string m_strategy_exception_what;
75  enum class shutdown_type {
76  normal_shutdown,
77  shutdown_with_exception
78  };
79  shutdown_type m_shutdown_type = shutdown_type::normal_shutdown;
80  void request_shutdown(shutdown_type type, const std::string& exception_what = "");
81 
82  public:
83  bool is_running(void)const;
84  bool is_connected(void)const;
85  void set_connected(bool);
86  public:
87  socket_base(void);
88  virtual ~socket_base(void);
89  void run(void);
90  void on_strategy_exception(const strategy*, const std::exception&);
91  public:
92  //==============
93  // Strategies
94  //==============
95 
96  // Calls platform initialize.
97  void init_strategies(void);
98  // Number of strategies
99  size_t strategy_count(void)const;
100  // Registers a new strategy
101  identifier_t register_strategy(strategy*);
102  void remove_strategy_ref(strategy*);
103  bool has_valid_strategy__no_lock(void)const;
104  //void exit(void);
105  // Removes a strategy with given identifier
106  void remove_strategy(identifier_t);
107  // Returns the strategy pointer for given pos
108  strategy* strategy_ptr(int pos)const;
109  // Returns the strategy for given id.
110  //tsa::strategy& strategy_ref(identifier_t id)const;
111  public:
112  // sends message
113  virtual void send(net::message&) = 0;
114  // respond to incoming message
115  virtual void on_message(net::message&);
116  // respond to error
117  virtual void on_error(const std::string& error);
118  //
119  virtual void on_connection_lost(void);
120  public:
121  // waits for a message to arrive within given timeout interval
122  virtual bool wait_message(net::message&, int usec_timeout) = 0;
123  public:
124  // dispatch message to one of the strategies
125  void dispatch_message(net::message&);
126  private:
127  void send_ctrl_c_exit_command_to_all_strategies(void);
128  protected:
129  //========
130  // RPC
131  //========
132  std::mutex m_mutex__RPC_values;
133  std::map<int, std::string> m_RPC_return_vals;
134  int m_RPC_token_count = 1;
135  int RPC_next_token(void);
136  public:
137  bool RPC(const json& call_definition, json& response, int timeout_millisec = 6000);
138  void RPC__except(const json& call_definition, json& response, int timeout_millisec = 6000);
139 
140  int send__RPC_request(const json& call_def);// , identifier_t strat_id);
141  bool fetch_RPC_result(int token, std::string& return_value, int timeout_millisec = 6000);
142 
143  void RPC__set_return_value(int token, const std::string& value);
144  bool RPC__return_value_exists(int token);
145  bool RPC__return_value_exists__no_lock(int token);
146 
147  void notify_strategy_stopped(identifier_t strat_id, const std::string& exit_return_string);
148  protected:
149  //========
150  // THREAD
151  //========
152  std::thread m_message_pump_thread;
153  void thread_fn__message_pump(void);
154  bool m_stop_msg_pump_thread = false;
155 
156  std::thread m_command_line_thread;
157  void thread_fn__command_line(void);
158  bool m_stop_command_line_thread = false;
159 
160  void start_message_pump_thread(void);
161  //void start_command_line_thread(void);
162  void start_threads(void);
163  public:
164  void stop_threads(void);
165  public:
166  //========
167  // LOG
168  //========
169  void write_log(const std::string& error);
170  };
171 
172 
173  }//namespace net
174 
175 }//namespace tsa
176 
177 #endif
Namespace for the &#39;Trading System API&#39; library.
Definition: original1.TSA3Core.cpp:20
Parent class for many library classes.
Definition: TSATypeDef.h:462
A class representing JSON objects.
Definition: TSAJSON.h:81
Class strategy represents a trading or investment strategy.
Definition: TSAStrategy.h:108
Definition: TSAPlatformSocket.h:46
int64_t identifier_t
type for ID&#39;s
Definition: TSATypeDef.h:117