update server.start(), server.poll().polloop() and /JOIN ok
This commit is contained in:
parent
150ada2f4e
commit
3aad000a49
10 changed files with 122 additions and 77 deletions
|
|
@ -1,14 +1,14 @@
|
|||
/******************************************************************************/
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* PollManager.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/19 19:15:13 by omoudni #+# #+# */
|
||||
/* Updated: 2025/05/22 17:30:00 by omoudni ### ########.fr */
|
||||
/* Updated: 2025/06/18 01:26:38 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
|
|
@ -26,7 +26,8 @@ public:
|
|||
void addClient(short unsigned fd);
|
||||
void removeClient(short unsigned fd);
|
||||
void updateServer(short unsigned fd);
|
||||
void pollLoop(int server_fd, std::vector<int> &newClients, std::vector<int> &disconnected, std::vector<std::pair<int, std::string> > &readyClients);
|
||||
void pollLoop(int server_fd, std::vector<int> &newClients, std::vector<int> &disconnected, std::vector<std::pair<int, std::string> > &readyClients, std::vector<int> &readyToWrite);
|
||||
void setWritable(short unsigned fd, bool enable);
|
||||
|
||||
private:
|
||||
std::vector<struct pollfd> _fds;
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/20 22:18:17 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/18 00:10:56 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/18 01:18:05 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "user.hpp"
|
||||
#include "server.hpp"
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
|
|
@ -44,7 +45,7 @@ class Channel {
|
|||
void removeOperator(User *user);
|
||||
|
||||
// utility functions
|
||||
void sendAllClientInAChannel(const std::string toSend);
|
||||
void sendAllClientInAChannel(const std::string toSend, User *sender);
|
||||
|
||||
private:
|
||||
std::string _name;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/17 23:16:58 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/18 01:09:21 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -36,9 +36,10 @@ public:
|
|||
Server(int port, const std::string &password);
|
||||
~Server();
|
||||
void start();
|
||||
unsigned short int getPort() const;
|
||||
std::list<User *> getUsersList() const;
|
||||
std::list<Channel *>& getChannelsList();
|
||||
std::list<User *> getUsersList() const;
|
||||
PollManager& getPollManager();
|
||||
unsigned short int getPort() const;
|
||||
std::string getPassword() const;
|
||||
void showInfo() const;
|
||||
void printUsers() const;
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/17 15:59:19 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/18 01:19:32 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "PollManager.hpp"
|
||||
|
||||
class User
|
||||
{
|
||||
|
|
@ -26,14 +27,17 @@ class User
|
|||
std::string _username;
|
||||
std::string _realname;
|
||||
std::string _ipAdress;
|
||||
|
||||
bool _hasNick;
|
||||
bool _hasUser;
|
||||
bool _hasPass;
|
||||
bool _passReceived;
|
||||
bool _passIsValid;
|
||||
|
||||
PollManager& _poll;
|
||||
|
||||
public:
|
||||
User(short unsigned fd);
|
||||
User(short unsigned fd, PollManager& poll);
|
||||
short unsigned int getFd() const;
|
||||
bool isReadyToSend() const;
|
||||
bool isRegistered() const;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/20 22:43:24 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/18 00:10:48 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/18 01:17:37 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -219,8 +219,11 @@ void Channel::removeOperator(User *user) {
|
|||
}
|
||||
}
|
||||
|
||||
void Channel::sendAllClientInAChannel(const std::string toSend) {
|
||||
void Channel::sendAllClientInAChannel(const std::string toSend, User *sender) {
|
||||
for(std::list<User *>::iterator it = this->_users.begin(); it != this->_users.end(); ++it) {
|
||||
if (*it == sender) {
|
||||
continue;
|
||||
}
|
||||
(*it)->appendToWriteBuffer(toSend);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/17 23:55:31 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/18 00:58:16 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -88,5 +88,5 @@ void Join::execute() {
|
|||
for (std::list<User *>::iterator it = _cTarget->getUsers().begin(); it != _cTarget->getUsers().end(); ++it) {
|
||||
msg353 += (*it)->getNickname() + " ";
|
||||
}
|
||||
_cTarget->sendAllClientInAChannel(msgJoin + msg332 + msg353 + "\r\n");
|
||||
_cTarget->sendAllClientInAChannel(msgJoin + msg332 + msg353 + "\r\n", _sender);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/12 13:25:02 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/18 00:56:49 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,30 +16,31 @@
|
|||
|
||||
using namespace cmd;
|
||||
|
||||
// exemple:
|
||||
// PRIVMSG #samy :salut
|
||||
|
||||
e_code PrivMsg::checkArgs() {
|
||||
if (_args.size() < 3) {
|
||||
WARNING_MSG("Not enough arguments for PRIVMSG command");
|
||||
return ERR_NEEDMOREPARAMS;
|
||||
}
|
||||
if (_args.at(1).at(0) != '#') {
|
||||
_uTarget = searchList(this->_users, _args.at(2));
|
||||
if (this->_uTarget == NULL) {
|
||||
WARNING_MSG("User not found");
|
||||
return ERR_NOSUCHNICK;
|
||||
}
|
||||
if (this->_uTarget->isRegistered() == false) {
|
||||
WARNING_MSG("User is not registered for PRIVMSG command");
|
||||
INFO_MSG("You can only privmsg registered users");
|
||||
return ERR_NOSUCHNICK;
|
||||
}
|
||||
} else {
|
||||
_cTarget = searchList(_channels, _args.at(1));
|
||||
|
||||
std::string target = _args.at(1);
|
||||
|
||||
if (target[0] == '#') {
|
||||
target.erase(0, 1); // Enlève le '#'
|
||||
_cTarget = searchList(_server->getChannelsList(), target);
|
||||
if (_cTarget == NULL) {
|
||||
WARNING_MSG("Channel not found for PRIVMSG command");
|
||||
INFO_MSG("You can only privmsg users to channels you are in");
|
||||
return ERR_NOSUCHCHANNEL;
|
||||
} else
|
||||
_args.at(1).erase(0, 1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
_uTarget = searchList(_users, target);
|
||||
if (_uTarget == NULL || !_uTarget->isRegistered()) {
|
||||
WARNING_MSG("User not found or not registered");
|
||||
return ERR_NOSUCHNICK;
|
||||
}
|
||||
}
|
||||
return _PARSING_OK;
|
||||
}
|
||||
|
|
@ -48,10 +49,26 @@ e_code PrivMsg::checkArgs() {
|
|||
* @brief Execute the PrivMsg command
|
||||
* @note To send a private message to a user / a channel
|
||||
*/
|
||||
|
||||
void PrivMsg::execute() {
|
||||
if (checkArgs() != _PARSING_OK) {
|
||||
ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)");
|
||||
return;
|
||||
}
|
||||
// check how the com
|
||||
|
||||
std::string target = _args.at(1);
|
||||
std::string content = _args.at(2);
|
||||
std::string msg = ":" + _sender->getPrefix() + " PRIVMSG " + target + " :" + content + "\r\n";
|
||||
|
||||
// Envoi vers un channel
|
||||
if (target[0] == '#') {
|
||||
target.erase(0, 1); // Enlève le #
|
||||
if (_cTarget)
|
||||
_cTarget->sendAllClientInAChannel(msg, _sender); // Optionnel: évite d'envoyer au sender
|
||||
}
|
||||
// Envoi vers un user
|
||||
else {
|
||||
if (_uTarget)
|
||||
_uTarget->appendToWriteBuffer(msg);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ PollManager::~PollManager()
|
|||
}
|
||||
}
|
||||
|
||||
void PollManager::pollLoop(int server_fd, std::vector<int> &newClients, std::vector<int> &disconnected, std::vector<std::pair<int, std::string> > &readyClients)
|
||||
void PollManager::pollLoop(int server_fd, std::vector<int> &newClients, std::vector<int> &disconnected, std::vector<std::pair<int, std::string> > &readyClients, std::vector<int> &readyToWrite)
|
||||
{
|
||||
int poll_count = poll(&_fds[0], _fds.size(), -1);
|
||||
if (poll_count == -1)
|
||||
|
|
@ -59,6 +59,8 @@ void PollManager::pollLoop(int server_fd, std::vector<int> &newClients, std::vec
|
|||
--i;
|
||||
}
|
||||
}
|
||||
else if (_fds[i].revents & POLLOUT)
|
||||
readyToWrite.push_back(fd);
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
|
@ -94,3 +96,15 @@ void PollManager::setServerFd(int fd)
|
|||
pfd.events = POLLIN;
|
||||
_fds.push_back(pfd);
|
||||
}
|
||||
|
||||
void PollManager::setWritable(short unsigned fd, bool enable) {
|
||||
for (size_t i = 0; i < _fds.size(); ++i) {
|
||||
if (_fds[i].fd == fd) {
|
||||
if (enable)
|
||||
_fds[i].events |= POLLOUT;
|
||||
else
|
||||
_fds[i].events &= ~POLLOUT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
|
||||
/* Updated: 2025/06/18 00:10:14 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/18 01:34:56 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -54,8 +54,7 @@ std::vector<std::string> splitLines(const std::string& input);
|
|||
void Server::start()
|
||||
{
|
||||
_serverFd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (_serverFd == -1)
|
||||
{
|
||||
if (_serverFd == -1) {
|
||||
std::cerr << "Erreur socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
|
@ -67,73 +66,76 @@ void Server::start()
|
|||
addr.sin_port = htons(_port);
|
||||
|
||||
if (bind(_serverFd, (sockaddr *)&addr, sizeof(addr)) == -1 ||
|
||||
listen(_serverFd, 10) == -1)
|
||||
{
|
||||
listen(_serverFd, 10) == -1) {
|
||||
std::cerr << "Erreur bind/listen" << std::endl;
|
||||
close(_serverFd);
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Serveur lancé sur le port " << _port << std::endl;
|
||||
|
||||
_pollManager.setServerFd(_serverFd);
|
||||
std::vector<int> newClients;
|
||||
std::vector<int> disconnected;
|
||||
std::vector<std::pair<int, std::string > > readyClients;
|
||||
while (true)
|
||||
{
|
||||
std::vector<int> readyToWrite;
|
||||
|
||||
while (true) {
|
||||
printUsers();
|
||||
|
||||
newClients.clear();
|
||||
disconnected.clear();
|
||||
readyClients.clear();
|
||||
_pollManager.pollLoop(_serverFd, newClients, disconnected,
|
||||
readyClients);
|
||||
std::cout << "New clients: " << newClients.size() << std::endl;
|
||||
for (size_t i = 0; i < newClients.size(); ++i)
|
||||
{
|
||||
_users[newClients[i]] = new User(newClients[i]);
|
||||
readyToWrite.clear();
|
||||
|
||||
_pollManager.pollLoop(_serverFd, newClients, disconnected, readyClients, readyToWrite);
|
||||
|
||||
for (size_t i = 0; i < newClients.size(); ++i) {
|
||||
_users[newClients[i]] = new User(newClients[i], _pollManager);
|
||||
}
|
||||
// Handle disconnected clients
|
||||
for (size_t i = 0; i < disconnected.size(); ++i)
|
||||
{
|
||||
|
||||
for (size_t i = 0; i < disconnected.size(); ++i) {
|
||||
disconnectClient(disconnected[i]);
|
||||
}
|
||||
for (size_t i = 0; i < readyClients.size(); ++i)
|
||||
{
|
||||
|
||||
for (size_t i = 0; i < readyClients.size(); ++i) {
|
||||
int fd = readyClients[i].first;
|
||||
const std::string &data = readyClients[i].second;
|
||||
if (_users.count(fd))
|
||||
{
|
||||
|
||||
if (_users.count(fd)) {
|
||||
_users[fd]->appendToReadBuffer(data);
|
||||
std::string rawCmd;
|
||||
while (!(rawCmd = _users[fd]->extractFullCommand()).empty())
|
||||
{
|
||||
|
||||
while (!(rawCmd = _users[fd]->extractFullCommand()).empty()) {
|
||||
std::vector<std::string> lines = splitLines(rawCmd);
|
||||
for (size_t i = 0; i < lines.size(); ++i) {
|
||||
std::cout << "Client " << fd << " says: " << lines[i] << std::endl;
|
||||
cmd::dispatch(_users[fd], NULL, this, lines[i]);
|
||||
}
|
||||
// This prints every command/message received from any client
|
||||
}
|
||||
}
|
||||
}
|
||||
for (std::vector<std::pair<int, std::string > > ::iterator it = readyClients.begin(); it != readyClients.end(); ++it)
|
||||
{
|
||||
int fd = it->first;
|
||||
if (_users.count(fd) && _users[fd]->isReadyToSend())
|
||||
{
|
||||
|
||||
for (size_t i = 0; i < readyToWrite.size(); ++i) {
|
||||
int fd = readyToWrite[i];
|
||||
if (_users.count(fd)) {
|
||||
std::string writeBuffer = _users[fd]->getWriteBuffer();
|
||||
ssize_t bytesSent = send(fd, writeBuffer.c_str(), writeBuffer.size(), 0);
|
||||
if (bytesSent < 0)
|
||||
{
|
||||
std::cerr << "Erreur send" << std::endl;
|
||||
}
|
||||
else {
|
||||
if (!writeBuffer.empty()) {
|
||||
ssize_t bytesSent = send(fd, writeBuffer.c_str(), writeBuffer.size(), MSG_NOSIGNAL);
|
||||
if (bytesSent < 0) {
|
||||
std::cerr << "Erreur send sur fd " << fd << std::endl;
|
||||
disconnectClient(fd);
|
||||
} else {
|
||||
_users[fd]->clearWriteBuffer();
|
||||
_pollManager.setWritable(fd, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Poll loop finished" << std::endl;
|
||||
}
|
||||
|
||||
close(_serverFd);
|
||||
}
|
||||
|
||||
|
|
@ -185,7 +187,6 @@ void Server::printUsers() const
|
|||
std::string Server::getPassword() const { return this->_password; }
|
||||
|
||||
std::list<User *> Server::getUsersList() const {
|
||||
WARNING_MSG("TO DO FILL")
|
||||
std::list<User*> userList;
|
||||
for (std::map<int, User*>::const_iterator it = _users.begin(); it != _users.end(); ++it) {
|
||||
userList.push_back(it->second);
|
||||
|
|
@ -218,3 +219,5 @@ void Server::disconnectClient(int fd) {
|
|||
delete user;
|
||||
_users.erase(fd);
|
||||
}
|
||||
|
||||
PollManager& Server::getPollManager() { return _pollManager; }
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */
|
||||
/* Updated: 2025/06/17 17:42:40 by sben-tay ### ########.fr */
|
||||
/* Updated: 2025/06/18 01:19:52 by sben-tay ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -18,8 +18,8 @@
|
|||
#include <netdb.h>
|
||||
|
||||
// Constructor
|
||||
User::User(short unsigned int fd) : _fd(fd), _registered(false), _hasNick(false), _hasUser(false), \
|
||||
_hasPass(false), _passReceived(false), _passIsValid(false) {}
|
||||
User::User(short unsigned int fd, PollManager& poll) : _fd(fd), _registered(false), _hasNick(false), _hasUser(false), \
|
||||
_hasPass(false), _passReceived(false), _passIsValid(false), _poll(poll) {}
|
||||
|
||||
/**
|
||||
* @brief Getter for the fd
|
||||
|
|
@ -81,6 +81,7 @@ void User::appendToReadBuffer(const std::string &data)
|
|||
void User::appendToWriteBuffer(const std::string &data)
|
||||
{
|
||||
_write_buffer += data;
|
||||
_poll.setWritable(_fd, true);
|
||||
}
|
||||
|
||||
// Check registration
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue