diff --git a/doorlockd/src/doorlock-client.cpp b/doorlockd/src/doorlock-client.cpp index e759aed..0772db2 100644 --- a/doorlockd/src/doorlock-client.cpp +++ b/doorlockd/src/doorlock-client.cpp @@ -36,33 +36,18 @@ static volatile bool run = true; std::unique_ptr mainWindow = nullptr; -static void doorlock_update(const Clientmessage &msg) +static void onDoorlockUpdate(const Clientmessage &msg) { + l("Updated to current token: " + msg.token(), LogLevel::info); if (mainWindow) { - mainWindow->setQRCode(QString::fromStdString(msg.token())); - mainWindow->setLabel(QString::fromStdString(msg.token())); + mainWindow->setQRCode(msg.token()); } } -static int startGui(int argc, char** argv) -{ - QApplication app(argc, argv); - - app.setOrganizationName("Binary Kitchen"); - app.setApplicationName("doorlock-client"); - - mainWindow = std::unique_ptr(new MainWindow); - - int retval = app.exec(); - run = false; - return retval; -} - static int doorlock_client(const std::string &hostname, const unsigned short port) { int retval = 0; - std::thread guiThread; try { tcp::resolver resolver(io_service); @@ -87,28 +72,13 @@ static int doorlock_client(const std::string &hostname, throw boost::system::system_error(error); data.resize(SOCKET_BUFFERLENGTH); - length = socket.read_some(boost::asio::buffer(data), error); - if (error) - throw boost::system::system_error(error); - - const auto response = Response::fromJson(std::string(data.begin(), data.begin()+length)); - if (!response) - throw std::runtime_error("Invalid response from server"); - - guiThread = std::thread([] () { - startGui(0, nullptr); - }); - for (;run;) { length = socket.read_some(boost::asio::buffer(data), error); if (error) throw boost::system::system_error(error); const auto message = Clientmessage::fromJson(std::string(data.begin(), data.begin()+length)); - if (!response) - throw std::runtime_error("Invalid response from server"); - - doorlock_update(message); + onDoorlockUpdate(message); } } catch(const std::exception &e) @@ -118,15 +88,11 @@ static int doorlock_client(const std::string &hostname, } out: - if (guiThread.joinable()) { - guiThread.join(); - } return retval; } int main(int argc, char** argv) { - int retval = -1; std::string hostname; unsigned short port; @@ -151,8 +117,7 @@ int main(int argc, char** argv) if (vm.count("help")) { std::cout << desc << std::endl; - retval = 0; - goto out; + exit(-1); } po::notify(vm); @@ -160,14 +125,33 @@ int main(int argc, char** argv) catch(const std::exception &e) { l(LogLevel::error, e.what()); - goto out; + exit(-1); } l(LogLevel::notice, "Starting doorlock-client"); - doorlock_client(hostname, port); + QApplication app(argc, argv); + app.setOrganizationName("Binary Kitchen"); + app.setApplicationName("doorlock-client"); + + mainWindow = std::unique_ptr(new MainWindow); + mainWindow->showFullScreen(); + + // Start the TCP client as thread + std::thread clientThread = std::thread([&] () { + // If the TCP client returns, an error has occured + // In normal operation, it never returns + doorlock_client(hostname, port); + + // Stop the QT application + app.quit(); + }); + + // This routine will never return in normal operation + app.exec(); + + clientThread.join(); -out: l(LogLevel::notice, "Stopping doorlock-client"); - return retval; + return 0; } diff --git a/doorlockd/src/mainwindow.h b/doorlockd/src/mainwindow.h index 5bdb561..0b23cd4 100644 --- a/doorlockd/src/mainwindow.h +++ b/doorlockd/src/mainwindow.h @@ -2,52 +2,43 @@ #define MAINWINDOW_H #include -#include -#include +#include +#include +#include #include "qrwidget.h" -class MainWindow : public QWidget +class MainWindow : public QMainWindow { public: MainWindow() : - QWidget() + QMainWindow() { _qrWidget = new QRWidget; - _layout = new QVBoxLayout; - _layout->addWidget(_qrWidget); + statusBar()->showMessage(tr("Boofar")); - _label = new QLabel; - _label->setText("Hello, world!"); + QHBoxLayout *layout = new QHBoxLayout; - QFont font("Times", 25, QFont::Bold); - _label->setFont(font); - _layout->addWidget(_label); + layout->addWidget(_qrWidget); - setLayout(_layout); - showFullScreen(); + QWidget* window = new QWidget; + window->setLayout(layout); + + setCentralWidget(window); } MainWindow(const MainWindow&); MainWindow& operator =(const MainWindow&); - void setQRCode(const QString &str) + void setQRCode(const std::string &str) { _qrWidget->setQRData(str); } - void setLabel(const QString &str) - { - _label->setText(str); - } - private: QRWidget* _qrWidget = { nullptr }; - QVBoxLayout* _layout = { nullptr }; - QLabel* _label = { nullptr }; - }; #endif