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.

NetServer.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "NetServer.h"
  2. #include <easylogging++.h>
  3. #include "DataModel.h"
  4. void NetServer::Stop()
  5. {
  6. net::ServerInterface<net::MessageTypes>::Stop();
  7. LOG(INFO) << "[SERVER] Stopped Server";
  8. serverRunning = false;
  9. }
  10. bool NetServer::Start()
  11. {
  12. if(serverRunning){
  13. LOG(WARNING) << "[SERVER] tried to start ip tcp server, but it is already running";
  14. return true;
  15. }
  16. bool res = net::ServerInterface<net::MessageTypes>::Start();
  17. if(res){
  18. LOG(INFO) << "[SERVER] Started Server";
  19. serverRunning = true;
  20. return res;
  21. }
  22. LOG(ERROR) << "[SERVER] Couldn't start Server";
  23. serverRunning = false;
  24. return res;
  25. }
  26. bool NetServer::OnClientConnect(std::shared_ptr<net::Connection<net::MessageTypes>> client)
  27. {
  28. //Decide whether to accept or deny connection (always accept in this case)
  29. net::Message<net::MessageTypes> msg;
  30. msg.header.id = net::MessageTypes::ServerAccept;
  31. client->Send(msg);
  32. return true;
  33. }
  34. // Called when a client appears to have disconnected
  35. void NetServer::OnClientDisconnect(std::shared_ptr<net::Connection<net::MessageTypes>> client)
  36. {
  37. LOG(INFO) << "Removing client [" << client->GetID() << "]";
  38. }
  39. // Called when a message arrives
  40. void NetServer::OnMessage(std::shared_ptr<net::Connection<net::MessageTypes>> client, net::Message<net::MessageTypes>& msgRcv)
  41. {
  42. net::Message<net::MessageTypes> msgSend;
  43. std::stringstream body;
  44. switch (msgRcv.header.id)
  45. {
  46. case net::MessageTypes::ServerData:
  47. LOG(INFO) << "[Client " << client->GetID() << "]: Request Server Data";
  48. msgSend.header.id = net::MessageTypes::ServerData;
  49. getBodyData(body);
  50. msgSend << body.str();
  51. client->Send(msgSend);
  52. break;
  53. case net::MessageTypes::ServerCondition:
  54. LOG(INFO) << "[Client " << client->GetID() << "]: Request temporary Condition Data";
  55. msgSend.header.id = net::MessageTypes::ServerCondition;
  56. getBodyCondition(body);
  57. msgSend << body.str();
  58. client->Send(msgSend);
  59. break;
  60. case net::MessageTypes::ServerAlert:
  61. LOG(INFO) << "[Client " << client->GetID() << "]: Request Alert Data";
  62. msgSend.header.id = net::MessageTypes::ServerAlert;
  63. client->Send(msgSend);
  64. break;
  65. case net::MessageTypes::ServerLog:
  66. LOG(INFO) << "[Client " << client->GetID() << "]: Request Log Files";
  67. msgSend.header.id = net::MessageTypes::ServerLog;
  68. getBodyLog(body, msgRcv);
  69. msgSend << body.str();
  70. client->Send(msgSend);
  71. break;
  72. case net::MessageTypes::ServerCloseConnection:
  73. LOG(INFO) << "[Client " << client->GetID() << "]: Request disconnection";
  74. client->Disconnect();
  75. break;
  76. default:
  77. LOG(ERROR) << "[Client " << client->GetID() << "]: Invaild reuqest code";
  78. break;
  79. }
  80. }
  81. void NetServer::notifyAllConnections(net::Message<net::MessageTypes> &message)
  82. {
  83. LOG(INFO) << "Sent broadcast to ";
  84. unsigned int counter = 0;
  85. //Send message to all listed active connections
  86. for(auto &connection: m_deqConnections){
  87. connection->Send(message);
  88. counter++;
  89. }
  90. LOG(INFO) << "Sent broadcast to " << counter << " devices";
  91. }
  92. std::stringstream& NetServer::getBodyData(std::stringstream& buffer)
  93. {
  94. DataModel::Instance()->readAllDataFiles(buffer);
  95. DataModel::Instance()->readPermanentData(buffer, false);
  96. return buffer;
  97. }
  98. std::stringstream& NetServer::getBodyLog(std::stringstream& buffer, net::Message<net::MessageTypes> msgRcv)
  99. {
  100. unsigned long long fromTime;
  101. if (msgRcv.header.size == 0)
  102. fromTime = 0;
  103. else
  104. msgRcv >> fromTime;
  105. DataModel::Instance()->readLogFile(buffer, fromTime);
  106. return buffer;
  107. }
  108. std::stringstream& NetServer::getBodyCondition(std::stringstream& buffer)
  109. {
  110. DataModel::Instance()->readTemporaryData(buffer);
  111. return buffer;
  112. }