Bladeren bron

Fixed GUI reopenCLI

Cyamond 4 jaren geleden
bovenliggende
commit
1e5870ce22
3 gewijzigde bestanden met toevoegingen van 18 en 21 verwijderingen
  1. 2 2
      gui/CMakeLists.txt
  2. 15 15
      gui/src/qmlhandler.cpp
  3. 1 4
      gui/src/qmlhandler.h

+ 2 - 2
gui/CMakeLists.txt

@@ -14,9 +14,9 @@ find_package(Qt5 COMPONENTS Core Quick REQUIRED)
 find_package(PkgConfig REQUIRED)
 pkg_check_modules(JSONCPP REQUIRED jsoncpp)
 
+add_definitions(-DQT_NO_DEBUG_OUTPUT)
+
 add_executable(${PROJECT_NAME} src/main.cpp src/qmlhandler.cpp src/qml.qrc)
 
 include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDEDIR} include)
 target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${JSONCPP_LIBRARIES} ${Boost_LIBRARIES} Qt5::Core Qt5::Quick)
-
-

+ 15 - 15
gui/src/qmlhandler.cpp

@@ -1,9 +1,11 @@
+#include <QDebug>
 #include <QGuiApplication>
 #include <csignal>
 #include <cstdio>
 #include <cstdlib>
 #include <iostream>
 #include <poll.h>
+#include <string>
 #include <sys/prctl.h>
 #include <sys/wait.h>
 #include <thread>
@@ -20,34 +22,33 @@ using namespace std;
 
 int inpipefd[2];
 int outpipefd[2];
+
 char buf[1025];
 QUrl sendFileUrl = QUrl("");
 QString _IP = "";
 bool _CLI_RUNNING = false;
 bool _RESTART = false;
+pid_t childpid;
 
 bool programActive = true;
 
 QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
 
-void QMLHandler::closeCLI() {
-	close(outpipefd[1]);
-	close(inpipefd[0]);
-	_CLI_RUNNING = false;
-}
+void QMLHandler::closeCLI() { _CLI_RUNNING = false; }
 
 void QMLHandler::reopenCLI(QString ip) {
 	if (_CLI_RUNNING) {
+		waitpid(childpid, NULL, 0);
 		closeCLI();
 	}
 
 	_IP = ip;
-	pid_t pid = 0;
 
 	pipe(inpipefd);
 	pipe(outpipefd);
-	pid = fork();
-	if (pid == 0) {
+
+	childpid = fork();
+	if (childpid == 0) {
 		// Child
 		dup2(outpipefd[0], STDIN_FILENO);
 		dup2(inpipefd[1], STDOUT_FILENO);
@@ -67,7 +68,7 @@ void QMLHandler::reopenCLI(QString ip) {
 
 	close(outpipefd[0]);
 	close(inpipefd[1]);
-	std::thread(&QMLHandler::readPipeLoop, this, pid).detach();
+	std::thread(&QMLHandler::readPipeLoop, this).detach();
 }
 
 std::vector<std::string> tokenizeByNewlines(std::string in) {
@@ -145,7 +146,7 @@ void QMLHandler::handleJSON(string buffer) {
 			QString errorMessage = QString::fromStdString(
 			    string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
 			emit ipPopupSetStatus(errorMessage);
-			reopenCLI(_IP);
+			closeCLI();
 			emit ipPopupEnableConnectButton();
 		}
 	}
@@ -164,7 +165,7 @@ void QMLHandler::handleJSON(string buffer) {
 		if (root["accept"] == true) {
 			emit loginSignupPopupClose();
 		} else {
-			emit loginSetStatus(root["error"].asString().c_str());
+			emit signupSetStatus(root["error"].asString().c_str());
 			reopenCLI(_IP);
 			emit signupEnableRegisterButton();
 		}
@@ -204,7 +205,7 @@ void QMLHandler::handleJSON(string buffer) {
 // If will read the Input Pipe, which containts the string that get printed
 // on stdout by the CLI/Server, and calls handleJSON with the read content
 // one it reads a newline.
-void QMLHandler::readPipeLoop(pid_t childid) {
+void QMLHandler::readPipeLoop() {
 	unsigned int readOffset = 0;
 	unsigned int pollCount = 0;
 	struct pollfd inPipeStatus;
@@ -225,7 +226,7 @@ void QMLHandler::readPipeLoop(pid_t childid) {
 			pollCount++;
 		}
 
-		if (pollCount > 9 && (readOffset || pipeInput.size())) {
+		if (pollCount > 4 && (readOffset || pipeInput.size())) {
 			pipeInput.append(buf);
 			inputs = tokenizeByNewlines(pipeInput);
 			for (string s : inputs) {
@@ -237,14 +238,13 @@ void QMLHandler::readPipeLoop(pid_t childid) {
 			memset(buf, 0, 1025);
 			pollCount = 0;
 			readOffset = 0;
-			if (waitpid(childid, NULL, WNOHANG)) {
+			if (waitpid(childpid, NULL, WNOHANG)) {
 				// nonzero means error or childid has changed state
 				// for us that means child has exited -> CLI is dead
 				break;
 			}
 		}
 
-		// Fixme
 		if (readOffset >= 1024) {
 			pipeInput.append(buf);
 			readOffset = 0;

+ 1 - 4
gui/src/qmlhandler.h

@@ -1,11 +1,8 @@
 #ifndef QMLHANDLER_H
 #define QMLHANDLER_H
 
-
-#include <QDebug>
 #include <QObject>
 #include <QUrl>
-#include <string>
 
 extern bool _RESTART;
 
@@ -14,7 +11,7 @@ class QMLHandler : public QObject {
 
 private:
   void handleJSON(std::string buffer);
-  void readPipeLoop(pid_t childid);
+  void readPipeLoop();
   void reopenCLI(QString ip);
   void closeCLI();