Added PollManager class
This commit is contained in:
parent
53a83308de
commit
4a17d88d80
7 changed files with 179 additions and 37 deletions
7
Makefile
7
Makefile
|
|
@ -1,4 +1,4 @@
|
||||||
# **************************************************************************** #
|
#******************************************************************************#
|
||||||
# #
|
# #
|
||||||
# ::: :::::::: #
|
# ::: :::::::: #
|
||||||
# Makefile :+: :+: :+: #
|
# Makefile :+: :+: :+: #
|
||||||
|
|
@ -6,9 +6,9 @@
|
||||||
# By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ #
|
# By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
|
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
|
||||||
# Updated: 2025/05/19 15:13:05 by omoudni ### ########.fr #
|
# Updated: 2025/05/19 20:14:35 by omoudni ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
#******************************************************************************#
|
||||||
|
|
||||||
|
|
||||||
# Name
|
# Name
|
||||||
|
|
@ -28,6 +28,7 @@ SRC = sources/core/main.cpp \
|
||||||
sources/core/server.cpp \
|
sources/core/server.cpp \
|
||||||
sources/core/check.cpp \
|
sources/core/check.cpp \
|
||||||
sources/core/parser.cpp \
|
sources/core/parser.cpp \
|
||||||
|
sources/core/pollManager.cpp \
|
||||||
|
|
||||||
|
|
||||||
INC_DIR = include/core \
|
INC_DIR = include/core \
|
||||||
|
|
|
||||||
46
diagram.puml
46
diagram.puml
|
|
@ -1,13 +1,16 @@
|
||||||
@startuml
|
@startuml
|
||||||
|
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
' CLASS: Server
|
' CLASS: Server
|
||||||
' ========================
|
' ========================
|
||||||
class Server {
|
class Server {
|
||||||
- _port : int
|
- _port : int
|
||||||
- _password : string
|
- _password : string
|
||||||
- _clients : List<Client>
|
- _users : List<User>
|
||||||
- _server_fd : int
|
- _server_fd : int
|
||||||
|
- _state_msg : std::pair<User, e_state>
|
||||||
|
- _pollManager : PollManager
|
||||||
|
|
||||||
+ showInfo() : void
|
+ showInfo() : void
|
||||||
+ getPort() : int
|
+ getPort() : int
|
||||||
|
|
@ -15,9 +18,9 @@ class Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
' CLASS: Client
|
' CLASS: User
|
||||||
' ========================
|
' ========================
|
||||||
class Client {
|
class User {
|
||||||
- _id : int
|
- _id : int
|
||||||
- _name : string
|
- _name : string
|
||||||
- _registered : bool
|
- _registered : bool
|
||||||
|
|
@ -29,7 +32,7 @@ class Client {
|
||||||
+ isRegistered() : bool
|
+ isRegistered() : bool
|
||||||
+ joinChannel(channel : Channel) : void
|
+ joinChannel(channel : Channel) : void
|
||||||
+ leaveChannel(channel : Channel) : void
|
+ leaveChannel(channel : Channel) : void
|
||||||
+ sendMessage(to : Client, message : string) : void
|
+ sendMessage(to : User, message : string) : void
|
||||||
}
|
}
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
|
|
@ -38,23 +41,23 @@ class Client {
|
||||||
class Channel {
|
class Channel {
|
||||||
- _name : string
|
- _name : string
|
||||||
- _password : string
|
- _password : string
|
||||||
- _owner : Client
|
- _owner : User
|
||||||
- _operators : List<Client>
|
- _operators : List<User>
|
||||||
- _socket_id : int
|
- _socket_id : int
|
||||||
- _topic : string
|
- _topic : string
|
||||||
|
|
||||||
+ getName() : string
|
+ getName() : string
|
||||||
+ setTopic(newTopic : string) : void
|
+ setTopic(newTopic : string) : void
|
||||||
+ addOperator(client : Client) : void
|
+ addOperator(User : User) : void
|
||||||
+ removeOperator(client : Client) : void
|
+ removeOperator(User : User) : void
|
||||||
}
|
}
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
' CLASS: Commandes
|
' CLASS: Commandes
|
||||||
' ========================
|
' ========================
|
||||||
class Commandes {
|
class Commandes {
|
||||||
- _form_user : Client
|
- _form_user : User
|
||||||
- _to_user : Client
|
- _to_user : User
|
||||||
- _channel : Channel
|
- _channel : Channel
|
||||||
|
|
||||||
+ MODE() : void
|
+ MODE() : void
|
||||||
|
|
@ -85,14 +88,29 @@ class Parser {
|
||||||
+ getErrorMsg() : string
|
+ getErrorMsg() : string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
' ========================
|
||||||
|
' CLASS: PollManager
|
||||||
|
' ========================
|
||||||
|
class PollManager {
|
||||||
|
- _fds : vector<pollfd>
|
||||||
|
- _fd_events : map<int, short>
|
||||||
|
|
||||||
|
+ addFd(fd : int, events : short) : void
|
||||||
|
+ removeFd(fd : int) : void
|
||||||
|
+ updateFd(fd : int, events : short) : void
|
||||||
|
+ pollEvents(timeout : int) : int
|
||||||
|
+ getFds() : vector<pollfd>
|
||||||
|
}
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
' RELATIONS
|
' RELATIONS
|
||||||
' ========================
|
' ========================
|
||||||
|
|
||||||
Server "1" o-- "*" Client : _clients
|
Server "1" o-- "*" User : _users
|
||||||
Client "1" o-- "*" Channel : _channels
|
User "1" o-- "*" Channel : _channels
|
||||||
Channel "1" *-- "1" Client : _owner
|
Channel "1" *-- "1" User : _owner
|
||||||
Channel "1" o-- "*" Client : _operators
|
Channel "1" o-- "*" User : _operators
|
||||||
main ..> Parser : uses
|
main ..> Parser : uses
|
||||||
|
Server "1" *-- "1" PollManager : pollManager
|
||||||
|
|
||||||
@enduml
|
@enduml
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
/* ************************************************************************** */
|
/******************************************************************************/
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* core.hpp :+: :+: :+: */
|
/* core.hpp :+: :+: :+: */
|
||||||
|
|
@ -6,9 +6,9 @@
|
||||||
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/12 14:16:03 by rparodi #+# #+# */
|
/* Created: 2025/05/12 14:16:03 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/05/19 15:07:26 by omoudni ### ########.fr */
|
/* Updated: 2025/05/19 20:15:14 by omoudni ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/******************************************************************************/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
@ -16,11 +16,21 @@
|
||||||
#define LOG std::endl << CLR_CYAN << "Debug: " << __FILE__ << ":" << __LINE__ << std::endl << CLR_RESET
|
#define LOG std::endl << CLR_CYAN << "Debug: " << __FILE__ << ":" << __LINE__ << std::endl << CLR_RESET
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
#define LOG ""
|
#define LOG ""
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
enum e_state {
|
||||||
|
ERROR = 0,
|
||||||
|
CMD,
|
||||||
|
MSG
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#include "pollManager.hpp"
|
||||||
#include "color.hpp"
|
#include "color.hpp"
|
||||||
#include "server.hpp"
|
#include "server.hpp"
|
||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
|
|
|
||||||
30
include/core/pollManager.hpp
Normal file
30
include/core/pollManager.hpp
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
/******************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pollManager.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/05/19 19:15:13 by omoudni #+# #+# */
|
||||||
|
/* Updated: 2025/05/19 19:25:45 by omoudni ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
class PollManager {
|
||||||
|
private:
|
||||||
|
std::vector<struct pollfd> _fds;
|
||||||
|
std::map<int, short> _fd_events; // fd -> event
|
||||||
|
|
||||||
|
public:
|
||||||
|
PollManager();
|
||||||
|
void addFd(int fd, short events);
|
||||||
|
void removeFd(int fd);
|
||||||
|
void updateFd(int fd, short events);
|
||||||
|
int pollEvents(int timeout = -1);
|
||||||
|
const std::vector<struct pollfd>& getFds() const;
|
||||||
|
};
|
||||||
|
|
@ -1,17 +1,18 @@
|
||||||
/* ************************************************************************** */
|
/******************************************************************************/
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* server.hpp :+: :+: :+: */
|
/* server.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: omoudni <omoudni@student.42.fr> +#+ +:+ +#+ */
|
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/13 11:06:56 by rparodi #+# #+# */
|
/* Created: 2025/05/13 11:06:56 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/05/14 23:23:13 by omoudni ### ########.fr */
|
/* Updated: 2025/05/19 20:12:46 by omoudni ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/******************************************************************************/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "core.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
|
|
@ -19,6 +20,7 @@ class Server {
|
||||||
unsigned short int _port;
|
unsigned short int _port;
|
||||||
std::string _password;
|
std::string _password;
|
||||||
int server_fd;
|
int server_fd;
|
||||||
|
PollManager _pollManager;
|
||||||
public:
|
public:
|
||||||
Server();
|
Server();
|
||||||
Server(int port, const std::string &password);
|
Server(int port, const std::string &password);
|
||||||
|
|
|
||||||
50
sources/core/pollManager.cpp
Normal file
50
sources/core/pollManager.cpp
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
/******************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pollManager.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/05/19 19:15:48 by omoudni #+# #+# */
|
||||||
|
/* Updated: 2025/05/19 20:21:11 by omoudni ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#include "pollManager.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
PollManager::PollManager() {}
|
||||||
|
|
||||||
|
void PollManager::addFd(int fd, short events) {
|
||||||
|
struct pollfd pfd = {fd, events, 0};
|
||||||
|
_fds.push_back(pfd);
|
||||||
|
_fd_events[fd] = events;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PollManager::removeFd(int fd) {
|
||||||
|
for (std::vector<struct pollfd>::iterator it = _fds.begin(); it != _fds.end(); ) {
|
||||||
|
if (it->fd == fd)
|
||||||
|
it = _fds.erase(it);
|
||||||
|
else
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
_fd_events.erase(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PollManager::updateFd(int fd, short events) {
|
||||||
|
for (size_t i = 0; i < _fds.size(); ++i) {
|
||||||
|
if (_fds[i].fd == fd) {
|
||||||
|
_fds[i].events = events;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_fd_events[fd] = events;
|
||||||
|
}
|
||||||
|
|
||||||
|
int PollManager::pollEvents(int timeout) {
|
||||||
|
return poll(_fds.data(), _fds.size(), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<struct pollfd>& PollManager::getFds() const {
|
||||||
|
return _fds;
|
||||||
|
}
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
/* ************************************************************************** */
|
/******************************************************************************/
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* server.cpp :+: :+: :+: */
|
/* server.cpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: omoudni <omoudni@student.42.fr> +#+ +:+ +#+ */
|
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
|
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/05/14 23:32:21 by omoudni ### ########.fr */
|
/* Updated: 2025/05/19 20:16:00 by omoudni ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/******************************************************************************/
|
||||||
|
|
||||||
#include "color.hpp"
|
#include "color.hpp"
|
||||||
#include "server.hpp"
|
#include "server.hpp"
|
||||||
|
|
@ -67,16 +67,47 @@ void Server::start() {
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
_pollManager.addFd(server_fd, POLLIN);
|
||||||
std::cout << CLR_GREEN << "Server started on port " << this->_port << CLR_RESET << std::endl;
|
std::cout << CLR_GREEN << "Server started on port " << this->_port << CLR_RESET << std::endl;
|
||||||
std::cout << CLR_GREEN << "Waiting for clients..." << CLR_RESET << std::endl;
|
std::cout << CLR_GREEN << "Waiting for clients..." << CLR_RESET << std::endl;
|
||||||
while (true) {
|
while (true) {
|
||||||
int client_fd = accept(server_fd, NULL, NULL);
|
int n = _pollManager.pollEvents();
|
||||||
if (client_fd == -1) {
|
if (n < 0) {
|
||||||
std::cerr << CLR_RED << "Error: Failed to accept client" << CLR_RESET << std::endl;
|
std::cerr << CLR_RED << "Poll error" << CLR_RESET << std::endl;
|
||||||
continue;
|
break;
|
||||||
}
|
}
|
||||||
|
const std::vector<struct pollfd>& fds = _pollManager.getFds();
|
||||||
|
for (size_t i = 0; i < fds.size(); ++i) {
|
||||||
|
if (fds[i].revents & POLLIN) {
|
||||||
|
if (fds[i].fd == server_fd) {
|
||||||
|
// Accept new client
|
||||||
|
int client_fd = accept(server_fd, NULL, NULL);
|
||||||
|
if (client_fd != -1) {
|
||||||
|
_pollManager.addFd(client_fd, POLLIN);
|
||||||
std::cout << CLR_GREEN << "Client connected" << CLR_RESET << std::endl;
|
std::cout << CLR_GREEN << "Client connected" << CLR_RESET << std::endl;
|
||||||
close(client_fd);
|
std::cout << "Welcome to IRC. Your client_id is: " << client_fd << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cerr << CLR_RED << "Error: Failed to accept client" << CLR_RESET << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Handle client communication
|
||||||
|
char buffer[1024];
|
||||||
|
ssize_t bytes_received = recv(fds[i].fd, buffer, sizeof(buffer) - 1, 0);
|
||||||
|
if (bytes_received <= 0) {
|
||||||
|
// Client disconnected
|
||||||
|
// if it's negative, it means an error occurred, maybe print it with perror?
|
||||||
|
std::cout << CLR_RED << "Client disconnected" << CLR_RESET << std::endl;
|
||||||
|
close(fds[i].fd);
|
||||||
|
_pollManager.removeFd(fds[i].fd);
|
||||||
|
} else {
|
||||||
|
buffer[bytes_received] = '\0';
|
||||||
|
std::cout << "Received: " << buffer << std::endl;
|
||||||
|
// Optionally: send response to client here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
std::cout << CLR_GREEN << "Server stopped" << CLR_RESET << std::endl;
|
std::cout << CLR_GREEN << "Server stopped" << CLR_RESET << std::endl;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue