/* 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 . */ #include "picloudserver.h" PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() { PIString server_name = "PCS_" + PIString::fromNumber(randomi()%1000); tcp.setRole(PICloud::TCP::Server); tcp.setServerName(server_name); setName("cloud_server__" + server_name); CONNECTU(&streampacker, packetReceiveEvent, this, _readed); CONNECTL(ð, connected, [this](){tcp.sendStart();}); CONNECTL(ð, disconnected, [this](bool){ piCoutObj << "disconnected"; opened_ = false; piMSleep(100); }); } PICloudServer::~PICloudServer() { stop(); eth.stop(); for (auto & c : clients) { c->stop(); c->close(); } 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) { eth.startThreadedRead(); 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; } void PICloudServer::clientDisconnect(uint client_id) { tcp.sendDisconnected(client_id); } int PICloudServer::sendData(const PIByteArray & data, uint client_id) { return tcp.sendData(data, client_id); } PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), client_id(id) { } bool PICloudServer::Client::openDevice() { return true; } bool PICloudServer::Client::closeDevice() { server->clientDisconnect(client_id); return true; } int PICloudServer::Client::readDevice(void * read_to, int max_size) { //piCoutObj << "readDevice"; mutex_buff.lock(); cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();}); int sz = piMini(max_size, buff.size()); memcpy(read_to, buff.data(), sz); buff.remove(0, sz); mutex_buff.unlock(); return sz; } int PICloudServer::Client::writeDevice(const void * data, int max_size) { return server->sendData(PIByteArray(data, max_size), client_id); } void PICloudServer::Client::pushBuffer(const PIByteArray & ba) { mutex_buff.lock(); buff.append(ba); cond_buff.notifyOne(); mutex_buff.unlock(); while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); } void PICloudServer::_readed(PIByteArray & ba) { PIPair hdr = tcp.parseHeader(ba); if (hdr.second == tcp.role()) { switch (hdr.first) { case PICloud::TCP::Connect: { uint id = tcp.parseConnect(ba); Client * oc = index_clients.value(id, nullptr); if (oc) { tcp.sendDisconnected(id); } else { Client * c = new Client(this, id); clients << c; index_clients.insert(id, c); newConnection(c); } } break; case PICloud::TCP::Disconnect: { uint id = tcp.parseDisconnect(ba); Client * oc = index_clients.value(id, nullptr); if (oc) oc->close(); } break; case PICloud::TCP::Data: { PIPair d = tcp.parseDataServer(ba); Client * oc = index_clients.value(d.first, nullptr); if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second); } break; default: break; } } }