This commit is contained in:
2020-08-02 15:57:27 +03:00
7 changed files with 133 additions and 7 deletions

View File

@@ -23,3 +23,18 @@
PICloudClient::PICloudClient() {
}
PICloudClient::~PICloudClient() {
}
bool PICloudClient::openDevice() {
}
bool PICloudClient::closeDevice() {
}

View File

@@ -26,14 +26,23 @@
#include "pip_cloud_export.h"
#include "piiodevice.h"
class PIEthernet;
class PIP_CLOUD_EXPORT PICloudClient {
class PIP_CLOUD_EXPORT PICloudClient : public PIIODevice
{
PIIODEVICE(PICloudClient)
public:
//!
explicit PICloudClient();
virtual ~PICloudClient();
protected:
bool openDevice();
bool closeDevice();
private:
PIEthernet * eth;
};
#endif // PICCLOUDCLIENT_H

View File

@@ -0,0 +1,28 @@
#include "dispatcherclient.h"
DispatcherClient::DispatcherClient(PIEthernet * eth_) {
eth = eth_;
eth->startThreadedRead();
CONNECTU(eth, threadedReadEvent, this, readed);
CONNECTU(eth, disconnected, this, disconnected);
piCoutObj << "client connected" << eth->sendAddress();
}
DispatcherClient::~DispatcherClient() {
}
void DispatcherClient::disconnected(bool withError) {
piCoutObj << "client disconnected" << eth->sendAddress();
disconnectEvent(this);
}
void DispatcherClient::readed(uchar *data, int size) {
PIByteArray ba(data, size);
piCoutObj << "readed" << ba.toHex();
eth->write(ba);
}

View File

@@ -0,0 +1,21 @@
#ifndef DISPATCHERCLIENT_H
#define DISPATCHERCLIENT_H
#include "piethernet.h"
class DispatcherClient: public PIObject {
PIOBJECT(DispatcherClient)
public:
DispatcherClient(PIEthernet * eth_);
~DispatcherClient();
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
EVENT_HANDLER1(void, disconnected, bool, withError);
EVENT1(disconnectEvent, DispatcherClient *, client)
private:
PIEthernet * eth;
};
#endif // DISPATCHERCLIENT_H

View File

@@ -0,0 +1,32 @@
#include "dispatcherserver.h"
DispatcherServer::DispatcherServer(PIEthernet::Address addr) {
eth = new PIEthernet(PIEthernet::TCP_Server);
eth->setParameter(PIEthernet::ReuseAddress);
CONNECTU(eth, newConnection, this, newConnection);
eth->listen(addr, true);
piCoutObj << eth << "server started" << addr;
}
DispatcherServer::~DispatcherServer() {
eth->close();
piCoutObj << "server stoped";
delete eth;
}
void DispatcherServer::disconnectClient(DispatcherClient *client) {
piCoutObj << "remove client" << client;
clients.removeOne(client);
delete client;
}
void DispatcherServer::newConnection(PIEthernet *cl) {
DispatcherClient * client = new DispatcherClient(cl);
piCoutObj << "add client" << client;
CONNECTU(client, disconnectEvent, this, disconnectClient);
clients.push_back(client);
}

View File

@@ -0,0 +1,20 @@
#ifndef DISPATCHERSERVER_H
#define DISPATCHERSERVER_H
#include "dispatcherclient.h"
class DispatcherServer: public PIObject {
PIOBJECT(DispatcherServer)
public:
DispatcherServer(PIEthernet::Address addr);
~DispatcherServer();
EVENT_HANDLER1(void, newConnection, PIEthernet * , cl);
EVENT_HANDLER1(void, disconnectClient, DispatcherClient *, client);
private:
PIEthernet * eth;
PIVector<DispatcherClient*> clients;
};
#endif // DISPATCHERSERVER_H

View File

@@ -19,12 +19,12 @@
#include "pip.h"
#include "picrypt.h"
#include "dispatcherserver.h"
using namespace PICoutManipulators;
PIString ip = "0.0.0.0";
int port = 10101;
PIEthernet::Address addr = PIEthernet::Address("0.0.0.0", 10101);
void usage() {
piCout << Bold << "PIP Cloud Dispatcher";
@@ -49,10 +49,11 @@ int main (int argc, char * argv[]) {
return 0;
}
if (cli.hasArgument("ip"))
ip = cli.argumentValue("ip");
addr.setIP(cli.argumentValue("ip"));
if (cli.hasArgument("port"))
port = cli.argumentValue("port").toInt();
addr.setPort(cli.argumentValue("port").toInt());
DispatcherServer server(addr);
WAIT_FOR_EXIT
return 0;
}