From 1bd1cb343a81bf99f886185695b663d41e4ede67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 4 Dec 2016 12:51:28 +0100 Subject: [PATCH] implemented message handling for every type of message via policies --- chat_networking.hpp | 35 +++++++++++++++++++++++++++++------ check_policy.hpp | 25 +++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/chat_networking.hpp b/chat_networking.hpp index 0751c25..af0cb29 100644 --- a/chat_networking.hpp +++ b/chat_networking.hpp @@ -23,7 +23,16 @@ namespace chat { std::cout << msg << '\n'; } static void handshake_do_what(chat_message msg) - { message_do_what(msg); } + { std::cout << "Handshake üzenetek:\n" << msg << '\n'; } + + static void serverdirection_do_what(chat_message msg) + { std::cout << "Szerver üzenet: " << msg << '\n'; } + + static void login_do_what(chat_message msg) + { std::cout << msg << " belépett.\n"; } + + static void logout_do_what(chat_message msg) + { std::cout << msg << " kilépett.\n"; } }; @@ -74,7 +83,7 @@ namespace chat { _os << chat_message(message::BYE); asio::write(_socket, _osb); - // TODO: handle last server message + // TODO: decide what to do with last server message _socket.close(); _ios.stop(); }); @@ -142,15 +151,29 @@ namespace chat std::string data; std::getline(_is, data, byte(message::TERM)); - switch (data[0]) + char header = data[0]; + auto content = data.substr(1, data.size()-1); + + switch (header) { - // TODO: handle stuff case byte(message::PING): send(chat_message(message::PONG)); break; + case byte(message::SERVER_DIRECTION): + receive_policy::serverdirection_do_what + (chat_message(message::SERVER_DIRECTION, content)); + break; case byte(message::MESSAGE): - chat_message msg(message::MESSAGE, data); - receive_policy::message_do_what(msg); + receive_policy::message_do_what + (chat_message(message::MESSAGE, content)); + break; + case byte(message::LOGIN): + receive_policy::login_do_what + (chat_message(message::LOGIN, content)); + break; + case byte(message::LOGOUT): + receive_policy::logout_do_what + (chat_message(message::LOGOUT, content)); break; } diff --git a/check_policy.hpp b/check_policy.hpp index cf112ac..9f5a51b 100644 --- a/check_policy.hpp +++ b/check_policy.hpp @@ -6,8 +6,11 @@ /* SFINAE compile-type checker for receive policies (as defined in chat_networking.hpp) * it checks whether PolicyCandidate supplies all of the following methods: - * - void message_do_what(chat_message) - * - void handshake_do_what(chat_message) */ + * - static void message_do_what(chat_message) + * - static void handshake_do_what(chat_message) + * - static void serverdirection_do_what(chat_message) + * - static void login_do_what(chat_message) + * - static void logout_do_what(chat_message) */ template struct is_valid_policy : std::false_type {}; @@ -27,5 +30,23 @@ struct is_valid_policy< ::handshake_do_what(std::declval())), void >::value + && + std::is_same< + decltype(PolicyCandidate + ::serverdirection_do_what(std::declval())), + void + >::value + && + std::is_same< + decltype(PolicyCandidate + ::login_do_what(std::declval())), + void + >::value + && + std::is_same< + decltype(PolicyCandidate + ::logout_do_what(std::declval())), + void + >::value >::type> : std::true_type {}; \ No newline at end of file