Jelajahi Sumber

fixed stuff

Cyamond 5 tahun lalu
induk
melakukan
f7c1c461cf
2 mengubah file dengan 44 tambahan dan 42 penghapusan
  1. 42 42
      gui/src/qmlhandler.cpp
  2. 2 0
      gui/src/qmlhandler.h

+ 42 - 42
gui/src/qmlhandler.cpp

@@ -22,13 +22,54 @@ int inpipefd[2];
 int outpipefd[2];
 char buf[1025];
 QUrl sendFileUrl = QUrl("");
-QString _IP = qsTr("");
+QString _IP = "";
 bool _CLI_RUNNING = false;
 
 bool programActive = true;
 
 QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
 
+void QMLHandler::closeCLI() {
+  close(outpipefd[1]);
+  close(inpipefd[0]);
+}
+
+void QMLHandler::reopenCLI(QString ip) {
+  if (_CLI_RUNNING) {
+    closeCLI();
+  }
+
+  _IP = ip;
+  pid_t pid = 0;
+
+  pipe(inpipefd);
+  pipe(outpipefd);
+  pid = fork();
+  if (pid == 0) {
+    // Child
+    dup2(outpipefd[0], STDIN_FILENO);
+    dup2(inpipefd[1], STDOUT_FILENO);
+    // dup2(inpipefd[1], STDERR_FILENO);
+
+    // ask kernel to deliver SIGTERM in case the parent dies
+    prctl(PR_SET_PDEATHSIG, SIGTERM);
+
+    // Set the path to the CLI - pass argument h (help) for now
+    // TODO: Change hardcoded path
+    execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(),
+          "--machine", (char *)NULL);
+
+    _CLI_RUNNING = true;
+
+    exit(1);
+  }
+
+  close(outpipefd[0]);
+  close(inpipefd[1]);
+
+  std::thread(&QMLHandler::readPipeLoop, this).detach();
+}
+
 void QMLHandler::onExit() {
   write(outpipefd[1], "disconnect\n", strlen("disconnect\n"));
 }
@@ -240,44 +281,3 @@ void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne,
 void QMLHandler::onFooterGetStatusButton() {
   write(outpipefd[1], "status\n", strlen("status\n"));
 }
-
-void QMLHandler::reopenCLI(QString ip) {
-  if (_CLI_RUNNING) {
-    closeCLI();
-  }
-
-  _IP = ip;
-  pid_t pid = 0;
-
-  pipe(inpipefd);
-  pipe(outpipefd);
-  pid = fork();
-  if (pid == 0) {
-    // Child
-    dup2(outpipefd[0], STDIN_FILENO);
-    dup2(inpipefd[1], STDOUT_FILENO);
-    // dup2(inpipefd[1], STDERR_FILENO);
-
-    // ask kernel to deliver SIGTERM in case the parent dies
-    prctl(PR_SET_PDEATHSIG, SIGTERM);
-
-    // Set the path to the CLI - pass argument h (help) for now
-    // TODO: Change hardcoded path
-    execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(),
-          "--machine", (char *)NULL);
-
-    _CLI_RUNNING = true;
-
-    exit(1);
-  }
-
-  close(outpipefd[0]);
-  close(inpipefd[1]);
-
-  std::thread(&QMLHandler::readPipeLoop, this).detach();
-}
-
-void QMLHandler::closeCLI() {
-  close(outpipefd[1]);
-  close(inpipefd[0]);
-}

+ 2 - 0
gui/src/qmlhandler.h

@@ -13,6 +13,8 @@ class QMLHandler : public QObject {
 private:
   void handleJSON(std::string buffer);
   void readPipeLoop();
+  void reopenCLI(QString ip);
+  void closeCLI();
 
 public:
   explicit QMLHandler(QObject *parent = 0);