|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #include "NetServer.h"
- #include <easylogging++.h>
- #include "DataModel.h"
-
- void NetServer::Stop()
- {
- net::ServerInterface<net::MessageTypes>::Stop();
- LOG(INFO) << "[SERVER] Stopped Server";
- serverRunning = false;
- }
-
- bool NetServer::Start()
- {
- if(serverRunning){
- LOG(WARNING) << "[SERVER] tried to start ip tcp server, but it is already running";
- return true;
- }
-
- bool res = net::ServerInterface<net::MessageTypes>::Start();
- if(res){
- LOG(INFO) << "[SERVER] Started Server";
- serverRunning = true;
- return res;
- }
- LOG(ERROR) << "[SERVER] Couldn't start Server";
- serverRunning = false;
- return res;
- }
-
- bool NetServer::OnClientConnect(std::shared_ptr<net::Connection<net::MessageTypes>> client)
- {
- //Decide whether to accept or deny connection (always accept in this case)
- net::Message<net::MessageTypes> msg;
- msg.header.id = net::MessageTypes::ServerAccept;
- client->Send(msg);
- return true;
- }
-
-
- // Called when a client appears to have disconnected
- void NetServer::OnClientDisconnect(std::shared_ptr<net::Connection<net::MessageTypes>> client)
- {
- LOG(INFO) << "Removing client [" << client->GetID() << "]";
- }
-
-
- // Called when a message arrives
- void NetServer::OnMessage(std::shared_ptr<net::Connection<net::MessageTypes>> client, net::Message<net::MessageTypes>& msgRcv)
- {
- net::Message<net::MessageTypes> msgSend;
- std::stringstream body;
-
- switch (msgRcv.header.id)
- {
- case net::MessageTypes::ServerData:
- LOG(INFO) << "[Client " << client->GetID() << "]: Request Server Data";
- msgSend.header.id = net::MessageTypes::ServerData;
- getBodyData(body);
- msgSend << body.str();
- client->Send(msgSend);
- break;
- case net::MessageTypes::ServerCondition:
- LOG(INFO) << "[Client " << client->GetID() << "]: Request temporary Condition Data";
- msgSend.header.id = net::MessageTypes::ServerCondition;
- getBodyCondition(body);
- msgSend << body.str();
- client->Send(msgSend);
- break;
- case net::MessageTypes::ServerAlert:
- LOG(INFO) << "[Client " << client->GetID() << "]: Request Alert Data";
- msgSend.header.id = net::MessageTypes::ServerAlert;
- client->Send(msgSend);
- break;
- case net::MessageTypes::ServerLog:
- LOG(INFO) << "[Client " << client->GetID() << "]: Request Log Files";
- msgSend.header.id = net::MessageTypes::ServerLog;
- getBodyLog(body, msgRcv);
- msgSend << body.str();
- client->Send(msgSend);
- break;
- case net::MessageTypes::ServerCloseConnection:
- LOG(INFO) << "[Client " << client->GetID() << "]: Request disconnection";
- client->Disconnect();
- break;
-
- default:
- LOG(ERROR) << "[Client " << client->GetID() << "]: Invaild reuqest code";
- break;
- }
- }
-
- void NetServer::notifyAllConnections(net::Message<net::MessageTypes> &message)
- {
- LOG(INFO) << "Sent broadcast to ";
- unsigned int counter = 0;
- //Send message to all listed active connections
- for(auto &connection: m_deqConnections){
- connection->Send(message);
- counter++;
- }
- LOG(INFO) << "Sent broadcast to " << counter << " devices";
- }
-
-
- std::stringstream& NetServer::getBodyData(std::stringstream& buffer)
- {
- DataModel::Instance()->readAllDataFiles(buffer);
- DataModel::Instance()->readPermanentData(buffer, false);
- return buffer;
- }
-
- std::stringstream& NetServer::getBodyLog(std::stringstream& buffer, net::Message<net::MessageTypes> msgRcv)
- {
- unsigned long long fromTime;
- if (msgRcv.header.size == 0)
- fromTime = 0;
- else
- msgRcv >> fromTime;
- DataModel::Instance()->readLogFile(buffer, fromTime);
- return buffer;
- }
-
- std::stringstream& NetServer::getBodyCondition(std::stringstream& buffer)
- {
- DataModel::Instance()->readTemporaryData(buffer);
- return buffer;
- }
-
|