-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a port manager to track client ports and allow clients to disc…
…onnect/reconnect to the server
- Loading branch information
1 parent
1455828
commit c380f67
Showing
6 changed files
with
206 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
|
||
#include "PortManager.h" | ||
|
||
|
||
void PortManager::liberatePorts(int port1, int port2) | ||
{ | ||
for (auto const& [key, val] : m_map_taken_ports) | ||
{ | ||
if (key == port1 || key == port2) | ||
{ | ||
m_map_taken_ports[key] = 0; | ||
} | ||
} | ||
|
||
} | ||
|
||
void PortManager::occupyPorts(int port1, int port2) | ||
{ | ||
for (auto const& [key, val] : m_map_taken_ports) | ||
{ | ||
if (key == port1 || key == port2) | ||
{ | ||
m_map_taken_ports[key] = 1; | ||
} | ||
} | ||
|
||
} | ||
|
||
int PortManager::returnAvailablePort() | ||
{ | ||
for (auto const& [key, val] : m_map_taken_ports) | ||
{ | ||
if (val == 0) | ||
{ | ||
return key; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
bool PortManager::removeClient(std::string client_name) | ||
{ | ||
|
||
for (unsigned int i = 0; i < m_clients.size(); i++) | ||
{ | ||
if (m_clients[i].name.find(client_name) != std::string::npos) | ||
{ | ||
// Free the ports | ||
liberatePorts(m_clients[i].publishing_port, m_clients[i].subscribing_port); | ||
|
||
// Found client object | ||
m_clients.erase(m_clients.begin() + i); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
std::pair<int, int> PortManager::storeClient(std::string client_name) | ||
{ | ||
// Store values into vector of client objects | ||
ClientObj new_client; | ||
|
||
int subscriber_port = returnAvailablePort(); | ||
int publisher_port = subscriber_port + 1; | ||
|
||
new_client.name = client_name; | ||
new_client.publishing_port = subscriber_port; | ||
new_client.subscribing_port = publisher_port; | ||
|
||
m_clients.push_back(new_client); | ||
std::pair<int, int> ports; | ||
ports.first = subscriber_port; | ||
ports.second = publisher_port; | ||
|
||
return ports; | ||
|
||
} | ||
|
||
|
||
ClientObj PortManager::getClient(std::string client_name) | ||
{ | ||
// Store values into vector of client objects | ||
ClientObj empty_client; | ||
|
||
for (unsigned int i = 0; i < m_clients.size(); i++) | ||
{ | ||
if (m_clients[i].name.find(client_name) != std::string::npos) | ||
{ | ||
// Found client object | ||
return m_clients[i]; | ||
} | ||
} | ||
|
||
return empty_client; | ||
} | ||
|
||
bool PortManager::isConnected(std::string client_name) | ||
{ | ||
|
||
for (unsigned int i = 0; i < m_clients.size(); i++) | ||
{ | ||
if (m_clients[i].name.find(client_name) != std::string::npos) | ||
{ | ||
// Found client object | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
||
PortManager::PortManager(int starting_port, int max_number_clients) | ||
{ | ||
m_starting_port = starting_port; | ||
m_max_clients = max_number_clients; | ||
|
||
|
||
for (unsigned int port_number = starting_port; port_number < (starting_port + (max_number_clients * 2)); port_number++) | ||
{ | ||
m_map_taken_ports[port_number] = 0; | ||
} | ||
|
||
} | ||
|
||
PortManager::~PortManager() | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
#pragma once | ||
|
||
|
||
#ifndef PORTMANAGER_SRC_PORTMANAGER_H_ | ||
#define PORTMANAGER_SRC_PORTMANAGER_H_ | ||
|
||
|
||
#include <future> | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
|
||
|
||
struct ClientObj | ||
{ | ||
std::string name = ""; | ||
unsigned int publishing_port = 0; | ||
unsigned int subscribing_port = 0; | ||
}; | ||
|
||
class PortManager | ||
{ | ||
private: | ||
|
||
int m_starting_port; // Starting port in which to store and search for ports | ||
int m_max_clients; // Max number of clients to consider | ||
std::vector<ClientObj> m_clients; // Clients to be managing | ||
std::map<int, int> m_map_taken_ports; // Store ports | ||
|
||
int returnAvailablePort(); // Return a port that is available | ||
void occupyPorts(int port1, int port2); // Label the ports specified as occupied | ||
void liberatePorts(int port1, int port2); // Label the ports specified as unoccupied | ||
|
||
public: | ||
PortManager(int starting_port, int max_number_clients); | ||
~PortManager(); | ||
|
||
|
||
std::pair<int, int> storeClient(std::string client_name); | ||
bool removeClient(std::string client_name); | ||
ClientObj getClient(std::string client_name); | ||
bool isConnected(std::string client_name); | ||
|
||
|
||
}; | ||
#endif /* CLIENTMANAGER_SRC_CLIENTMANAGER_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters