Skip to content

Commit

Permalink
to test state
Browse files Browse the repository at this point in the history
  • Loading branch information
Matisse Chenavas committed Aug 27, 2024
1 parent 7a5456f commit 7fbaa44
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 32 deletions.
1 change: 1 addition & 0 deletions site/upload/test.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions site/upload/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions site/upload/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{kashdalskjd}
17 changes: 5 additions & 12 deletions src/ConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ ConnectionHandler::ConnectionHandler(
_startTime(time(NULL)),
_cgiHandler(NULL),
_cgiState(NONE),
_step(0),
_events(events)
{
}
Expand Down Expand Up @@ -252,8 +251,7 @@ void ConnectionHandler::_processRequest(struct epoll_event& event) {

void ConnectionHandler::_processExecutingState() {
if (_cgiState == NONE) {
_log.error("CONNECTION_HANDLER(" + Utils::to_string(_step) +
"): CGIHandler is NULL or in invalid state");
_log.error("CONNECTION_HANDLER: CGIHandler is NULL or in invalid state");
_setConnectionStatus(SENDING);
return;
}
Expand All @@ -263,7 +261,6 @@ void ConnectionHandler::_processExecutingState() {
}

int ConnectionHandler::processConnection(struct epoll_event& event) {
_step++;
if ((time(NULL) - _startTime) > TIMEOUT)
{
HTTPResponse::sendResponse(HTTPResponse::GATEWAY_TIMEOUT, _clientSocket);
Expand All @@ -287,25 +284,22 @@ int ConnectionHandler::processConnection(struct epoll_event& event) {
_sendResponse();
} catch (const Exception& e) {
_setConnectionStatus(CLOSED);
_log.error(std::string("CONNECTION_HANDLER(" + Utils::to_string(_step) +
"): " + " Status: " + getStatusString() +
_log.error(std::string("CONNECTION_HANDLER Status: " + getStatusString() +
" Exception caught: " + e.what()));
}
switch (_connectionStatus) {
case READING:
event.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
if (epoll_ctl(_epollSocket, EPOLL_CTL_MOD, _clientSocket, &event) == -1) {
_log.error(std::string("CONNECTION_HANDLER(" + Utils::to_string(_step) +
"): epoll_ctl: ") +
_log.error(std::string("CONNECTION_HANDLER: epoll_ctl: ") +
strerror(errno));
close(_clientSocket);
}
break;

case EXECUTING:
if (!_cgiHandler) {
throw Exception("CONNECTION_HANDLER(" + Utils::to_string(_step) +
"): CGIHandler is NULL");
throw Exception("CONNECTION_HANDLER: CGIHandler is NULL");
}
_events.push(event);
break;
Expand All @@ -316,8 +310,7 @@ int ConnectionHandler::processConnection(struct epoll_event& event) {
case SENDING:
event.events = EPOLLOUT | EPOLLET | EPOLLONESHOT;
if (epoll_ctl(_epollSocket, EPOLL_CTL_MOD, _clientSocket, &event) == -1) {
_log.error(std::string("CONNECTION_HANDLER(" + Utils::to_string(_step) +
"): epoll_ctl: ") +
_log.error(std::string("CONNECTION_HANDLER: epoll_ctl: ") +
strerror(errno));
close(_clientSocket);
}
Expand Down
1 change: 0 additions & 1 deletion src/ConnectionHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class ConnectionHandler {
time_t _startTime;
CGIHandler* _cgiHandler;
CGIState _cgiState;
int _step;
EventQueue& _events;

void _receiveRequest(struct epoll_event& event);
Expand Down
99 changes: 94 additions & 5 deletions src/EventData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,98 @@
/* ************************************************************************** */

#include "EventData.hpp"
#include "EventData.hpp"

EventData::EventData(int newFd, ConnectionHandler* ptr, pid_t newThreadId, bool newIsListening, bool newRecordTime, bool newOpened) {
pthread_mutex_init(&mutex, NULL);
setFd(newFd);
setHandler(ptr);
setThreadId(newThreadId);
setIsListening(newIsListening);
setRecordTime(newRecordTime);
setStartTime(0);
setOpened(newOpened);
}

int EventData::getFd(){
pthread_mutex_lock(&mutex);
int ret = fd;
pthread_mutex_unlock(&mutex);
return ret;
}
ConnectionHandler* EventData::getHandler(){
pthread_mutex_lock(&mutex);
ConnectionHandler* ret = handler;
pthread_mutex_unlock(&mutex);
return ret;
}
pid_t EventData::getThreadId(){
pthread_mutex_lock(&mutex);
pid_t ret = threadId;
pthread_mutex_unlock(&mutex);
return ret;
}
bool EventData::getIsListening(){
pthread_mutex_lock(&mutex);
bool ret = isListening;
pthread_mutex_unlock(&mutex);
return ret;
}
bool EventData::getRecordTime(){
pthread_mutex_lock(&mutex);
bool ret = recordTime;
pthread_mutex_unlock(&mutex);
return ret;
}
time_t EventData::getStartTime(){
pthread_mutex_lock(&mutex);
time_t ret = startTime;
pthread_mutex_unlock(&mutex);
return ret;
}
bool EventData::getOpened(){
pthread_mutex_lock(&mutex);
bool ret = opened;
pthread_mutex_unlock(&mutex);
return ret;
}

void EventData::setFd(int newFd){
pthread_mutex_lock(&mutex);
fd = newFd;
pthread_mutex_unlock(&mutex);
}
void EventData::setHandler(ConnectionHandler* newHandler){
pthread_mutex_lock(&mutex);
handler = newHandler;
pthread_mutex_unlock(&mutex);
}
void EventData::setThreadId(pid_t id){
pthread_mutex_lock(&mutex);
threadId = id;
pthread_mutex_unlock(&mutex);
}
void EventData::setIsListening(bool newIsListening){
pthread_mutex_lock(&mutex);
isListening = newIsListening;
pthread_mutex_unlock(&mutex);
}
void EventData::setRecordTime(bool record){
pthread_mutex_lock(&mutex);
recordTime = record;
pthread_mutex_unlock(&mutex);
}
void EventData::setStartTime(time_t time){
pthread_mutex_lock(&mutex);
startTime = time;
pthread_mutex_unlock(&mutex);
}
void EventData::setOpened(bool newOpened){
pthread_mutex_lock(&mutex);
opened = newOpened;
pthread_mutex_unlock(&mutex);
}

EventData::EventData(int fd,
ConnectionHandler* ptr,
pid_t threadId,
bool isListening)
: fd(fd), handler(ptr), threadId(threadId), isListening(isListening) {}
EventData::~EventData() {
pthread_mutex_destroy(&mutex);
}
27 changes: 26 additions & 1 deletion src/EventData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#ifndef EVENTDATA_HPP
#define EVENTDATA_HPP

#include <pthread.h>

#include "ConnectionHandler.hpp"

class ConnectionHandler;
Expand All @@ -22,11 +24,34 @@ struct EventData {
ConnectionHandler* handler;
pid_t threadId;
bool isListening;
bool recordTime;
time_t startTime;
bool opened;
pthread_mutex_t mutex;

int getFd();
ConnectionHandler* getHandler();
pid_t getThreadId();
bool getIsListening();
bool getRecordTime();
time_t getStartTime();
bool getOpened();

void setFd(int fd);
void setHandler(ConnectionHandler* handler);
void setThreadId(pid_t pid);
void setIsListening(bool isListening);
void setRecordTime(bool record);
void setStartTime(time_t time);
void setOpened(bool opened);

EventData(int fd,
ConnectionHandler* ptr,
pid_t threadId = -1,
bool isListening = false);
bool isListening = false,
bool recordTime = true,
bool opened = true);
~EventData(void);
};

#endif
2 changes: 1 addition & 1 deletion src/HTTPResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ int HTTPResponse::sendResponse(int statusCode, int clientSocket) {
std::string body = defaultErrorPage(statusCode);
headers += "Content-Length: " + Utils::to_string(body.length()) + "\r\n\r\n";
std::string response = statusLine + headers + body;
if (send(clientSocket, response.c_str(), response.length(), 0) == -1) {
if (send(clientSocket, response.c_str(), response.length(), 0) != 0) {
return -1;
}
return 0;
Expand Down
4 changes: 4 additions & 0 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

Logger* Logger::_instance = NULL;

pthread_mutex_t Logger::_mutex;

Logger::Logger() : _progLogFile(new std::ofstream()) {
pthread_mutex_init(&_mutex, NULL);
}
Expand All @@ -35,6 +37,8 @@ Logger& Logger::getInstance() {

void Logger::deleteInstance() {
if (_instance != NULL) {
pthread_mutex_lock(&_mutex);
pthread_mutex_unlock(&_mutex);
delete _instance;
_instance = NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Logger {

static Logger* _instance;

mutable pthread_mutex_t _mutex;
static pthread_mutex_t _mutex;
Config _config;
std::ofstream* _progLogFile;
std::string _progLogFileName;
Expand Down
19 changes: 17 additions & 2 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Server::~Server() {
}
_listenSockets.clear();
_listenEventData.clear();
pthread_mutex_lock(&_mutex);
_running = false;
pthread_mutex_unlock(&_mutex);
pthread_mutex_destroy(&_mutex);
CacheHandler::deleteInstance();
ConfigManager::deleteInstance();
Expand All @@ -81,8 +84,18 @@ void Server::workerFinished() {
pthread_mutex_unlock(&_mutex);
}

bool Server::_shouldRun()
{
pthread_mutex_lock(&_mutex);
bool ret = _running;
pthread_mutex_unlock(&_mutex);
return ret;
}

void Server::start() {
pthread_mutex_lock(&_mutex);
_running = true;
pthread_mutex_unlock(&_mutex);
for (size_t i = 0; i < _workers.size(); i++) {
try {
_workers[i]->start();
Expand All @@ -97,7 +110,7 @@ void Server::start() {
_log.info("SERVER: All workers started (" + Utils::to_string(_activeWorkers) +
")");
struct epoll_event events[MAX_EVENTS];
while (_running) {
while (_shouldRun()) {
int nfds = epoll_wait(_epollSocket, events, MAX_EVENTS, -1);
if (nfds == 0) {
_log.info("SERVER: epoll_wait: 0 events");
Expand All @@ -107,7 +120,7 @@ void Server::start() {
usleep(1000);
continue;
}
for (int i = 0; i < nfds && _running; i++)
for (int i = 0; i < nfds && _shouldRun(); i++)
_events.push(events[i]);
}
}
Expand All @@ -122,7 +135,9 @@ void Server::stop(int signum) {
_workers[i]->stop();
}
_log.info("SERVER: workers stopped");
pthread_mutex_lock(&_mutex);
_running = false;
pthread_mutex_unlock(&_mutex);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Server {
void _setupServerSockets();
void _setupWorkers();
void _setupEpoll();
bool _shouldRun();

public:
Server();
Expand Down
Loading

0 comments on commit 7fbaa44

Please sign in to comment.