diff --git a/Makefile b/Makefile index 86e4656..65a86fc 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: omoudni +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/05/02 15:40:00 by rparodi #+# #+# # -# Updated: 2025/05/21 21:51:13 by omoudni ### ########.fr # +# Updated: 2025/05/22 17:35:48 by omoudni ### ########.fr # # # #******************************************************************************# @@ -30,7 +30,7 @@ SRC = sources/core/logs.cpp \ sources/core/parser.cpp \ sources/core/main.cpp \ sources/core/Server.cpp \ - sources/core/user.cpp \ + sources/user/user.cpp \ sources/channel/channel.cpp INC_DIR = include/core \ diff --git a/diagram.puml b/diagram.puml index 93491fb..21e7a04 100644 --- a/diagram.puml +++ b/diagram.puml @@ -10,12 +10,13 @@ class "main()" ' ======================== class Server { - _port : int + - _serverFd : int - _password : string - - _server_fd : int - - _poll : PollManager - - _users : map + - _pollManager : PollManager + - _users : map + Server(port : int, password : string) + + ~Server() + start() : void + getPort() : int + showInfo() : void @@ -27,36 +28,41 @@ class Server { class PollManager { - _fds : vector - + addUser(fd : int) : void - + removeUser(fd : int) : void - + updateUser(fd : int, events : short) : void - + pollLoop(server_fd : int, newClients : std::vector, disconnected : std::vector) : void - + readFromUser(fd : int) : void - + writeToUser(fd : int) : void + + PollManager() + + ~PollManager() + + setServerFd(fd : int) : void + + addClient(fd : short unsigned) : void + + removeClient(fd : short unsigned) : void + + updateServer(fd : short unsigned) : void + + pollLoop(server_fd : int, newClients : vector, disconnected : vector, readyClients : vector>) : void } ' ======================== ' CLASS: User ' ======================== class User { - - _fd : int - - _nickname : string - - _username : string - - _hostname : string - - _readBuffer : string - - _writeBuffer : string + - _fd : short unsigned int - _registered : bool - - + getFd() : int - + getNickname() : string - + setNickname(name : string) : void - + appendToReadBuffer(data : string) : void - + appendToWriteBuffer(data : string) : void - + extractFullCommand() : string + - _nickname : string + - _hostname : string + - _read_buffer : string + - _write_buffer : string + - _username : string + - _hasNick : bool + - _hasUser : bool + + + User(fd : short unsigned int) + + getFd() : short unsigned int + isReadyToSend() : bool + isRegistered() : bool + + getNickname() : string + + extractFullCommand() : string + + appendToReadBuffer(data : string) : void + + appendToWriteBuffer(data : string) : void + + setNickname(nickname : string) : void + + setUsername(username : string) : void + + checkRegistration() : void } - ' ======================== ' CLASS: Channel ' ======================== diff --git a/include/PollManager.hpp b/include/PollManager.hpp index 4b7623a..562614c 100644 --- a/include/PollManager.hpp +++ b/include/PollManager.hpp @@ -6,7 +6,7 @@ /* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/19 19:15:13 by omoudni #+# #+# */ -/* Updated: 2025/05/21 21:34:20 by omoudni ### ########.fr */ +/* Updated: 2025/05/22 17:30:00 by omoudni ### ########.fr */ /* */ /******************************************************************************/ @@ -22,6 +22,7 @@ public: PollManager(); ~PollManager(); + void setServerFd(int fd); void addClient(short unsigned fd); void removeClient(short unsigned fd); void updateServer(short unsigned fd); diff --git a/sources/core/PollManager.cpp b/sources/core/PollManager.cpp index 9e916be..54d5702 100644 --- a/sources/core/PollManager.cpp +++ b/sources/core/PollManager.cpp @@ -20,15 +20,6 @@ PollManager::~PollManager() void PollManager::pollLoop(int server_fd, std::vector &newClients, std::vector &disconnected, std::vector > &readyClients) { - - struct pollfd server_pollfd; - server_pollfd.fd = server_fd; - server_pollfd.events = POLLIN; - _fds.push_back(server_pollfd); - - std::cout << "Serveur prêt à accepter des connexions..." << std::endl; - - // while (true) { int poll_count = poll(&_fds[0], _fds.size(), -1); if (poll_count == -1) { @@ -38,8 +29,8 @@ void PollManager::pollLoop(int server_fd, std::vector &newClients, std::vec } for (size_t i = 0; i < _fds.size(); ++i) { - short unsigned fd = _fds[i].fd; - + short unsigned fd = _fds[i].fd; + std::cout << "I'm here 1" << std::endl; if ((fd == server_fd) && (_fds[i].revents & POLLIN)) { int client_fd = accept(server_fd, NULL, NULL); @@ -48,17 +39,19 @@ void PollManager::pollLoop(int server_fd, std::vector &newClients, std::vec std::cerr << "Error accept()" << std::endl; continue; } - addClient(client_fd); + std::cout << "I'm here 2" << std::endl; + addClient(client_fd); newClients.push_back(client_fd); - } - else if (_fds[i].revents & POLLIN) - { + } else if (_fds[i].revents & POLLIN) { + std::cout << "I'm here 3" << std::endl; char buffer[1024]; ssize_t bytes = recv(fd, buffer, sizeof(buffer) - 1, 0); if (bytes > 0) { buffer[bytes] = '\0'; readyClients.push_back(std::make_pair(fd, std::string(buffer))); + std::cout << "Received data from fd " << fd << ": " << buffer << std::endl; + std::cout << std::flush; } else { @@ -93,3 +86,11 @@ void PollManager::removeClient(short unsigned fd) close(fd); std::cout << "Client disconnected (fd " << fd << ")" << std::endl; } + +void PollManager::setServerFd(int fd) +{ + struct pollfd pfd; + pfd.fd = fd; + pfd.events = POLLIN; + _fds.push_back(pfd); +} diff --git a/sources/core/Server.cpp b/sources/core/Server.cpp index a371d15..9093ff5 100644 --- a/sources/core/Server.cpp +++ b/sources/core/Server.cpp @@ -6,13 +6,14 @@ /* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ -/* Updated: 2025/05/21 21:50:03 by omoudni ### ########.fr */ +/* Updated: 2025/05/22 17:31:42 by omoudni ### ########.fr */ /* */ /******************************************************************************/ #include "color.hpp" #include "server.hpp" #include "core.hpp" +#include "PollManager.hpp" #include #include #include @@ -64,6 +65,7 @@ void Server::start() { } std::cout << "Serveur lancé sur le port " << _port << std::endl; + _pollManager.setServerFd(_serverFd); std::vector newClients; std::vector disconnected; std::vector > readyClients; @@ -72,11 +74,12 @@ void Server::start() { newClients.clear(); disconnected.clear(); readyClients.clear(); - _pollManager.pollLoop(_serverFd, newClients, disconnected, readyClients); - + _pollManager.pollLoop(_serverFd, newClients, disconnected, + readyClients); + std::cout << "Poll loop finished" << std::endl; // Handle new clients for (size_t i = 0; i < newClients.size(); ++i) - _users[newClients[i]] = new User(newClients[i]); + _users[newClients[i]] = new User(newClients[i]); // Handle disconnected clients for (size_t i = 0; i < disconnected.size(); ++i) { diff --git a/sources/core/user.cpp b/sources/user/user.cpp similarity index 87% rename from sources/core/user.cpp rename to sources/user/user.cpp index 2ae0172..3465de6 100644 --- a/sources/core/user.cpp +++ b/sources/user/user.cpp @@ -6,7 +6,7 @@ /* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */ -/* Updated: 2025/05/21 21:44:58 by omoudni ### ########.fr */ +/* Updated: 2025/05/22 17:13:35 by omoudni ### ########.fr */ /* */ /******************************************************************************/ @@ -95,14 +95,17 @@ bool User::isReadyToSend() const } // Extract full command from read buffer -std::string User::extractFullCommand() -{ +std::string User::extractFullCommand() { std::string command; size_t pos = _read_buffer.find("\r\n"); - if (pos != std::string::npos) - { + if (pos == std::string::npos) + pos = _read_buffer.find("\n"); // fallback + + if (pos != std::string::npos) { command = _read_buffer.substr(0, pos); - _read_buffer.erase(0, pos + 2); + _read_buffer.erase(0, pos + 1); + if (_read_buffer[pos] == '\r') // clean up stray \r + _read_buffer.erase(0, 1); } return command; -} \ No newline at end of file +}