picloud
This commit is contained in:
@@ -1,5 +1,24 @@
|
||||
#include "cloudserver.h"
|
||||
|
||||
CloudServer::CloudServer(DispatcherClient * client) {
|
||||
CloudServer::CloudServer(DispatcherClient * c) : server(c) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
CloudServer::~CloudServer() {
|
||||
for (auto c :clients) {
|
||||
c->close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CloudServer::addClient(DispatcherClient * c) {
|
||||
clients << c;
|
||||
}
|
||||
|
||||
void CloudServer::printStatus() {
|
||||
piCout << " " << "Clients for" << server->address() << ":";
|
||||
for (auto c: clients) {
|
||||
piCout << " " << c->address();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,18 @@
|
||||
|
||||
#include "dispatcherclient.h"
|
||||
|
||||
class CloudServer
|
||||
{
|
||||
|
||||
class CloudServer : public PIObject {
|
||||
PIOBJECT(CloudServer)
|
||||
public:
|
||||
CloudServer(DispatcherClient * client);
|
||||
CloudServer(DispatcherClient * c);
|
||||
~CloudServer();
|
||||
void addClient(DispatcherClient * c);
|
||||
EVENT_HANDLER0(void, printStatus);
|
||||
|
||||
private:
|
||||
DispatcherClient * server;
|
||||
PIVector<DispatcherClient*> clients;
|
||||
};
|
||||
|
||||
#endif // CLOUDSERVER_H
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "dispatcherclient.h"
|
||||
#include "picloudtcp.h"
|
||||
|
||||
|
||||
DispatcherClient::DispatcherClient(PIEthernet * eth_) {
|
||||
eth = eth_;
|
||||
DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(false) {
|
||||
CONNECTU(&disconnect_tm, tickEvent, eth, close);
|
||||
eth->startThreadedRead();
|
||||
CONNECTU(eth, threadedReadEvent, this, readed);
|
||||
@@ -21,6 +21,10 @@ PIString DispatcherClient::address() {
|
||||
return eth->path();
|
||||
}
|
||||
|
||||
void DispatcherClient::close() {
|
||||
eth->close();
|
||||
}
|
||||
|
||||
|
||||
void DispatcherClient::disconnected(bool withError) {
|
||||
piCoutObj << "client disconnected" << eth->sendAddress();
|
||||
@@ -30,7 +34,18 @@ void DispatcherClient::disconnected(bool withError) {
|
||||
|
||||
void DispatcherClient::readed(uchar *data, int size) {
|
||||
PIByteArray ba(data, size);
|
||||
piCoutObj << "readed" << ba.toHex();
|
||||
eth->write(ba);
|
||||
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);
|
||||
}
|
||||
if ((PICloud::HeaderType)hdr.type == PICloud::Client) {
|
||||
registerClient(hdr.sname, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,10 @@ public:
|
||||
~DispatcherClient();
|
||||
EVENT1(disconnectEvent, DispatcherClient *, client)
|
||||
EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
|
||||
EVENT2(registerClient, PIString, sname, DispatcherClient *, client)
|
||||
PIString address();
|
||||
void close();
|
||||
EVENT1(dataReaded, PIByteArray, ba)
|
||||
|
||||
private:
|
||||
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
||||
@@ -19,6 +22,7 @@ private:
|
||||
|
||||
PIEthernet * eth;
|
||||
PITimer disconnect_tm;
|
||||
bool authorised;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ void DispatcherServer::printStatus() {
|
||||
auto it = c_servers.makeIterator();
|
||||
while(it.next()){
|
||||
piCout << " " << it.key();
|
||||
it.value()->printStatus();
|
||||
}
|
||||
map_mutex.unlock();
|
||||
}
|
||||
@@ -50,6 +51,13 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||
c_servers.insert(sname, new CloudServer(c));
|
||||
map_mutex.unlock();
|
||||
});
|
||||
CONNECTL(client, registerClient, [this](PIString sname, DispatcherClient * c){
|
||||
map_mutex.lock();
|
||||
if (c_servers.contains(sname)) {
|
||||
c_servers[sname]->addClient(c);
|
||||
}
|
||||
map_mutex.unlock();
|
||||
});
|
||||
piCoutObj << "add client" << client;
|
||||
map_mutex.lock();
|
||||
clients.push_back(client);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef DISPATCHERSERVER_H
|
||||
#define DISPATCHERSERVER_H
|
||||
|
||||
#include "dispatcherclient.h"
|
||||
#include "cloudserver.h"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user