PICloud change protocol > server_uuid
This commit is contained in:
@@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.0)
|
||||
cmake_policy(SET CMP0017 NEW) # need include() with .cmake
|
||||
project(pip)
|
||||
set(pip_MAJOR 2)
|
||||
set(pip_MINOR 21)
|
||||
set(pip_MINOR 22)
|
||||
set(pip_REVISION 0)
|
||||
set(pip_SUFFIX alpha)
|
||||
set(pip_SUFFIX )
|
||||
set(pip_COMPANY SHS)
|
||||
set(pip_DOMAIN org.SHS)
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ const char hash_def_key[] = "_picrypt_";
|
||||
|
||||
|
||||
PICloud::TCP::Header::Header() {
|
||||
version = Version_1;
|
||||
version = Version_2;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,15 +42,20 @@ void PICloud::TCP::setRole(PICloud::TCP::Role r) {
|
||||
|
||||
|
||||
void PICloud::TCP::setServerName(const PIString & server_name) {
|
||||
sname = server_name;
|
||||
suuid = PICrypt::hash(server_name);
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::sendStart() {
|
||||
//piCout << "sendStart";
|
||||
if (suuid.size() != PICrypt::sizeHash()) {
|
||||
piCout << "PICloud ERROR, server not set, invoke setServerName first";
|
||||
return;
|
||||
}
|
||||
header.type = PICloud::TCP::Connect;
|
||||
PIByteArray ba;
|
||||
ba << header << sname;
|
||||
ba << header;
|
||||
ba.append(suuid);
|
||||
streampacker->send(ba);
|
||||
}
|
||||
|
||||
@@ -100,7 +105,7 @@ PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteA
|
||||
PICloud::TCP::Header hdr;
|
||||
ba >> hdr;
|
||||
if (hdr.version != header.version) {
|
||||
piCout << "[PICloud]" << "invalid TCP version!";
|
||||
piCout << "[PICloud]" << "invalid PICloud::TCP version!";
|
||||
return ret;
|
||||
}
|
||||
ret.first = (Type)hdr.type;
|
||||
@@ -128,10 +133,13 @@ PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
|
||||
}
|
||||
|
||||
|
||||
PIString PICloud::TCP::parseConnect_d(PIByteArray & ba) {
|
||||
PIString ret;
|
||||
ba >> ret;
|
||||
return ret;
|
||||
PIByteArray PICloud::TCP::parseConnect_d(PIByteArray & ba) {
|
||||
if (ba.size() != PICrypt::sizeHash()) {
|
||||
piCout << "PICloud ERROR, invalid server uuid";
|
||||
return PIByteArray();
|
||||
} else {
|
||||
return ba;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -169,6 +169,11 @@ PIByteArray PICrypt::hash(const PIByteArray & data) {
|
||||
}
|
||||
|
||||
|
||||
size_t PICrypt::sizeHash() {
|
||||
return crypto_generichash_BYTES;
|
||||
}
|
||||
|
||||
|
||||
ullong PICrypt::shorthash(const PIString& s, PIByteArray key) {
|
||||
ullong hash = 0;
|
||||
#ifdef PIP_CRYPT
|
||||
|
||||
@@ -38,6 +38,7 @@ class PIP_CLOUD_EXPORT TCP {
|
||||
public:
|
||||
enum Version {
|
||||
Version_1 = 1,
|
||||
Version_2 = 2,
|
||||
};
|
||||
|
||||
enum Role {
|
||||
@@ -66,7 +67,7 @@ public:
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
|
||||
PIByteArray parseData(PIByteArray & ba);
|
||||
PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
|
||||
PIString parseConnect_d(PIByteArray & ba);
|
||||
PIByteArray parseConnect_d(PIByteArray & ba);
|
||||
uint parseConnect(PIByteArray & ba);
|
||||
uint parseDisconnect(PIByteArray & ba);
|
||||
|
||||
@@ -79,7 +80,7 @@ private:
|
||||
};
|
||||
|
||||
Header header;
|
||||
PIString sname;
|
||||
PIByteArray suuid;
|
||||
PIStreamPacker * streampacker;
|
||||
};
|
||||
|
||||
|
||||
@@ -181,6 +181,12 @@ public:
|
||||
return true;
|
||||
}
|
||||
inline bool operator !=(const PIDeque<T> & t) const {return !(*this == t);}
|
||||
inline bool operator >(const PIDeque<T> & t) const {
|
||||
if (pid_size != t.pid_size) return pid_size > t.pid_size;
|
||||
for (size_t i = 0; i < pid_size; ++i)
|
||||
if (t[i] != (*this)[i]) return t[i] > (*this)[i];
|
||||
return false;
|
||||
}
|
||||
inline bool contains(const T & v) const {
|
||||
for (size_t i = pid_start; i < pid_start + pid_size; ++i)
|
||||
if (v == pid_data[i])
|
||||
|
||||
@@ -58,6 +58,9 @@ public:
|
||||
//! Generate hash from bytearray
|
||||
static PIByteArray hash(const PIByteArray & data);
|
||||
|
||||
//! Returns hash size
|
||||
static size_t sizeHash();
|
||||
|
||||
//! Generate short hash from string "s", may be used for hash table
|
||||
static ullong shorthash(const PIString & s, PIByteArray key = PIByteArray());
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "cloudserver.h"
|
||||
|
||||
CloudServer::CloudServer(DispatcherClient * c, const PIString & sname) : server(c) {
|
||||
setName(sname);
|
||||
server_name = sname;
|
||||
CloudServer::CloudServer(DispatcherClient * c, const PIByteArray & sname) : server(c) {
|
||||
setName(sname.toHex());
|
||||
server_uuid = sname;
|
||||
CONNECTL(c, dataReadedServer, ([this](uint id, PIByteArray & ba){
|
||||
DispatcherClient * cl = index_clients.value(id, nullptr);
|
||||
if (cl) cl->sendData(ba);
|
||||
@@ -17,8 +17,8 @@ CloudServer::~CloudServer() {
|
||||
}
|
||||
|
||||
|
||||
PIString CloudServer::serverName() const {
|
||||
return server_name;
|
||||
PIByteArray CloudServer::serverUUID() const {
|
||||
return server_uuid;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ PIVector<DispatcherClient *> CloudServer::getClients() {
|
||||
|
||||
|
||||
void CloudServer::printStatus() {
|
||||
piCout << " " << "Clients for" << server->address() << server_name << ":";
|
||||
piCout << " " << "Clients for" << server->address() << server_uuid.toHex() << ":";
|
||||
for (auto c: clients) {
|
||||
piCout << " " << c->address() << c->clientId();
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
class CloudServer : public PIObject {
|
||||
PIOBJECT(CloudServer)
|
||||
public:
|
||||
CloudServer(DispatcherClient * c, const PIString & sname);
|
||||
CloudServer(DispatcherClient * c, const PIByteArray & sname);
|
||||
~CloudServer();
|
||||
PIString serverName() const;
|
||||
PIByteArray serverUUID() const;
|
||||
void addClient(DispatcherClient * c);
|
||||
void removeClient(DispatcherClient * c);
|
||||
PIVector<DispatcherClient*> getClients();
|
||||
@@ -20,7 +20,7 @@ private:
|
||||
DispatcherClient * server;
|
||||
PIVector<DispatcherClient*> clients;
|
||||
PIMap<uint, DispatcherClient*> index_clients;
|
||||
PIString server_name;
|
||||
PIByteArray server_uuid;
|
||||
};
|
||||
|
||||
#endif // CLOUDSERVER_H
|
||||
|
||||
@@ -101,7 +101,7 @@ void DispatcherClient::readed(PIByteArray & ba) {
|
||||
switch (hdr.first) {
|
||||
case PICloud::TCP::Connect: {
|
||||
tcp.setRole(hdr.second);
|
||||
PIString sn = tcp.parseConnect_d(ba);
|
||||
PIByteArray sn = tcp.parseConnect_d(ba);
|
||||
if (hdr.second == PICloud::TCP::Server) registerServer(sn, this);
|
||||
if (hdr.second == PICloud::TCP::Client) registerClient(sn, this);
|
||||
return;}
|
||||
|
||||
@@ -23,8 +23,8 @@ public:
|
||||
PIString address();
|
||||
uint clientId() const {return client_id;}
|
||||
EVENT1(disconnectEvent, DispatcherClient *, client)
|
||||
EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
|
||||
EVENT2(registerClient, PIString, sname, DispatcherClient *, client)
|
||||
EVENT2(registerServer, const PIByteArray &, sname, DispatcherClient *, client)
|
||||
EVENT2(registerClient, const PIByteArray &, sname, DispatcherClient *, client)
|
||||
EVENT1(dataReaded, PIByteArray &, ba)
|
||||
EVENT2(dataReadedServer, uint, id, PIByteArray &, ba)
|
||||
|
||||
|
||||
@@ -70,12 +70,12 @@ void DispatcherServer::updateConnectionsTile(TileList * tl) {
|
||||
case PICloud::TCP::Client : {
|
||||
role = "Client";
|
||||
CloudServer * cs = index_c_clients.value(c, nullptr);
|
||||
if (cs) role += " \"" + cs->serverName() + "\"";
|
||||
if (cs) role += " \"" + cs->serverUUID().toHex().left(8) + "...\"";
|
||||
} break;
|
||||
case PICloud::TCP::Server : {
|
||||
role = "Server";
|
||||
CloudServer * cs = index_c_servers.value(c, nullptr);
|
||||
if (cs) role += " \"" + cs->serverName() + "\"";
|
||||
if (cs) role += " \"" + cs->serverUUID().toHex().left(8) + "...\"";
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
@@ -91,7 +91,7 @@ void DispatcherServer::updateServersTile(TileList * tl, PISet<const DispatcherCl
|
||||
tl->content.clear();
|
||||
auto mi = c_servers.makeIterator();
|
||||
while (mi.next()) {
|
||||
tl->content << TileList::Row(mi.value()->serverName() + " - " + PIString::fromNumber(mi.value()->getClients().size()), PIScreenTypes::CellFormat());
|
||||
tl->content << TileList::Row(mi.value()->serverUUID().toHex().left(8) + "... - " + PIString::fromNumber(mi.value()->getClients().size()), PIScreenTypes::CellFormat());
|
||||
if (servers.contains(mi.value()->getConnection())) tl->selected << (tl->content.size_s() - 1);
|
||||
}
|
||||
map_mutex.unlock();
|
||||
@@ -167,7 +167,7 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
||||
csc->close();
|
||||
csc->deleteLater();
|
||||
}
|
||||
c_servers.remove(cs->serverName());
|
||||
c_servers.remove(cs->serverUUID());
|
||||
index_c_servers.removeOne(client);
|
||||
delete cs;
|
||||
}
|
||||
@@ -185,13 +185,13 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
||||
void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||
DispatcherClient * client = new DispatcherClient(cl, client_gid++);
|
||||
CONNECTU(client, disconnectEvent, this, disconnectClient);
|
||||
CONNECTL(client, registerServer, [this](PIString sname, DispatcherClient * c){
|
||||
CONNECTL(client, registerServer, [this](const PIByteArray & sname, DispatcherClient * c){
|
||||
map_mutex.lock();
|
||||
CloudServer * cs = c_servers.value(sname, nullptr);
|
||||
if (cs) {
|
||||
rm_clients << c;
|
||||
} else {
|
||||
piCoutObj << "add new Server ->" << sname;
|
||||
piCoutObj << "add new Server ->" << sname.toHex();
|
||||
CloudServer * cs = new CloudServer(c, sname);
|
||||
c_servers.insert(sname, cs);
|
||||
index_c_servers.insert(c, cs);
|
||||
@@ -199,11 +199,11 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||
}
|
||||
map_mutex.unlock();
|
||||
});
|
||||
CONNECTL(client, registerClient, [this](PIString sname, DispatcherClient * c){
|
||||
CONNECTL(client, registerClient, [this](const PIByteArray & sname, DispatcherClient * c){
|
||||
map_mutex.lock();
|
||||
CloudServer * cs = c_servers.value(sname, nullptr);
|
||||
if (cs) {
|
||||
piCoutObj << "add new Client to Server ->" << sname;
|
||||
piCoutObj << "add new Client to Server ->" << sname.toHex();
|
||||
cs->addClient(c);
|
||||
index_c_clients.insert(c, cs);
|
||||
c->authorise(true);
|
||||
|
||||
@@ -28,7 +28,7 @@ private:
|
||||
|
||||
PIEthernet eth;
|
||||
PIVector<DispatcherClient*> clients;
|
||||
PIMap<PIString, CloudServer *> c_servers;
|
||||
PIMap<PIByteArray, CloudServer *> c_servers;
|
||||
PIMap<const DispatcherClient *, CloudServer *> index_c_servers;
|
||||
PIMap<const DispatcherClient *, CloudServer *> index_c_clients;
|
||||
PIVector<DispatcherClient*> rm_clients;
|
||||
|
||||
Reference in New Issue
Block a user