Commit 1bf308b7 authored by louiz’'s avatar louiz’

Add comments to all other modules than database.

parent b283ecad
/** @addtogroup Config
* @{
*/
/**
* Read the config file and save all the values in a map.
* Also, a singleton.
* Config::read_conf("some_path"); should be called first, then
* Config::get() should be used to access the values in the conf.
* @class Config
*/
#ifndef __CONFIG_HPP__
# define __CONFIG_HPP__
......@@ -7,28 +19,42 @@
#include <string>
#include <boost/algorithm/string/trim_all.hpp>
// Read the config file and save all the values in
// a map.
// Also, a singleton
// Config::read_conf("some_path"); should be called first, then
// Config::get() should be used to access the values in the conf.
class Config
{
public:
~Config(){};
/**
* returns a value from the config. If it doesn’t exist, use
* the second argument as the default.
* @param option The option we want
* @param def The default value in case the option does not exist
*/
static std::string get(const std::string&, const std::string&);
/**
* Set a value for the given option. And write all the config
* in the file from which it was read.
* @param option The option to set
* @param value The value to use
*/
static void set(const std::string&, const std::string&);
/**
* Read the configuration file at the given path.
* @param filename The path to open and read.
*/
static bool read_conf(const std::string&);
static Config* instance;
private:
Config(){};
Config(const Config &){};
/**
* Write all the config values into the configuration file
*/
void save_to_file();
std::string filename;
std::string filename;
std::map<std::string, std::string> values;
};
#endif // __CONFIG_HPP__
/**@}*/
......@@ -48,39 +48,48 @@ public:
*/
static Database* inst();
/**
* Get an object of the database.
* @param const std::string& columns The database columns to fetch.
* @param const std::string& table The database table to query.
* @param const std::string& where The where query.
* @retval DbObject*
* Get an object of the database. If the SQL query would return more than one
* row, you should use get_objects instead.
* If there’s no row, it returns NULL.
* @param columns The database columns to fetch.
* @param table The database table to query.
* @param where The where query.
* @return DbObject*
*/
DbObject* get_object(const std::string&, const std::string&, const std::string&) const;
/**
* Get objects of the database.
* @param const std::string& columns
* @param const std::string& table
* @param const std::string& where
* Get a list of objects of the database.
* If no row matches, the returned vector is empty, otherwise it contains
* one or more objects.
* @param columns The database columns to fetch.
* @param table The database table to query.
* @param where The where query.
* @return std::vector<DbObject*>
*/
std::vector<DbObject*> get_objects(const std::string&, const std::string&, const std::string&) const;
/**
* Query the database.
* @param const std::string& query
* @return MYSQL_RES*
*/
MYSQL_RES* do_query(const std::string&) const;
/**
* Update a database object (insert it if it doesn’t already exist
* or create it if it’s new).
* @return bool
*/
bool const update(const DbObject* object, const std::string&) const;
bool do_update(const std::string&) const;
private:
/**
* Do a SELECT query on the database and get the results. Returns NULL if an
* error occured or if there are no result.
* @param query The SQL query.
* @return MYSQL_RES*
*/
MYSQL_RES* do_query(const std::string&) const;
/**
* Do a UPDATE or INSERT query on the database.
* Returns false if it failed.
* @param query The SQL query.
*/
bool do_update(const std::string&) const;
Database();
void connect();
DbObject query_object(std::string const&, bool);
void close();
static Database* instance;
......
/** @addtogroup Logging
* @{
*/
/**
* Singleton used in logger macros to write into files or stdout, with
* various levels of severity.
* Only the macros should be used.
* @class Logger
*/
#include <config/config.hpp>
#include <iostream>
......@@ -13,6 +25,11 @@
#define WHERE\
__FILE__ << ":" << __LINE__
/**
* @def log_debug
* Log a debug message. If the code is compiled as non-debug,
* the argument is not even computed and nothing happens.
*/
#ifdef DEBUG
# define log_debug(text)
#else
......@@ -20,13 +37,28 @@
Logger::inst()->stream(debug_lvl) << "[DEBUG]:" << WHERE << ":\t\t" << text << std::endl;
#endif
/**
* @def log_info
* Log an info message
*/
#define log_info(text)\
Logger::inst()->stream(info_lvl) << "[INFO]:" << WHERE << ":\t\t" << text << std::endl;
/**
* @def log_warning
* Log a warning message
*/
#define log_warning(text)\
Logger::inst()->stream(warning_lvl) << "[WARNING]:" << WHERE << ":\t\t" << text << std::endl;
/**
* @def log_error
* Log an error message
*/
#define log_error(text)\
Logger::inst()->stream(error_lvl) << "[ERROR]:" << WHERE << ":\t\t" << text << std::endl;
/**
* Juste a structure representing a stream doing nothing with its input.
*/
struct nullstream: public std::ostream
{
nullstream():
......@@ -34,8 +66,6 @@ struct nullstream: public std::ostream
{ }
};
// Singleton used in logger macros to write into files or stdout, with
// various levels of severity.
class Logger
{
public:
......@@ -55,3 +85,4 @@ private:
};
#endif // __LOGGING_HPP__
/**@}*/
/** @addtogroup Network
* @{
*/
/**
* The main network class for the client.
* Connects to the remote server, sends requests to it and
* calls the appropriate callbacks when the answer is received (or
* when a standalone command is received from the server)
* @class Client
*/
#include <network/command_handler.hpp>
#include <boost/asio.hpp>
......@@ -16,17 +28,29 @@ class Client: public CommandHandler
public:
Client();
~Client();
/**
* Tries to connect to the remote server. Calls one of the given callbacks, if it
* succeeds or fails.
* @return void
*/
void connect(const std::string&,
const short&,
boost::function< void(void) > on_success = 0,
boost::function< void(void) > on_failure = 0);
/**
* Check for data on the sockets (to send or write), and execute the
* appropriate handlers. Does nothing if there’s nothing to read or write.
* This is non-blocking.
*/
void poll(void);
private:
void connect_handler(boost::function< void(void) >,
boost::function< void(void) >,
const boost::system::error_code&);
void poll(void);
private:
boost::asio::io_service io_service;
};
#endif /*__CLIENT_HPP__ */
/**@}*/
/** @addtogroup Network
* @{
*/
/**
* Handles the reading part of the socket.
* Keeps a list of callbacks to call, associated with a command name, and
* execute them when the associated command is received.
* @class CommandHandler
*/
#include <map>
#include <boost/function.hpp>
#include <boost/asio.hpp>
......@@ -10,27 +21,62 @@
using boost::asio::ip::tcp;
// Handles the reading part of the socket
// Keeps a list of callbacks to call, associated with a command name
class CommandHandler
{
public:
CommandHandler();
~CommandHandler() {}
void read_handler(const boost::system::error_code& error, const std::size_t bytes_transferred);
void install_read_handler();
void install_read_handler();
/**
* called when there's something to read on the socket. Reads the command
* the size of the arguments, and then calls binary_read_handler to read
* the arguments of the commad, if any.
*/
void read_handler(const boost::system::error_code& error, const std::size_t bytes_transferred);
/**
* Read the arguments after a command (can read 0 bytes too) and pass that
* to the callback that was associated with this command.
*/
void binary_read_handler(const boost::system::error_code&, std::size_t, boost::function<void(std::string)>);
/**
* Sends a commad, and use install_callback_once to wait for the answer
* and call that callback to handle it.
*/
void request_answer(const char*, const char*, boost::function< void(const std::string&) >);
protected:
tcp::socket* socket;
/**
* Install a new callback associated with a command. This callback will
* be called upon receiving that command.
*/
void install_callback(const std::string&, boost::function< void(std::string) >);
/**
* Install a new callback associated with a command. This callback will
* be called upon receiving that command, but only once. This is used
* for example if you send a command waiting for and answer, you install
* a callback that will handle that answer, and only this one.
*/
void install_callback_once(const std::string&, boost::function< void(std::string) >);
/**
* Returns the callback associated with the passed command name.
* Returns 0 if nothing was found, in that case the execution of the
* return value cause a failure.
*/
boost::function< void(std::string) > get_callback(const std::string&);
boost::asio::streambuf data;
/**
* Send the given data on the socket.
*/
void send(const char*);
/**
* @todo Check if the data was correctly sent on the socket
*/
void send_handler(const boost::system::error_code&, std::size_t);
/**
* A buffer keeping the data that is read on the socket.
*/
boost::asio::streambuf data;
tcp::socket* socket;
private:
CommandHandler(const CommandHandler&);
......@@ -41,3 +87,4 @@ private:
};
#endif // __COMMAND_HANDLER_HPP__
/**@}*/
/** @addtogroup Network
* @{
*/
/**
* Represents one single remote client. It is kept in a list, by the Server
* object. Using the CommandHandler interface, it executes callbacks
* upon receiving command from the client, and sends commands when
* we need to communicate something to it.
* @class RemoteClient
*/
#include <network/command_handler.hpp>
#include <boost/asio.hpp>
......@@ -13,17 +25,31 @@ class RemoteClient: public CommandHandler
public:
RemoteClient(boost::asio::io_service&);
~RemoteClient();
/**
* starts the client (install the read handler, etc)
*/
void start(void);
/**
* @return tcp::socket&
*/
tcp::socket& get_socket(void);
/**
* The number of clients is incremented each time
* a new client is accepted.
*/
static int clients_number;
private:
/**
* Creates the default callbacks associated with a network command.
* It is executed whenever that command is received.
* See CommandHandler for details
*/
void install_callbacks();
void install_read_handler(void);
void auth_callback(const std::string&);
const int number;
};
#endif
/**@}*/
......@@ -13,9 +13,6 @@ Server::~Server()
delete this->acceptor;
}
// Callback that is called from io_service.run(), from the main (and only) thread.
// Even if there is data to read on two sockets at the same time, this handler will never be
// called twice at the same time. No need to use a mutex or lock.
void Server::install_accept_handler(void)
{
RemoteClient* new_client = new RemoteClient(this->io_service);
......
// BaseServer is the server handling clients authentication,
// serving menus, history, players’ informations, etc
/** @addtogroup Network
* @{
*/
/**
* The server handling clients authentication, serving menus, history, players’
* informations, etc
* @class Server
*/
#include <boost/asio.hpp>
#include <boost/bind.hpp>
......@@ -14,8 +21,16 @@ using boost::asio::ip::tcp;
class Server
{
public:
/**
* Create the server instance. Use run() to start its loop.
* @param port The port on which the servers accepts new connections.
*/
Server(short port);
~Server();
/**
* Starts the main loop.
* @return void
*/
void run(void);
private:
......@@ -29,3 +44,4 @@ private:
};
#endif /*__SERVER_HPP__ */
/**#@}*/
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment