58 lines
1.4 KiB
C++
58 lines
1.4 KiB
C++
#include "dispatcherserver.h"
|
|
|
|
|
|
DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::TCP_Server) {
|
|
eth.setParameter(PIEthernet::ReuseAddress);
|
|
CONNECTU(ð, 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();
|
|
piCoutObj << "server stoped";
|
|
}
|
|
|
|
|
|
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);
|
|
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();
|
|
}
|