implemented message handling for every type of message via policies

This commit is contained in:
Kjistóf 2016-12-04 12:51:28 +01:00
parent 7eb1191a1d
commit 1bd1cb343a
2 changed files with 52 additions and 8 deletions

View File

@ -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;
}

View File

@ -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 <class PolicyCandidate, typename = void>
struct is_valid_policy : std::false_type {};
@ -27,5 +30,23 @@ struct is_valid_policy<
::handshake_do_what(std::declval<chat::chat_message>())),
void
>::value
&&
std::is_same<
decltype(PolicyCandidate
::serverdirection_do_what(std::declval<chat::chat_message>())),
void
>::value
&&
std::is_same<
decltype(PolicyCandidate
::login_do_what(std::declval<chat::chat_message>())),
void
>::value
&&
std::is_same<
decltype(PolicyCandidate
::logout_do_what(std::declval<chat::chat_message>())),
void
>::value
>::type>
: std::true_type {};