PICloud change protocol > server_uuid

This commit is contained in:
2021-04-12 11:35:34 +03:00
parent f75ce1e8e0
commit dbd2267a8c
12 changed files with 56 additions and 33 deletions

View File

@@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0017 NEW) # need include() with .cmake cmake_policy(SET CMP0017 NEW) # need include() with .cmake
project(pip) project(pip)
set(pip_MAJOR 2) set(pip_MAJOR 2)
set(pip_MINOR 21) set(pip_MINOR 22)
set(pip_REVISION 0) set(pip_REVISION 0)
set(pip_SUFFIX alpha) set(pip_SUFFIX )
set(pip_COMPANY SHS) set(pip_COMPANY SHS)
set(pip_DOMAIN org.SHS) set(pip_DOMAIN org.SHS)

View File

@@ -28,7 +28,7 @@ const char hash_def_key[] = "_picrypt_";
PICloud::TCP::Header::Header() { 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) { void PICloud::TCP::setServerName(const PIString & server_name) {
sname = server_name; suuid = PICrypt::hash(server_name);
} }
void PICloud::TCP::sendStart() { void PICloud::TCP::sendStart() {
//piCout << "sendStart"; //piCout << "sendStart";
if (suuid.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, server not set, invoke setServerName first";
return;
}
header.type = PICloud::TCP::Connect; header.type = PICloud::TCP::Connect;
PIByteArray ba; PIByteArray ba;
ba << header << sname; ba << header;
ba.append(suuid);
streampacker->send(ba); streampacker->send(ba);
} }
@@ -100,7 +105,7 @@ PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteA
PICloud::TCP::Header hdr; PICloud::TCP::Header hdr;
ba >> hdr; ba >> hdr;
if (hdr.version != header.version) { if (hdr.version != header.version) {
piCout << "[PICloud]" << "invalid TCP version!"; piCout << "[PICloud]" << "invalid PICloud::TCP version!";
return ret; return ret;
} }
ret.first = (Type)hdr.type; ret.first = (Type)hdr.type;
@@ -128,10 +133,13 @@ PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
} }
PIString PICloud::TCP::parseConnect_d(PIByteArray & ba) { PIByteArray PICloud::TCP::parseConnect_d(PIByteArray & ba) {
PIString ret; if (ba.size() != PICrypt::sizeHash()) {
ba >> ret; piCout << "PICloud ERROR, invalid server uuid";
return ret; return PIByteArray();
} else {
return ba;
}
} }

View File

@@ -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 PICrypt::shorthash(const PIString& s, PIByteArray key) {
ullong hash = 0; ullong hash = 0;
#ifdef PIP_CRYPT #ifdef PIP_CRYPT

View File

@@ -38,6 +38,7 @@ class PIP_CLOUD_EXPORT TCP {
public: public:
enum Version { enum Version {
Version_1 = 1, Version_1 = 1,
Version_2 = 2,
}; };
enum Role { enum Role {
@@ -66,7 +67,7 @@ public:
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba); PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
PIByteArray parseData(PIByteArray & ba); PIByteArray parseData(PIByteArray & ba);
PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba); PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
PIString parseConnect_d(PIByteArray & ba); PIByteArray parseConnect_d(PIByteArray & ba);
uint parseConnect(PIByteArray & ba); uint parseConnect(PIByteArray & ba);
uint parseDisconnect(PIByteArray & ba); uint parseDisconnect(PIByteArray & ba);
@@ -79,7 +80,7 @@ private:
}; };
Header header; Header header;
PIString sname; PIByteArray suuid;
PIStreamPacker * streampacker; PIStreamPacker * streampacker;
}; };

View File

