PageRenderTime 74ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/MT/MT_GUI/server/MT_Client.cpp

http://github.com/leonard-lab/MADTraC
C++ | 377 lines | 315 code | 56 blank | 6 comment | 47 complexity | fd55b18c64799fab175a7f183b607a40 MD5 | raw file
Possible License(s): GPL-2.0
  1. #include "MT_Client.h"
  2. #include <iostream>
  3. #include <sstream>
  4. #include <iomanip>
  5. MT_Client::MT_Client()
  6. : m_pSocketClient(NULL),
  7. m_bDebugOutput(false),
  8. m_pDebugFile(stdout),
  9. m_bIsConnected(false)
  10. {
  11. }
  12. MT_Client::~MT_Client()
  13. {
  14. if(m_pSocketClient)
  15. {
  16. m_pSocketClient->Destroy();
  17. }
  18. }
  19. MT_Server::err_code MT_Client::Connect(wxString hostname,
  20. long port)
  21. {
  22. if(port <= 0)
  23. {
  24. return MT_Server::err_invalid_port;
  25. }
  26. if(!m_pSocketClient)
  27. {
  28. m_pSocketClient = new wxSocketClient();
  29. }
  30. wxIPV4address addr;
  31. addr.Hostname(hostname);
  32. addr.Service(port);
  33. if(m_bDebugOutput)
  34. {
  35. fprintf(m_pDebugFile,
  36. "Attempting to connect to server at %s:%ld\n",
  37. (const char*) hostname.mb_str(),
  38. port);
  39. }
  40. /* non-blocking connect */
  41. m_pSocketClient->Connect(addr, false);
  42. /* wait */
  43. m_pSocketClient->WaitOnConnect(10);
  44. if(!m_pSocketClient->IsConnected())
  45. {
  46. if(m_bDebugOutput)
  47. {
  48. fprintf(m_pDebugFile,
  49. "Failure to connect to server at %s:%ld\n",
  50. (const char*) hostname.mb_str(),
  51. port);
  52. }
  53. return MT_Server::err_connect_failed;
  54. }
  55. else
  56. {
  57. if(m_bDebugOutput)
  58. {
  59. fprintf(m_pDebugFile,
  60. "Success connecting to server at %s:%ld\n",
  61. (const char*) hostname.mb_str(),
  62. port);
  63. }
  64. m_bIsConnected = true;
  65. return MT_Server::err_no_error;
  66. }
  67. }
  68. bool MT_Client::PingServer()
  69. {
  70. if(!m_bIsConnected)
  71. {
  72. return false;
  73. }
  74. if(m_bDebugOutput)
  75. {
  76. fprintf(m_pDebugFile,
  77. "Initiating ping to server.\n");
  78. }
  79. MT_Server::t_msg reply = MT_SendCommand(MT_Server::cmd_Ping,
  80. m_pSocketClient);
  81. if(reply != MT_Server::msg_OK)
  82. {
  83. if(m_bDebugOutput)
  84. {
  85. fprintf(m_pDebugFile,
  86. "Error in ping reply. Got %d, expecting %d\n",
  87. reply,
  88. MT_Server::msg_OK);
  89. }
  90. return false;
  91. }
  92. else
  93. {
  94. if(m_bDebugOutput)
  95. {
  96. fprintf(m_pDebugFile,
  97. "Success.\n");
  98. }
  99. MT_SendAck(m_pSocketClient);
  100. }
  101. return true;
  102. }
  103. double MT_Client::getServerUptime()
  104. {
  105. if(!m_bIsConnected)
  106. {
  107. return -1.0;
  108. }
  109. if(m_bDebugOutput)
  110. {
  111. fprintf(m_pDebugFile, "Getting uptime from server.\n");
  112. }
  113. double t = -1.0;
  114. bool sent = MT_SendMessage(MT_Server::msg_UPTIME,
  115. m_pSocketClient);
  116. if(!sent)
  117. {
  118. if(m_bDebugOutput)
  119. {
  120. fprintf(m_pDebugFile,
  121. "Error: Unable to send message to server..\n");
  122. }
  123. return t;
  124. }
  125. t = MT_ReadDouble(m_pSocketClient);
  126. if(m_bDebugOutput)
  127. {
  128. fprintf(m_pDebugFile, "Uptime was %f\n", t);
  129. }
  130. return t;
  131. }
  132. bool MT_Client::InitiateExchange()
  133. {
  134. if(!m_bIsConnected)
  135. {
  136. return false;
  137. }
  138. if(m_bDebugOutput)
  139. {
  140. fprintf(m_pDebugFile,
  141. "Initiating exchange with server.\n");
  142. }
  143. MT_Server::t_cmd init = MT_Server::cmd_Init;
  144. MT_Server::t_msg reply = MT_Server::msg_ERR;
  145. m_pSocketClient->Write(&init, 1);
  146. m_pSocketClient->Read(&reply, 1);
  147. if(reply != MT_Server::msg_OK)
  148. {
  149. if(m_bDebugOutput)
  150. {
  151. fprintf(m_pDebugFile,
  152. "Error in exchange reply. Got %d, expecting %d\n",
  153. reply,
  154. MT_Server::msg_OK);
  155. }
  156. return false;
  157. }
  158. else
  159. {
  160. if(m_bDebugOutput)
  161. {
  162. fprintf(m_pDebugFile,
  163. "Succes.\n");
  164. }
  165. MT_SendAck(m_pSocketClient);
  166. return true;
  167. }
  168. }
  169. bool MT_Client::EndExchange()
  170. {
  171. if(!m_bIsConnected)
  172. {
  173. return false;
  174. }
  175. if(m_bDebugOutput)
  176. {
  177. fprintf(m_pDebugFile,
  178. "Ending exchange with server.\n");
  179. }
  180. MT_Server::t_msg msg = MT_Server::msg_END;
  181. MT_Server::t_msg reply = MT_Server::msg_ERR;
  182. m_pSocketClient->Write(&msg, 1);
  183. m_pSocketClient->Read(&reply, 1); /* ack */
  184. m_pSocketClient->Read(&reply, 1); /* end */
  185. MT_SendAck(m_pSocketClient);
  186. if(reply != MT_Server::msg_END)
  187. {
  188. if(m_bDebugOutput)
  189. {
  190. fprintf(m_pDebugFile,
  191. "Error in exchange reply. Got %d, expecting %d\n",
  192. reply,
  193. MT_Server::msg_END);
  194. }
  195. return false;
  196. }
  197. else
  198. {
  199. if(m_bDebugOutput)
  200. {
  201. fprintf(m_pDebugFile,
  202. "Succes.\n");
  203. }
  204. return true;
  205. }
  206. }
  207. std::string MT_Client::getMOTD()
  208. {
  209. if(!m_bIsConnected)
  210. {
  211. return std::string("!error: not connected!");
  212. }
  213. if(m_bDebugOutput)
  214. {
  215. fprintf(m_pDebugFile, "Getting MOTD from server.\n");
  216. }
  217. MT_Server::t_msg motd = MT_Server::msg_MOTD;
  218. MT_Server::t_msg ack = MT_Server::msg_ACK;
  219. std::string MOTD("!error: couldn't get MOTD!");
  220. m_pSocketClient->Write(&motd, 1);
  221. m_pSocketClient->Read(&ack, 1);
  222. MOTD = MT_ReadString(m_pSocketClient);
  223. if(m_bDebugOutput)
  224. {
  225. fprintf(m_pDebugFile, "MOTD was %s\n", MOTD.c_str());
  226. }
  227. return MOTD;
  228. }
  229. void MT_Client::getMessageTable()
  230. {
  231. if(!m_bIsConnected){return;};
  232. if(m_bDebugOutput)
  233. {
  234. fprintf(m_pDebugFile,
  235. "Requesting message table.\n");
  236. }
  237. MT_Server::t_cmd list_cmd = MT_Server::cmd_List;
  238. MT_Server::t_msg reply = MT_Server::msg_ERR;
  239. m_pSocketClient->Write(&list_cmd, 1);
  240. m_pSocketClient->Read(&reply, 1);
  241. if(reply != MT_Server::msg_OK)
  242. {
  243. if(m_bDebugOutput)
  244. {
  245. fprintf(m_pDebugFile,
  246. "Error in exchange reply. Got %d, expecting %d\n",
  247. reply,
  248. MT_Server::msg_OK);
  249. }
  250. return;
  251. }
  252. //MT_SendAck(m_pSocketClient);
  253. int n_messages = MT_ReadInt(m_pSocketClient);
  254. t_msg_table_entry c_entry;
  255. for(int i = 0; i < n_messages; i++)
  256. {
  257. /* force ack because the message might *be* an ack */
  258. c_entry.server_code = MT_ReceiveMessage(m_pSocketClient, true);
  259. c_entry.client_code = MT_ReceiveMessage(m_pSocketClient, true);
  260. c_entry.description = MT_ReadString(m_pSocketClient);
  261. c_entry.owner_name = MT_ReadString(m_pSocketClient);
  262. m_MessageTable.push_back(c_entry);
  263. }
  264. std::string mod_name;
  265. unsigned char s_c;
  266. /* skip the first one */
  267. for(unsigned int i = 1; i < m_MessageTable.size(); i++)
  268. {
  269. mod_name = m_MessageTable[i].owner_name;
  270. s_c = m_MessageTable[i].server_code;
  271. if(mod_name.compare("MT_Server") == 0)
  272. {
  273. /* TODO should also message map server... */
  274. }
  275. else
  276. {
  277. for(unsigned int j = 0; j < m_vpModules.size(); j++)
  278. {
  279. if(mod_name.compare(m_vpModules[j]->getName()) == 0)
  280. {
  281. m_vpModules[j]->m_vMessageMap.push_back(s_c);
  282. }
  283. }
  284. }
  285. }
  286. }
  287. std::string MT_Client::getMessageTableAsString()
  288. {
  289. std::ostringstream s;
  290. unsigned int w_d = 30;
  291. unsigned int w_c = 12;
  292. unsigned int w_m = 20;
  293. s << std::left << std::setw(w_d) << "Header" <<
  294. std::right <<
  295. std::setw(w_c) << "Server Code" <<
  296. std::setw(w_c) << "Client Code" <<
  297. std::left << " " <<
  298. std::setw(w_m) << "Module Name" << std::endl;
  299. for(unsigned int i = 0; i < m_MessageTable.size(); i++)
  300. {
  301. std::ostringstream s2;
  302. s2 << "'" << m_MessageTable[i].description << "'";
  303. s << std::left <<
  304. std::setw(w_d) << s2.str() <<
  305. std::right <<
  306. std::setw(w_c - 3) << (int) m_MessageTable[i].server_code <<
  307. " " <<
  308. std::setw(w_c - 3) << (int) m_MessageTable[i].client_code <<
  309. " " <<
  310. std::left << " " <<
  311. std::setw(w_m) << m_MessageTable[i].owner_name <<
  312. std::endl;
  313. }
  314. return s.str();
  315. }
  316. bool MT_Client::addModule(MT_ServerModule* p_module)
  317. {
  318. if(!p_module->isForClient()){return false;};
  319. m_vpModules.push_back(p_module);
  320. return true;
  321. }