mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「🏗️」 wip: redone correctly ResourceManager and commentary
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/29 14:20:09 by mmoussou #+# #+# */
|
/* Created: 2025/04/29 14:20:09 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/05/12 19:05:51 by adjoly ### ########.fr */
|
/* Updated: 2025/05/13 09:56:02 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -18,12 +18,17 @@
|
|||||||
namespace webserv {
|
namespace webserv {
|
||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
|
enum clientResType {
|
||||||
|
CGI,
|
||||||
|
UP_FILE
|
||||||
|
};
|
||||||
|
|
||||||
class AClientResource {
|
class AClientResource {
|
||||||
public:
|
public:
|
||||||
virtual ~AClientResource() {}
|
virtual ~AClientResource() {}
|
||||||
|
|
||||||
bool operator==(int i) const {
|
bool operator==(int i) const {
|
||||||
if (i == _client_id)
|
if (i == _res_id)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -31,10 +36,12 @@ public:
|
|||||||
void addFileDescriptor(struct pollfd fd);
|
void addFileDescriptor(struct pollfd fd);
|
||||||
struct pollfd getFileDescriptor();
|
struct pollfd getFileDescriptor();
|
||||||
|
|
||||||
private:
|
virtual clientResType type(void);
|
||||||
|
|
||||||
|
protected:
|
||||||
struct pollfd _fd;
|
struct pollfd _fd;
|
||||||
|
|
||||||
int _client_id;
|
int _res_id;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -6,13 +6,15 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */
|
/* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/05/12 19:06:28 by adjoly ### ########.fr */
|
/* Updated: 2025/05/13 09:55:45 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <cppeleven.hpp>
|
#include <cppeleven.hpp>
|
||||||
@ -25,22 +27,45 @@ namespace server {
|
|||||||
|
|
||||||
class ResourceManager {
|
class ResourceManager {
|
||||||
public:
|
public:
|
||||||
static AClientResource *get(int i) {
|
/**
|
||||||
for (auto it = range(_res)) {
|
* @brief Can be used to get a resource correcponding to the id of the
|
||||||
if ((**it) == i) {
|
* resource
|
||||||
return *it;
|
*
|
||||||
}
|
* @param The id of the resource
|
||||||
}
|
*
|
||||||
std::stringstream str;
|
* @return The resource
|
||||||
str << "resource not found for client";
|
* @throw std::out_of_range If the resource does not exist
|
||||||
str << i;
|
*/
|
||||||
_log->debug(str.str());
|
static AClientResource *get(int id) {
|
||||||
return not_nullptr;
|
auto it = std::find(_res.begin(), _res.end(), id);
|
||||||
|
|
||||||
|
if (it != _res.end())
|
||||||
|
return (*it);
|
||||||
|
throw std::out_of_range("resource does not exist for client D:");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Can be used to append a resource to the ResourceManager
|
||||||
|
*
|
||||||
|
* @param The resource to add
|
||||||
|
*/
|
||||||
static void append(AClientResource *new_client) {
|
static void append(AClientResource *new_client) {
|
||||||
_res.push_back(new_client);
|
_res.push_back(new_client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Can be used to remove a resource by giving it it's id
|
||||||
|
*
|
||||||
|
* @param The id of the resource to remove
|
||||||
|
*/
|
||||||
|
static void remove(int id) {
|
||||||
|
auto it = std::find(_res.begin(), _res.end(), id);
|
||||||
|
|
||||||
|
if (it != _res.end())
|
||||||
|
delete (*it);
|
||||||
|
// TODO throw or not - to see
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
private:
|
private:
|
||||||
static std::vector<AClientResource *> _res;
|
static std::vector<AClientResource *> _res;
|
||||||
|
Reference in New Issue
Block a user