code format
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
#include "dispatcherserver.h"
|
||||
|
||||
#include "piscreentiles.h"
|
||||
|
||||
|
||||
DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::TCP_Server) {
|
||||
client_gid = 0;
|
||||
DispatcherServer::DispatcherServer(PIEthernet::Address addr): eth(PIEthernet::TCP_Server) {
|
||||
client_gid = 0;
|
||||
max_connections = 1000;
|
||||
eth.setParameter(PIEthernet::ReuseAddress);
|
||||
eth.setReadAddress(addr);
|
||||
// eth.setDebug(false);
|
||||
// eth.setDebug(false);
|
||||
CONNECTU(ð, newConnection, this, newConnection);
|
||||
CONNECTU(&timeout_timer, tickEvent, this, cleanClients);
|
||||
}
|
||||
@@ -15,7 +16,7 @@ DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::T
|
||||
|
||||
DispatcherServer::~DispatcherServer() {
|
||||
eth.close();
|
||||
//piCoutObj << "server stoped";
|
||||
// piCoutObj << "server stoped";
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +36,7 @@ void DispatcherServer::picoutStatus() {
|
||||
}
|
||||
piCout << "Servers:";
|
||||
auto it = c_servers.makeIterator();
|
||||
while(it.next()){
|
||||
while (it.next()) {
|
||||
piCout << " " << it.key();
|
||||
it.value()->printStatus();
|
||||
}
|
||||
@@ -52,14 +53,15 @@ void DispatcherServer::cleanClients() {
|
||||
for (auto c: clients) {
|
||||
if (!index_c_servers.contains(c) && !index_c_clients.contains(c)) {
|
||||
if (!rm_clients.contains(c)) rm_clients << c;
|
||||
} else rm_clients.removeAll(c);
|
||||
} else
|
||||
rm_clients.removeAll(c);
|
||||
}
|
||||
auto ss = c_servers.values();
|
||||
for (auto c: ss) {
|
||||
if (c->lastPing() > 15.0) {
|
||||
piCout << "remove Server by ping timeout" << c->getConnection()->clientId();
|
||||
PIVector<DispatcherClient *> cscv = c->getClients();
|
||||
for(auto csc : cscv) {
|
||||
for (auto csc: cscv) {
|
||||
clients.removeAll(csc);
|
||||
index_c_clients.remove(csc);
|
||||
c->removeClient(csc);
|
||||
@@ -79,7 +81,7 @@ void DispatcherServer::cleanClients() {
|
||||
}
|
||||
for (auto c: rmrf_clients) {
|
||||
clients.removeAll(c);
|
||||
if(index_c_servers.contains(c)) {
|
||||
if (index_c_servers.contains(c)) {
|
||||
c_servers.remove(c_servers.key(index_c_servers[c]));
|
||||
index_c_servers.remove(c);
|
||||
}
|
||||
@@ -96,18 +98,17 @@ void DispatcherServer::updateConnectionsTile(TileList * tl) {
|
||||
for (auto c: clients) {
|
||||
PIString role = "Invalid";
|
||||
switch (c->role()) {
|
||||
case PICloud::TCP::Client : {
|
||||
role = "Client";
|
||||
case PICloud::TCP::Client: {
|
||||
role = "Client";
|
||||
CloudServer * cs = index_c_clients.value(c, nullptr);
|
||||
if (cs) role += " \"" + cs->serverUUID().toHex().left(8) + "...\"";
|
||||
} break;
|
||||
case PICloud::TCP::Server : {
|
||||
role = "Server";
|
||||
case PICloud::TCP::Server: {
|
||||
role = "Server";
|
||||
CloudServer * cs = index_c_servers.value(c, nullptr);
|
||||
if (cs) role += " \"" + cs->serverUUID().toHex().left(8) + "...\"";
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
tl->content << TileList::Row(c->address() + " " + role, PIScreenTypes::CellFormat());
|
||||
}
|
||||
@@ -126,7 +127,9 @@ void DispatcherServer::updateServersTile(TileList * tl, PISet<const DispatcherCl
|
||||
tl->content.clear();
|
||||
auto mi = c_servers.makeIterator();
|
||||
while (mi.next()) {
|
||||
tl->content << TileList::Row(mi.value()->serverUUID().toHex().left(8) + "... - " + PIString::fromNumber(mi.value()->getClients().size()), PIScreenTypes::CellFormat());
|
||||
tl->content << TileList::Row(mi.value()->serverUUID().toHex().left(8) + "... - " +
|
||||
PIString::fromNumber(mi.value()->getClients().size()),
|
||||
PIScreenTypes::CellFormat());
|
||||
if (servers.contains(mi.value()->getConnection())) tl->selected << (tl->content.size_s() - 1);
|
||||
}
|
||||
map_mutex.unlock();
|
||||
@@ -138,7 +141,7 @@ void DispatcherServer::updateClientsTile(TileList * tl, PISet<const DispatcherCl
|
||||
tl->content.clear();
|
||||
auto mi = c_servers.makeIterator();
|
||||
while (mi.next()) {
|
||||
for (auto c : mi.value()->getClients()) {
|
||||
for (auto c: mi.value()->getClients()) {
|
||||
if (servers.isEmpty() || servers.contains(mi.value()->getConnection()))
|
||||
tl->content << TileList::Row(c->address(), PIScreenTypes::CellFormat());
|
||||
}
|
||||
@@ -150,7 +153,7 @@ void DispatcherServer::updateClientsTile(TileList * tl, PISet<const DispatcherCl
|
||||
const DispatcherClient * DispatcherServer::getConnection(int index) {
|
||||
const DispatcherClient * ret = nullptr;
|
||||
map_mutex.lock();
|
||||
if (index >=0 && index < clients.size_s()) ret = clients[index];
|
||||
if (index >= 0 && index < clients.size_s()) ret = clients[index];
|
||||
map_mutex.unlock();
|
||||
return ret;
|
||||
}
|
||||
@@ -159,7 +162,7 @@ const DispatcherClient * DispatcherServer::getConnection(int index) {
|
||||
const DispatcherClient * DispatcherServer::getServer(int index) {
|
||||
const DispatcherClient * ret = nullptr;
|
||||
map_mutex.lock();
|
||||
if (index >=0 && index < clients.size_s()) {
|
||||
if (index >= 0 && index < clients.size_s()) {
|
||||
if (index_c_servers.contains(clients[index])) ret = clients[index];
|
||||
}
|
||||
map_mutex.unlock();
|
||||
@@ -171,7 +174,7 @@ PISet<const DispatcherClient *> DispatcherServer::getServers(PISet<int> ids) {
|
||||
PISet<const DispatcherClient *> ret;
|
||||
if (ids.isEmpty()) return ret;
|
||||
map_mutex.lock();
|
||||
int i = 0;
|
||||
int i = 0;
|
||||
auto mi = c_servers.makeIterator();
|
||||
while (mi.next()) {
|
||||
if (ids.contains(i)) ret << mi.value()->getConnection();
|
||||
@@ -187,9 +190,9 @@ void DispatcherServer::setMaxConnections(uint max_count) {
|
||||
}
|
||||
|
||||
|
||||
void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
||||
void DispatcherServer::disconnectClient(DispatcherClient * client) {
|
||||
if (!clients.contains(client)) {
|
||||
//piCoutObj << "INVALID client" << client;
|
||||
// piCoutObj << "INVALID client" << client;
|
||||
return;
|
||||
}
|
||||
piCoutObj << "remove ..." << client->clientId();
|
||||
@@ -200,7 +203,7 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
||||
if (cs) {
|
||||
piCoutObj << "remove Server" << client->clientId();
|
||||
PIVector<DispatcherClient *> cscv = cs->getClients();
|
||||
for(auto csc : cscv) {
|
||||
for (auto csc: cscv) {
|
||||
clients.removeAll(csc);
|
||||
index_c_clients.remove(csc);
|
||||
cs->removeClient(csc);
|
||||
@@ -217,21 +220,21 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
||||
cc->removeClient(client);
|
||||
index_c_clients.remove(client);
|
||||
}
|
||||
//client->close();
|
||||
// client->close();
|
||||
rmrf_clients << client;
|
||||
map_mutex.unlock();
|
||||
piCoutObj << "remove done" << client->clientId();
|
||||
}
|
||||
|
||||
|
||||
void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||
void DispatcherServer::newConnection(PIEthernet * cl) {
|
||||
if (clients.size() >= max_connections) {
|
||||
delete cl;
|
||||
return;
|
||||
}
|
||||
DispatcherClient * client = new DispatcherClient(cl, client_gid++);
|
||||
CONNECTU(client, disconnectEvent, this, disconnectClient);
|
||||
CONNECTL(client, registerServer, [this](const PIByteArray & sname, DispatcherClient * c){
|
||||
CONNECTL(client, registerServer, [this](const PIByteArray & sname, DispatcherClient * c) {
|
||||
map_mutex.lock();
|
||||
CloudServer * cs = c_servers.value(sname, nullptr);
|
||||
if (cs) {
|
||||
@@ -246,7 +249,7 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||
}
|
||||
map_mutex.unlock();
|
||||
});
|
||||
CONNECTL(client, registerClient, [this](const PIByteArray & sname, DispatcherClient * c){
|
||||
CONNECTL(client, registerClient, [this](const PIByteArray & sname, DispatcherClient * c) {
|
||||
map_mutex.lock();
|
||||
CloudServer * cs = c_servers.value(sname, nullptr);
|
||||
if (cs) {
|
||||
@@ -260,7 +263,7 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||
}
|
||||
map_mutex.unlock();
|
||||
});
|
||||
//piCoutObj << "add client" << client;
|
||||
// piCoutObj << "add client" << client;
|
||||
map_mutex.lock();
|
||||
clients.push_back(client);
|
||||
map_mutex.unlock();
|
||||
|
||||
Reference in New Issue
Block a user