picloud next iteration

This commit is contained in:
2021-04-05 17:42:02 +03:00
parent 8eff5d24c9
commit f0d4801d3c
13 changed files with 237 additions and 90 deletions

View File

@@ -1,7 +1,8 @@
#include "cloudserver.h"
CloudServer::CloudServer(DispatcherClient * c) : server(c) {
CloudServer::CloudServer(DispatcherClient * c, const PIString & sname) : server(c) {
setName(sname);
server_name = sname;
}
@@ -12,13 +13,30 @@ CloudServer::~CloudServer() {
}
PIString CloudServer::serverName() const {
return server_name;
}
void CloudServer::addClient(DispatcherClient * c) {
clients << c;
}
void CloudServer::removeClient(DispatcherClient * c) {
clients.removeOne(c);
}
PIVector<DispatcherClient *> CloudServer::getClients() {
return clients;
}
void CloudServer::printStatus() {
piCout << " " << "Clients for" << server->address() << ":";
for (auto c: clients) {
piCout << " " << c->address();
}
}

View File

@@ -7,14 +7,18 @@
class CloudServer : public PIObject {
PIOBJECT(CloudServer)
public:
CloudServer(DispatcherClient * c);
CloudServer(DispatcherClient * c, const PIString & sname);
~CloudServer();
PIString serverName() const;
void addClient(DispatcherClient * c);
void removeClient(DispatcherClient * c);
PIVector<DispatcherClient*> getClients();
EVENT_HANDLER0(void, printStatus);
private:
DispatcherClient * server;
PIVector<DispatcherClient*> clients;
PIString server_name;
};
#endif // CLOUDSERVER_H

View File

@@ -4,10 +4,14 @@
DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(false) {
CONNECTU(&disconnect_tm, tickEvent, eth, close);
eth->startThreadedRead();
CONNECTU(eth, threadedReadEvent, this, readed);
CONNECTU(eth, disconnected, this, disconnected);
piCoutObj << "client connected" << eth->sendAddress();
}
void DispatcherClient::start() {
eth->startThreadedRead();
disconnect_tm.start(10000);
}
@@ -33,18 +37,40 @@ void DispatcherClient::disconnected(bool withError) {
void DispatcherClient::readed(uchar *data, int size) {
piCout << size;
PIByteArray ba(data, size);
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
if (hdr.first == PICloud::TCP::Invalid) {
disconnected(true);
return;
}
if (authorised) {
dataReaded(ba);
} else {
if (ba.size() < 4) return;
PICloud::Header hdr;
ba >> hdr;
if ((PICloud::HeaderType)hdr.type == PICloud::Server) {
registerServer(hdr.sname, this);
switch (hdr.first) {
case PICloud::TCP::Connect:
return;
case PICloud::TCP::Disconnect:
disconnected(false);
return;
case PICloud::TCP::Data:
dataReaded(tcp.parseData(ba));
return;
default:
disconnected(true);
return;
}
if ((PICloud::HeaderType)hdr.type == PICloud::Client) {
registerClient(hdr.sname, this);
} else {
switch (hdr.first) {
case PICloud::TCP::Connect: {
PIString sn = tcp.parseConnect(ba);
if (hdr.second == PICloud::TCP::Server) registerServer(sn, this);
if (hdr.second == PICloud::TCP::Client) registerClient(sn, this);
return;}
case PICloud::TCP::Disconnect:
disconnected(false);
return;
default:
disconnected(true);
return;
}
}
}

View File

@@ -2,12 +2,14 @@
#define DISPATCHERCLIENT_H
#include "piethernet.h"
#include "picloudtcp.h"
class DispatcherClient: public PIObject {
PIOBJECT(DispatcherClient)
public:
DispatcherClient(PIEthernet * eth_);
void start();
~DispatcherClient();
EVENT1(disconnectEvent, DispatcherClient *, client)
EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
@@ -23,6 +25,7 @@ private:
PIEthernet * eth;
PITimer disconnect_tm;
bool authorised;
PICloud::TCP tcp;
};

View File

@@ -35,9 +35,26 @@ void DispatcherServer::printStatus() {
void DispatcherServer::disconnectClient(DispatcherClient *client) {
if (!clients.contains(client)) {
piCoutObj << "INVALID client" << client;
return;
}
piCoutObj << "remove client" << client;
map_mutex.lock();
clients.removeOne(client);
CloudServer * cs = index_c_servers.value(client, nullptr);
if (cs) {
PIVector<DispatcherClient *> cscv = cs->getClients();
for(auto csc : cscv) {
csc->close();
clients.removeOne(csc);
delete csc;
}
c_servers.remove(cs->serverName());
}
CloudServer * cc = index_c_clients.value(client, nullptr);
if (cc) cc->removeClient(client);
client->close();
map_mutex.unlock();
delete client;
}
@@ -48,17 +65,24 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
CONNECTU(client, disconnectEvent, this, disconnectClient);
CONNECTL(client, registerServer, [this](PIString sname, DispatcherClient * c){
map_mutex.lock();
c_servers.insert(sname, new CloudServer(c));
piCoutObj << "add new Server ->" << sname;
CloudServer * cs = new CloudServer(c, sname);
c_servers.insert(sname, cs);
index_c_servers.insert(c, cs);
map_mutex.unlock();
});
CONNECTL(client, registerClient, [this](PIString sname, DispatcherClient * c){
map_mutex.lock();
if (c_servers.contains(sname)) {
c_servers[sname]->addClient(c);
CloudServer * cs = c_servers.value(sname, nullptr);
if (cs) {
piCoutObj << "add new Client to Server ->" << sname;
cs->addClient(c);
index_c_clients.insert(c, cs);
}
map_mutex.unlock();
});
piCoutObj << "add client" << client;
client->start();
map_mutex.lock();
clients.push_back(client);
map_mutex.unlock();

View File

@@ -18,6 +18,8 @@ private:
PIEthernet eth;
PIVector<DispatcherClient*> clients;
PIMap<PIString, CloudServer *> c_servers;
PIMap<const DispatcherClient *, CloudServer *> index_c_servers;
PIMap<const DispatcherClient *, CloudServer *> index_c_clients;
PITimer status_timer;
PIMutex map_mutex;
};