PICloud connect/disconnect
This commit is contained in:
6
libs/cloud/picloudbase.cpp
Normal file
6
libs/cloud/picloudbase.cpp
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#include "picloudbase.h"
|
||||||
|
|
||||||
|
|
||||||
|
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client) {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
#include "picloudtcp.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) {
|
||||||
tcp.setRole(PICloud::TCP::Client);
|
tcp.setRole(PICloud::TCP::Client);
|
||||||
setName("cloud_client");
|
setName("cloud_client");
|
||||||
}
|
}
|
||||||
@@ -39,28 +39,49 @@ void PICloudClient::setServerName(const PIString & server_name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloudClient::setKeepConnection(bool on) {
|
||||||
|
eth.setParameter(PIEthernet::KeepConnection, on);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PICloudClient::openDevice() {
|
bool PICloudClient::openDevice() {
|
||||||
piCout << "PICloudClient open device" << path();
|
piCout << "PICloudClient open device" << path();
|
||||||
bool op = eth.connect(path(), false);
|
bool op = eth.connect(path(), false);
|
||||||
if (op) {
|
if (op) {
|
||||||
CONNECTL(ð, disconnected, [this](bool){opened_ = false;});
|
CONNECTL(ð, connected, [this](){tcp.sendStart(ð);});
|
||||||
CONNECTU(ð, threadedReadEvent, this, readed);
|
CONNECTU(ð, threadedReadEvent, this, readed);
|
||||||
|
CONNECTL(ð, disconnected, [this](bool){
|
||||||
|
opened_ = false;
|
||||||
|
eth.close();
|
||||||
|
piCoutObj << "disconnected";// << !isOpened() << isClosed();
|
||||||
|
piMSleep(100);
|
||||||
|
});
|
||||||
eth.startThreadedRead();
|
eth.startThreadedRead();
|
||||||
tcp.sendStart(ð);
|
tcp.sendStart(ð);
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
eth.close();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
eth.close();
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PICloudClient::closeDevice() {
|
bool PICloudClient::closeDevice() {
|
||||||
return eth.close();
|
eth.stop();
|
||||||
|
eth.close();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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);
|
piCoutObj << "readDevice";
|
||||||
|
mutex_buff.lock();
|
||||||
|
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
||||||
|
int sz = piMini(max_size, buff.size());
|
||||||
|
memcpy(read_to, buff.data(), sz);
|
||||||
|
buff.remove(0, sz);
|
||||||
|
mutex_buff.unlock();
|
||||||
|
return sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -70,5 +91,15 @@ int PICloudClient::writeDevice(const void * data, int max_size) {
|
|||||||
|
|
||||||
|
|
||||||
void PICloudClient::readed(uchar *data, int size) {
|
void PICloudClient::readed(uchar *data, int size) {
|
||||||
|
PIByteArray ba(data, size);
|
||||||
|
mutex_buff.lock();
|
||||||
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||||
|
if (hdr.second == tcp.role()) {
|
||||||
|
piCoutObj << "readed" << ba.toHex();
|
||||||
|
buff.append(data, size);
|
||||||
|
cond_buff.notifyOne();
|
||||||
|
}
|
||||||
|
mutex_buff.unlock();
|
||||||
|
while (buff.size() > threadedReadBufferSize()) piMSleep(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,27 +20,41 @@
|
|||||||
#include "picloudserver.h"
|
#include "picloudserver.h"
|
||||||
|
|
||||||
|
|
||||||
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), PICloudBase() {
|
||||||
PIString name = "cloud_server_" + PIString::fromNumber(randomi()%1000);
|
PIString name = "PCS_" + PIString::fromNumber(randomi()%1000);
|
||||||
tcp.setRole(PICloud::TCP::Server);
|
tcp.setRole(PICloud::TCP::Server);
|
||||||
tcp.setServerName(name);
|
tcp.setServerName(name);
|
||||||
setName(name);
|
setName("cloud_server__" + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PICloudServer::~PICloudServer() {
|
PICloudServer::~PICloudServer() {
|
||||||
|
for (auto & c : clients) {
|
||||||
|
c.stop();
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
stop();
|
stop();
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloudServer::setServerName(const PIString & server_name) {
|
||||||
|
setName("cloud_server__" + server_name);
|
||||||
|
tcp.setServerName(server_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PICloudServer::openDevice() {
|
bool PICloudServer::openDevice() {
|
||||||
piCout << "PICloudServer open device" << path();
|
piCout << "PICloudServer open device" << path();
|
||||||
bool op = eth.connect(path(), false);
|
bool op = eth.connect(path(), false);
|
||||||
if (op) {
|
if (op) {
|
||||||
CONNECTL(ð, disconnected, [this](bool){opened_ = false;});
|
|
||||||
CONNECTU(ð, threadedReadEvent, this, readed);
|
CONNECTU(ð, threadedReadEvent, this, readed);
|
||||||
CONNECTL(ð, connected, [this](){tcp.sendStart(ð);});
|
CONNECTL(ð, connected, [this](){tcp.sendStart(ð);});
|
||||||
|
CONNECTL(ð, disconnected, [this](bool){
|
||||||
|
opened_ = false;
|
||||||
|
eth.close();
|
||||||
|
piCoutObj << "disconnected" << !isOpened() << isClosed();
|
||||||
|
});
|
||||||
eth.startThreadedRead();
|
eth.startThreadedRead();
|
||||||
tcp.sendStart(ð);
|
tcp.sendStart(ð);
|
||||||
return true;
|
return true;
|
||||||
@@ -51,7 +65,13 @@ bool PICloudServer::openDevice() {
|
|||||||
|
|
||||||
|
|
||||||
bool PICloudServer::closeDevice() {
|
bool PICloudServer::closeDevice() {
|
||||||
return eth.close();
|
eth.stop();
|
||||||
|
for (auto & c : clients) {
|
||||||
|
c.stop();
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
eth.close();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -71,10 +91,14 @@ PICloudServer::Client::Client(PICloudServer * srv) : server(srv) {
|
|||||||
|
|
||||||
|
|
||||||
bool PICloudServer::Client::openDevice() {
|
bool PICloudServer::Client::openDevice() {
|
||||||
return server;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PICloudServer::readed(uchar *data, int size) {
|
void PICloudServer::readed(uchar *data, int size) {
|
||||||
|
PIByteArray ba(data, size);
|
||||||
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||||
|
if (hdr.second == tcp.role()) {
|
||||||
|
piCoutObj << "readed" << ba.toHex();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,24 @@ void PICloud::TCP::sendStart(PIEthernet * eth) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloud::TCP::sendConnected(PIEthernet * eth, uint client_id) {
|
||||||
|
header.type = PICloud::TCP::Connect;
|
||||||
|
PIByteArray ba;
|
||||||
|
ba << header << client_id;
|
||||||
|
eth->send(ba);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloud::TCP::sendData(PIEthernet * eth, const PIByteArray & data) {
|
||||||
|
header.type = PICloud::TCP::Data;
|
||||||
|
PIByteArray ba;
|
||||||
|
ba << header;
|
||||||
|
ba.append(data);
|
||||||
|
// piCout << "sendData" << ba.toHex();
|
||||||
|
eth->send(ba);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
|
||||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
|
||||||
ret.first = Invalid;
|
ret.first = Invalid;
|
||||||
|
|||||||
40
libs/main/cloud/picloudbase.h
Normal file
40
libs/main/cloud/picloudbase.h
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/*! \file picloudbase.h
|
||||||
|
* \brief PICloud Base - Base class for PICloudClient and PICloud Server
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
PIP - Platform Independent Primitives
|
||||||
|
PICloud Base - Base class for PICloudClient and PICloud Server
|
||||||
|
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PICLOUDBASE_H
|
||||||
|
#define PICLOUDBASE_H
|
||||||
|
|
||||||
|
#include "picloudtcp.h"
|
||||||
|
#include "piethernet.h"
|
||||||
|
|
||||||
|
|
||||||
|
class PICloudBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PICloudBase();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
PIEthernet eth;
|
||||||
|
PICloud::TCP tcp;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // PICLOUDBASE_H
|
||||||
@@ -23,11 +23,11 @@
|
|||||||
#ifndef PICLOUDCLIENT_H
|
#ifndef PICLOUDCLIENT_H
|
||||||
#define PICLOUDCLIENT_H
|
#define PICLOUDCLIENT_H
|
||||||
|
|
||||||
#include "picloudtcp.h"
|
#include "picloudbase.h"
|
||||||
#include "piethernet.h"
|
#include "piconditionvar.h"
|
||||||
|
|
||||||
|
|
||||||
class PIP_CLOUD_EXPORT PICloudClient : public PIIODevice
|
class PIP_CLOUD_EXPORT PICloudClient : public PIIODevice, private PICloudBase
|
||||||
{
|
{
|
||||||
PIIODEVICE(PICloudClient)
|
PIIODEVICE(PICloudClient)
|
||||||
public:
|
public:
|
||||||
@@ -36,6 +36,7 @@ public:
|
|||||||
virtual ~PICloudClient();
|
virtual ~PICloudClient();
|
||||||
|
|
||||||
void setServerName(const PIString & server_name);
|
void setServerName(const PIString & server_name);
|
||||||
|
void setKeepConnection(bool on);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool openDevice();
|
bool openDevice();
|
||||||
@@ -45,9 +46,9 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
||||||
|
PIByteArray buff;
|
||||||
PIEthernet eth;
|
PIMutex mutex_buff;
|
||||||
PICloud::TCP tcp;
|
PIConditionVariable cond_buff;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PICLOUDCLIENT_H
|
#endif // PICLOUDCLIENT_H
|
||||||
|
|||||||
@@ -23,11 +23,10 @@
|
|||||||
#ifndef PICLOUDSERVER_H
|
#ifndef PICLOUDSERVER_H
|
||||||
#define PICLOUDSERVER_H
|
#define PICLOUDSERVER_H
|
||||||
|
|
||||||
#include "picloudtcp.h"
|
#include "picloudbase.h"
|
||||||
#include "piethernet.h"
|
|
||||||
|
|
||||||
|
|
||||||
class PIP_CLOUD_EXPORT PICloudServer : public PIIODevice
|
class PIP_CLOUD_EXPORT PICloudServer : public PIIODevice, private PICloudBase
|
||||||
{
|
{
|
||||||
PIIODEVICE(PICloudServer)
|
PIIODEVICE(PICloudServer)
|
||||||
public:
|
public:
|
||||||
@@ -45,6 +44,8 @@ public:
|
|||||||
PICloudServer * server;
|
PICloudServer * server;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void setServerName(const PIString & server_name);
|
||||||
|
|
||||||
EVENT1(newConnection, PICloudServer::Client * , client)
|
EVENT1(newConnection, PICloudServer::Client * , client)
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -56,9 +57,7 @@ protected:
|
|||||||
private:
|
private:
|
||||||
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
||||||
|
|
||||||
PIEthernet eth;
|
|
||||||
PIVector<Client> clients;
|
PIVector<Client> clients;
|
||||||
PICloud::TCP tcp;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PICLOUDSERVER_H
|
#endif // PICLOUDSERVER_H
|
||||||
|
|||||||
@@ -53,9 +53,12 @@ public:
|
|||||||
|
|
||||||
TCP();
|
TCP();
|
||||||
void setRole(Role r);
|
void setRole(Role r);
|
||||||
|
Role role() const {return (Role)header.role;}
|
||||||
void setServerName(const PIString & server_name);
|
void setServerName(const PIString & server_name);
|
||||||
|
|
||||||
void sendStart(PIEthernet * eth);
|
void sendStart(PIEthernet * eth);
|
||||||
|
void sendConnected(PIEthernet * eth, uint client_id);
|
||||||
|
void sendData(PIEthernet * eth, const PIByteArray & data);
|
||||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
|
||||||
PIByteArray parseData(PIByteArray & ba);
|
PIByteArray parseData(PIByteArray & ba);
|
||||||
PIString parseConnect(PIByteArray & ba);
|
PIString parseConnect(PIByteArray & ba);
|
||||||
|
|||||||
3
main.cpp
3
main.cpp
@@ -4,8 +4,11 @@
|
|||||||
int main(int argc, char * argv[]) {
|
int main(int argc, char * argv[]) {
|
||||||
PICLI cli(argc, argv);
|
PICLI cli(argc, argv);
|
||||||
cli.addArgument("connect", true);
|
cli.addArgument("connect", true);
|
||||||
|
cli.addArgument("name", true);
|
||||||
PICloudClient c("127.0.0.1:10101");
|
PICloudClient c("127.0.0.1:10101");
|
||||||
|
// c.setReopenEnabled(true);
|
||||||
PICloudServer s("127.0.0.1:10101");
|
PICloudServer s("127.0.0.1:10101");
|
||||||
|
if (cli.hasArgument("name")) s.setServerName(cli.argumentValue("name"));
|
||||||
if (cli.hasArgument("connect")) {
|
if (cli.hasArgument("connect")) {
|
||||||
c.setServerName(cli.argumentValue("connect"));
|
c.setServerName(cli.argumentValue("connect"));
|
||||||
c.startThreadedRead();
|
c.startThreadedRead();
|
||||||
|
|||||||
@@ -20,11 +20,13 @@ PIString CloudServer::serverName() const {
|
|||||||
|
|
||||||
void CloudServer::addClient(DispatcherClient * c) {
|
void CloudServer::addClient(DispatcherClient * c) {
|
||||||
clients << c;
|
clients << c;
|
||||||
|
index_clients.insert(c->clientId(), c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void CloudServer::removeClient(DispatcherClient * c) {
|
void CloudServer::removeClient(DispatcherClient * c) {
|
||||||
clients.removeOne(c);
|
clients.removeOne(c);
|
||||||
|
index_clients.removeOne(c->clientId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -34,9 +36,11 @@ PIVector<DispatcherClient *> CloudServer::getClients() {
|
|||||||
|
|
||||||
|
|
||||||
void CloudServer::printStatus() {
|
void CloudServer::printStatus() {
|
||||||
piCout << " " << "Clients for" << server->address() << ":";
|
piCout << " " << "Clients for" << server->address() << server_name << ":";
|
||||||
for (auto c: clients) {
|
for (auto c: clients) {
|
||||||
piCout << " " << c->address();
|
piCout << " " << c->address() << c->clientId();
|
||||||
}
|
}
|
||||||
|
for (auto c: clients) c->sendData(PIByteArray::fromHex("000000"));
|
||||||
|
server->sendData(PIByteArray::fromHex("000000"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
DispatcherClient * server;
|
DispatcherClient * server;
|
||||||
PIVector<DispatcherClient*> clients;
|
PIVector<DispatcherClient*> clients;
|
||||||
|
PIMap<uint, DispatcherClient*> index_clients;
|
||||||
PIString server_name;
|
PIString server_name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#include "picloudtcp.h"
|
#include "picloudtcp.h"
|
||||||
|
|
||||||
|
|
||||||
DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(false) {
|
DispatcherClient::DispatcherClient(PIEthernet * eth_, int id) : eth(eth_), authorised(false), client_id(id) {
|
||||||
CONNECTU(&disconnect_tm, tickEvent, eth, close);
|
CONNECTU(&disconnect_tm, tickEvent, eth, close);
|
||||||
CONNECTU(eth, threadedReadEvent, this, readed);
|
CONNECTU(eth, threadedReadEvent, this, readed);
|
||||||
CONNECTU(eth, disconnected, this, disconnected);
|
CONNECTU(eth, disconnected, this, disconnected);
|
||||||
@@ -12,7 +12,7 @@ DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(fa
|
|||||||
|
|
||||||
void DispatcherClient::start() {
|
void DispatcherClient::start() {
|
||||||
eth->startThreadedRead();
|
eth->startThreadedRead();
|
||||||
disconnect_tm.start(10000);
|
//disconnect_tm.start(10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -30,6 +30,16 @@ void DispatcherClient::close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DispatcherClient::sendConnected() {
|
||||||
|
tcp.sendConnected(eth, client_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DispatcherClient::sendData(const PIByteArray & data) {
|
||||||
|
tcp.sendData(eth, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void DispatcherClient::disconnected(bool withError) {
|
void DispatcherClient::disconnected(bool withError) {
|
||||||
piCoutObj << "client disconnected" << eth->sendAddress();
|
piCoutObj << "client disconnected" << eth->sendAddress();
|
||||||
disconnectEvent(this);
|
disconnectEvent(this);
|
||||||
@@ -37,7 +47,7 @@ void DispatcherClient::disconnected(bool withError) {
|
|||||||
|
|
||||||
|
|
||||||
void DispatcherClient::readed(uchar *data, int size) {
|
void DispatcherClient::readed(uchar *data, int size) {
|
||||||
piCout << size;
|
// piCout << size;
|
||||||
PIByteArray ba(data, size);
|
PIByteArray ba(data, size);
|
||||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||||
if (hdr.first == PICloud::TCP::Invalid) {
|
if (hdr.first == PICloud::TCP::Invalid) {
|
||||||
@@ -61,6 +71,7 @@ void DispatcherClient::readed(uchar *data, int size) {
|
|||||||
} else {
|
} else {
|
||||||
switch (hdr.first) {
|
switch (hdr.first) {
|
||||||
case PICloud::TCP::Connect: {
|
case PICloud::TCP::Connect: {
|
||||||
|
tcp.setRole(hdr.second);
|
||||||
PIString sn = tcp.parseConnect(ba);
|
PIString sn = tcp.parseConnect(ba);
|
||||||
if (hdr.second == PICloud::TCP::Server) registerServer(sn, this);
|
if (hdr.second == PICloud::TCP::Server) registerServer(sn, this);
|
||||||
if (hdr.second == PICloud::TCP::Client) registerClient(sn, this);
|
if (hdr.second == PICloud::TCP::Client) registerClient(sn, this);
|
||||||
|
|||||||
@@ -8,14 +8,17 @@
|
|||||||
class DispatcherClient: public PIObject {
|
class DispatcherClient: public PIObject {
|
||||||
PIOBJECT(DispatcherClient)
|
PIOBJECT(DispatcherClient)
|
||||||
public:
|
public:
|
||||||
DispatcherClient(PIEthernet * eth_);
|
DispatcherClient(PIEthernet * eth_, int id);
|
||||||
void start();
|
|
||||||
~DispatcherClient();
|
~DispatcherClient();
|
||||||
|
void start();
|
||||||
|
void close();
|
||||||
|
void sendConnected();
|
||||||
|
void sendData(const PIByteArray & data);
|
||||||
|
PIString address();
|
||||||
|
uint clientId() const {return client_id;}
|
||||||
EVENT1(disconnectEvent, DispatcherClient *, client)
|
EVENT1(disconnectEvent, DispatcherClient *, client)
|
||||||
EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
|
EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
|
||||||
EVENT2(registerClient, PIString, sname, DispatcherClient *, client)
|
EVENT2(registerClient, PIString, sname, DispatcherClient *, client)
|
||||||
PIString address();
|
|
||||||
void close();
|
|
||||||
EVENT1(dataReaded, PIByteArray, ba)
|
EVENT1(dataReaded, PIByteArray, ba)
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -26,6 +29,7 @@ private:
|
|||||||
PITimer disconnect_tm;
|
PITimer disconnect_tm;
|
||||||
bool authorised;
|
bool authorised;
|
||||||
PICloud::TCP tcp;
|
PICloud::TCP tcp;
|
||||||
|
uint client_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,15 @@
|
|||||||
|
|
||||||
|
|
||||||
DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::TCP_Server) {
|
DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::TCP_Server) {
|
||||||
|
client_gid = 0;
|
||||||
eth.setParameter(PIEthernet::ReuseAddress);
|
eth.setParameter(PIEthernet::ReuseAddress);
|
||||||
CONNECTU(ð, newConnection, this, newConnection);
|
CONNECTU(ð, newConnection, this, newConnection);
|
||||||
eth.listen(addr, true);
|
eth.listen(addr, true);
|
||||||
piCoutObj << "server started" << addr;
|
piCoutObj << "server started" << addr;
|
||||||
CONNECTU(&status_timer, tickEvent, this, printStatus);
|
CONNECTU(&status_timer, tickEvent, this, printStatus);
|
||||||
|
CONNECTU(&timeout_timer, tickEvent, this, cleanClients);
|
||||||
status_timer.start(1000);
|
status_timer.start(1000);
|
||||||
|
timeout_timer.start(5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -17,6 +20,26 @@ DispatcherServer::~DispatcherServer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DispatcherServer::cleanClients() {
|
||||||
|
PIVector<DispatcherClient*> rm;
|
||||||
|
map_mutex.lock();
|
||||||
|
for (auto c: clients) {
|
||||||
|
if (!index_c_servers.contains(c) && !index_c_clients.contains(c)) {
|
||||||
|
if (rm_clients.contains(c)) rm << c;
|
||||||
|
else rm_clients << c;
|
||||||
|
} else rm_clients.removeAll(c);
|
||||||
|
}
|
||||||
|
for (auto c: rm_clients) {
|
||||||
|
if (clients.contains(c)) rm << c;
|
||||||
|
}
|
||||||
|
map_mutex.unlock();
|
||||||
|
for (auto c: rm) {
|
||||||
|
c->close();
|
||||||
|
// c->deleteLater();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void DispatcherServer::printStatus() {
|
void DispatcherServer::printStatus() {
|
||||||
map_mutex.lock();
|
map_mutex.lock();
|
||||||
piCout << PICoutManipulators::NewLine;
|
piCout << PICoutManipulators::NewLine;
|
||||||
@@ -48,20 +71,25 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
|||||||
for(auto csc : cscv) {
|
for(auto csc : cscv) {
|
||||||
csc->close();
|
csc->close();
|
||||||
clients.removeOne(csc);
|
clients.removeOne(csc);
|
||||||
delete csc;
|
index_c_clients.removeOne(csc);
|
||||||
|
csc->deleteLater();
|
||||||
}
|
}
|
||||||
c_servers.remove(cs->serverName());
|
c_servers.remove(cs->serverName());
|
||||||
|
index_c_servers.removeOne(client);
|
||||||
}
|
}
|
||||||
CloudServer * cc = index_c_clients.value(client, nullptr);
|
CloudServer * cc = index_c_clients.value(client, nullptr);
|
||||||
if (cc) cc->removeClient(client);
|
if (cc) {
|
||||||
|
cc->removeClient(client);
|
||||||
|
index_c_clients.removeOne(client);
|
||||||
|
}
|
||||||
client->close();
|
client->close();
|
||||||
map_mutex.unlock();
|
map_mutex.unlock();
|
||||||
delete client;
|
client->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void DispatcherServer::newConnection(PIEthernet *cl) {
|
void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||||
DispatcherClient * client = new DispatcherClient(cl);
|
DispatcherClient * client = new DispatcherClient(cl, client_gid++);
|
||||||
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();
|
||||||
@@ -78,6 +106,8 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
|
|||||||
piCoutObj << "add new Client to Server ->" << sname;
|
piCoutObj << "add new Client to Server ->" << sname;
|
||||||
cs->addClient(c);
|
cs->addClient(c);
|
||||||
index_c_clients.insert(c, cs);
|
index_c_clients.insert(c, cs);
|
||||||
|
} else {
|
||||||
|
rm_clients << c;
|
||||||
}
|
}
|
||||||
map_mutex.unlock();
|
map_mutex.unlock();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -14,14 +14,18 @@ public:
|
|||||||
private:
|
private:
|
||||||
EVENT_HANDLER1(void, newConnection, PIEthernet * , cl);
|
EVENT_HANDLER1(void, newConnection, PIEthernet * , cl);
|
||||||
EVENT_HANDLER1(void, disconnectClient, DispatcherClient *, client);
|
EVENT_HANDLER1(void, disconnectClient, DispatcherClient *, client);
|
||||||
|
EVENT_HANDLER0(void, cleanClients);
|
||||||
|
|
||||||
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_servers;
|
||||||
PIMap<const DispatcherClient *, CloudServer *> index_c_clients;
|
PIMap<const DispatcherClient *, CloudServer *> index_c_clients;
|
||||||
|
PIVector<DispatcherClient*> rm_clients;
|
||||||
PITimer status_timer;
|
PITimer status_timer;
|
||||||
|
PITimer timeout_timer;
|
||||||
PIMutex map_mutex;
|
PIMutex map_mutex;
|
||||||
|
uint client_gid;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DISPATCHERSERVER_H
|
#endif // DISPATCHERSERVER_H
|
||||||
|
|||||||
Reference in New Issue
Block a user