Compare commits

20 Commits

Author SHA1 Message Date
eb21c85170 version 3.16.1 2024-03-31 20:38:13 +03:00
fb68a9f9fe another segv on cloud 2024-03-31 20:31:35 +03:00
c7c3852747 cloud! finally fixed bug! 2024-03-25 22:40:29 +03:00
c18d0c918e 4 2024-03-25 21:45:59 +03:00
9c4fd41eef 3 2024-03-25 21:37:05 +03:00
9173dc936d 2 2024-03-25 21:34:30 +03:00
77c8681b43 1 2024-03-25 21:31:41 +03:00
2db9440a38 fix cloud ... :-/ 2024-03-25 21:24:29 +03:00
0d585cfebf cloud fix ... (( 2024-03-25 21:17:05 +03:00
02a9bfb76f another try fix 2024-03-22 22:29:14 +03:00
ad7385c21f more couts 2024-03-22 21:40:04 +03:00
dd6d91ac1d cloud fixes ... 2024-03-18 10:45:57 +03:00
576d9c79be Merge branch 'master' of https://git.shstk.ru/SHS/pip 2024-03-15 19:49:46 +03:00
3fa5d9d9df cloud inspecting ... 2024-03-15 19:49:37 +03:00
b14d30108a Optimization removeAll and removeWhere in PIVector and PIDeque (#180)
Reviewed-on: #180
Co-authored-by: Andrey Bychkov <andrey@signalmodelling.ru>
Co-committed-by: Andrey Bychkov <andrey@signalmodelling.ru>
2024-03-13 10:43:02 +03:00
2b738f6f43 more safety cloud_dispatcher 2024-03-10 21:22:05 +03:00
263fa18726 add warnings 2024-03-05 19:38:53 +03:00
f47bc411bc version 3.16.0
new PISystemTime::Frequency type
2024-03-05 17:55:25 +03:00
154fb7d9fd pip_vtt support special characters 2024-02-29 13:03:38 +03:00
9f09af9f27 doc to shstk.ru 2024-02-28 10:49:29 +03:00
18 changed files with 403 additions and 77 deletions

View File

@@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0017 NEW) # need include() with .cmake
project(PIP)
set(PIP_MAJOR 3)
set(PIP_MINOR 15)
set(PIP_REVISION 2)
set(PIP_MINOR 16)
set(PIP_REVISION 1)
set(PIP_SUFFIX )
set(PIP_COMPANY SHS)
set(PIP_DOMAIN org.SHS)

View File

@@ -33,10 +33,10 @@ You should add ${<out_var>} to your target.
## Documentation
[🇺🇸 Online documentation](https://shs.tools/pip/html/en/index.html)
[🇺🇸 Online documentation](https://shstk.ru/pip/html/en/index.html)
[🇺🇸 Qt-help](https://shs.tools/pip/pip_en.qch)
[🇺🇸 Qt-help](https://shstk.ru/pip/pip_en.qch)
[🇷🇺 Онлайн документация](https://shs.tools/pip/html/ru/index.html)
[🇷🇺 Онлайн документация](https://shstk.ru/pip/html/ru/index.html)
[🇷🇺 Qt-help](https://shs.tools/pip/pip_ru.qch)
[🇷🇺 Qt-help](https://shstk.ru/pip/pip_ru.qch)

View File

@@ -9,8 +9,14 @@ struct S {
};
// Operators
PIByteArray & operator <<(PIByteArray & b, const S & s) {b << s.i << s.f << s.s; return b;}
PIByteArray & operator >>(PIByteArray & b, S & s) {b >> s.i >> s.f >> s.s; return b;}
PIByteArray & operator<<(PIByteArray & b, const S & s) {
b << s.i << s.f << s.s;
return b;
}
PIByteArray & operator>>(PIByteArray & b, S & s) {
b >> s.i >> s.f >> s.s;
return b;
}
//! [struct]
//! [write]
// Write chunk stream
@@ -22,10 +28,7 @@ PIVector<float> f;
f << -1. << 2.5 << 11.;
// write some data to empty stream
PIChunkStream cs;
cs << cs.chunk(1, int(10))
<< cs.chunk(2, PIString("text"))
<< cs.chunk(4, f)
<< cs.chunk(3, s);
cs << cs.chunk(1, int(10)) << cs.chunk(2, PIString("text")) << cs.chunk(4, f) << cs.chunk(3, s);
// now you can take cs.data() and send or place it somewhere ...
//! [write]
//! [read]
@@ -42,14 +45,14 @@ while (!cs2.atEnd()) {
case 1: i = cs2.getData<int>(); break;
case 2: str = cs2.getData<PIString>(); break;
case 3: s = cs2.getData<S>(); break;
case 4: f = cs2.getData<PIVector<float> >(); break;
case 4: f = cs2.getData<PIVector<float>>(); break;
}
}
piCout << i << str << f << s.i << s.f << s.s;
//! [read]
//! [write_new]
PIByteArray & operator <<(PIByteArray & s, const S & value) {
PIByteArray & operator<<(PIByteArray & s, const S & value) {
PIChunkStream cs;
cs.add(1, value.i).add(2, value.f).add(3, value.s);
s << cs.data();
@@ -57,11 +60,11 @@ PIByteArray & operator <<(PIByteArray & s, const S & value) {
}
//! [write_new]
//! [read_new]
PIByteArray & operator >>(PIByteArray & s, S & value) {
PIByteArray & operator>>(PIByteArray & s, S & value) {
PIChunkStream cs;
if (!cs.extract(s)) return s;
cs.readAll();
cs.get(1, value.i).get(2, value.f).get(3, value.s);
return b;
return s;
}
//! [read_new]

View File

@@ -1630,11 +1630,14 @@ public:
//! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIDeque<T> & removeAll(const T & e) {
for (size_t i = 0; i < pid_size; ++i) {
if (pid_data[i + pid_start] == e) {
remove(i);
--i;
ssize_t j = indexOf(e);
if (j != -1) {
for (size_t i = j + 1; i < pid_size; ++i) {
if (pid_data[i + pid_start] != e) {
pid_data[j++ + pid_start] = std::move(pid_data[i + pid_start]);
}
}
remove(j, pid_size - j);
}
return *this;
}
@@ -1651,11 +1654,14 @@ public:
//! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIDeque<T> & removeWhere(std::function<bool(const T & e)> test) {
for (size_t i = 0; i < pid_size; ++i) {
if (test(pid_data[i + pid_start])) {
remove(i);
--i;
ssize_t j = indexWhere(test);
if (j != -1) {
for (size_t i = j + 1; i < pid_size; ++i) {
if (!test(pid_data[i + pid_start])) {
pid_data[j++ + pid_start] = std::move(pid_data[i + pid_start]);
}
}
remove(j, pid_size - j);
}
return *this;
}

View File

@@ -1555,11 +1555,14 @@ public:
//! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIVector<T> & removeAll(const T & e) {
for (ssize_t i = 0; i < size_s(); ++i) {
if (piv_data[i] == e) {
remove(i);
--i;
ssize_t j = indexOf(e);
if (j != -1) {
for (size_t i = j + 1; i < piv_size; ++i) {
if (piv_data[i] != e) {
piv_data[j++] = std::move(piv_data[i]);
}
}
remove(j, piv_size - j);
}
return *this;
}
@@ -1576,11 +1579,14 @@ public:
//! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIVector<T> & removeWhere(std::function<bool(const T & e)> test) {
for (ssize_t i = 0; i < size_s(); ++i) {
if (test(piv_data[i])) {
remove(i);
--i;
ssize_t j = indexWhere(test);
if (j != -1) {
for (size_t i = j + 1; i < piv_size; ++i) {
if (!test(piv_data[i])) {
piv_data[j++] = std::move(piv_data[i]);
}
}
remove(j, piv_size - j);
}
return *this;
}

View File

@@ -312,8 +312,9 @@ bool PIEthernet::closeDevice() {
if (sock_s == sock) sock_s = -1;
closeSocket(sock);
closeSocket(sock_s);
while (!clients_.isEmpty())
delete clients_.back();
clients_mutex.lock();
piDeleteAllAndClear(clients_);
clients_mutex.unlock();
if (ned) {
// piCoutObj << "Disconnect on close";
disconnected(false);
@@ -529,6 +530,21 @@ bool PIEthernet::listen(const PINetworkAddress & addr, bool threaded) {
return listen(threaded);
}
PIEthernet * PIEthernet::client(int index) {
PIMutexLocker locker(clients_mutex);
return clients_[index];
}
int PIEthernet::clientsCount() const {
PIMutexLocker locker(clients_mutex);
return clients_.size_s();
}
PIVector<PIEthernet *> PIEthernet::clients() const {
PIMutexLocker locker(clients_mutex);
return clients_;
}
bool PIEthernet::send(const void * data, int size, bool threaded) {
if (threaded) {
@@ -823,9 +839,8 @@ void PIEthernet::applyParameters() {
void PIEthernet::clientDeleted(PIObject * o) {
clients_mutex.lock();
PIMutexLocker locker(clients_mutex);
clients_.removeOne((PIEthernet *)o);
clients_mutex.unlock();
}

View File

@@ -251,9 +251,9 @@ public:
//! Start listen for incoming TCP connections on address "addr". Use only for TCP_Server
bool listen(const PINetworkAddress & addr, bool threaded = false);
PIEthernet * client(int index) { return clients_[index]; }
int clientsCount() const { return clients_.size_s(); }
PIVector<PIEthernet *> clients() const { return clients_; }
PIEthernet * client(int index);
int clientsCount() const;
PIVector<PIEthernet *> clients() const;
//! Send data "data" with size "size" to address \a sendAddress() for UDP or \a readAddress() for TCP_Client
@@ -484,7 +484,7 @@ protected:
mutable PINetworkAddress addr_r, addr_s, addr_lr;
Type eth_type;
PIThread server_thread_;
PIMutex clients_mutex;
mutable PIMutex clients_mutex;
PIVector<PIEthernet *> clients_;
PIQueue<PIString> mcast_queue;
PIStringList mcast_groups;

View File

@@ -82,4 +82,64 @@ inline PISystemTime operator""_ns(unsigned long long v) {
}
//! \~\brief
//! \~english PISystemTime::Frequency from hertz
//! \~russian PISystemTime::Frequency из герц
inline PISystemTime::Frequency operator""_Hz(long double v) {
return PISystemTime::Frequency::fromHertz(v);
}
//! \~\brief
//! \~english PISystemTime::Frequency from hertz
//! \~russian PISystemTime::Frequency из герц
inline PISystemTime::Frequency operator""_Hz(unsigned long long v) {
return PISystemTime::Frequency::fromHertz(v);
}
//! \~\brief
//! \~english PISystemTime::Frequency from kilohertz
//! \~russian PISystemTime::Frequency из килогерц
inline PISystemTime::Frequency operator""_KHz(long double v) {
return PISystemTime::Frequency::fromKHertz(v);
}
//! \~\brief
//! \~english PISystemTime::Frequency from kilohertz
//! \~russian PISystemTime::Frequency из килогерц
inline PISystemTime::Frequency operator""_KHz(unsigned long long v) {
return PISystemTime::Frequency::fromKHertz(v);
}
//! \~\brief
//! \~english PISystemTime::Frequency from megahertz
//! \~russian PISystemTime::Frequency из мегагерц
inline PISystemTime::Frequency operator""_MHz(long double v) {
return PISystemTime::Frequency::fromMHertz(v);
}
//! \~\brief
//! \~english PISystemTime::Frequency from megahertz
//! \~russian PISystemTime::Frequency из мегагерц
inline PISystemTime::Frequency operator""_MHz(unsigned long long v) {
return PISystemTime::Frequency::fromMHertz(v);
}
//! \~\brief
//! \~english PISystemTime::Frequency from gigahertz
//! \~russian PISystemTime::Frequency из гигагерц
inline PISystemTime::Frequency operator""_GHz(long double v) {
return PISystemTime::Frequency::fromGHertz(v);
}
//! \~\brief
//! \~english PISystemTime::Frequency from gigahertz
//! \~russian PISystemTime::Frequency из гигагерц
inline PISystemTime::Frequency operator""_GHz(unsigned long long v) {
return PISystemTime::Frequency::fromGHertz(v);
}
#endif

View File

@@ -220,3 +220,23 @@ double PITimeMeasurer::elapsed_s() const {
PISystemTime PITimeMeasurer::elapsed() const {
return (PISystemTime::current(true) - t_st);
}
// PISystemTime::Frequency
PISystemTime PISystemTime::Frequency::toSystemTime() const {
if (value_hz <= 0.) {
piCout << "[PISystemTime::Frequency] toSystemTime() warning: invalid hertz" << value_hz;
return PISystemTime();
}
return PISystemTime::fromSeconds(1. / value_hz);
}
PISystemTime::Frequency PISystemTime::Frequency::fromSystemTime(const PISystemTime & st) {
if (st == PISystemTime()) {
piCout << "[PISystemTime::Frequency] fromSystemTime() warning: null frequency";
return Frequency();
}
return Frequency(1. / st.toSeconds());
}

View File

@@ -48,6 +48,143 @@ public:
checkOverflows();
}
//! \~english Contructs time with "s" seconds and "ns" nanoseconds
//! \~russian Создает время с секундами "s" и наносекундами "ns"
PISystemTime(std::pair<int, int> s_ns) {
seconds = s_ns.first;
nanoseconds = s_ns.second;
checkOverflows();
}
//! \ingroup Types
//! \~\brief
//! \~english Frequency type.
//! \~russian Тип частоты.
class PIP_EXPORT Frequency {
public:
//! \~english Contructs null frequency
//! \~russian Создает нулевую частоту
Frequency() {}
//! \~english Returns frequency as hertz
//! \~russian Возвращает частоту в герцах
double toHertz() const { return value_hz; }
//! \~english Returns frequency as kilohertz
//! \~russian Возвращает частоту в килогерцах
double toKHertz() const { return value_hz / 1E+3; }
//! \~english Returns frequency as megahertz
//! \~russian Возвращает частоту в мегагерцах
double toMHertz() const { return value_hz / 1E+6; }
//! \~english Returns frequency as gigahertz
//! \~russian Возвращает частоту в гигагерцах
double toGHertz() const { return value_hz / 1E+9; }
//! \~english Returns frequency as \a PISystemTime time interval
//! \~russian Возвращает частоту как \a PISystemTime временной интервал
PISystemTime toSystemTime() const;
//! \~english Returns frequency as \a PISystemTime time interval
//! \~russian Возвращает частоту как \a PISystemTime временной интервал
operator PISystemTime() const { return toSystemTime(); }
//! \~english Returns \a Frequency contains "hz" hertz
//! \~russian Возвращает \a Frequency содержащую "hz" герц
static Frequency fromHertz(double hz) { return Frequency(hz); }
//! \~english Returns \a Frequency contains "khz" kilohertz
//! \~russian Возвращает \a Frequency содержащую "khz" килогерц
static Frequency fromKHertz(double khz) { return Frequency(khz * 1E+3); }
//! \~english Returns \a Frequency contains "mhz" megahertz
//! \~russian Возвращает \a Frequency содержащую "mhz" мегагерц
static Frequency fromMHertz(double mhz) { return Frequency(mhz * 1E+6); }
//! \~english Returns \a Frequency contains "ghz" gigahertz
//! \~russian Возвращает \a Frequency содержащую "ghz" гигагерц
static Frequency fromGHertz(double ghz) { return Frequency(ghz * 1E+9); }
//! \~english Returns \a Frequency that corresponds "st" time interval
//! \~russian Возвращает \a Frequency соответствующую временному интервалу "st"
static Frequency fromSystemTime(const PISystemTime & st);
//! \~english Returns sum between this frequency and "f"
//! \~russian Возвращает сумму этой частоты с "f"
Frequency operator+(const Frequency & f) const { return Frequency(value_hz + f.value_hz); }
//! \~english Returns difference between this frequency and "f"
//! \~russian Возвращает разницу этой частоты с "f"
Frequency operator-(const Frequency & f) const { return Frequency(value_hz - f.value_hz); }
//! \~english Returns multiplication between this frequency and "v"
//! \~russian Возвращает эту частоту умноженную на "v"
Frequency operator*(const double & v) const { return Frequency(value_hz * v); }
//! \~english Returns division between this frequency and "v"
//! \~russian Возвращает эту частоту поделённую на "v"
Frequency operator/(const double & v) const { return Frequency(value_hz / v); }
//! \~english Multiply frequency by "v"
//! \~russian Умножает частоту на "v"
Frequency & operator*=(const double & v) {
value_hz *= v;
return *this;
}
//! \~english Divide frequency by "v"
//! \~russian Делит частоту на "v"
Frequency & operator/=(const double & v) {
value_hz /= v;
return *this;
}
//! \~english Add to frequency "f"
//! \~russian Добавляет к частоте "f"
Frequency & operator+=(const Frequency & f) {
value_hz += f.value_hz;
return *this;
}
//! \~english Subtract from frequency "f"
//! \~russian Вычитает из частоты "f"
Frequency & operator-=(const Frequency & f) {
value_hz -= f.value_hz;
return *this;
}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator==(const Frequency & f) const { return value_hz == f.value_hz; }
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator!=(const Frequency & f) const { return value_hz != f.value_hz; }
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator>(const Frequency & f) const { return value_hz > f.value_hz; }
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator<(const Frequency & f) const { return value_hz < f.value_hz; }
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator>=(const Frequency & f) const { return value_hz >= f.value_hz; }
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator<=(const Frequency & f) const { return value_hz <= f.value_hz; }
private:
Frequency(double hz): value_hz(hz) {}
double value_hz = 0.;
};
//! \~english Returns time value in seconds
//! \~russian Возвращает значение времени в секундах
@@ -128,11 +265,11 @@ public:
}
//! \~english Returns multiplication between this time and "t"
//! \~russian Возвращает это временя умноженное на "t"
//! \~russian Возвращает это время умноженное на "t"
PISystemTime operator*(const double & v) const { return fromMilliseconds(toMilliseconds() * v); }
//! \~english Returns division between this time and "t"
//! \~russian Возвращает это временя поделённое на "t"
//! \~russian Возвращает это время поделённое на "t"
PISystemTime operator/(const double & v) const { return fromMilliseconds(toMilliseconds() / v); }
//! \~english Add to time "t"
@@ -269,6 +406,17 @@ inline PICout operator<<(PICout s, const PISystemTime & v) {
return s;
}
//! \relatesalso PICout
//! \~english \brief Output operator to PICout
//! \~russian \brief Оператор вывода в PICout
inline PICout operator<<(PICout s, const PISystemTime::Frequency & f) {
s.space();
s.saveAndSetControls(0);
s << f.toHertz() << " Hz";
s.restoreControls();
return s;
}
//! \ingroup Types
//! \~\brief

View File

@@ -2,6 +2,7 @@
#include "picodeparser.h"
#include "piiostream.h"
#include "pijson.h"
#include "piliterals_time.h"
#include "pimathbase.h"
#include "pip.h"
#include "pivaluetree.h"
@@ -10,7 +11,14 @@ using namespace PICoutManipulators;
int main(int argc, char * argv[]) {
PICodeParser cp;
cp.parseFile("kmm_types.h", false);
// PICodeParser cp;
// cp.parseFile("kmm_types.h", false);
piCout << (1_Hz * 100).toSystemTime().toSeconds();
piCout << (1_Hz * 100);
piCout << (1_Hz + 10_GHz);
piCout << (100_Hz + 1_KHz - 10_Hz);
piCout << PISystemTime::Frequency::fromSystemTime({0, 200000000});
piCout << PISystemTime::Frequency::fromSystemTime(25_ms);
piCout << PISystemTime::Frequency::fromSystemTime(25_ms).toSystemTime().toMilliseconds();
return 0;
}

View File

@@ -3,23 +3,19 @@
CloudServer::CloudServer(DispatcherClient * c, const PIByteArray & sname): server(c) {
setName(sname.toHex());
server_uuid = sname;
CONNECTL(c, dataReadedServer, ([this](uint id, PIByteArray & ba) {
last_ping.reset();
mutex_clients.lock();
DispatcherClient * cl = index_clients.value(id, nullptr);
mutex_clients.unlock();
if (cl) cl->sendData(ba);
}));
CONNECTL(c, pingReceived, [this]() { last_ping.reset(); });
connects << CONNECTL(c, dataReadedServer, ([this](uint id, PIByteArray & ba) {
last_ping.reset();
mutex_clients.lock();
DispatcherClient * cl = index_clients.value(id, nullptr);
if (cl) cl->sendData(ba);
mutex_clients.unlock();
}));
connects << CONNECTL(c, pingReceived, [this]() { last_ping.reset(); });
last_ping.reset();
}
CloudServer::~CloudServer() {
for (auto c: clients) {
c->close();
}
}
CloudServer::~CloudServer() {}
PIByteArray CloudServer::serverUUID() const {
@@ -54,9 +50,8 @@ void CloudServer::removeClient(DispatcherClient * c) {
PIVector<DispatcherClient *> CloudServer::getClients() {
mutex_clients.lock();
PIMutexLocker locker(mutex_clients);
PIVector<DispatcherClient *> cl = clients;
mutex_clients.unlock();
return cl;
}
@@ -66,12 +61,23 @@ double CloudServer::lastPing() {
}
void CloudServer::close() {
server->close();
}
void CloudServer::stop() {
for (auto c: clients)
c->close();
server->stop();
}
void CloudServer::printStatus() {
mutex_clients.lock();
PIMutexLocker locker(mutex_clients);
piCout << " "
<< "Clients for" << server->address() << server_uuid.toHex() << ":";
for (auto c: clients) {
piCout << " " << c->address() << c->clientId();
}
mutex_clients.unlock();
}

View File

@@ -17,8 +17,11 @@ public:
EVENT_HANDLER0(void, printStatus);
const DispatcherClient * getConnection() const { return server; }
double lastPing();
void close();
void stop();
private:
PIVector<PIObject::Connection> connects;
DispatcherClient * server;
PIVector<DispatcherClient *> clients;
PIMap<uint, DispatcherClient *> index_clients;

View File

@@ -22,7 +22,7 @@ void DispatcherClient::start() {
DispatcherClient::~DispatcherClient() {
delete eth;
piDeleteSafety(eth);
}
@@ -36,6 +36,11 @@ void DispatcherClient::close() {
}
void DispatcherClient::stop() {
eth->stop();
}
void DispatcherClient::sendConnected(uint client_id) {
piCoutObj << "sendConnected" << client_id;
tcp.sendConnected(client_id);

View File

@@ -14,6 +14,7 @@ public:
~DispatcherClient();
void start();
void close();
void stop();
void sendConnected(uint client_id);
void sendDisconnected(uint client_id);
void sendData(const PIByteArray & data);

View File

@@ -45,7 +45,16 @@ void DispatcherServer::picoutStatus() {
void DispatcherServer::cleanClients() {
PIMutexLocker locker(map_mutex);
piDeleteAllAndClear(rmrf_clients);
for (auto s: rmrf_servers)
s->close();
piDeleteAllAndClear(rmrf_servers);
for (auto c: rmrf_clients) {
if (!c->isPIObject())
piCout << "ACHTUNG! Non-piobject client!";
else
delete c;
}
rmrf_clients.clear();
for (auto c: clients) {
if (!index_c_servers.contains(c) && !index_c_clients.contains(c)) {
if (!rm_clients.contains(c)) rm_clients << c;
@@ -56,23 +65,28 @@ void DispatcherServer::cleanClients() {
for (auto c: ss) {
if (c->lastPing() > 15.0) {
piCout << "remove Server by ping timeout" << c->getConnection()->clientId();
c->close();
PIVector<DispatcherClient *> cscv = c->getClients();
for (auto csc: cscv) {
if (!csc->isPIObject()) {
piCout << "ACHTUNG! Non-piobject DispatcherClient!";
continue;
}
clients.removeAll(csc);
index_c_clients.remove(csc);
c->removeClient(csc);
csc->close();
rmrf_clients << csc;
if (!rmrf_clients.contains(csc)) rmrf_clients << csc;
}
c_servers.remove(c->serverUUID());
index_c_servers.remove(c->getConnection());
rmrf_clients << const_cast<DispatcherClient *>(c->getConnection());
delete c;
rmrf_servers << c;
}
}
for (auto c: rm_clients) {
if (clients.contains(c)) {
rmrf_clients << c;
if (!rmrf_clients.contains(c)) rmrf_clients << c;
}
}
for (auto c: rmrf_clients) {
@@ -180,28 +194,27 @@ void DispatcherServer::setMaxConnections(uint max_count) {
void DispatcherServer::disconnectClient(DispatcherClient * client) {
PIMutexLocker locker(map_mutex);
if (!clients.contains(client)) {
// piCoutObj << "INVALID client" << client;
return;
}
piCoutObj << "remove ..." << client->clientId();
PIMutexLocker locker(map_mutex);
clients.removeAll(client);
rm_clients.removeAll(client);
CloudServer * cs = index_c_servers.value(client, nullptr);
if (cs) {
piCoutObj << "remove Server" << client->clientId();
cs->stop();
PIVector<DispatcherClient *> cscv = cs->getClients();
for (auto csc: cscv) {
clients.removeAll(csc);
index_c_clients.remove(csc);
cs->removeClient(csc);
csc->close();
rmrf_clients << csc;
if (!rmrf_clients.contains(csc)) rmrf_clients << csc;
}
c_servers.remove(cs->serverUUID());
index_c_servers.remove(client);
delete cs;
if (!rmrf_servers.contains(cs)) rmrf_servers << cs;
}
CloudServer * cc = index_c_clients.value(client, nullptr);
if (cc) {
@@ -216,7 +229,10 @@ void DispatcherServer::disconnectClient(DispatcherClient * client) {
void DispatcherServer::newConnection(PIEthernet * cl) {
// piCout << "DispatcherServer::newConnection" << (void *)cl;
PIMutexLocker locker(map_mutex);
if (clients.size() >= max_connections) {
// piCout << "DispatcherServer::newConnection overflow" << (void *)cl;
delete cl;
return;
}
@@ -227,7 +243,7 @@ void DispatcherServer::newConnection(PIEthernet * cl) {
CloudServer * cs = c_servers.value(sname, nullptr);
if (cs) {
rm_clients << c;
piCoutObj << "dublicate Server ->" << sname.toHex();
piCoutObj << "duplicate Server ->" << sname.toHex();
} else {
piCoutObj << "add new Server ->" << sname.toHex();
CloudServer * cs = new CloudServer(c, sname);
@@ -250,7 +266,7 @@ void DispatcherServer::newConnection(PIEthernet * cl) {
}
});
// piCoutObj << "add client" << client;
PIMutexLocker locker(map_mutex);
clients.push_back(client);
client->start();
// piCout << "DispatcherServer::newConnection started" << (void *)cl;
}

View File

@@ -36,6 +36,7 @@ private:
PIMap<const DispatcherClient *, CloudServer *> index_c_clients;
PIVector<DispatcherClient *> rm_clients;
PIVector<DispatcherClient *> rmrf_clients;
PIVector<CloudServer *> rmrf_servers;
PITimer timeout_timer;
PIMutex map_mutex;
uint client_gid;

View File

@@ -77,6 +77,34 @@ void printError(const PIString & msg) {
PISet<PIString> strings;
PIMap<uint, PIString> locations;
PIString mask(const PIString & in) {
static PIVector<PIPair<PIString, PIString>> map = {
{"&", "&amp;" },
{"<", "&lt;" },
{">", "&gt;" },
{"'", "&apos;"},
{"\"", "&quot;"},
};
PIString ret = in;
for (const auto & i: map)
ret.replaceAll(i.first, i.second);
return ret;
}
PIString unmask(const PIString & in) {
static PIVector<PIPair<PIString, PIString>> map = {
{"<", "&lt;" },
{">", "&gt;" },
{"'", "&apos;"},
{"\"", "&quot;"},
{"&", "&amp;" },
};
PIString ret = in;
for (const auto & i: map)
ret.replaceAll(i.second, i.first);
return ret;
}
void addString(const PIString & s, const PIString & loc) {
if (s.isEmpty()) return;
strings << s;
@@ -138,7 +166,7 @@ PIMap<PIString, TSMessage> readTS(const PIString & path) {
phase = 2;
} else if (line.startsWith("<source>")) {
line.cutLeft(8).cutRight(9);
msg.source = line;
msg.source = unmask(line);
} else if (line.startsWith("<location")) {
PIString trs = line.takeRange('<', '>').cutLeft(8);
while (trs.isNotEmpty()) {
@@ -158,7 +186,7 @@ PIMap<PIString, TSMessage> readTS(const PIString & path) {
if (t == "type") msg.type = v;
}
line.cutRight(14);
msg.translation = line;
msg.translation = unmask(line);
}
break;
}
@@ -236,7 +264,7 @@ int main(int argc, char * argv[]) {
PIIOTextStream ts(&outf);
auto writeTSMessage = [&ts](const PIString & s, const TSMessage & m) {
ts << " <message>\n";
ts << " <source>" << s << "</source>\n";
ts << " <source>" << mask(s) << "</source>\n";
if (m.filename.isNotEmpty()) {
ts << " <location filename=\"" << m.filename << "\"";
if (m.line.isNotEmpty()) ts << " line=\"" << m.line << "\"";
@@ -248,7 +276,7 @@ int main(int argc, char * argv[]) {
} else {
if (m.type.isNotEmpty()) ts << " type=\"" << m.type << "\"";
}
ts << ">" << m.translation << "</translation>\n";
ts << ">" << mask(m.translation) << "</translation>\n";
ts << " </message>\n";
};
ts << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";