Skip to content

Commit

Permalink
update(Client): Refactor ClientManager class
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Jan 8, 2024
1 parent 11d1587 commit 0d22af7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 10 additions & 3 deletions Flakkari/Server/Client/ClientManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Flakkari {


std::shared_ptr<ClientManager> ClientManager::_instance = nullptr;

std::shared_ptr<ClientManager> ClientManager::getInstance() {
std::shared_ptr<ClientManager> ClientManager::getInstance()
{
if (!_instance)
_instance = std::make_shared<ClientManager>();
return _instance;
Expand All @@ -24,16 +26,17 @@ void ClientManager::addClient(std::shared_ptr<Network::Address> client)
auto &clients = getInstance()->_clients;
if (clients.find(client->toString().value_or("")) == clients.end()) {
clients[client->toString().value_or("")] = std::make_shared<Client>(client);
FLAKKARI_LOG_LOG("Client " + client->toString().value_or("") + " connected");
FLAKKARI_LOG_LOG("Client " + client->toString().value_or("Unknown") + " connected");
} else
clients[client->toString().value_or("")]->keepAlive();
}

void ClientManager::removeClient(std::shared_ptr<Network::Address> client)
{
auto &clients = getInstance()->_clients;
if (clients.find(client->toString().value_or("")) != clients.end())
if (clients.find(client->toString().value_or("")) != clients.end()) {
clients.erase(client->toString().value_or(""));
}
}

void ClientManager::checkInactiveClients()
Expand All @@ -49,6 +52,10 @@ void ClientManager::checkInactiveClients()
}
}

std::shared_ptr<Client> ClientManager::getClient(std::shared_ptr<Network::Address> client) {
return getInstance()->_clients[client->toString().value_or("")];
}

std::shared_ptr<Client> ClientManager::getClient(std::string ip) {
return getInstance()->_clients[ip];
}
Expand Down
13 changes: 10 additions & 3 deletions Flakkari/Server/Client/ClientManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ namespace Flakkari {
* @code
* #include "ClientManager.hpp"
*
* Flakkari::ClientManager clientManager;
* clientManager.addClient(std::make_shared<Flakkari::Network::Address>("...")>);
* clientManager.checkInactiveClients();
* Flakkari::ClientManager::addClient(std::make_shared<Flakkari::Network::Address>("...")>);
* Flakkari::ClientManager::checkInactiveClients();
* @endcode
*/
class ClientManager {
Expand Down Expand Up @@ -102,6 +101,14 @@ class ClientManager {
*/
static void checkInactiveClients();

/**
* @brief Get the Client object
*
* @param client The client's address
* @return std::shared_ptr<Client> The client object
*/
static std::shared_ptr<Client> getClient(std::shared_ptr<Network::Address> client);

/**
* @brief Get the Client object
*
Expand Down

0 comments on commit 0d22af7

Please sign in to comment.