Compare commits

3 Commits

Author SHA1 Message Date
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
7 changed files with 57 additions and 31 deletions

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

@@ -54,9 +54,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;
}
@@ -67,11 +66,10 @@ double CloudServer::lastPing() {
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

@@ -22,7 +22,7 @@ void DispatcherClient::start() {
DispatcherClient::~DispatcherClient() {
delete eth;
piDeleteSafety(eth);
}

View File

@@ -61,6 +61,7 @@ void DispatcherServer::cleanClients() {
piCout << "remove Server by ping timeout" << c->getConnection()->clientId();
PIVector<DispatcherClient *> cscv = c->getClients();
for (auto csc: cscv) {
if (!csc->isPIObject()) piCout << "ACHTUNG! Non-piobject DispatcherClient!";
clients.removeAll(csc);
index_c_clients.remove(csc);
c->removeClient(csc);
@@ -183,12 +184,12 @@ 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);
@@ -219,6 +220,7 @@ void DispatcherServer::disconnectClient(DispatcherClient * client) {
void DispatcherServer::newConnection(PIEthernet * cl) {
PIMutexLocker locker(map_mutex);
if (clients.size() >= max_connections) {
delete cl;
return;
@@ -253,7 +255,6 @@ void DispatcherServer::newConnection(PIEthernet * cl) {
}
});
// piCoutObj << "add client" << client;
PIMutexLocker locker(map_mutex);
clients.push_back(client);
client->start();
}