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