105 lines
2.6 KiB
C++
105 lines
2.6 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
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/>.
|
|
*/
|
|
|
|
#include "picloudserver.h"
|
|
|
|
|
|
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
|
|
PIString name = "PCS_" + PIString::fromNumber(randomi()%1000);
|
|
tcp.setRole(PICloud::TCP::Server);
|
|
tcp.setServerName(name);
|
|
setName("cloud_server__" + name);
|
|
}
|
|
|
|
|
|
PICloudServer::~PICloudServer() {
|
|
for (auto & c : clients) {
|
|
c.stop();
|
|
c.close();
|
|
}
|
|
stop();
|
|
close();
|
|
}
|
|
|
|
|
|
void PICloudServer::setServerName(const PIString & server_name) {
|
|
setName("cloud_server__" + server_name);
|
|
tcp.setServerName(server_name);
|
|
}
|
|
|
|
|
|
bool PICloudServer::openDevice() {
|
|
piCout << "PICloudServer open device" << path();
|
|
bool op = eth.connect(path(), false);
|
|
if (op) {
|
|
CONNECTU(ð, threadedReadEvent, this, readed);
|
|
CONNECTL(ð, connected, [this](){tcp.sendStart(ð);});
|
|
CONNECTL(ð, disconnected, [this](bool){
|
|
opened_ = false;
|
|
eth.close();
|
|
piCoutObj << "disconnected" << !isOpened() << isClosed();
|
|
});
|
|
eth.startThreadedRead();
|
|
tcp.sendStart(ð);
|
|
return true;
|
|
}
|
|
eth.close();
|
|
return false;
|
|
}
|
|
|
|
|
|
bool PICloudServer::closeDevice() {
|
|
eth.stop();
|
|
for (auto & c : clients) {
|
|
c.stop();
|
|
c.close();
|
|
}
|
|
eth.close();
|
|
return true;
|
|
}
|
|
|
|
|
|
int PICloudServer::readDevice(void * read_to, int max_size) {
|
|
return -1;
|
|
}
|
|
|
|
|
|
int PICloudServer::writeDevice(const void * data, int max_size) {
|
|
return -1;
|
|
}
|
|
|
|
|
|
PICloudServer::Client::Client(PICloudServer * srv) : server(srv) {
|
|
|
|
}
|
|
|
|
|
|
bool PICloudServer::Client::openDevice() {
|
|
return true;
|
|
}
|
|
|
|
|
|
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();
|
|
}
|
|
}
|