Commit ef14f68d authored by louiz’'s avatar louiz’

Rename the network::Command class to network::Message

Makes a lot more sense
parent bf3c7bce
...@@ -15,9 +15,9 @@ void Client::install_callbacks() ...@@ -15,9 +15,9 @@ void Client::install_callbacks()
this->install_callback("TRANSFER", std::bind(&Client::transfer_init_callback, this, std::placeholders::_1)); this->install_callback("TRANSFER", std::bind(&Client::transfer_init_callback, this, std::placeholders::_1));
} }
void Client::transfer_init_callback(Command* received_command) void Client::transfer_init_callback(Message* received_message)
{ {
std::string arg(received_command->body, received_command->body_size); std::string arg(received_message->body, received_message->body_size);
std::vector<std::string> args; std::vector<std::string> args;
boost::split(args, arg, boost::is_any_of("|")); boost::split(args, arg, boost::is_any_of("|"));
if (args.size() != 3) if (args.size() != 3)
......
...@@ -37,7 +37,7 @@ private: ...@@ -37,7 +37,7 @@ private:
* Called when the server initiate a file transfer with us. * Called when the server initiate a file transfer with us.
* Install the temporary callback to receive each file chunk. * Install the temporary callback to receive each file chunk.
*/ */
void transfer_init_callback(Command*); void transfer_init_callback(Message*);
std::vector<TransferReceiver*> receivers; std::vector<TransferReceiver*> receivers;
boost::asio::io_service io_service; boost::asio::io_service io_service;
......
#include <logging/logging.hpp> #include <logging/logging.hpp>
#include <network/client_base.hpp> #include <network/client_base.hpp>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#if defined(_WIN32) || defined(_WIN64)
# include <WinBase.h>
#endif
/** /**
* Does nothing, it is just used to exit the io_service.run_one() after * Does nothing, it is just used to exit the io_service.run_one() after
...@@ -15,7 +12,7 @@ static void poll_timeout_handler(const boost::system::error_code&) ...@@ -15,7 +12,7 @@ static void poll_timeout_handler(const boost::system::error_code&)
ClientBase::ClientBase(): ClientBase::ClientBase():
BaseIoservice(), BaseIoservice(),
CommandHandler(io_service), MessageHandler(io_service),
timeout(io_service) timeout(io_service)
{ {
} }
...@@ -65,13 +62,13 @@ void ClientBase::connect_handler(std::function< void(void) > on_success, ...@@ -65,13 +62,13 @@ void ClientBase::connect_handler(std::function< void(void) > on_success,
} }
} }
void ClientBase::ping_callback(Command*) void ClientBase::ping_callback(Message*)
{ {
log_debug("Received PING"); log_debug("Received PING");
Command* command = new Command; Message* message = new Message;
command->set_name("PONG"); message->set_name("PONG");
this->send(command); this->send(message);
} }
void ClientBase::poll(long timeout) void ClientBase::poll(long timeout)
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* The main network class for the clients. * The main network class for the clients.
* Connects to the remote server, sends requests to it and * Connects to the remote server, sends requests to it and
* calls the appropriate callbacks when the answer is received (or * calls the appropriate callbacks when the answer is received (or
* when a standalone command is received from the server) * when a standalone message is received from the server)
* @class ClientBase * @class ClientBase
*/ */
...@@ -18,13 +18,13 @@ ...@@ -18,13 +18,13 @@
# define __CLIENT_BASE_HPP__ # define __CLIENT_BASE_HPP__
#include <network/base_ioservice.hpp> #include <network/base_ioservice.hpp>
#include <network/command_handler.hpp> #include <network/message_handler.hpp>
#include <network/ping_handler.hpp> #include <network/ping_handler.hpp>
#include <network/timed_event_handler.hpp> #include <network/timed_event_handler.hpp>
#include <network/command.hpp> #include <network/message.hpp>
#include <network/timed_event.hpp> #include <network/timed_event.hpp>
class ClientBase: public BaseIoservice, public CommandHandler, class ClientBase: public BaseIoservice, public MessageHandler,
public TimedEventHandler, public PingHandler public TimedEventHandler, public PingHandler
{ {
public: public:
...@@ -59,7 +59,7 @@ private: ...@@ -59,7 +59,7 @@ private:
/** /**
* Called when the server sends us a PING request. Sends a PONG back. * Called when the server sends us a PING request. Sends a PONG back.
*/ */
void ping_callback(Command*); void ping_callback(Message*);
boost::asio::deadline_timer timeout; boost::asio::deadline_timer timeout;
}; };
......
#include <network/command.hpp> #include <network/message.hpp>
#include <assert.h> #include <assert.h>
Command::Command(): Message::Message():
body(0), body(0),
body_size(0), body_size(0),
callback(nullptr) callback(nullptr)
{ {
} }
Command::Command(const Command& command) Message::Message(const Message& message)
{ {
this->callback = command.callback; this->callback = message.callback;
this->header = command.header; this->header = message.header;
this->name = command.name; this->name = message.name;
this->set_body(command.body, command.body_size); this->set_body(message.body, message.body_size);
} }
Command::~Command() Message::~Message()
{ {
delete[] this->body; delete[] this->body;
} }
void Command::set_body(const char* body, int size) void Message::set_body(const char* body, int size)
{ {
if (size == -1) if (size == -1)
size = ::strlen(body); size = ::strlen(body);
...@@ -30,17 +30,17 @@ void Command::set_body(const char* body, int size) ...@@ -30,17 +30,17 @@ void Command::set_body(const char* body, int size)
this->set_body_size(size); this->set_body_size(size);
} }
void Command::set_name(const std::string name) void Message::set_name(const std::string name)
{ {
this->name = name; this->name = name;
} }
void Command::set_body_size(int size) void Message::set_body_size(int size)
{ {
this->body_size = size; this->body_size = size;
} }
void Command::pack() void Message::pack()
{ {
assert(this->name.size() > 0); assert(this->name.size() > 0);
......
...@@ -3,23 +3,23 @@ ...@@ -3,23 +3,23 @@
*/ */
/** /**
* Represents one single network command. * Represents one single network message, for the base network protocol.
* A command has a header and a body (also called binary part, which can have * A message has a header and a body (also called binary part, which can have
* size of 0). * size of 0).
* The header is COMMAND_NAME.BODY_SIZE: * The header is MESSAGE_NAME.BODY_SIZE:
* The body can be anything, but it is BODY_SIZE bytes long. * The body can be anything, but it is BODY_SIZE bytes long.
* The \n char is not used anywhere in the protocol, and there's no delimiter * The \n char is not used anywhere in the protocol, and there's no delimiter
* between a body and the header of the next message. * between a body and the header of the next message.
* *
* To send a command, a Command object must be created anywhere and filled with * To send a message, a Message object must be created anywhere and filled with
* the correct data and then passed to the CommandHandler::send() method. * the correct data and then passed to the MessageHandler::send() method.
* The object will be deleted by the send_handler, after it has been * The object will be deleted by the send_handler, after it has been
* succesfully sent. * succesfully sent.
* *
* A Command object is passed by a CommandHandler to the callback associated * A Message object is passed by a MessageHandler to the callback associated
* with a command name. This callback is responsible for deleting the command * with a message name. This callback is responsible for deleting the message
* object. (MAYBE) * object. (MAYBE)
* @class Command * @class Message
*/ */
#include <string> #include <string>
...@@ -29,15 +29,15 @@ ...@@ -29,15 +29,15 @@
#include <sstream> #include <sstream>
#include <functional> #include <functional>
#ifndef __COMMAND_HPP__ #ifndef MESSAGE_HPP
# define __COMMAND_HPP__ # define MESSAGE_HPP
class Command class Message
{ {
public: public:
Command(); Message();
Command(const Command&); Message(const Message&);
~Command(); ~Message();
/** /**
* Sets the body of the message. A char* will be new[]ed using the size, and * Sets the body of the message. A char* will be new[]ed using the size, and
* the data will be copied in it. To avoid that copy, see the body attribute * the data will be copied in it. To avoid that copy, see the body attribute
...@@ -51,7 +51,7 @@ public: ...@@ -51,7 +51,7 @@ public:
*/ */
void set_body_size(int size); void set_body_size(int size);
/** /**
* This must be called before the object is passed to CommandHandler::send(), * This must be called before the object is passed to MessageHandler::send(),
* it will set the header correctly, using the body size etc. * it will set the header correctly, using the body size etc.
*/ */
void pack(); void pack();
...@@ -72,7 +72,7 @@ public: ...@@ -72,7 +72,7 @@ public:
std::function< void(void) > callback; std::function< void(void) > callback;
private: private:
Command& operator=(const Command&); Message& operator=(const Message&);
}; };
#endif // __COMMAND_HPP__ #endif // __MESSAGE_HPP__
#include <logging/logging.hpp> #include <logging/logging.hpp>
#include <network/command_handler.hpp> #include <network/message_handler.hpp>
#include <network/command.hpp> #include <network/message.hpp>
CommandHandler::CommandHandler(boost::asio::io_service& io_service): MessageHandler::MessageHandler(boost::asio::io_service& io_service):
BaseSocket(io_service), BaseSocket(io_service),
writing(false) writing(false)
{ {
} }
CommandHandler::~CommandHandler() MessageHandler::~MessageHandler()
{ {
} }
void CommandHandler::install_callback(const std::string& command, void MessageHandler::install_callback(const std::string& message,
t_read_callback callback) t_read_callback callback)
{ {
log_debug("installing callback for command " << command); log_debug("installing callback for message " << message);
this->callbacks[command] = callback; this->callbacks[message] = callback;
} }
void CommandHandler::install_callback_once(const std::string& command, void MessageHandler::install_callback_once(const std::string& message,
t_read_callback callback) t_read_callback callback)
{ {
log_debug("installing ONCE callback for command " << command); log_debug("installing ONCE callback for message " << message);
this->callbacks_once[command] = callback; this->callbacks_once[message] = callback;
} }
void CommandHandler::remove_callback(const std::string& command) void MessageHandler::remove_callback(const std::string& message)
{ {
std::map<const std::string, t_read_callback >::iterator it; auto it = this->callbacks.find(message);
it = this->callbacks.find(command);
if (it != this->callbacks.end()) if (it != this->callbacks.end())
this->callbacks.erase(it); this->callbacks.erase(it);
else else
log_warning("Could not remove callback: " << command); log_warning("Could not remove callback: " << message);
} }
t_read_callback CommandHandler::get_callback(const std::string& command) t_read_callback MessageHandler::get_callback(const std::string& message)
{ {
std::map<const std::string, t_read_callback >::iterator it; auto it = this->callbacks.find(message);
it = this->callbacks.find(command);
if (it != this->callbacks.end()) if (it != this->callbacks.end())
return it->second; return it->second;
it = this->callbacks_once.find(command); it = this->callbacks_once.find(message);
if (it != this->callbacks_once.end()) if (it != this->callbacks_once.end())
{ {
log_debug("Removing callback for command " << command); log_debug("Removing callback for message " << message);
t_read_callback callback = it->second; t_read_callback callback = it->second;
this->callbacks_once.erase(it); this->callbacks_once.erase(it);
return callback; return callback;
...@@ -56,7 +52,7 @@ t_read_callback CommandHandler::get_callback(const std::string& command) ...@@ -56,7 +52,7 @@ t_read_callback CommandHandler::get_callback(const std::string& command)
return 0; return 0;
} }
void CommandHandler::read_handler(const boost::system::error_code& error, const std::size_t bytes_transferred) void MessageHandler::read_handler(const boost::system::error_code& error, const std::size_t bytes_transferred)
{ {
log_debug("read_handler, size: " << bytes_transferred << " bytes."); log_debug("read_handler, size: " << bytes_transferred << " bytes.");
if (error) if (error)
...@@ -67,7 +63,7 @@ void CommandHandler::read_handler(const boost::system::error_code& error, const ...@@ -67,7 +63,7 @@ void CommandHandler::read_handler(const boost::system::error_code& error, const
} }
if (bytes_transferred <= 1) if (bytes_transferred <= 1)
{ {
log_warning("Not enough data received for a command header to be valid."); log_warning("Not enough data received for a message header to be valid.");
return ; return ;
} }
...@@ -81,40 +77,40 @@ void CommandHandler::read_handler(const boost::system::error_code& error, const ...@@ -81,40 +77,40 @@ void CommandHandler::read_handler(const boost::system::error_code& error, const
while (c[pos] && c[pos] != '.') while (c[pos] && c[pos] != '.')
pos++; pos++;
std::string command_name; std::string message_name;
std::size_t size; std::size_t size;
if (pos == bytes_transferred) if (pos == bytes_transferred)
{ // no '.' was found, the command name is assumed to 1 char long. { // no '.' was found, the message name is assumed to 1 char long.
command_name = std::string(1, c[0]); message_name = std::string(1, c[0]);
size = atoi(std::string(c+1, bytes_transferred-2).data());; size = atoi(std::string(c+1, bytes_transferred-2).data());;
} }
else else
{ {
command_name = std::string(c, pos); message_name = std::string(c, pos);
size = atoi(std::string(c+pos+1, bytes_transferred-pos-2).data()); // remove the ending : size = atoi(std::string(c+pos+1, bytes_transferred-pos-2).data()); // remove the ending :
} }
log_debug("Received : " << command_name << " . " << size); log_debug("Received : " << message_name << " . " << size);
Command* command = new Command; Message* message = new Message;
command->set_name(command_name); message->set_name(message_name);
delete[] c; delete[] c;
// Find out if a callback was registered for that command. // Find out if a callback was registered for that message.
t_read_callback callback = this->get_callback(command_name); t_read_callback callback = this->get_callback(message_name);
// We check what we need to read on the socket to have the rest of the binary datas // We check what we need to read on the socket to have the rest of the binary datas
const std::size_t length_to_read = this->data.size() >= size ? 0 : size - this->data.size(); const std::size_t length_to_read = this->data.size() >= size ? 0 : size - this->data.size();
boost::asio::async_read(this->socket, boost::asio::async_read(this->socket,
this->data, this->data,
boost::asio::transfer_at_least(length_to_read), boost::asio::transfer_at_least(length_to_read),
std::bind(&CommandHandler::binary_read_handler, this, std::bind(&MessageHandler::binary_read_handler, this,
std::placeholders::_1, std::placeholders::_1,
command, message,
size, callback)); size, callback));
} }
void CommandHandler::binary_read_handler(const boost::system::error_code& error, void MessageHandler::binary_read_handler(const boost::system::error_code& error,
Command* command, Message* message,
std::size_t bytes_transferred, std::size_t bytes_transferred,
t_read_callback callback) t_read_callback callback)
{ {
...@@ -124,84 +120,84 @@ void CommandHandler::binary_read_handler(const boost::system::error_code& error, ...@@ -124,84 +120,84 @@ void CommandHandler::binary_read_handler(const boost::system::error_code& error,
exit(1); exit(1);
} }
log_debug("binary_read_handler " << bytes_transferred); log_debug("binary_read_handler " << bytes_transferred);
command->body = new char[bytes_transferred]; message->body = new char[bytes_transferred];
this->data.sgetn(command->body, bytes_transferred); this->data.sgetn(message->body, bytes_transferred);
command->set_body_size(bytes_transferred); message->set_body_size(bytes_transferred);
if (callback) if (callback)
callback(command); callback(message);
else else
log_debug("no callback"); log_debug("no callback");
delete command; delete message;
this->install_read_handler(); this->install_read_handler();
} }
void CommandHandler::install_read_handler(void) void MessageHandler::install_read_handler(void)
{ {
boost::asio::async_read_until(this->socket, boost::asio::async_read_until(this->socket,
this->data, this->data,
':', ':',
std::bind(&CommandHandler::read_handler, this, std::bind(&MessageHandler::read_handler, this,
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2)); std::placeholders::_2));
} }
void CommandHandler::request_answer(Command* command, t_read_callback on_answer, std::string name) void MessageHandler::request_answer(Message* message, t_read_callback on_answer, std::string name)
{ {
if (name.size() == 0) if (name.size() == 0)
name = command->get_name(); name = message->get_name();
this->install_callback_once(name, on_answer); this->install_callback_once(name, on_answer);
this->send(command); this->send(message);
} }
void CommandHandler::send(Command* command, std::function< void(void) > on_sent) void MessageHandler::send(Message* message, std::function< void(void) > on_sent)
{ {
if (on_sent) if (on_sent)
command->callback = on_sent; message->callback = on_sent;
log_debug("Sending command: " << command->get_name()); log_debug("Sending message: " << message->get_name());
this->commands_to_send.push_front(command); this->messages_to_send.push_front(message);
this->check_commands_to_send(); this->check_messages_to_send();
} }
bool CommandHandler::check_commands_to_send() bool MessageHandler::check_messages_to_send()
{ {
log_debug("Length of the queue: " << this->commands_to_send.size()); log_debug("Length of the queue: " << this->messages_to_send.size());
if (this->writing || this->commands_to_send.empty()) if (this->writing || this->messages_to_send.empty())
return false; return false;
this->actually_send(this->commands_to_send.back()); this->actually_send(this->messages_to_send.back());
this->commands_to_send.pop_back(); this->messages_to_send.pop_back();
return true; return true;
} }
void CommandHandler::actually_send(Command* command) void MessageHandler::actually_send(Message* message)
{ {
this->writing = true; this->writing = true;
command->pack(); message->pack();
std::vector<boost::asio::const_buffer> buffs; std::vector<boost::asio::const_buffer> buffs;
buffs.push_back(boost::asio::buffer(command->header.data(), command->header.length())); buffs.push_back(boost::asio::buffer(message->header.data(), message->header.length()));
buffs.push_back(boost::asio::buffer(command->body, command->body_size)); buffs.push_back(boost::asio::buffer(message->body, message->body_size));
async_write(this->socket, async_write(this->socket,
buffs, buffs,
std::bind(&CommandHandler::send_handler, this, std::bind(&MessageHandler::send_handler, this,
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
command)); message));