PICloud Dispatcher screen mode

This commit is contained in:
2021-04-09 16:44:05 +03:00
parent f0e2410929
commit f75ce1e8e0
8 changed files with 194 additions and 29 deletions

View File

@@ -1,16 +1,13 @@
#include "dispatcherserver.h"
#include "piscreentiles.h"
DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::TCP_Server) {
client_gid = 0;
eth.setParameter(PIEthernet::ReuseAddress);
eth.setReadAddress(addr);
CONNECTU(&eth, newConnection, this, newConnection);
eth.listen(addr, true);
piCoutObj << "server started" << addr;
CONNECTU(&status_timer, tickEvent, this, printStatus);
CONNECTU(&timeout_timer, tickEvent, this, cleanClients);
status_timer.start(1000);
timeout_timer.start(5000);
}
@@ -20,6 +17,30 @@ DispatcherServer::~DispatcherServer() {
}
void DispatcherServer::start() {
eth.listen(true);
timeout_timer.start(5000);
piCoutObj << "server started" << eth.readAddress();
}
void DispatcherServer::picoutStatus() {
map_mutex.lock();
piCout << PICoutManipulators::NewLine;
piCout << "Connections:";
for (auto c: clients) {
piCout << " " << c->address();
}
piCout << "Servers:";
auto it = c_servers.makeIterator();
while(it.next()){
piCout << " " << it.key();
it.value()->printStatus();
}
map_mutex.unlock();
}
void DispatcherServer::cleanClients() {
PIVector<DispatcherClient*> rm;
map_mutex.lock();
@@ -40,23 +61,94 @@ void DispatcherServer::cleanClients() {
}
void DispatcherServer::printStatus() {
void DispatcherServer::updateConnectionsTile(TileList * tl) {
map_mutex.lock();
piCout << PICoutManipulators::NewLine;
piCout << "Connections:";
tl->content.clear();
for (auto c: clients) {
piCout << " " << c->address();
}
piCout << "Servers:";
auto it = c_servers.makeIterator();
while(it.next()){
piCout << " " << it.key();
it.value()->printStatus();
PIString role = "Invalid";
switch (c->role()) {
case PICloud::TCP::Client : {
role = "Client";
CloudServer * cs = index_c_clients.value(c, nullptr);
if (cs) role += " \"" + cs->serverName() + "\"";
} break;
case PICloud::TCP::Server : {
role = "Server";
CloudServer * cs = index_c_servers.value(c, nullptr);
if (cs) role += " \"" + cs->serverName() + "\"";
} break;
default:
break;
}
tl->content << TileList::Row(c->address() + " " + role, PIScreenTypes::CellFormat());
}
map_mutex.unlock();
}
void DispatcherServer::updateServersTile(TileList * tl, PISet<const DispatcherClient *> servers) {
map_mutex.lock();
tl->content.clear();
auto mi = c_servers.makeIterator();
while (mi.next()) {
tl->content << TileList::Row(mi.value()->serverName() + " - " + PIString::fromNumber(mi.value()->getClients().size()), PIScreenTypes::CellFormat());
if (servers.contains(mi.value()->getConnection())) tl->selected << (tl->content.size_s() - 1);
}
map_mutex.unlock();
}
void DispatcherServer::updateClientsTile(TileList * tl, PISet<const DispatcherClient *> servers) {
map_mutex.lock();
tl->content.clear();
auto mi = c_servers.makeIterator();
while (mi.next()) {
for (auto c : mi.value()->getClients()) {
if (servers.isEmpty() || servers.contains(mi.value()->getConnection()))
tl->content << TileList::Row(c->address(), PIScreenTypes::CellFormat());
}
}
map_mutex.unlock();
}
const DispatcherClient * DispatcherServer::getConnection(int index) {
const DispatcherClient * ret = nullptr;
map_mutex.lock();
if (index >=0 && index < clients.size_s()) ret = clients[index];
map_mutex.unlock();
return ret;
}
const DispatcherClient * DispatcherServer::getServer(int index) {
const DispatcherClient * ret = nullptr;
const DispatcherClient * tmp;
map_mutex.lock();
if (index >=0 && index < clients.size_s()) {
tmp = clients[index];
}
if (index_c_servers.contains(tmp)) ret = tmp;
map_mutex.unlock();
return ret;
}
PISet<const DispatcherClient *> DispatcherServer::getServers(PISet<int> ids) {
PISet<const DispatcherClient *> ret;
if (ids.isEmpty()) return ret;
map_mutex.lock();
int i = 0;
auto mi = c_servers.makeIterator();
while (mi.next()) {
if (ids.contains(i)) ret << mi.value()->getConnection();
i++;
}
map_mutex.unlock();
return ret;
}
void DispatcherServer::disconnectClient(DispatcherClient *client) {
if (!clients.contains(client)) {
//piCoutObj << "INVALID client" << client;