cloud test
This commit is contained in:
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