Parser accept server functions, and first stage for implementing client.send function #20

Merged
aortigos merged 1 commits from implement-pass-command into main 2026-05-14 15:24:47 +00:00
3 changed files with 16 additions and 20 deletions

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* Server.cpp :+: :+: :+: */ /* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */ /* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
/* Updated: 2026/05/12 20:34:58 by iherman- ### ########.fr */ /* Updated: 2026/05/14 17:00:22 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -35,13 +35,6 @@
const int Server::kConnectionQueueLimit = 10; const int Server::kConnectionQueueLimit = 10;
// TEMPORARY TESTING FUNCTION
void echo(User& client, std::istringstream& input)
{
std::string message = "Server received: " + input.str();
send(client.getFd(), message.c_str(), message.size(), 0);
}
Server::Server() : Server::Server() :
port_(PORT_DEFAULT), port_(PORT_DEFAULT),
password_("password") password_("password")
@@ -119,7 +112,7 @@ Server::Server(int port, const std::string& password) :
} }
// Add all new commands to commands_ here // Add all new commands to commands_ here
commands_["echo"] = &echo; commands_["PASS"] = &Server::pass_cmd;
std::cout << "Server port: " << port_ std::cout << "Server port: " << port_
<< "\nServer Password: " << password_ << "\nServer Password: " << password_
@@ -154,7 +147,7 @@ void Server::parseCommand(User& client)
args >> command; args >> command;
std::map<std::string, void (*)(User&, std::istringstream&)>::iterator it = commands_.find(command); std::map<std::string, void (Server::*)(User&, std::istringstream&)>::iterator it = commands_.find(command);
if (it == commands_.end()) if (it == commands_.end())
{ {
std::string message = "Error: command not found!\n"; std::string message = "Error: command not found!\n";
@@ -162,7 +155,7 @@ void Server::parseCommand(User& client)
client.clearBuffer(); client.clearBuffer();
return ; return ;
} }
it->second(client, args); (this->*(it->second))(client, args);
client.clearBuffer(); client.clearBuffer();
} }

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* Server.hpp :+: :+: :+: */ /* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */ /* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
/* Updated: 2026/05/12 19:57:32 by iherman- ### ########.fr */ /* Updated: 2026/05/14 16:58:31 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -39,7 +39,7 @@ class Server
std::vector<struct pollfd> sockets_; std::vector<struct pollfd> sockets_;
std::map<int, User> clients_; std::map<int, User> clients_;
std::map<std::string, void (*)(User&, std::istringstream&)> commands_; std::map<std::string, void (Server::*)(User&, std::istringstream&)> commands_;
bool handleClient(User& client); bool handleClient(User& client);
@@ -51,6 +51,10 @@ class Server
Server(const Server& other); Server(const Server& other);
Server &operator=(const Server& other); Server &operator=(const Server& other);
// Commands
void pass_cmd(User &client, std::istringstream &ss);
public: public:
Server(); Server();
Server(int port, const std::string& password); Server(int port, const std::string& password);

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */ /* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
/* Updated: 2026/05/10 22:45:31 by aortigos ### ########.fr */ /* Updated: 2026/05/14 17:23:56 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -20,15 +20,14 @@ void Server::pass_cmd(User &client, std::istringstream &ss)
if (client.isAuthenticated()) if (client.isAuthenticated())
{ {
// Still need to find what server should reply to different errors // Still need to find what server should reply to different errors
std::string res = "You are already authenticated"; client.send("You are already logged in\r\n");
send(client.getFd(), res.c_str(), res.size(), 0);
return ; return ;
} }
if (this->password_ == args) if (this->password_ == args)
client->authenticated = true; client.setAuthenticated(true);
else { else {
// Still need to find what server should reply to different errors // Still need to find what server should reply to different errors
std::string res = "Invalid password"; client.send("Invalid password\r\n");
send(client.getFd(), res.c_str(), res.size(), 0); send(client.getFd(), res.c_str(), res.size(), 0);
} }
} }