Commit b75a3fbc authored by Thomas Bailleux's avatar Thomas Bailleux
Browse files

setting only one object for sms handler, instead of multiple

parent bb51cfbb
......@@ -55,18 +55,17 @@ class Server {
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
unsigned short m_port;
std::string m_host;
std::map<Event, std::vector<Handler>> m_handlers;
std::unique_ptr<Handler> m_sms_handler;
std::vector<std::shared_ptr<Connection>> m_connections;
void handle_accept(std::shared_ptr<Connection> connection,
const boost::system::error_code& error);
void create_map_if_not_exists(Event event);
void remove_connection(Connection* connection);
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 add_event_handler(Handler& handler);
bool set_event_handler(std::unique_ptr<Handler> handler);
void start_accept();
void run();
......
......@@ -32,12 +32,11 @@ Server::Server(std::string &host, unsigned short port) {
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), m_port));
}
bool Server::add_event_handler(Handler& handler) {
bool Server::set_event_handler(std::unique_ptr<Handler> handler) {
bool added = false;
switch (handler.m_event) {
switch (handler->m_event) {
case MESSAGE:
create_map_if_not_exists(handler.m_event);
m_handlers[handler.m_event].push_back(handler);
m_sms_handler = std::move(handler);
added = true;
break;
default:
......@@ -47,13 +46,6 @@ bool Server::add_event_handler(Handler& handler) {
return added;
}
void Server::create_map_if_not_exists(Event event) {
auto search = m_handlers.find(event);
if (search == m_handlers.end()) {
m_handlers[event] = std::vector<Handler>();
}
}
boost::asio::io_service &Server::get_io_service() {
return m_io_service;
}
......@@ -86,9 +78,7 @@ void Server::run() {
}
void Server::notify_on_sms_received(std::unique_ptr<Sms> sms) {
for(Handler& h: m_handlers[Event::MESSAGE]) {
h.m_callback(std::move(sms));
}
m_sms_handler->m_callback(std::move(sms));
}
boost::asio::ip::tcp::socket &Connection::get_socket() {
......
......@@ -34,7 +34,7 @@ int main() {
handler.m_event = sms::Event::MESSAGE;
handler.m_callback = message_handler;
server.add_event_handler(handler);
server.set_event_handler(std::make_unique<sms::Handler>(handler));
server.start_accept();
server.run();
......
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