Commit 15acbe6f authored by Thomas Bailleux's avatar Thomas Bailleux
Browse files

Add comment, move private section below public section according to google C++ style guide

parent 87992789
...@@ -32,13 +32,27 @@ ...@@ -32,13 +32,27 @@
namespace sms { namespace sms {
/* An HTTP Header */
class Header { class Header {
public: public:
std::string m_name; std::string m_name;
std::string m_value; std::string m_value;
}; };
/* Parser for HTTP Request.
* This parser extracts the method, the path and the header. It does not support the content and the query. */
class HttpParser { class HttpParser {
public:
/* Construct a parser from the buffer to parse. */
HttpParser(char *buffer, size_t buffer_length): m_buffer(buffer), m_buffer_length(buffer_length) {};
/* Parse the buffer. */
int parse();
/* Get the headers. */
std::vector<Header>& get_headers();
private: private:
std::string m_method; std::string m_method;
std::string m_path; std::string m_path;
...@@ -49,10 +63,6 @@ class HttpParser { ...@@ -49,10 +63,6 @@ class HttpParser {
int parse_first_line(std::string& line); int parse_first_line(std::string& line);
int parse_header(std::string& line); int parse_header(std::string& line);
void uri_decode(std::string& data); void uri_decode(std::string& data);
public:
HttpParser(char *buffer, size_t buffer_length): m_buffer(buffer), m_buffer_length(buffer_length) {};
int parse();
std::vector<Header>& get_headers();
}; };
} /* namespace sms */ } /* namespace sms */
......
...@@ -35,14 +35,19 @@ ...@@ -35,14 +35,19 @@
namespace sms { namespace sms {
/* Type of events
* For now, there is only MESSAGE. This means the event when the server receives a message. */
enum Event { enum Event {
MESSAGE MESSAGE
}; };
/* Handle an event. */
class Handler { class Handler {
public: public:
/* Type of event. */
Event m_event; Event m_event;
/* Function (or method) which will be called by the server whenever a event happens. */
std::function<void(std::unique_ptr<Sms> sms)> m_callback; std::function<void(std::unique_ptr<Sms> sms)> m_callback;
}; };
...@@ -50,6 +55,24 @@ class Connection; ...@@ -50,6 +55,24 @@ class Connection;
class Server { class Server {
friend class Connection; friend class Connection;
public:
/* Construct a server which is going to listen on host:port.
* NOTE: host is not still supported, host is 0.0.0.0 for now. */
Server(std::string& host, unsigned short port);
/* Retrieve a reference of the asio service which handles the server. */
boost::asio::io_service& get_io_service();
/* Set an handler for a specified event. */
bool set_event_handler(std::unique_ptr<Handler> handler);
/* Initiate acceptor for clients. */
void start_accept();
/* Run server (aka run asio service). */
void run();
private: private:
boost::asio::io_service m_io_service; boost::asio::io_service m_io_service;
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_acceptor; std::unique_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
...@@ -62,18 +85,19 @@ class Server { ...@@ -62,18 +85,19 @@ class Server {
void remove_connection(Connection* connection); void remove_connection(Connection* connection);
void notify_on_sms_received(std::unique_ptr<Sms> sms); void notify_on_sms_received(std::unique_ptr<Sms> sms);
public:
Server(std::string& host, unsigned short port);
boost::asio::io_service& get_io_service();
bool set_event_handler(std::unique_ptr<Handler> handler);
void start_accept();
void run();
}; };
/* Class which describes an HTTP request. */
class Connection: std::enable_shared_from_this<Connection> { class Connection: std::enable_shared_from_this<Connection> {
friend class Server; friend class Server;
public:
/* Construct an object from the pending server, the asio service and a random uuid. */
explicit Connection(Server& server): m_socket(server.get_io_service()),
m_server(server),
m_data{0},
m_uuid(boost::uuids::random_generator()()) {}
private: private:
boost::asio::ip::tcp::socket m_socket; boost::asio::ip::tcp::socket m_socket;
Server& m_server; Server& m_server;
...@@ -84,12 +108,6 @@ class Connection: std::enable_shared_from_this<Connection> { ...@@ -84,12 +108,6 @@ class Connection: std::enable_shared_from_this<Connection> {
boost::asio::ip::tcp::socket& get_socket(); boost::asio::ip::tcp::socket& get_socket();
void receive_http_request(const boost::system::error_code& error, size_t bytes_transferred); void receive_http_request(const boost::system::error_code& error, size_t bytes_transferred);
void parse_http_request(size_t length); void parse_http_request(size_t length);
public:
explicit Connection(Server& server): m_socket(server.get_io_service()),
m_server(server),
m_data{0},
m_uuid(boost::uuids::random_generator()()) {}
}; };
} /* namespace sms */ } /* namespace sms */
......
...@@ -25,21 +25,32 @@ ...@@ -25,21 +25,32 @@
namespace sms { namespace sms {
/* Type of message.
* For now, only SMS are supported. */
enum SmsType { enum SmsType {
SMS, SMS,
MMS MMS
}; };
/* A simple SMS. */
class Sms { class Sms {
private:
std::string m_number;
std::string m_content;
SmsType m_type;
public: public:
/* Construct a SMS from the sender's number, the content and the type. */
Sms(std::string& number, std::string& content, SmsType type): m_number(number), m_content(content), m_type(type) {}; Sms(std::string& number, std::string& content, SmsType type): m_number(number), m_content(content), m_type(type) {};
/* Get the content. */
const std::string& get_content(); const std::string& get_content();
/* Get the number. */
const std::string& get_number(); const std::string& get_number();
/* Get the message's type (SMS or MMS). */
SmsType get_type(); SmsType get_type();
private:
std::string m_number;
std::string m_content;
SmsType m_type;
}; };
} /* namespace sms */ } /* namespace sms */
......
...@@ -26,12 +26,14 @@ namespace sms { ...@@ -26,12 +26,14 @@ namespace sms {
int HttpParser::parse() { int HttpParser::parse() {
std::string buffer(m_buffer); std::string buffer(m_buffer);
/* Remove the \r for commodity. */
buffer.erase(std::remove(buffer.begin(), buffer.end(), '\r'), buffer.end()); buffer.erase(std::remove(buffer.begin(), buffer.end(), '\r'), buffer.end());
std::istringstream stream(buffer); std::istringstream stream(buffer);
std::string s; std::string s;
int ret = NO_ERROR; int ret = NO_ERROR;
if (std::getline(stream, s, '\n')) { if (std::getline(stream, s, '\n')) {
ret = parse_first_line(s); ret = parse_first_line(s); /* This actually parses the method and the path. */
} else { } else {
ret = BAD_REQUEST; ret = BAD_REQUEST;
} }
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
namespace sms { namespace sms {
Server::Server(std::string &host, unsigned short port) { Server::Server(std::string &host, unsigned short port) {
m_host = host; m_host = host;
m_port = port; m_port = port;
...@@ -97,11 +98,10 @@ void Connection::run() { ...@@ -97,11 +98,10 @@ void Connection::run() {
void Connection::receive_http_request(const boost::system::error_code &error, size_t bytes_transferred) { void Connection::receive_http_request(const boost::system::error_code &error, size_t bytes_transferred) {
if (!error) { if (!error) {
parse_http_request(bytes_transferred); parse_http_request(bytes_transferred);
/* Answer a simple 200, in order to close the connection. */
std::string response("HTTP/1.1 200 OK\nContent-Length: 0\n\n"); std::string response("HTTP/1.1 200 OK\nContent-Length: 0\n\n");
m_socket.send(boost::asio::buffer(response)); m_socket.send(boost::asio::buffer(response));
} else {
std::cout << "Error: " << error << std::endl;
} }
m_socket.close(); m_socket.close();
m_server.remove_connection(this); m_server.remove_connection(this);
......
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