@@ -181,6 +181,12 @@ public:
return true; return true;
} }
inline bool operator !=(const PIDeque<T> & t) const {return !(*this == t);} 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 { inline bool contains(const T & v) const {
for (size_t i = pid_start; i < pid_start + pid_size; ++i) for (size_t i = pid_start; i < pid_start + pid_size; ++i)
if (v == pid_data[i]) if (v == pid_data[i])

View File

@@ -58,6 +58,9 @@ public:
//! Generate hash from bytearray //! Generate hash from bytearray
static PIByteArray hash(const PIByteArray & data); 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 //! Generate short hash from string "s", may be used for hash table
static ullong shorthash(const PIString & s, PIByteArray key = PIByteArray()); static ullong shorthash(const PIString & s, PIByteArray key = PIByteArray());

View File

@@ -1,8 +1,8 @@
#include "cloudserver.h" #include "cloudserver.h"
CloudServer::CloudServer(DispatcherClient * c, const PIString & sname) : server(c) { CloudServer::CloudServer(DispatcherClient * c, const PIByteArray & sname) : server(c) {
setName(sname); setName(sname.toHex());
server_name = sname; server_uuid = sname;
CONNECTL(c, dataReadedServer, ([this](uint id, PIByteArray & ba){ CONNECTL(c, dataReadedServer, ([this](uint id, PIByteArray & ba){
DispatcherClient * cl = index_clients.value(id, nullptr); DispatcherClient * cl = index_clients.value(id, nullptr);
if (cl) cl->sendData(ba); if (cl) cl->sendData(ba);
@@ -17,8 +17,8 @@ CloudServer::~CloudServer() {
} }
PIString CloudServer::serverName() const { PIByteArray CloudServer::serverUUID() const {
return server_name; return server_uuid;
} }
@@ -49,7 +49,7 @@ PIVector<DispatcherClient *> CloudServer::getClients() {
void CloudServer::printStatus() { void CloudServer::printStatus() {
piCout << " " << "Clients for" << server->address() << server_name << ":"; piCout << " " << "Clients for" << server->address() << server_uuid.toHex() << ":";
for (auto c: clients) { for (auto c: clients) {
piCout << " " << c->address() << c->clientId(); piCout << " " << c->address() << c->clientId();
} }

View File

@@ -7,9 +7,9 @@
class CloudServer : public PIObject { class CloudServer : public PIObject {
PIOBJECT(CloudServer) PIOBJECT(CloudServer)
public: public:
CloudServer(DispatcherClient * c, const PIString & sname); CloudServer(DispatcherClient * c, const PIByteArray & sname);
~CloudServer(); ~CloudServer();
PIString serverName() const; PIByteArray serverUUID() const;
void addClient(DispatcherClient * c); void addClient(DispatcherClient * c);
void removeClient(DispatcherClient * c); void removeClient(DispatcherClient * c);
PIVector<DispatcherClient*> getClients(); PIVector<DispatcherClient*> getClients();
@@ -20,7 +20,7 @@ private:
DispatcherClient * server; DispatcherClient * server;
PIVector<DispatcherClient*> clients; PIVector<DispatcherClient*> clients;
PIMap<uint, DispatcherClient*> index_clients; PIMap<uint, DispatcherClient*> index_clients;
PIString server_name; PIByteArray server_uuid;
}; };
#endif // CLOUDSERVER_H #endif // CLOUDSERVER_H

View File

@@ -101,7 +101,7 @@ void DispatcherClient::readed(PIByteArray & ba) {
switch (hdr.first) { switch (hdr.first) {
case PICloud::TCP::Connect: { case PICloud::TCP::Connect: {
tcp.setRole(hdr.second); 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::Server) registerServer(sn, this);
if (hdr.second == PICloud::TCP::Client) registerClient(sn, this); if (hdr.second == PICloud::TCP::Client) registerClient(sn, this);
return;} return;}

View File

@@ -23,8 +23,8 @@ public:
PIString address(); PIString address();
uint clientId() const {return client_id;} uint clientId() const {return client_id;}
EVENT1(disconnectEvent, DispatcherClient *, client) EVENT1(disconnectEvent, DispatcherClient *, client)
EVENT2(registerServer, PIString, sname, DispatcherClient *, client) EVENT2(registerServer, const PIByteArray &, sname, DispatcherClient *, client)
EVENT2(registerClient, PIString, sname, DispatcherClient *, client) EVENT2(registerClient, const PIByteArray &, sname, DispatcherClient *, client)
EVENT1(dataReaded, PIByteArray &, ba) EVENT1(dataReaded, PIByteArray &, ba)
EVENT2(dataReadedServer, uint, id, PIByteArray &, ba) EVENT2(dataReadedServer, uint, id, PIByteArray &, ba)

View File

@@ -70,12 +70,12 @@ void DispatcherServer::updateConnectionsTile(TileList * tl) {
case PICloud::TCP::Client : { case PICloud::TCP::Client : {
role = "Client"; role = "Client";
CloudServer * cs = index_c_clients.value(c, nullptr); CloudServer * cs = index_c_clients.value(c, nullptr);
if (cs) role += " \"" + cs->serverName() + "\""; if (cs) role += " \"" + cs->serverUUID().toHex().left(8) + "...\"";
} break; } break;
case PICloud::TCP::Server : { case PICloud::TCP::Server : {
role = "Server"; role = "Server";
CloudServer * cs = index_c_servers.value(c, nullptr); CloudServer * cs = index_c_servers.value(c, nullptr);
if (cs) role += " \"" + cs->serverName() + "\""; if (cs) role += " \"" + cs->serverUUID().toHex().left(8) + "...\"";
} break; } break;
default: default:
break; break;
@@ -91,7 +91,7 @@ void DispatcherServer::updateServersTile(TileList * tl, PISet<const DispatcherCl
tl->content.clear(); tl->content.clear();
auto mi = c_servers.makeIterator(); auto mi = c_servers.makeIterator();
while (mi.next()) { 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); if (servers.contains(mi.value()->getConnection())) tl->selected << (tl->content.size_s() - 1);
} }
map_mutex.unlock(); map_mutex.unlock();
@@ -167,7 +167,7 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
csc->close(); csc->close();
csc->deleteLater(); csc->deleteLater();
} }
c_servers.remove(cs->serverName()); c_servers.remove(cs->serverUUID());
index_c_servers.removeOne(client); index_c_servers.removeOne(client);
delete cs; delete cs;
} }
@@ -185,13 +185,13 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
void DispatcherServer::newConnection(PIEthernet *cl) { void DispatcherServer::newConnection(PIEthernet *cl) {
DispatcherClient * client = new DispatcherClient(cl, client_gid++); DispatcherClient * client = new DispatcherClient(cl, client_gid++);
CONNECTU(client, disconnectEvent, this, disconnectClient); 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(); map_mutex.lock();
CloudServer * cs = c_servers.value(sname, nullptr); CloudServer * cs = c_servers.value(sname, nullptr);
if (cs) { if (cs) {
rm_clients << c; rm_clients << c;
} else { } else {
piCoutObj << "add new Server ->" << sname; piCoutObj << "add new Server ->" << sname.toHex();
CloudServer * cs = new CloudServer(c, sname); CloudServer * cs = new CloudServer(c, sname);
c_servers.insert(sname, cs); c_servers.insert(sname, cs);
index_c_servers.insert(c, cs); index_c_servers.insert(c, cs);
@@ -199,11 +199,11 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
} }
map_mutex.unlock(); map_mutex.unlock();
}); });
CONNECTL(client, registerClient, [this](PIString sname, DispatcherClient * c){ CONNECTL(client, registerClient, [this](const PIByteArray & sname, DispatcherClient * c){
map_mutex.lock(); map_mutex.lock();
CloudServer * cs = c_servers.value(sname, nullptr); CloudServer * cs = c_servers.value(sname, nullptr);
if (cs) { if (cs) {
piCoutObj << "add new Client to Server ->" << sname; piCoutObj << "add new Client to Server ->" << sname.toHex();
cs->addClient(c); cs->addClient(c);
index_c_clients.insert(c, cs); index_c_clients.insert(c, cs);
c->authorise(true); c->authorise(true);

View File

@@ -28,7 +28,7 @@ private:
PIEthernet eth; PIEthernet eth;
PIVector<DispatcherClient*> clients; 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_servers;
PIMap<const DispatcherClient *, CloudServer *> index_c_clients; PIMap<const DispatcherClient *, CloudServer *> index_c_clients;
PIVector<DispatcherClient*> rm_clients; PIVector<DispatcherClient*> rm_clients;