-
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.
- Loading branch information
1 parent
6810629
commit f6e4a7a
Showing
16 changed files
with
127 additions
and
181 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
# By: agaley <[email protected]> +#+ +:+ +#+ # | ||
# +#+#+#+#+#+ +#+ # | ||
# 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 \ | ||
|
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: agaley <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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<struct epoll_event>& eventQueue) { | ||
_instance = new CacheHandler(eventQueue); | ||
return *_instance; | ||
} | ||
|
@@ -53,7 +53,7 @@ void CacheHandler::deleteInstance() { | |
_instance = NULL; | ||
} | ||
|
||
CacheHandler::CacheHandler(EventQueue& eventQueue) | ||
CacheHandler::CacheHandler(SPMCQueue<struct epoll_event>& 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); | ||
|
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: agaley <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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,17 +21,17 @@ | |
|
||
#include "Common.hpp" | ||
#include "EventData.hpp" | ||
#include "EventQueue.hpp" | ||
#include "HTTPRequest.hpp" | ||
#include "HTTPResponse.hpp" | ||
#include "SPMCQueue.hpp" | ||
|
||
struct EventData; | ||
|
||
class CacheHandler { | ||
public: | ||
static const time_t MAX_AGE; | ||
|
||
static CacheHandler& init(EventQueue& eventQueue); | ||
static CacheHandler& init(SPMCQueue<struct epoll_event>& 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<struct epoll_event>& eventQueue); | ||
~CacheHandler(); | ||
CacheHandler(const CacheHandler&); | ||
CacheHandler& operator=(const CacheHandler&); | ||
|
||
typedef std::map<std::string, CacheEntry> CacheMap; | ||
|
||
static CacheHandler* _instance; | ||
Logger& _log; | ||
EventQueue& _eventQueue; | ||
CacheMap _cache; | ||
time_t _maxAge; | ||
pthread_mutex_t _mutex; | ||
static CacheHandler* _instance; | ||
Logger& _log; | ||
SPMCQueue<struct epoll_event>& _eventQueue; | ||
CacheMap _cache; | ||
time_t _maxAge; | ||
pthread_mutex_t _mutex; | ||
|
||
unsigned long _hash(const std::string& str) const; | ||
}; | ||
|
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,75 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* SPMCQueue.hpp :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: agaley <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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 <typename T> | ||
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 |
Oops, something went wrong.