Compare commits
20 Commits
960e4a7cce
...
eb21c85170
| Author | SHA1 | Date | |
|---|---|---|---|
| eb21c85170 | |||
| fb68a9f9fe | |||
| c7c3852747 | |||
| c18d0c918e | |||
| 9c4fd41eef | |||
| 9173dc936d | |||
| 77c8681b43 | |||
| 2db9440a38 | |||
| 0d585cfebf | |||
| 02a9bfb76f | |||
| ad7385c21f | |||
| dd6d91ac1d | |||
| 576d9c79be | |||
| 3fa5d9d9df | |||
| b14d30108a | |||
| 2b738f6f43 | |||
| 263fa18726 | |||
| f47bc411bc | |||
| 154fb7d9fd | |||
| 9f09af9f27 |
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
12
main.cpp
12
main.cpp
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 = {
|
||||
{"&", "&" },
|
||||
{"<", "<" },
|
||||
{">", ">" },
|
||||
{"'", "'"},
|
||||
{"\"", """},
|
||||
};
|
||||
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 = {
|
||||
{"<", "<" },
|
||||
{">", ">" },
|
||||
{"'", "'"},
|
||||
{"\"", """},
|
||||
{"&", "&" },
|
||||
};
|
||||
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";
|
||||
|
||||
Reference in New Issue
Block a user