2016-12-04 13:00:39 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <thread>
|
2016-12-05 11:57:57 +00:00
|
|
|
#include <stdexcept>
|
2016-12-04 13:00:39 +00:00
|
|
|
#include "chat_networking.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct receive_policy_stdout
|
|
|
|
{
|
2016-12-04 13:54:15 +00:00
|
|
|
static void message_do_what(const chat::chat_message& msg)
|
2016-12-04 13:00:39 +00:00
|
|
|
{ std::cout << msg.get_content() << '\n'; }
|
|
|
|
|
2016-12-04 13:54:15 +00:00
|
|
|
static void handshake_do_what(const chat::chat_message& msg)
|
2016-12-04 13:00:39 +00:00
|
|
|
{ std::cout << "Handshake üzenetek:\n" << msg.get_content() << '\n'; }
|
|
|
|
|
2016-12-04 13:54:15 +00:00
|
|
|
static void serverdirection_do_what(const chat::chat_message& msg)
|
2016-12-04 13:00:39 +00:00
|
|
|
{ std::cout << "Szerver üzenet: " << msg.get_content() << '\n'; }
|
|
|
|
|
2016-12-04 13:54:15 +00:00
|
|
|
static void login_do_what(const chat::chat_message& msg)
|
2016-12-04 13:00:39 +00:00
|
|
|
{ std::cout << msg.get_content() << " belépett.\n"; }
|
|
|
|
|
2016-12-04 13:54:15 +00:00
|
|
|
static void logout_do_what(const chat::chat_message& msg)
|
2016-12-04 13:00:39 +00:00
|
|
|
{ std::cout << msg.get_content() << " kilépett.\n"; }
|
2016-12-05 11:57:57 +00:00
|
|
|
|
|
|
|
static void invalid_msg_do_what(const chat::chat_message& msg)
|
|
|
|
{ throw std::runtime_error("Invalid message received!"); }
|
2016-12-04 13:00:39 +00:00
|
|
|
};
|
|
|
|
|
2016-12-04 13:31:26 +00:00
|
|
|
struct send_policy_stdout
|
|
|
|
{
|
2016-12-04 13:54:15 +00:00
|
|
|
static bool check_msg_length(const chat::chat_message& msg)
|
2016-12-04 13:31:26 +00:00
|
|
|
{
|
|
|
|
bool good = msg.length() <= (256-34);
|
|
|
|
if (!good)
|
|
|
|
std::cout << "Az üzenet túl hosszú!\n";
|
|
|
|
return good;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-04 13:00:39 +00:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
boost::asio::io_service ios;
|
|
|
|
boost::asio::ip::tcp::resolver resolver(ios);
|
|
|
|
boost::asio::ip::tcp::resolver::query query("infoc.eet.bme.hu", "8888");
|
|
|
|
auto epit = resolver.resolve(query);
|
|
|
|
|
|
|
|
|
2016-12-04 13:31:26 +00:00
|
|
|
chat::client_network_manager<receive_policy_stdout, send_policy_stdout> cnm(ios, epit, "BATMAN");
|
2016-12-04 13:00:39 +00:00
|
|
|
std::thread t([&ios]{ ios.run(); });
|
|
|
|
|
|
|
|
std::string input;
|
|
|
|
while (true)
|
|
|
|
{
|
2016-12-04 13:31:26 +00:00
|
|
|
std::getline(std::cin, input);
|
2016-12-04 13:00:39 +00:00
|
|
|
if (input == "exit")
|
|
|
|
break;
|
|
|
|
|
|
|
|
chat::chat_message msg(chat::message::MESSAGE, input);
|
|
|
|
cnm.send(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
cnm.close_connection();
|
|
|
|
t.join();
|
|
|
|
}
|