Digitalisierte Elektroverteilung zur permanenten Verbraucherüberwachung
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClientSimulation.cpp 6.2KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //Simulation of an external IP-TCP client using ASIO lib
  2. #include <iostream>
  3. #include "asio.hpp"
  4. #include <iostream>
  5. #include <array>
  6. #include <sstream>
  7. #include <string>
  8. #include <fstream>
  9. #include "PublisherData.h"
  10. #include "DataModel.h"
  11. #include "IpTcpClient.h"
  12. #include "NetClient.h"
  13. #include <fstream>
  14. #include <filesystem>
  15. #include <thread>
  16. #include <memory>
  17. #include <atomic>
  18. #include <regex>
  19. #include "easylogging++.h"
  20. INITIALIZE_EASYLOGGINGPP
  21. constexpr unsigned int BUFFER_SIZE = 256;
  22. //Format of Date in the log files
  23. constexpr char logDateFormat[] = "%Y-%m-%d %H:%M:%S";
  24. constexpr unsigned short DATE_HEADER_SIZE = 19;
  25. const auto session_id = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  26. using asio::ip::tcp;
  27. std::atomic_char keyboardInput = '0';
  28. void readInput() {
  29. while (1){
  30. keyboardInput = std::cin.get();
  31. std::cin.get();
  32. }
  33. }
  34. //Takes a message with log file body and extracts the last written log date
  35. //This values is taken for the next log file downloading, to minimize download data size
  36. //and prevent the aplication from downloading data twice
  37. long long parseDate(net::Message<net::MessageTypes>& msg) {
  38. if (msg.header.size < DATE_HEADER_SIZE)
  39. return 0;
  40. std::stringstream ss;
  41. //Find last occurence of '\n' with reverse iteration
  42. auto res = std::find_if(msg.body.rbegin(), msg.body.rend(),
  43. [](const uint8_t i) { return i == '\n'; });
  44. if (res == msg.body.rend()) {
  45. //No occurence of '\n', log file is empty
  46. return 0;
  47. }
  48. //Find last but one occurence of '\n' using reverse iteration
  49. res = std::find_if(res+1, msg.body.rend(),
  50. [](const uint8_t i) { return i == '\n'; });
  51. if (res == msg.body.rend()) {
  52. //No second occurence of '\n'
  53. //-> only one line of log, set iterator to first position
  54. res = msg.body.rend();
  55. }
  56. //Extreact the header and parse it to proper date format
  57. for (size_t i = 0; i < DATE_HEADER_SIZE; i++) {
  58. ss << *(--res);
  59. }
  60. std::tm tm = {};
  61. ss >> std::get_time(&tm, logDateFormat);
  62. long long tp = std::chrono::system_clock::from_time_t(std::mktime(&tm)).time_since_epoch().count();
  63. return tp;
  64. }
  65. void writeToFile(net::Message<net::MessageTypes>& msg) {
  66. std::ofstream file;
  67. std::stringstream fileName;
  68. switch(msg.header.id){
  69. case net::MessageTypes::ServerData:
  70. fileName << "backup_data_" << session_id << ".log";
  71. break;
  72. case net::MessageTypes::ServerLog:
  73. fileName << "backup_log_" << session_id << ".log";
  74. break;
  75. default: break;
  76. }
  77. file.open(fileName.str(), std::ios_base::app);
  78. for (auto c : msg.body)
  79. file << c;
  80. file.close();
  81. }
  82. int main()
  83. {
  84. NetClient c;
  85. long long lastLog = 0;
  86. LOG(INFO) << "press key to connect";
  87. std::cin.get();
  88. c.Connect("127.0.0.1", 7777);
  89. std::thread keyboardInputThread(readInput);
  90. bool connectionCancelled = false;
  91. while (!connectionCancelled)
  92. {
  93. if (c.IsConnected())
  94. {
  95. if (!c.Incoming().empty())
  96. {
  97. auto msg = c.Incoming().pop_front().msg;
  98. net::Message<net::MessageTypes> messageSend;
  99. std::string buf = "";
  100. switch (msg.header.id)
  101. {
  102. case net::MessageTypes::ServerAlert:
  103. // Server has responded to a ping request
  104. LOG(INFO) << "Server Alert notification";
  105. for (size_t index = 0; index < msg.header.size; index++) {
  106. buf += msg.body.at(index);
  107. }
  108. LOG(INFO) << buf;
  109. break;
  110. case net::MessageTypes::ServerAccept:
  111. // Server has responded to a ping request
  112. LOG(INFO) << "Server Accepted Connection";
  113. break;
  114. case net::MessageTypes::ServerDeny:
  115. // Server has responded to a ping request
  116. LOG(INFO) << "Server Denied Connection";
  117. connectionCancelled = true;
  118. break;
  119. case net::MessageTypes::ServerLog:
  120. // Server has send log data
  121. LOG(INFO) << "Received Server Log File";
  122. LOG(INFO) << "Received " << msg.header.size << " Bytes";
  123. writeToFile(msg);
  124. lastLog = parseDate(msg);
  125. break;
  126. case net::MessageTypes::ServerData:
  127. LOG(INFO) << "Received Server Data";
  128. LOG(INFO) << "Received " << msg.header.size << " Bytes";
  129. writeToFile(msg);
  130. break;
  131. default: break;
  132. }
  133. }
  134. if (keyboardInput != '0') {
  135. net::Message<net::MessageTypes> messageSend;
  136. switch (keyboardInput)
  137. {
  138. //Log
  139. case 'l':
  140. LOG(INFO) << "Request server log files";
  141. messageSend.header.id = net::MessageTypes::ServerLog;
  142. messageSend << lastLog;
  143. c.Send(messageSend);
  144. break;
  145. case 'd':
  146. LOG(INFO) << "Request permanent server data";
  147. messageSend.header.id = net::MessageTypes::ServerData;
  148. c.Send(messageSend);
  149. break;
  150. case 't':
  151. LOG(INFO) << "Request temporary server data";
  152. messageSend.header.id = net::MessageTypes::ServerData;
  153. c.Send(messageSend);
  154. break;
  155. case 'c':
  156. LOG(INFO) << "Request closing connection";
  157. messageSend.header.id = net::MessageTypes::ServerCloseConnection;
  158. c.Send(messageSend);
  159. break;
  160. default:
  161. break;
  162. }
  163. keyboardInput = '0';
  164. }
  165. }
  166. else
  167. {
  168. LOG(INFO) << "Server Down";
  169. break;
  170. }
  171. }
  172. LOG(INFO) << "End excution";
  173. return 0;
  174. }