Compare commits
2 Commits
644dbe92a8
...
78aa891d38
| Author | SHA1 | Date | |
|---|---|---|---|
| 78aa891d38 | |||
|
|
3578bd5b5c |
@@ -6,7 +6,7 @@
|
|||||||
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/11 20:34:33 by iherman- ### ########.fr */
|
/* Updated: 2026/05/12 19:25:29 by iherman- ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -35,6 +35,14 @@
|
|||||||
|
|
||||||
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")
|
||||||
@@ -56,6 +64,8 @@ Server::Server() :
|
|||||||
if (listen(serverSocket_, kConnectionQueueLimit))
|
if (listen(serverSocket_, kConnectionQueueLimit))
|
||||||
throw std::runtime_error("Failed to listen");
|
throw std::runtime_error("Failed to listen");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::cout << "Server port: " << port_
|
std::cout << "Server port: " << port_
|
||||||
<< "\nServer Password: " << password_
|
<< "\nServer Password: " << password_
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
@@ -96,6 +106,9 @@ Server::Server(int port, const std::string& password) :
|
|||||||
if (listen(serverSocket_, kConnectionQueueLimit))
|
if (listen(serverSocket_, kConnectionQueueLimit))
|
||||||
throw std::runtime_error("Failed to listen");
|
throw std::runtime_error("Failed to listen");
|
||||||
|
|
||||||
|
// Add all new commands to commands_ here
|
||||||
|
commands_["echo"] = &echo;
|
||||||
|
|
||||||
std::cout << "Server port: " << port_
|
std::cout << "Server port: " << port_
|
||||||
<< "\nServer Password: " << password_
|
<< "\nServer Password: " << password_
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
@@ -122,33 +135,22 @@ Server &Server::operator=(const Server& other)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEMPORARY TESTING FUNCTION
|
|
||||||
void echo(User& client)
|
|
||||||
{
|
|
||||||
std::string message = "Server received: " + client.getBuffer();
|
|
||||||
send(client.getFd(), message.c_str(), message.size(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Server::parseCommand(User& client)
|
void Server::parseCommand(User& client)
|
||||||
{
|
{
|
||||||
std::stringstream args(client.getBuffer());
|
std::istringstream args(client.getBuffer());
|
||||||
std::string command;
|
std::string command;
|
||||||
|
|
||||||
args >> command;
|
args >> command;
|
||||||
|
|
||||||
std::map<std::string, void (*)(User&)> commands;
|
std::map<std::string, void (*)(User&, std::istringstream&)>::iterator it = commands_.find(command);
|
||||||
// TEMP
|
if (it == commands_.end())
|
||||||
commands["echo"] = &echo;
|
|
||||||
|
|
||||||
std::map<std::string, void (*)(User&)>::iterator it = commands.find(command);
|
|
||||||
if (it == commands.end())
|
|
||||||
{
|
{
|
||||||
std::string message = "Error: command not found!";
|
std::string message = "Error: command not found!\n";
|
||||||
send(client.getFd(), message.c_str(), message.size(), 0);
|
send(client.getFd(), message.c_str(), message.size(), 0);
|
||||||
client.clearBuffer();
|
client.clearBuffer();
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
it->second(client);
|
it->second(client, args);
|
||||||
client.clearBuffer();
|
client.clearBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/11 18:11:59 by iherman- ### ########.fr */
|
/* Updated: 2026/05/12 19:57:32 by iherman- ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -39,6 +39,8 @@ 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_;
|
||||||
|
|
||||||
bool handleClient(User& client);
|
bool handleClient(User& client);
|
||||||
|
|
||||||
void addClient();
|
void addClient();
|
||||||
|
|||||||
Reference in New Issue
Block a user