cloud test
This commit is contained in:
@@ -18,11 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "picloudserver.h"
|
#include "picloudserver.h"
|
||||||
#include "piethernet.h"
|
|
||||||
|
|
||||||
|
|
||||||
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) {
|
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), eth(PIEthernet::TCP_Client) {
|
||||||
eth = new PIEthernet(PIEthernet::TCP_Client);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -33,18 +31,28 @@ PICloudServer::~PICloudServer() {
|
|||||||
|
|
||||||
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(eth, disconnected, [this](bool){eth->connect(path());});
|
|
||||||
eth->startThreadedRead();
|
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
eth.close();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PICloudServer::closeDevice() {
|
bool PICloudServer::closeDevice() {
|
||||||
eth->stopThreadedRead();
|
return eth.close();
|
||||||
return eth->close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int PICloudServer::readDevice(void * read_to, int max_size) {
|
||||||
|
return eth.read(read_to, max_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int PICloudServer::writeDevice(const void * data, int max_size) {
|
||||||
|
return eth.write(data, max_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
#define PICLOUDSERVER_H
|
#define PICLOUDSERVER_H
|
||||||
|
|
||||||
#include "pip_cloud_export.h"
|
#include "pip_cloud_export.h"
|
||||||
#include "piiodevice.h"
|
#include "piethernet.h"
|
||||||
|
|
||||||
class PIEthernet;
|
class PIEthernet;
|
||||||
|
|
||||||
@@ -40,9 +40,12 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
bool openDevice();
|
bool openDevice();
|
||||||
bool closeDevice();
|
bool closeDevice();
|
||||||
|
int readDevice(void * read_to, int max_size);
|
||||||
|
int writeDevice(const void * data, int max_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PIEthernet * eth;
|
PIEthernet eth;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PICLOUDSERVER_H
|
#endif // PICLOUDSERVER_H
|
||||||
|
|||||||
@@ -564,8 +564,9 @@ bool PIEthernet::connect(bool threaded) {
|
|||||||
PRIVATE->addr_.sin_len = sizeof(PRIVATE->addr_);
|
PRIVATE->addr_.sin_len = sizeof(PRIVATE->addr_);
|
||||||
#endif
|
#endif
|
||||||
connected_ = (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == 0);
|
connected_ = (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == 0);
|
||||||
if (!connected_)
|
if (!connected_) {
|
||||||
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
|
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
|
||||||
|
}
|
||||||
opened_ = connected_;
|
opened_ = connected_;
|
||||||
if (connected_) {
|
if (connected_) {
|
||||||
connecting_ = false;
|
connecting_ = false;
|
||||||
|
|||||||
19
main.cpp
19
main.cpp
@@ -2,13 +2,16 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
PICloudServer s("127.0.0.1:10101");
|
PICloudServer s("127.0.0.1:10101");
|
||||||
for (int i=0; i<3; ++i) {
|
// for (int i=0; i<3; ++i) {
|
||||||
s.open();
|
// s.open();
|
||||||
piCout() << "opened";
|
// piCout << "opened";
|
||||||
piSleep(1);
|
// piSleep(10);
|
||||||
s.close();
|
// s.close();
|
||||||
piCout() << "closed";
|
// piCout << "closed";
|
||||||
piSleep(1);
|
// piSleep(1);
|
||||||
}
|
// }
|
||||||
|
// s.open();
|
||||||
|
s.startThreadedRead();
|
||||||
|
piSleep(200);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
5
utils/cloud_dispatcher/cloudserver.cpp
Normal file
5
utils/cloud_dispatcher/cloudserver.cpp
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#include "cloudserver.h"
|
||||||
|
|
||||||
|
CloudServer::CloudServer(DispatcherClient * client) {
|
||||||
|
|
||||||
|
}
|
||||||
12
utils/cloud_dispatcher/cloudserver.h
Normal file
12
utils/cloud_dispatcher/cloudserver.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef CLOUDSERVER_H
|
||||||
|
#define CLOUDSERVER_H
|
||||||
|
|
||||||
|
#include "dispatcherclient.h"
|
||||||
|
|
||||||
|
class CloudServer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CloudServer(DispatcherClient * client);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CLOUDSERVER_H
|
||||||
@@ -3,14 +3,22 @@
|
|||||||
|
|
||||||
DispatcherClient::DispatcherClient(PIEthernet * eth_) {
|
DispatcherClient::DispatcherClient(PIEthernet * eth_) {
|
||||||
eth = eth_;
|
eth = eth_;
|
||||||
|
CONNECTU(&disconnect_tm, tickEvent, eth, close);
|
||||||
eth->startThreadedRead();
|
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();
|
||||||
|
disconnect_tm.start(10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DispatcherClient::~DispatcherClient() {
|
DispatcherClient::~DispatcherClient() {
|
||||||
|
// delete eth;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIString DispatcherClient::address() {
|
||||||
|
return eth->path();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,12 +9,16 @@ class DispatcherClient: public PIObject {
|
|||||||
public:
|
public:
|
||||||
DispatcherClient(PIEthernet * eth_);
|
DispatcherClient(PIEthernet * eth_);
|
||||||
~DispatcherClient();
|
~DispatcherClient();
|
||||||
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
|
||||||
EVENT_HANDLER1(void, disconnected, bool, withError);
|
|
||||||
EVENT1(disconnectEvent, DispatcherClient *, client)
|
EVENT1(disconnectEvent, DispatcherClient *, client)
|
||||||
|
EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
|
||||||
|
PIString address();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
|
||||||
|
EVENT_HANDLER1(void, disconnected, bool, withError);
|
||||||
|
|
||||||
PIEthernet * eth;
|
PIEthernet * eth;
|
||||||
|
PITimer disconnect_tm;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,57 @@
|
|||||||
#include "dispatcherserver.h"
|
#include "dispatcherserver.h"
|
||||||
|
|
||||||
|
|
||||||
DispatcherServer::DispatcherServer(PIEthernet::Address addr) {
|
DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::TCP_Server) {
|
||||||
eth = new PIEthernet(PIEthernet::TCP_Server);
|
eth.setParameter(PIEthernet::ReuseAddress);
|
||||||
eth->setParameter(PIEthernet::ReuseAddress);
|
CONNECTU(ð, newConnection, this, newConnection);
|
||||||
CONNECTU(eth, newConnection, this, newConnection);
|
eth.listen(addr, true);
|
||||||
eth->listen(addr, true);
|
piCoutObj << "server started" << addr;
|
||||||
piCoutObj << eth << "server started" << addr;
|
CONNECTU(&status_timer, tickEvent, this, printStatus);
|
||||||
|
status_timer.start(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DispatcherServer::~DispatcherServer() {
|
DispatcherServer::~DispatcherServer() {
|
||||||
eth->close();
|
eth.close();
|
||||||
piCoutObj << "server stoped";
|
piCoutObj << "server stoped";
|
||||||
delete eth;
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DispatcherServer::printStatus() {
|
||||||
|
map_mutex.lock();
|
||||||
|
piCout << PICoutManipulators::NewLine;
|
||||||
|
piCout << "Connections:";
|
||||||
|
for (auto c: clients) {
|
||||||
|
piCout << " " << c->address();
|
||||||
|
}
|
||||||
|
piCout << "Servers:";
|
||||||
|
auto it = c_servers.makeIterator();
|
||||||
|
while(it.next()){
|
||||||
|
piCout << " " << it.key();
|
||||||
|
}
|
||||||
|
map_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
||||||
piCoutObj << "remove client" << client;
|
piCoutObj << "remove client" << client;
|
||||||
|
map_mutex.lock();
|
||||||
clients.removeOne(client);
|
clients.removeOne(client);
|
||||||
|
map_mutex.unlock();
|
||||||
delete client;
|
delete client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void DispatcherServer::newConnection(PIEthernet *cl) {
|
void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||||
DispatcherClient * client = new DispatcherClient(cl);
|
DispatcherClient * client = new DispatcherClient(cl);
|
||||||
piCoutObj << "add client" << client;
|
|
||||||
CONNECTU(client, disconnectEvent, this, disconnectClient);
|
CONNECTU(client, disconnectEvent, this, disconnectClient);
|
||||||
|
CONNECTL(client, registerServer, [this](PIString sname, DispatcherClient * c){
|
||||||
|
map_mutex.lock();
|
||||||
|
c_servers.insert(sname, new CloudServer(c));
|
||||||
|
map_mutex.unlock();
|
||||||
|
});
|
||||||
|
piCoutObj << "add client" << client;
|
||||||
|
map_mutex.lock();
|
||||||
clients.push_back(client);
|
clients.push_back(client);
|
||||||
|
map_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define DISPATCHERSERVER_H
|
#define DISPATCHERSERVER_H
|
||||||
|
|
||||||
#include "dispatcherclient.h"
|
#include "dispatcherclient.h"
|
||||||
|
#include "cloudserver.h"
|
||||||
|
|
||||||
|
|
||||||
class DispatcherServer: public PIObject {
|
class DispatcherServer: public PIObject {
|
||||||
@@ -9,12 +10,17 @@ class DispatcherServer: public PIObject {
|
|||||||
public:
|
public:
|
||||||
DispatcherServer(PIEthernet::Address addr);
|
DispatcherServer(PIEthernet::Address addr);
|
||||||
~DispatcherServer();
|
~DispatcherServer();
|
||||||
|
EVENT_HANDLER0(void, printStatus);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
private:
|
PIEthernet eth;
|
||||||
PIEthernet * eth;
|
|
||||||
PIVector<DispatcherClient*> clients;
|
PIVector<DispatcherClient*> clients;
|
||||||
|
PIMap<PIString, CloudServer *> c_servers;
|
||||||
|
PITimer status_timer;
|
||||||
|
PIMutex map_mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DISPATCHERSERVER_H
|
#endif // DISPATCHERSERVER_H
|
||||||
|
|||||||
@@ -53,6 +53,9 @@ int main (int argc, char * argv[]) {
|
|||||||
if (cli.hasArgument("port"))
|
if (cli.hasArgument("port"))
|
||||||
addr.setPort(cli.argumentValue("port").toInt());
|
addr.setPort(cli.argumentValue("port").toInt());
|
||||||
DispatcherServer server(addr);
|
DispatcherServer server(addr);
|
||||||
|
PIKbdListener ls;
|
||||||
|
ls.enableExitCapture(PIKbdListener::F10);
|
||||||
|
ls.start();
|
||||||
WAIT_FOR_EXIT
|
WAIT_FOR_EXIT
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user