hooked up GUI and networking, first release candidate
This commit is contained in:
parent
0c77d04a3c
commit
253929fa79
@ -7,6 +7,9 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -pthread")
|
||||
find_package(Boost COMPONENTS system REQUIRED)
|
||||
include_directories(${Boost_INCLUDE_DIR})
|
||||
|
||||
set(SOURCE_FILES commandline_chat.cpp chat_messages.hpp chat_networking.hpp)
|
||||
find_package(Qt5Widgets)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
||||
set(SOURCE_FILES gui_chat.cpp chat_window.hpp chat_messages.hpp chat_networking.hpp check_policy.hpp)
|
||||
add_executable(cpp11NHF2_chat ${SOURCE_FILES})
|
||||
target_link_libraries(cpp11NHF2_chat ${Boost_LIBRARIES})
|
||||
target_link_libraries(cpp11NHF2_chat ${Boost_LIBRARIES} Qt5::Widgets)
|
@ -3,9 +3,31 @@
|
||||
#include <QGridLayout>
|
||||
#include <QLineEdit>
|
||||
#include <QListWidget>
|
||||
#include <boost/asio.hpp>
|
||||
#include <thread>
|
||||
#include "chat_networking.hpp"
|
||||
|
||||
|
||||
|
||||
struct receive_policy_ignore
|
||||
{
|
||||
static void message_do_what(const chat::chat_message& msg) {}
|
||||
static void handshake_do_what(const chat::chat_message& msg) {}
|
||||
static void serverdirection_do_what(const chat::chat_message& msg) {}
|
||||
static void login_do_what(const chat::chat_message& msg) {}
|
||||
static void logout_do_what(const chat::chat_message& msg) {}
|
||||
static void invalid_msg_do_what(const chat::chat_message& msg)
|
||||
{ throw std::runtime_error("Invalid message received!"); }
|
||||
};
|
||||
|
||||
struct send_policy_ignore
|
||||
{
|
||||
/* this is handled in the gui: QLineEdit.setMaxLength() */
|
||||
static bool check_msg_length(const chat::chat_message& msg)
|
||||
{ return true; }
|
||||
};
|
||||
|
||||
|
||||
class chat_window : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -15,15 +37,47 @@ class chat_window : public QWidget
|
||||
QListWidget* _people;
|
||||
QLineEdit* _msg;
|
||||
|
||||
|
||||
boost::asio::io_service _ios;
|
||||
boost::asio::ip::tcp::resolver::query _query;
|
||||
chat::client_network_manager<receive_policy_ignore, send_policy_ignore> _cnm;
|
||||
std::thread _iosthread;
|
||||
|
||||
|
||||
public:
|
||||
explicit chat_window(QWidget* parent = 0)
|
||||
:QWidget(parent)
|
||||
{ setupUi(); }
|
||||
:QWidget(parent), _ios(), _query("infoc.eet.bme.hu", "8888"),
|
||||
_cnm(_ios, boost::asio::ip::tcp::resolver(_ios).resolve(_query), "BATMAN"),
|
||||
_iosthread([this]{ _ios.run(); })
|
||||
{
|
||||
_cnm.subscribe(std::bind(&chat_window::receive_handler, this, std::placeholders::_1));
|
||||
setupUi();
|
||||
}
|
||||
|
||||
void receive_handler(chat::chat_message msg)
|
||||
{
|
||||
switch (msg.get_header())
|
||||
{
|
||||
case chat::message::MESSAGE:
|
||||
case chat::message::SERVER_DIRECTION:
|
||||
_chat->addItem(msg.get_content().c_str());
|
||||
break;
|
||||
case chat::message::LOGIN:
|
||||
_people->addItem(msg.get_content().c_str());
|
||||
break;
|
||||
case chat::message::LOGOUT:
|
||||
auto list = _people->findItems(msg.get_content().c_str(), Qt::MatchExactly);
|
||||
if (list.size() > 0)
|
||||
delete list[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public slots:
|
||||
void return_handler()
|
||||
{
|
||||
_chat->addItem(_msg->text());
|
||||
_cnm.send(chat::chat_message(chat::message::MESSAGE, _msg->text().toStdString()));
|
||||
_msg->clear();
|
||||
};
|
||||
|
||||
@ -38,6 +92,7 @@ private:
|
||||
_people = new QListWidget();
|
||||
|
||||
_msg = new QLineEdit();
|
||||
_msg->setMaxLength(220);
|
||||
connect(_msg, &QLineEdit::returnPressed, this, &chat_window::return_handler);
|
||||
|
||||
_layout->addWidget(_chat, 0, 0, 1, 1);
|
||||
@ -46,4 +101,10 @@ private:
|
||||
|
||||
this->setLayout(_layout);
|
||||
}
|
||||
};
|
||||
|
||||
void closeEvent(QCloseEvent* event)
|
||||
{
|
||||
_cnm.close_connection();
|
||||
_iosthread.join();
|
||||
}
|
||||
};
|
||||
|
13
gui_chat.cpp
Normal file
13
gui_chat.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include <QApplication>
|
||||
#include "chat_window.hpp"
|
||||
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
chat_window w;
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
Loading…
Reference in New Issue
Block a user