cloud test

This commit is contained in:
2020-08-26 17:28:56 +03:00
parent 3965e54e38
commit cfebf8cf23
11 changed files with 110 additions and 32 deletions

View File

@@ -1,32 +1,57 @@
#include "dispatcherserver.h"
DispatcherServer::DispatcherServer(PIEthernet::Address addr) {
eth = new PIEthernet(PIEthernet::TCP_Server);
eth->setParameter(PIEthernet::ReuseAddress);
CONNECTU(eth, newConnection, this, newConnection);
eth->listen(addr, true);
piCoutObj << eth << "server started" << addr;
DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::TCP_Server) {
eth.setParameter(PIEthernet::ReuseAddress);
CONNECTU(&eth, newConnection, this, newConnection);
eth.listen(addr, true);
piCoutObj << "server started" << addr;
CONNECTU(&status_timer, tickEvent, this, printStatus);
status_timer.start(1000);
}
DispatcherServer::~DispatcherServer() {
eth->close();
eth.close();
piCoutObj << "server stoped";
delete eth;
}
void DispatcherServer::printStatus() {
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();
}
map_mutex.unlock();
}
void DispatcherServer::disconnectClient(DispatcherClient *client) {
piCoutObj << "remove client" << client;
map_mutex.lock();
clients.removeOne(client);
map_mutex.unlock();
delete client;
}
void DispatcherServer::newConnection(PIEthernet *cl) {
DispatcherClient * client = new DispatcherClient(cl);
piCoutObj << "add client" << client;
CONNECTU(client, disconnectEvent, this, disconnectClient);
CONNECTL(client, registerServer, [this](PIString sname, DispatcherClient * c){
map_mutex.lock();
c_servers.insert(sname, new CloudServer(c));
map_mutex.unlock();
});
piCoutObj << "add client" << client;
map_mutex.lock();
clients.push_back(client);
map_mutex.unlock();
}