71 lines
1.6 KiB
C++
71 lines
1.6 KiB
C++
#include "cloudserver.h"
|
|
|
|
CloudServer::CloudServer(DispatcherClient * c, const PIByteArray & sname) : server(c) {
|
|
setName(sname.toHex());
|
|
server_uuid = sname;
|
|
CONNECTL(c, dataReadedServer, ([this](uint id, PIByteArray & ba){
|
|
DispatcherClient * cl = index_clients.value(id, nullptr);
|
|
if (cl) cl->sendData(ba);
|
|
}));
|
|
CONNECTL(c, pingReceived, [this]() {last_ping.reset();});
|
|
last_ping.reset();
|
|
}
|
|
|
|
|
|
CloudServer::~CloudServer() {
|
|
for (auto c :clients) {
|
|
c->close();
|
|
}
|
|
}
|
|
|
|
|
|
PIByteArray CloudServer::serverUUID() const {
|
|
return server_uuid;
|
|
}
|
|
|
|
|
|
void CloudServer::addClient(DispatcherClient * c) {
|
|
last_ping.reset();
|
|
clients << c;
|
|
uint cid = c->clientId();
|
|
index_clients.insert(cid, c);
|
|
c->sendConnected(1);
|
|
server->sendConnected(cid);
|
|
CONNECTL(c, dataReaded, ([this, cid](PIByteArray & ba){
|
|
// piCoutObj << c->clientId() << "dataReaded";
|
|
server->sendDataToClient(ba, cid);
|
|
}));
|
|
}
|
|
|
|
|
|
void CloudServer::removeClient(DispatcherClient * c) {
|
|
last_ping.reset();
|
|
clients.removeOne(c);
|
|
index_clients.removeOne(c->clientId());
|
|
server->sendDisconnected(c->clientId());
|
|
}
|
|
|
|
|
|
PIVector<DispatcherClient *> CloudServer::getClients() {
|
|
return clients;
|
|
}
|
|
|
|
|
|
double CloudServer::lastPing() {
|
|
return last_ping.elapsed_s();
|
|
}
|
|
|
|
|
|
void CloudServer::printStatus() {
|
|
piCout << " " << "Clients for" << server->address() << server_uuid.toHex() << ":";
|
|
for (auto c: clients) {
|
|
piCout << " " << c->address() << c->clientId();
|
|
}
|
|
// for (auto c: clients) {
|
|
// c->sendData(PIByteArray::fromHex("000000"));
|
|
// server->sendDataToClient(PIByteArray::fromHex("000000"), c->clientId());
|
|
// }
|
|
}
|
|
|
|
|