picloud next iteration
This commit is contained in:
@@ -18,9 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "picloudclient.h"
|
#include "picloudclient.h"
|
||||||
|
#include "picloudtcp.h"
|
||||||
|
|
||||||
|
|
||||||
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), eth(PIEthernet::TCP_Client) {
|
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), eth(PIEthernet::TCP_Client) {
|
||||||
|
tcp.setRole(PICloud::TCP::Client);
|
||||||
|
setName("cloud_client");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -30,15 +33,32 @@ PICloudClient::~PICloudClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloudClient::setServerName(const PIString & server_name) {
|
||||||
|
setName("cloud_client__" + server_name);
|
||||||
|
tcp.setServerName(server_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PICloudClient::openDevice() {
|
bool PICloudClient::openDevice() {
|
||||||
|
piCout << "PICloudClient open device" << path();
|
||||||
|
bool op = eth.connect(path(), false);
|
||||||
|
if (op) {
|
||||||
|
CONNECTL(ð, disconnected, [this](bool){opened_ = false;});
|
||||||
|
CONNECTU(ð, threadedReadEvent, this, readed);
|
||||||
|
eth.startThreadedRead();
|
||||||
|
tcp.sendStart(ð);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
eth.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PICloudClient::closeDevice() {
|
bool PICloudClient::closeDevice() {
|
||||||
return false;
|
return eth.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int PICloudClient::readDevice(void * read_to, int max_size) {
|
int PICloudClient::readDevice(void * read_to, int max_size) {
|
||||||
return eth.read(read_to, max_size);
|
return eth.read(read_to, max_size);
|
||||||
}
|
}
|
||||||
@@ -47,3 +67,8 @@ int PICloudClient::readDevice(void * read_to, int max_size) {
|
|||||||
int PICloudClient::writeDevice(const void * data, int max_size) {
|
int PICloudClient::writeDevice(const void * data, int max_size) {
|
||||||
return eth.write(data, max_size);
|
return eth.write(data, max_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloudClient::readed(uchar *data, int size) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,10 @@
|
|||||||
|
|
||||||
|
|
||||||
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), eth(PIEthernet::TCP_Client) {
|
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), eth(PIEthernet::TCP_Client) {
|
||||||
|
PIString name = "cloud_server_" + PIString::fromNumber(randomi()%1000);
|
||||||
|
tcp.setRole(PICloud::TCP::Server);
|
||||||
|
tcp.setServerName(name);
|
||||||
|
setName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -36,8 +40,9 @@ bool PICloudServer::openDevice() {
|
|||||||
if (op) {
|
if (op) {
|
||||||
CONNECTL(ð, disconnected, [this](bool){opened_ = false;});
|
CONNECTL(ð, disconnected, [this](bool){opened_ = false;});
|
||||||
CONNECTU(ð, threadedReadEvent, this, readed);
|
CONNECTU(ð, threadedReadEvent, this, readed);
|
||||||
|
CONNECTL(ð, connected, [this](){tcp.sendStart(ð);});
|
||||||
eth.startThreadedRead();
|
eth.startThreadedRead();
|
||||||
sendStart();
|
tcp.sendStart(ð);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
eth.close();
|
eth.close();
|
||||||
@@ -60,12 +65,6 @@ int PICloudServer::writeDevice(const void * data, int max_size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PICloudServer::sendStart() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PICloudServer::Client::Client(PICloudServer * srv) : server(srv) {
|
PICloudServer::Client::Client(PICloudServer * srv) : server(srv) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,23 +20,70 @@
|
|||||||
#include "picloudtcp.h"
|
#include "picloudtcp.h"
|
||||||
#include "picrypt.h"
|
#include "picrypt.h"
|
||||||
#include "pichunkstream.h"
|
#include "pichunkstream.h"
|
||||||
|
#include "piethernet.h"
|
||||||
|
|
||||||
|
|
||||||
const char hash_def_key[] = "_picrypt_";
|
const char hash_def_key[] = "_picrypt_";
|
||||||
|
|
||||||
|
|
||||||
PIByteArray & operator <<(PIByteArray & s, const PICloud::Header & v) {
|
PICloud::TCP::Header::Header() {
|
||||||
s << v.version << v.type << v.sname;
|
version = Version_1;
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
PIByteArray & operator >>(PIByteArray & s, PICloud::Header & v) {
|
|
||||||
s >> v.version >> v.type >> v.sname;
|
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PICloud::TCP::TCP() {
|
PICloud::TCP::TCP() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloud::TCP::setRole(PICloud::TCP::Role r) {
|
||||||
|
header.role = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloud::TCP::setServerName(const PIString & server_name) {
|
||||||
|
sname = server_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloud::TCP::sendStart(PIEthernet * eth) {
|
||||||
|
header.type = PICloud::TCP::Connect;
|
||||||
|
PIByteArray ba;
|
||||||
|
ba << header << sname;
|
||||||
|
eth->send(ba);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
|
||||||
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
|
||||||
|
ret.first = Invalid;
|
||||||
|
if (ba.size() < sizeof(Header)) return ret;
|
||||||
|
PICloud::TCP::Header hdr;
|
||||||
|
ba >> hdr;
|
||||||
|
if (hdr.version != header.version) {
|
||||||
|
piCout << "[PICloud]" << "invalid TCP version!";
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
ret.first = (Type)hdr.type;
|
||||||
|
ret.second = (Role)hdr.role;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIByteArray PICloud::TCP::parseData(PIByteArray & ba) {
|
||||||
|
if (header.role == Server) {
|
||||||
|
PIString client;
|
||||||
|
ba >> client;
|
||||||
|
return ba;
|
||||||
|
}
|
||||||
|
if (header.role == Client) {
|
||||||
|
return ba;
|
||||||
|
}
|
||||||
|
return PIByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIString PICloud::TCP::parseConnect(PIByteArray & ba) {
|
||||||
|
PIString ret;
|
||||||
|
ba >> ret;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
#ifndef PICLOUDCLIENT_H
|
#ifndef PICLOUDCLIENT_H
|
||||||
#define PICLOUDCLIENT_H
|
#define PICLOUDCLIENT_H
|
||||||
|
|
||||||
#include "pip_cloud_export.h"
|
#include "picloudtcp.h"
|
||||||
#include "piethernet.h"
|
#include "piethernet.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -35,15 +35,19 @@ public:
|
|||||||
explicit PICloudClient(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
|
explicit PICloudClient(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
|
||||||
virtual ~PICloudClient();
|
virtual ~PICloudClient();
|
||||||
|
|
||||||
|
void setServerName(const PIString & server_name);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool openDevice();
|
bool openDevice();
|
||||||
bool closeDevice();
|
bool closeDevice();
|
||||||
|
|
||||||
private:
|
|
||||||
int readDevice(void * read_to, int max_size);
|
int readDevice(void * read_to, int max_size);
|
||||||
int writeDevice(const void * data, int max_size);
|
int writeDevice(const void * data, int max_size);
|
||||||
|
|
||||||
|
private:
|
||||||
|
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
||||||
|
|
||||||
PIEthernet eth;
|
PIEthernet eth;
|
||||||
|
PICloud::TCP tcp;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PICLOUDCLIENT_H
|
#endif // PICLOUDCLIENT_H
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
#ifndef PICLOUDSERVER_H
|
#ifndef PICLOUDSERVER_H
|
||||||
#define PICLOUDSERVER_H
|
#define PICLOUDSERVER_H
|
||||||
|
|
||||||
#include "pip_cloud_export.h"
|
#include "picloudtcp.h"
|
||||||
#include "piethernet.h"
|
#include "piethernet.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -55,11 +55,10 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
||||||
void sendStart();
|
|
||||||
|
|
||||||
PIEthernet eth;
|
PIEthernet eth;
|
||||||
PIVector<Client> clients;
|
PIVector<Client> clients;
|
||||||
|
PICloud::TCP tcp;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PICLOUDSERVER_H
|
#endif // PICLOUDSERVER_H
|
||||||
|
|||||||
@@ -27,38 +27,51 @@
|
|||||||
#include "pistring.h"
|
#include "pistring.h"
|
||||||
|
|
||||||
|
|
||||||
|
class PIEthernet;
|
||||||
|
|
||||||
namespace PICloud {
|
namespace PICloud {
|
||||||
|
|
||||||
enum Version {
|
|
||||||
Version_1 = 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum HeaderType {
|
|
||||||
Server = 1,
|
|
||||||
Client = 2,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PIP_CLOUD_EXPORT Header {
|
|
||||||
Header() {
|
|
||||||
version = Version_1;
|
|
||||||
}
|
|
||||||
uchar version; // PICloud::Version
|
|
||||||
uchar type; // PICloud::HeaderType
|
|
||||||
PIString sname; // server name
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class PIP_CLOUD_EXPORT TCP {
|
class PIP_CLOUD_EXPORT TCP {
|
||||||
public:
|
public:
|
||||||
|
enum Version {
|
||||||
|
Version_1 = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Role {
|
||||||
|
Server = 1,
|
||||||
|
Client = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Type {
|
||||||
|
Invalid = 0,
|
||||||
|
Connect = 1,
|
||||||
|
Disconnect = 2,
|
||||||
|
Data = 3,
|
||||||
|
};
|
||||||
|
|
||||||
TCP();
|
TCP();
|
||||||
|
void setRole(Role r);
|
||||||
|
void setServerName(const PIString & server_name);
|
||||||
|
|
||||||
|
void sendStart(PIEthernet * eth);
|
||||||
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
|
||||||
|
PIByteArray parseData(PIByteArray & ba);
|
||||||
|
PIString parseConnect(PIByteArray & ba);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct Header {
|
||||||
|
Header();
|
||||||
|
uchar version; // PICloud::Version
|
||||||
|
uchar type; // PICloud::Type
|
||||||
|
uchar role; // PICloud::Role
|
||||||
|
};
|
||||||
|
|
||||||
|
Header header;
|
||||||
|
PIString sname;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PIP_CLOUD_EXPORT PIByteArray & operator <<(PIByteArray & s, const PICloud::Header & v);
|
|
||||||
PIP_CLOUD_EXPORT PIByteArray & operator >>(PIByteArray & s, PICloud::Header & v);
|
|
||||||
|
|
||||||
#endif // PICLOUDTCP_H
|
#endif // PICLOUDTCP_H
|
||||||
|
|||||||
45
main.cpp
45
main.cpp
@@ -1,37 +1,20 @@
|
|||||||
#include "pip.h"
|
#include "pip.h"
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char * argv[]) {
|
||||||
PIVector<int> indexes;
|
PICLI cli(argc, argv);
|
||||||
PIEvaluator eval;
|
cli.addArgument("connect", true);
|
||||||
int dims = 3;
|
PICloudClient c("127.0.0.1:10101");
|
||||||
indexes << eval.setVariable("l", complexd(1.));
|
PICloudServer s("127.0.0.1:10101");
|
||||||
indexes << eval.setVariable("mc", complexd(1.));
|
if (cli.hasArgument("connect")) {
|
||||||
for (int i = 0; i < dims; ++i) {
|
c.setServerName(cli.argumentValue("connect"));
|
||||||
indexes << eval.setVariable("t" + PIString::fromNumber(i), complexd(1.));
|
c.startThreadedRead();
|
||||||
indexes << eval.setVariable("tv" + PIString::fromNumber(i), complexd(1.));
|
} else {
|
||||||
|
s.startThreadedRead();
|
||||||
}
|
}
|
||||||
for (int i = 0; i < dims; ++i) {
|
PIKbdListener ls;
|
||||||
indexes << eval.setVariable("m" + PIString::fromNumber(i), complexd(1.));
|
ls.enableExitCapture(PIKbdListener::F10);
|
||||||
indexes << eval.setVariable("mv" + PIString::fromNumber(i), complexd(1.));
|
ls.start();
|
||||||
}
|
WAIT_FOR_EXIT
|
||||||
piCout << indexes;
|
|
||||||
|
|
||||||
indexes.clear();
|
|
||||||
indexes << eval.setVariable("l", complexd(1.));
|
|
||||||
indexes << eval.setVariable("mc", complexd(1.));
|
|
||||||
for (int i = 0; i < dims; ++i) {
|
|
||||||
indexes << eval.setVariable("t" + PIString::fromNumber(i), complexd(1.));
|
|
||||||
indexes << eval.setVariable("tv" + PIString::fromNumber(i), complexd(1.));
|
|
||||||
}
|
|
||||||
for (int i = 0; i < dims; ++i) {
|
|
||||||
indexes << eval.setVariable("m" + PIString::fromNumber(i), complexd(1.));
|
|
||||||
indexes << eval.setVariable("mv" + PIString::fromNumber(i), complexd(1.));
|
|
||||||
}
|
|
||||||
piCout << indexes;
|
|
||||||
|
|
||||||
PIEvaluator * eval_x = new PIEvaluator();
|
|
||||||
piCout << eval_x->setVariable("t");
|
|
||||||
piCout << eval_x->setVariable("t");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#include "cloudserver.h"
|
#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) {
|
void CloudServer::addClient(DispatcherClient * c) {
|
||||||
clients << c;
|
clients << c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CloudServer::removeClient(DispatcherClient * c) {
|
||||||
|
clients.removeOne(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIVector<DispatcherClient *> CloudServer::getClients() {
|
||||||
|
return clients;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void CloudServer::printStatus() {
|
void CloudServer::printStatus() {
|
||||||
piCout << " " << "Clients for" << server->address() << ":";
|
piCout << " " << "Clients for" << server->address() << ":";
|
||||||
for (auto c: clients) {
|
for (auto c: clients) {
|
||||||
piCout << " " << c->address();
|
piCout << " " << c->address();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,18 @@
|
|||||||
class CloudServer : public PIObject {
|
class CloudServer : public PIObject {
|
||||||
PIOBJECT(CloudServer)
|
PIOBJECT(CloudServer)
|
||||||
public:
|
public:
|
||||||
CloudServer(DispatcherClient * c);
|
CloudServer(DispatcherClient * c, const PIString & sname);
|
||||||
~CloudServer();
|
~CloudServer();
|
||||||
|
PIString serverName() const;
|
||||||
void addClient(DispatcherClient * c);
|
void addClient(DispatcherClient * c);
|
||||||
|
void removeClient(DispatcherClient * c);
|
||||||
|
PIVector<DispatcherClient*> getClients();
|
||||||
EVENT_HANDLER0(void, printStatus);
|
EVENT_HANDLER0(void, printStatus);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DispatcherClient * server;
|
DispatcherClient * server;
|
||||||
PIVector<DispatcherClient*> clients;
|
PIVector<DispatcherClient*> clients;
|
||||||
|
PIString server_name;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CLOUDSERVER_H
|
#endif // CLOUDSERVER_H
|
||||||
|
|||||||
@@ -4,10 +4,14 @@
|
|||||||
|
|
||||||
DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(false) {
|
DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(false) {
|
||||||
CONNECTU(&disconnect_tm, tickEvent, eth, close);
|
CONNECTU(&disconnect_tm, tickEvent, eth, close);
|
||||||
eth->startThreadedRead();
|
|
||||||
CONNECTU(eth, threadedReadEvent, this, readed);
|
CONNECTU(eth, threadedReadEvent, this, readed);
|
||||||
CONNECTU(eth, disconnected, this, disconnected);
|
CONNECTU(eth, disconnected, this, disconnected);
|
||||||
piCoutObj << "client connected" << eth->sendAddress();
|
piCoutObj << "client connected" << eth->sendAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DispatcherClient::start() {
|
||||||
|
eth->startThreadedRead();
|
||||||
disconnect_tm.start(10000);
|
disconnect_tm.start(10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,18 +37,40 @@ void DispatcherClient::disconnected(bool withError) {
|
|||||||
|
|
||||||
|
|
||||||
void DispatcherClient::readed(uchar *data, int size) {
|
void DispatcherClient::readed(uchar *data, int size) {
|
||||||
|
piCout << size;
|
||||||
PIByteArray ba(data, 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) {
|
if (authorised) {
|
||||||
dataReaded(ba);
|
switch (hdr.first) {
|
||||||
} else {
|
case PICloud::TCP::Connect:
|
||||||
if (ba.size() < 4) return;
|
return;
|
||||||
PICloud::Header hdr;
|
case PICloud::TCP::Disconnect:
|
||||||
ba >> hdr;
|
disconnected(false);
|
||||||
if ((PICloud::HeaderType)hdr.type == PICloud::Server) {
|
return;
|
||||||
registerServer(hdr.sname, this);
|
case PICloud::TCP::Data:
|
||||||
|
dataReaded(tcp.parseData(ba));
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
disconnected(true);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if ((PICloud::HeaderType)hdr.type == PICloud::Client) {
|
} else {
|
||||||
registerClient(hdr.sname, this);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,14 @@
|
|||||||
#define DISPATCHERCLIENT_H
|
#define DISPATCHERCLIENT_H
|
||||||
|
|
||||||
#include "piethernet.h"
|
#include "piethernet.h"
|
||||||
|
#include "picloudtcp.h"
|
||||||
|
|
||||||
|
|
||||||
class DispatcherClient: public PIObject {
|
class DispatcherClient: public PIObject {
|
||||||
PIOBJECT(DispatcherClient)
|
PIOBJECT(DispatcherClient)
|
||||||
public:
|
public:
|
||||||
DispatcherClient(PIEthernet * eth_);
|
DispatcherClient(PIEthernet * eth_);
|
||||||
|
void start();
|
||||||
~DispatcherClient();
|
~DispatcherClient();
|
||||||
EVENT1(disconnectEvent, DispatcherClient *, client)
|
EVENT1(disconnectEvent, DispatcherClient *, client)
|
||||||
EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
|
EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
|
||||||
@@ -23,6 +25,7 @@ private:
|
|||||||
PIEthernet * eth;
|
PIEthernet * eth;
|
||||||
PITimer disconnect_tm;
|
PITimer disconnect_tm;
|
||||||
bool authorised;
|
bool authorised;
|
||||||
|
PICloud::TCP tcp;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -35,9 +35,26 @@ void DispatcherServer::printStatus() {
|
|||||||
|
|
||||||
|
|
||||||
void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
||||||
|
if (!clients.contains(client)) {
|
||||||
|
piCoutObj << "INVALID client" << client;
|
||||||
|
return;
|
||||||
|
}
|
||||||
piCoutObj << "remove client" << client;
|
piCoutObj << "remove client" << client;
|
||||||
map_mutex.lock();
|
map_mutex.lock();
|
||||||
clients.removeOne(client);
|
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();
|
map_mutex.unlock();
|
||||||
delete client;
|
delete client;
|
||||||
}
|
}
|
||||||
@@ -48,17 +65,24 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
|
|||||||
CONNECTU(client, disconnectEvent, this, disconnectClient);
|
CONNECTU(client, disconnectEvent, this, disconnectClient);
|
||||||
CONNECTL(client, registerServer, [this](PIString sname, DispatcherClient * c){
|
CONNECTL(client, registerServer, [this](PIString sname, DispatcherClient * c){
|
||||||
map_mutex.lock();
|
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();
|
map_mutex.unlock();
|
||||||
});
|
});
|
||||||
CONNECTL(client, registerClient, [this](PIString sname, DispatcherClient * c){
|
CONNECTL(client, registerClient, [this](PIString sname, DispatcherClient * c){
|
||||||
map_mutex.lock();
|
map_mutex.lock();
|
||||||
if (c_servers.contains(sname)) {
|
CloudServer * cs = c_servers.value(sname, nullptr);
|
||||||
c_servers[sname]->addClient(c);
|
if (cs) {
|
||||||
|
piCoutObj << "add new Client to Server ->" << sname;
|
||||||
|
cs->addClient(c);
|
||||||
|
index_c_clients.insert(c, cs);
|
||||||
}
|
}
|
||||||
map_mutex.unlock();
|
map_mutex.unlock();
|
||||||
});
|
});
|
||||||
piCoutObj << "add client" << client;
|
piCoutObj << "add client" << client;
|
||||||
|
client->start();
|
||||||
map_mutex.lock();
|
map_mutex.lock();
|
||||||
clients.push_back(client);
|
clients.push_back(client);
|
||||||
map_mutex.unlock();
|
map_mutex.unlock();
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ private:
|
|||||||
PIEthernet eth;
|
PIEthernet eth;
|
||||||
PIVector<DispatcherClient*> clients;
|
PIVector<DispatcherClient*> clients;
|
||||||
PIMap<PIString, CloudServer *> c_servers;
|
PIMap<PIString, CloudServer *> c_servers;
|
||||||
|
PIMap<const DispatcherClient *, CloudServer *> index_c_servers;
|
||||||
|
PIMap<const DispatcherClient *, CloudServer *> index_c_clients;
|
||||||
PITimer status_timer;
|
PITimer status_timer;
|
||||||
PIMutex map_mutex;
|
PIMutex map_mutex;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user