diff --git a/Makefile b/Makefile index 0614520..1b71fa0 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: agaley +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/12/15 15:51:13 by agaley #+# #+# # -# Updated: 2024/07/22 18:45:13 by agaley ### ########lyon.fr # +# Updated: 2024/07/22 23:16:54 by agaley ### ########lyon.fr # # # # **************************************************************************** # @@ -35,7 +35,7 @@ SRC = $(SRC_DIR)/Server.cpp \ $(SRC_DIR)/Config.cpp $(SRC_DIR)/ConfigManager.cpp $(SRC_DIR)/ConfigParser.cpp \ $(SRC_DIR)/FileManager.cpp \ $(SRC_DIR)/ConnectionHandler.cpp $(SRC_DIR)/CacheHandler.cpp \ - $(SRC_DIR)/Worker.cpp $(SRC_DIR)/EventQueue.cpp $(SRC_DIR)/EventData.cpp \ + $(SRC_DIR)/Worker.cpp $(SRC_DIR)/EventData.cpp \ $(SRC_DIR)/HTTPRequest.cpp $(SRC_DIR)/HTTPResponse.cpp $(SRC_DIR)/URI.cpp \ $(SRC_DIR)/CGIHandler.cpp $(SRC_DIR)/VirtualServer.cpp \ $(SRC_DIR)/Exception.cpp $(SRC_DIR)/HTTPMethods.cpp \ diff --git a/src/CGIHandler.cpp b/src/CGIHandler.cpp index 461c8af..9f4c417 100644 --- a/src/CGIHandler.cpp +++ b/src/CGIHandler.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* CGIHandler.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mchenava < mchenava@student.42lyon.fr> +#+ +:+ +#+ */ +/* By: agaley +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/30 16:11:05 by agaley #+# #+# */ -/* Updated: 2024/07/23 01:08:08 by mchenava ### ########.fr */ +/* Updated: 2024/07/23 02:12:47 by agaley ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -44,10 +44,9 @@ CGIHandler::CGIHandler(HTTPRequest& request, CGIHandler::~CGIHandler() { kill(_pid, SIGKILL); waitpid(_pid, NULL, 0); - if (_inpipefd[0] != -1) - { + if (_outpipefd[0] != -1) { delEvent(); - close(_inpipefd[0]); + close(_outpipefd[0]); } if (_inpipefd[1] != -1) close(_inpipefd[1]); diff --git a/src/CacheHandler.cpp b/src/CacheHandler.cpp index aee6866..f9af8e7 100644 --- a/src/CacheHandler.cpp +++ b/src/CacheHandler.cpp @@ -6,7 +6,7 @@ /* By: agaley +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 23:54:38 by agaley #+# #+# */ -/* Updated: 2024/07/18 13:46:07 by agaley ### ########lyon.fr */ +/* Updated: 2024/07/22 23:21:10 by agaley ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ CacheHandler::CacheEntry::~CacheEntry() { delete response; } -CacheHandler& CacheHandler::init(EventQueue& eventQueue) { +CacheHandler& CacheHandler::init(SPMCQueue& eventQueue) { _instance = new CacheHandler(eventQueue); return *_instance; } @@ -53,7 +53,7 @@ void CacheHandler::deleteInstance() { _instance = NULL; } -CacheHandler::CacheHandler(EventQueue& eventQueue) +CacheHandler::CacheHandler(SPMCQueue& eventQueue) : _log(Logger::getInstance()), _eventQueue(eventQueue), _cache(), _maxAge(MAX_AGE) { pthread_mutex_init(&_mutex, NULL); } @@ -113,7 +113,7 @@ void CacheHandler::storeResponse(const std::string& key, const HTTPResponse& res struct epoll_event event; event.data.ptr = *it; event.events = EPOLLIN | EPOLLET | EPOLLONESHOT; - _eventQueue.push(event); + _eventQueue.enqueue(event); } entry.waitingEventsData.clear(); pthread_mutex_unlock(&_mutex); diff --git a/src/CacheHandler.hpp b/src/CacheHandler.hpp index f93bc9a..f7383ef 100644 --- a/src/CacheHandler.hpp +++ b/src/CacheHandler.hpp @@ -6,7 +6,7 @@ /* By: agaley +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 23:54:58 by agaley #+# #+# */ -/* Updated: 2024/07/18 13:46:13 by agaley ### ########lyon.fr */ +/* Updated: 2024/07/22 23:20:59 by agaley ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -21,9 +21,9 @@ #include "Common.hpp" #include "EventData.hpp" -#include "EventQueue.hpp" #include "HTTPRequest.hpp" #include "HTTPResponse.hpp" +#include "SPMCQueue.hpp" struct EventData; @@ -31,7 +31,7 @@ class CacheHandler { public: static const time_t MAX_AGE; - static CacheHandler& init(EventQueue& eventQueue); + static CacheHandler& init(SPMCQueue& eventQueue); static CacheHandler& getInstance(); static void deleteInstance(); @@ -54,19 +54,19 @@ class CacheHandler { void deleteCache(const std::string& key); private: - CacheHandler(EventQueue& eventQueue); + CacheHandler(SPMCQueue& eventQueue); ~CacheHandler(); CacheHandler(const CacheHandler&); CacheHandler& operator=(const CacheHandler&); typedef std::map CacheMap; - static CacheHandler* _instance; - Logger& _log; - EventQueue& _eventQueue; - CacheMap _cache; - time_t _maxAge; - pthread_mutex_t _mutex; + static CacheHandler* _instance; + Logger& _log; + SPMCQueue& _eventQueue; + CacheMap _cache; + time_t _maxAge; + pthread_mutex_t _mutex; unsigned long _hash(const std::string& str) const; }; diff --git a/src/ConfigManager.hpp b/src/ConfigManager.hpp index bdc0ab6..7e6c045 100644 --- a/src/ConfigManager.hpp +++ b/src/ConfigManager.hpp @@ -37,8 +37,8 @@ class ConfigManager { * Retrieve all config. * @return The configuration object. */ - Config& getConfig(); - static void printConfig(); + Config& getConfig(); + static void printConfig(); static void loadConfig(const std::string& filepath); diff --git a/src/ConnectionHandler.hpp b/src/ConnectionHandler.hpp index fe68e0b..24ac01e 100644 --- a/src/ConnectionHandler.hpp +++ b/src/ConnectionHandler.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ConnectionHandler.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mchenava < mchenava@student.42lyon.fr> +#+ +:+ +#+ */ +/* By: agaley +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/30 16:11:25 by agaley #+# #+# */ -/* Updated: 2024/07/23 02:07:00 by mchenava ### ########.fr */ +/* Updated: 2024/07/23 02:24:08 by agaley ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,10 @@ #include "CacheHandler.hpp" #include "Common.hpp" #include "Config.hpp" -#include "EventQueue.hpp" #include "HTTPRequest.hpp" #include "HTTPResponse.hpp" #include "Logger.hpp" +#include "SPMCQueue.hpp" #include "VirtualServer.hpp" class VirtualServer; @@ -34,10 +34,10 @@ class CGIHandler; class ConnectionHandler { public: - ConnectionStatus getConnectionStatus() const; - std::string getStatusString() const; - int processConnection(struct epoll_event& event); - void setInternalServerError(); + ConnectionStatus getConnectionStatus() const; + std::string getStatusString() const; + int processConnection(struct epoll_event& event); + void setInternalServerError(); ConnectionHandler(int clientSocket, int epollSocket, @@ -88,7 +88,6 @@ class ConnectionHandler { CGIHandler* _cgiHandler; CGIState _cgiState; int _step; - pthread_mutex_t _requestTimesMutex; void _receiveRequest(struct epoll_event& event); void _processRequest(struct epoll_event& event); diff --git a/src/EventQueue.cpp b/src/EventQueue.cpp deleted file mode 100644 index b41e687..0000000 --- a/src/EventQueue.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* EventQueue.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mchenava < mchenava@student.42lyon.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/06/27 16:00:52 by mchenava #+# #+# */ -/* Updated: 2024/06/27 16:10:05 by mchenava ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "EventQueue.hpp" -#include -#include -#include -#include "Utils.hpp" - -EventQueue::EventQueue() { - pthread_mutex_init(&_mutex, NULL); - pthread_cond_init(&_cond, NULL); -} - -EventQueue::~EventQueue() { - pthread_mutex_destroy(&_mutex); - pthread_cond_destroy(&_cond); -} - -void EventQueue::push(const struct epoll_event& event) { - pthread_mutex_lock(&_mutex); - _queue.push(event); - pthread_cond_signal(&_cond); - pthread_mutex_unlock(&_mutex); -} - -bool EventQueue::pop(struct epoll_event& event) { - pthread_mutex_lock(&_mutex); - while (_queue.empty()) { - pthread_cond_wait(&_cond, &_mutex); - } - event = _queue.front(); - _queue.pop(); - pthread_mutex_unlock(&_mutex); - return true; -} - -bool EventQueue::try_pop(struct epoll_event& event) { - pthread_mutex_lock(&_mutex); - if (_queue.empty()) { - pthread_mutex_unlock(&_mutex); - return false; - } - event = _queue.front(); - _queue.pop(); - pthread_mutex_unlock(&_mutex); - return true; -} - -bool EventQueue::empty() { - pthread_mutex_lock(&_mutex); - bool empty = _queue.empty(); - pthread_mutex_unlock(&_mutex); - return empty; -} diff --git a/src/EventQueue.hpp b/src/EventQueue.hpp deleted file mode 100644 index 5719362..0000000 --- a/src/EventQueue.hpp +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* EventQueue.hpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mchenava < mchenava@student.42lyon.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/06/27 16:00:17 by mchenava #+# #+# */ -/* Updated: 2024/06/27 16:02:40 by mchenava ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef EVENTQUEUE_HPP -#define EVENTQUEUE_HPP - -#include -#include -#include - -class EventQueue { - public: - EventQueue(); - ~EventQueue(); - void push(const struct epoll_event& event); - bool pop(struct epoll_event& event); - bool try_pop(struct epoll_event& event); - bool empty(); - - private: - std::queue _queue; - pthread_mutex_t _mutex; - pthread_cond_t _cond; -}; - -#endif diff --git a/src/SPMCQueue.hpp b/src/SPMCQueue.hpp new file mode 100644 index 0000000..67cdfb3 --- /dev/null +++ b/src/SPMCQueue.hpp @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* SPMCQueue.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agaley +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/18 02:48:04 by agaley #+# #+# */ +/* Updated: 2024/07/22 23:16:34 by agaley ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SPSCQUEUE_H +#define SPSCQUEUE_H + +template +class SPMCQueue { + private: + struct Node { + Node* next; + T data; + Node() : next(NULL) {} + }; + + Node* _head; + Node* _tail; + pthread_mutex_t _head_mutex; + pthread_mutex_t _tail_mutex; + + public: + SPMCQueue() : _head(new Node()), _tail(_head) { + pthread_mutex_init(&_head_mutex, NULL); + pthread_mutex_init(&_tail_mutex, NULL); + } + + ~SPMCQueue() { + while (Node* old_head = _head) { + _head = _head->next; + delete old_head; + } + pthread_mutex_destroy(&_head_mutex); + pthread_mutex_destroy(&_tail_mutex); + } + + void enqueue(const T& data) { + Node* n = new Node(); + n->data = data; + n->next = NULL; + + pthread_mutex_lock(&_head_mutex); + _head->next = n; + _head = n; + pthread_mutex_unlock(&_head_mutex); + } + + bool dequeue(T& result) { + pthread_mutex_lock(&_tail_mutex); + Node* old_tail = _tail; + Node* new_tail = old_tail->next; + + if (new_tail == NULL) { + pthread_mutex_unlock(&_tail_mutex); + return false; + } + + result = new_tail->data; + _tail = new_tail; + pthread_mutex_unlock(&_tail_mutex); + + delete old_tail; + return true; + } +}; + +#endif diff --git a/src/Server.cpp b/src/Server.cpp index 73aac01..2947b76 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Server.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mchenava < mchenava@student.42lyon.fr> +#+ +:+ +#+ */ +/* By: agaley +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/28 15:34:02 by agaley #+# #+# */ -/* Updated: 2024/07/23 02:09:20 by mchenava ### ########.fr */ +/* Updated: 2024/07/23 02:18:52 by agaley ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ #include "CacheHandler.hpp" #include "Common.hpp" #include "ConfigManager.hpp" -#include "EventQueue.hpp" +#include "SPMCQueue.hpp" #include "Server.hpp" #include "Utils.hpp" @@ -121,7 +121,7 @@ void Server::start() { eventData->recordTime = false; } pthread_mutex_unlock(&_requestTimesMutex); - _events.push(events[i]); + _events.enqueue(events[i]); } } } diff --git a/src/Server.hpp b/src/Server.hpp index 9e3d52a..fa2d7af 100644 --- a/src/Server.hpp +++ b/src/Server.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Server.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mchenava < mchenava@student.42lyon.fr> +#+ +:+ +#+ */ +/* By: agaley +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/28 15:34:01 by agaley #+# #+# */ -/* Updated: 2024/07/23 01:51:20 by mchenava ### ########.fr */ +/* Updated: 2024/07/23 02:15:21 by agaley ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -26,8 +26,8 @@ #include "Common.hpp" #include "Config.hpp" #include "EventData.hpp" -#include "EventQueue.hpp" #include "Logger.hpp" +#include "SPMCQueue.hpp" #include "Worker.hpp" class Worker; @@ -45,7 +45,7 @@ class Server { pthread_mutex_t _requestTimesMutex; int _epollSocket; int _activeWorkers; - EventQueue _events; + SPMCQueue _events; bool _running; std::map > _virtualServers; std::list _requestTimes; diff --git a/src/Utils.cpp b/src/Utils.cpp index 2dd50ba..068aab8 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -120,4 +120,3 @@ std::string Utils::generateSessionId(void) { } return sessionId; } - diff --git a/src/VirtualServer.cpp b/src/VirtualServer.cpp index e827128..2223093 100644 --- a/src/VirtualServer.cpp +++ b/src/VirtualServer.cpp @@ -6,7 +6,7 @@ /* By: agaley +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/24 15:10:25 by mchenava #+# #+# */ -/* Updated: 2024/07/02 23:38:18 by agaley ### ########.fr */ +/* Updated: 2024/07/23 00:22:18 by agaley ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -152,26 +152,6 @@ std::string VirtualServer::getRoot() const { return _serverConfig.root; } -void VirtualServer::storeSessionData(const std::string& sessionId, - const std::string& key, - const std::string& value) { - _sessionStore[sessionId][key] = value; -} - -std::string VirtualServer::getSessionData(const std::string& sessionId, - const std::string& key) { - return _sessionStore[sessionId][key]; -} - -void VirtualServer::deleteSessionData(const std::string& sessionId, - const std::string& key) { - _sessionStore[sessionId].erase(key); -} - -void VirtualServer::clearSessionData(const std::string& sessionId) { - _sessionStore[sessionId].clear(); -} - VirtualServer::~VirtualServer() { delete _httpMethods; } diff --git a/src/VirtualServer.hpp b/src/VirtualServer.hpp index 5f75cf4..1e22437 100644 --- a/src/VirtualServer.hpp +++ b/src/VirtualServer.hpp @@ -6,7 +6,7 @@ /* By: agaley +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/24 10:22:15 by mchenava #+# #+# */ -/* Updated: 2024/07/02 23:44:22 by agaley ### ########lyon.fr */ +/* Updated: 2024/07/23 00:29:24 by agaley ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -24,12 +24,11 @@ class HTTPMethods; class VirtualServer { private: - const ServerConfig& _serverConfig; - Logger& _log; - bool _defaultServer; - std::vector _hostNames; - HTTPMethods* _httpMethods; - std::map > _sessionStore; + const ServerConfig& _serverConfig; + Logger& _log; + bool _defaultServer; + std::vector _hostNames; + HTTPMethods* _httpMethods; bool _hasDefaultListenConfig(); std::map _getErrorPages(const std::string& uri); @@ -44,12 +43,6 @@ class VirtualServer { HTTPResponse* handleRequest(HTTPRequest& request); const LocationConfig& getLocationConfig(const std::string& uri) const; std::string getRoot() const; - void storeSessionData(const std::string& sessionId, - const std::string& key, - const std::string& value); - std::string getSessionData(const std::string& sessionId, const std::string& key); - void deleteSessionData(const std::string& sessionId, const std::string& key); - void clearSessionData(const std::string& sessionId); }; #endif diff --git a/src/Worker.cpp b/src/Worker.cpp index 189e250..6ed61e6 100644 --- a/src/Worker.cpp +++ b/src/Worker.cpp @@ -18,7 +18,7 @@ Worker::Worker(Server& server, int epollSocket, std::map& listenSockets, - EventQueue& events, + SPMCQueue& events, pthread_mutex_t& requestTimesMutex) : _server(server), _events(events), @@ -75,7 +75,7 @@ void Worker::_runEventLoop() { while (!_shouldStop) { struct epoll_event event; - if (!_events.try_pop(event)) { + if (!_events.dequeue(event)) { usleep(1000); continue; } diff --git a/src/Worker.hpp b/src/Worker.hpp index a9a9f45..cd02b4f 100644 --- a/src/Worker.hpp +++ b/src/Worker.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Worker.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mchenava < mchenava@student.42lyon.fr> +#+ +:+ +#+ */ +/* By: agaley +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/22 12:06:51 by mchenava #+# #+# */ -/* Updated: 2024/07/23 02:08:54 by mchenava ### ########.fr */ +/* Updated: 2024/07/23 02:17:48 by agaley ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -28,8 +28,8 @@ #include "Common.hpp" #include "ConnectionHandler.hpp" #include "EventData.hpp" -#include "EventQueue.hpp" #include "Logger.hpp" +#include "SPMCQueue.hpp" #include "Server.hpp" #include "Utils.hpp" #include "VirtualServer.hpp" @@ -41,7 +41,7 @@ class Worker { Worker(Server& server, int epollSocket, std::map& listenSockets, - EventQueue& events, + SPMCQueue& events, pthread_mutex_t& requestTimesMutex); ~Worker(); @@ -68,8 +68,8 @@ class Worker { const ListenConfig& listenConfig); void _launchEventProcessing(EventData* eventData, struct epoll_event& event); - Server& _server; - EventQueue& _events; + Server& _server; + SPMCQueue& _events; Thread _thread; std::map _eventsData; const Config& _config;