main.cpp
上传用户:wenyu2k02
上传日期:2016-04-13
资源大小:28k
文件大小:8k
源码类别:

金融证券系统

开发平台:

Delphi

  1. #include "config.h"
  2. #include <string>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6. #include <queue>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <loki/Factory.h>
  10. #include <boost/thread/detail/config.hpp>
  11. #include <boost/thread/condition.hpp>
  12. #include <boost/thread/thread.hpp>
  13. #include <boost/thread/recursive_mutex.hpp>
  14. // #include <boost/smart_assert/assert_impl.hpp>
  15. #include <boost/date_time/posix_time/posix_time_types.hpp>
  16. #include "response.h"
  17. #include "socketHandler.h"
  18. #include "ClientSocket.h"
  19. #include "stock.h"
  20. #include "request.h"
  21. #include "commands.h"
  22. #include "cli.hpp"
  23. #include "util.hpp"
  24. #include "StockIo.h"
  25. // using namespace smart_assert;
  26. using namespace StockMarket;
  27. using namespace boost;
  28. static condition initialize_c;
  29. static recursive_mutex initialize_m;
  30. static void send_queued_request(ClientSocket& cs)
  31. {
  32. static bool initial_send = true;
  33. // cout << "send_queued_request enterrn";
  34. recursive_mutex::scoped_lock guard(req_queue_mutex);
  35. CmdQueue_t &q = CmdQueue::Instance();
  36. if(!q.empty())
  37. {
  38. CmdData_t &d = q.front(); // retreive a command
  39. if(! initial_send)
  40. {
  41. d->next();
  42. }
  43. else
  44. {
  45. initial_send = false;
  46. }
  47. if(*d)
  48. {
  49. d->send(cs);
  50. }
  51. else
  52. {
  53. initial_send = true;
  54. q.pop(); // if no request, then destroy the command
  55. }
  56. }
  57. // cout << "send_queued_request exitrn";
  58. }
  59. // this is the thread to request data
  60. void communication(int running_mode_deal, gregorian::date load_tr_date_from, gregorian::date load_tr_date_to)
  61. {
  62. SocketHandler h;
  63. ClientSocket cs(h);
  64. cs.Open("218.18.103.38",7709);
  65. // cs.Open("210.22.9.235",7709);
  66. // cs.Open("61.141.193.172",7709);
  67.   // Add after Open
  68. h.Add(&cs);
  69. //// To do: if disconnect then reconnect
  70. //// use to count time
  71. posix_time::ptime now(posix_time::second_clock::local_time());
  72. gregorian::date today_date = now.date();
  73. uint today = date_to_uint(today_date);
  74. posix_time::ptime t1 = now;
  75. posix_time::time_duration reqInterval(0,2,0);
  76. posix_time::ptime td_start(now.date(), posix_time::time_duration(9,30,0));
  77. posix_time::ptime td_end(now.date(), posix_time::time_duration(15,0,0) + reqInterval);
  78. bool initial_stocksets_finished = false;
  79. bool initial_gbbq_info_started = false;
  80. bool initial_gbbq_info_finished = false;
  81. bool initial_all_finished = false;
  82. // calculate the initial transaction period
  83. bool get_first_instant_transact = true;
  84. if(running_mode_deal)
  85. {
  86. if(now > td_end)
  87. load_tr_date_to = gregorian::day_clock::local_day();
  88. else
  89. load_tr_date_to = gregorian::day_clock::local_day() - gregorian::date_duration(1);
  90. }
  91. gregorian::date tr_date(load_tr_date_from);
  92. // just to calculate the total stocks in each market
  93. AddStockListRequest(false);
  94. hist_transact.clear();
  95. h.Select(1,0);
  96. while (true)
  97. {
  98. h.GetCount();
  99. h.Select(1,0); // wait for ready first before sending request and handle response if any
  100. recursive_mutex::scoped_lock guard(req_queue_mutex);
  101. CmdQueue_t &q = CmdQueue::Instance();
  102. if(q.empty()) // Hmm...All initial request has been sent
  103. {
  104. if(initial_all_finished)
  105. {
  106. if(!running_mode_deal) break;
  107. now = posix_time::second_clock::local_time();
  108. if(now - t1 >= reqInterval && now < td_end && now >= td_start)
  109. {
  110. t1 = now;
  111. if(get_first_instant_transact)
  112. {
  113. AddInstantTransRequest(1000);
  114. get_first_instant_transact = false;
  115. }
  116. else
  117. {
  118. AddInstantTransRequest(100);
  119. }
  120. AddStockListRequest(true);
  121. }
  122. else
  123. {
  124. break;
  125. }
  126. }
  127. else if(initial_gbbq_info_finished)
  128. {
  129. if(!hist_transact.empty())
  130. {
  131. uint d = date_to_uint(tr_date);
  132. save_day_transaction(d, hist_transact[d]);
  133. tr_date += gregorian::date_duration(1);
  134. }
  135. hist_transact.clear();
  136. bool bNewRequestAdded = false;
  137. while(tr_date <= load_tr_date_to)
  138. {
  139. if(!transact_data_exsist(tr_date))
  140. {
  141. cout << "AddHistTransRequest start " << tr_date << endl;
  142. AddHistTransRequest(tr_date, tr_date);
  143. bNewRequestAdded = true;
  144. break;
  145. }
  146. tr_date += gregorian::date_duration(1);
  147. }
  148. if(!bNewRequestAdded)
  149. {
  150. initial_all_finished = true;
  151. }
  152. }
  153. else if(initial_gbbq_info_started)
  154. {
  155. cout << "initial_gbbq_info_finished" << endl;
  156. // clear the unused data at initial stage
  157. stock_basic_info::Instance().save_gbbq_info(date_to_uint(today_date));
  158. initial_gbbq_info_finished = true;
  159. }
  160. else if( initial_stocksets_finished)
  161. {
  162. stock_basic_info::Instance().save_stock_set();
  163. if(!stock_basic_info::Instance().is_gbbq_file_exist(today))
  164. {
  165. cout << "AddGBBQRequest start" << endl;
  166. stock_basic_info::Instance().clear_gbbq_info();
  167. AddGBBQRequest();
  168. }
  169. else
  170. {
  171. initial_gbbq_info_finished = true;
  172. }
  173. initial_gbbq_info_started = true;
  174. }
  175. else
  176. {
  177. cout << "AddStockListRequest start" << endl;
  178. // to get all stocks list
  179. AddStockListRequest(true);
  180. initial_stocksets_finished = true;
  181. }
  182. if(q.empty())
  183. {
  184. CmdData_t r(new StockHeartBeat());
  185. q.push(r);
  186. }
  187. }
  188.   // if yes, then check time-interval, if more than 1 minite..then add new request to it
  189.   //  当收到Response 的时候才设置Ready, 这些避免发送重复的序列号
  190.   if(Request::ready())
  191. {
  192. send_queued_request(cs);
  193. }
  194. }
  195. cout << "Communitcation exit" << endl;
  196. }
  197. void cli()
  198. {
  199. recursive_mutex::scoped_lock lk(initialize_m);
  200. initialize_c.wait(lk);
  201. cout << "/////////////////////////////////////////////////////////nn";
  202. cout << "tt Stock share analyse system...nn";
  203. cout << "/////////////////////////////////////////////////////////nn";
  204. cout << "Type anything or [q or Q] to quitnn";
  205. cli_grammar g;
  206. string str;
  207. while ( getline( cin, str ) )
  208. {
  209. if ( str[0] == 'q' || str[0] == 'Q' )
  210. break;
  211. if ( parse( str.c_str(), g).full ) // No skip
  212. {
  213. cout << "Success n";
  214. }
  215. else
  216. {
  217. cout << "ERROR: bad commandn";
  218. }
  219. }
  220. cout << "Bye... :-) nn";
  221. }
  222. // The main program to receive commands from user and show information
  223. int main()
  224. {
  225. {
  226. // init response handler
  227. recursive_mutex::scoped_lock guard(res_fact_mutex);
  228. ResponseFactory::Instance().Register(CMD_STOCK_LIST, CreateStockListRes);
  229. ResponseFactory::Instance().Register(CMD_STOCKHOLD_CHANGE, CreateStockHoldChgRes);
  230. ResponseFactory::Instance().Register(CMD_HEART_BEAT, CreateStockHeartBeatRes);
  231. ResponseFactory::Instance().Register(CMD_INSTANT_TRANS, CreateInstantTransRes);
  232. ResponseFactory::Instance().Register(CMD_HIS_TRANS, CreateHisTransRes);
  233. }
  234. // if(!stock_basic_info::Instance().load_basic_info()) return 1;
  235. // stock_basic_info::Instance().save_basic_info();
  236. // if(!stock_basic_info::Instance().load_block_info()) return 1;
  237. // stock_basic_info::Instance().save_block_info();
  238. // stock_basic_info::Instance().load_gbbq_info_mat(gregorian::day_clock::local_day(), 20);
  239. int running_mode_deal = 0; gregorian::date load_tr_date_from, load_tr_date_to;
  240. // load_tr_date_from = gregorian::day_clock::local_day() - gregorian::date_duration(5);
  241. // load_tr_date_to = gregorian::day_clock::local_day() - gregorian::date_duration(1);
  242. load_tr_date_from = gregorian::date(2007, 9, 22);
  243. load_tr_date_to = gregorian::date(2007, 9, 22);
  244. // load_tr_date_to = gregorian::day_clock::local_day();
  245. communication(running_mode_deal, load_tr_date_from, load_tr_date_to);
  246. // system("D:\temp\shutdown.bat");
  247. return 0;
  248. }