Selaa lähdekoodia

linking up the UI to use the client. Need to create a functor or a wrapped function or lambda expression so we can delegate work to a separate thread

logicp 5 vuotta sitten
vanhempi
commit
3f89d69d89
5 muutettua tiedostoa jossa 120 lisäystä ja 67 poistoa
  1. 34 28
      client.cpp
  2. 9 0
      client.hpp
  3. 16 32
      mainwindow.cpp
  4. 3 1
      mainwindow.h
  5. 58 6
      mainwindow.ui

+ 34 - 28
client.cpp

@@ -9,6 +9,7 @@
 #include <thread>
 #include <cstring>
 #include <QDebug>
+#include <QByteArray>
 #include <string>
 #include <iostream>
 
@@ -37,11 +38,27 @@ void input_loop(char buffer[]) {
 //}
 
 
-Client::Client(QWidget *parent, int count, char** arguments) : QDialog(parent), argc(count), argv(arguments) {}
+Client::Client(QWidget *parent, int count, char** arguments) : QDialog(parent), argc(count), argv(arguments), m_client_socket_fd(-1) {}
+
+    void Client::handleMessages() {
+        char receive_buffer[2048];
+        for (;;) {
+            memset(receive_buffer, 0, 2048);
+            int bytes_received = 0;
+            bytes_received = recv(m_client_socket_fd, receive_buffer, 2048 - 2, 0);
+            receive_buffer[2047] = 0;
+            if (bytes_received == 0) {
+                break;
+            }
+            emit Client::messageReceived(QString::fromUtf8(receive_buffer, 2048));
+        }
+        memset(receive_buffer, 0, 2048);
+        ::close(m_client_socket_fd);
+    }
 
     int Client::start() {
-        int client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
-        if (client_socket_fd != -1) {
+        m_client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
+        if (m_client_socket_fd != -1) {
             sockaddr_in server_socket;
             char* end;
             server_socket.sin_family = AF_INET;
@@ -52,37 +69,19 @@ Client::Client(QWidget *parent, int count, char** arguments) : QDialog(parent),
 
             int socket_option = 1;
             // Free up the port to begin listening again
-            setsockopt(client_socket_fd, SOL_SOCKET, SO_REUSEADDR, &socket_option,
+            setsockopt(m_client_socket_fd, SOL_SOCKET, SO_REUSEADDR, &socket_option,
                        sizeof(socket_option));
 
             server_socket.sin_port = htons(port_value);
             inet_pton(AF_INET, argv[1], &server_socket.sin_addr.s_addr);
 
-            if (::connect(client_socket_fd, (sockaddr*)&server_socket,
+            if (::connect(m_client_socket_fd, (sockaddr*)&server_socket,
                         sizeof(server_socket)) != -1) {
-                char send_buffer[2048];
-                char receive_buffer[2048];
-                for (;;) {
-                    memset(send_buffer, 0, 2048);
-                    memset(receive_buffer, 0, 2048);
-                    int bytes_received = 0;
-                    std::thread user_input_loop(input_loop, send_buffer);
-
-                    if (user_input_loop.joinable()) {
-                        user_input_loop.join();
-                        send(client_socket_fd, send_buffer, 2048, 0);
-                        bytes_received = recv(client_socket_fd, receive_buffer, 2048 - 2, 0);
-                        receive_buffer[2047] = 0;
-                        if (bytes_received == 0) {
-                            break;
-                        }
-                    }
-                }
-                memset(send_buffer, 0, 2048);
-                memset(receive_buffer, 0, 2048);
+//                std::thread t(handleMessages);
+            } else {
+                qDebug() << errno;
+                ::close(m_client_socket_fd);
             }
-            qDebug() << errno;
-            ::close(client_socket_fd);
             return 0;
         }
     }
@@ -92,4 +91,11 @@ void Client::requestNewFortune() {
                              tr("Gibs me dat fortune, punk!"));
 }
 
-
+void Client::sendMessage(const QString& s) {
+    if (m_client_socket_fd != -1) {
+        QByteArray byte_array = s.toLocal8Bit();
+        ::send(m_client_socket_fd, byte_array.data(), byte_array.size(), 0);
+    } else {
+        qDebug() << "You must first open a connection";
+    }
+}

+ 9 - 0
client.hpp

@@ -5,6 +5,7 @@
 #include <QLineEdit>
 #include <QTimer>
 #include <QLabel>
+#include <QString>
 
 class Client : public QDialog
 {
@@ -16,7 +17,15 @@ public:
     void requestNewFortune();
     int start();
 
+public slots:
+    void sendMessage(const QString& s);
+
+signals:
+    void messageReceived(QString s);
+
 private:
+    void handleMessages();
     int argc;
     char** argv;
+    int m_client_socket_fd;
 };

+ 16 - 32
mainwindow.cpp

@@ -3,7 +3,9 @@
 #include <QCommandLinkButton>
 #include <QProcess>
 #include <QDebug>
+#include <QLabel>
 #include <QTextStream>
+#include <QString>
 #include <QVector>
 #include <QLayout>
 #include <client.hpp>
@@ -16,53 +18,35 @@ MainWindow::MainWindow(int argc, char** argv, QWidget *parent) :
     cli_argv(argv) {
     this->process = new QProcess(this);
     ui->setupUi(this);
-    ui->textEdit->setText("KSupStyle YO?");
+    ui->inputText->setText("KSupStyle YO?");
     this->connectUi();
 }
 
 /**
  * @brief MainWindow::buttonClicked
  */
-void MainWindow::buttonClicked() {
+void MainWindow::connectClient() {
     qDebug() << "You clicked the button!!";
 
-//    QVBoxLayout *layout = new QVBoxLayout;
     Client* q_client = new Client(this, cli_argc, cli_argv);
-//    layout->addWidget(q_client);
-//    setLayout(layout);
-//    q_client->exec();
-//    q_client->show();
+    QObject::connect(q_client, &Client::messageReceived, this, &MainWindow::updateMessages);
     q_client->start();
+    QTextEdit* send_message_box = this->findChild<QTextEdit*>("inputText");
 
-//    runApp();
+    QPushButton* send_message_button = this->findChild<QPushButton*>("sendMessage");
+    QObject::connect(send_message_button, &QPushButton::clicked, this, [&q_client, &send_message_box]() {
+        q_client->sendMessage(send_message_box->toPlainText());
+    });
 }
 
-void MainWindow::connectUi() {
-    QCommandLinkButton *button = this->findChild<QCommandLinkButton*>("kbutton");
-    connect(button, &QCommandLinkButton::clicked, this, &MainWindow::buttonClicked);
+void MainWindow::updateMessages(const QString& s) {
+    QLabel* message_display = this->findChild<QLabel*>("messages");
+    message_display->setText(s);
 }
 
-void MainWindow::runApp() {
-    QString appString = "python /data/www/kiq/requests/test.py";
-    process->start(appString);
-
-    QVector<QString> app_result{};
-
-    qDebug() << "trying to run app";
-
-    connect(process, &QProcess::readyReadStandardOutput, [=] () {
-        this->process->setProcessChannelMode(QProcess::MergedChannels);
-        QTextStream reader(this->process->readAllStandardOutput());
-        do {
-            QString s;
-            reader.readLineInto(&s);
-            qDebug() << s;
-        } while (!reader.readLine().isNull());
-    });
-
-    for (auto qString: app_result) {
-        qDebug() << qString;
-    }
+void MainWindow::connectUi() {
+    QCommandLinkButton *button = this->findChild<QCommandLinkButton*>("connect");
+    connect(button, &QCommandLinkButton::clicked, this, &MainWindow::connectClient);
 }
 
 

+ 3 - 1
mainwindow.h

@@ -3,6 +3,7 @@
 
 #include <QMainWindow>
 #include <QProcess>
+#include <QString>
 
 namespace Ui {
 class MainWindow;
@@ -24,7 +25,8 @@ private:
     char** cli_argv;
 
 private slots:
-    void buttonClicked();
+    void connectClient();
+    void updateMessages(const QString& s);
 };
 
 

+ 58 - 6
mainwindow.ui

@@ -33,27 +33,79 @@
      <bool>true</bool>
     </property>
    </widget>
-   <widget class="QTextEdit" name="textEdit">
+   <widget class="QTextEdit" name="inputText">
     <property name="geometry">
      <rect>
       <x>20</x>
-      <y>170</y>
+      <y>370</y>
       <width>571</width>
       <height>61</height>
      </rect>
     </property>
    </widget>
-   <widget class="QCommandLinkButton" name="kbutton">
+   <widget class="QCommandLinkButton" name="connect">
     <property name="geometry">
      <rect>
-      <x>250</x>
-      <y>110</y>
+      <x>20</x>
+      <y>280</y>
       <width>168</width>
       <height>41</height>
      </rect>
     </property>
     <property name="text">
-     <string>YouTube Query</string>
+     <string>Connect</string>
+    </property>
+   </widget>
+   <widget class="QProgressBar" name="progressBar">
+    <property name="geometry">
+     <rect>
+      <x>87</x>
+      <y>530</y>
+      <width>401</width>
+      <height>23</height>
+     </rect>
+    </property>
+    <property name="value">
+     <number>0</number>
+    </property>
+   </widget>
+   <widget class="QPushButton" name="pushButton">
+    <property name="geometry">
+     <rect>
+      <x>190</x>
+      <y>290</y>
+      <width>80</width>
+      <height>26</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>Disconnect</string>
+    </property>
+   </widget>
+   <widget class="QPushButton" name="sendMessage">
+    <property name="geometry">
+     <rect>
+      <x>510</x>
+      <y>470</y>
+      <width>80</width>
+      <height>26</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>Send</string>
+    </property>
+   </widget>
+   <widget class="QLabel" name="messages">
+    <property name="geometry">
+     <rect>
+      <x>120</x>
+      <y>100</y>
+      <width>371</width>
+      <height>171</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string/>
     </property>
    </widget>
   </widget>