Преглед на файлове

allow GUI to handle multiple lines from stdin

Missingmew преди 5 години
родител
ревизия
8abb20cc1b
променени са 1 файла, в които са добавени 34 реда и са изтрити 12 реда
  1. 34 12
      gui/src/qmlhandler.cpp

+ 34 - 12
gui/src/qmlhandler.cpp

@@ -29,6 +29,20 @@ bool programActive = true;
 
 QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
 
+std::vector<std::string> tokenizeByNewlines(std::string in) {
+  vector<string> res;
+  size_t index;
+  for (index = in.find("\n"); index != std::string::npos;
+       index = in.find("\n")) {
+    if (index != 0)
+      res.push_back(in.substr(0, index));
+    in = in.substr(index + 1);
+  }
+  if (in.length() > 0)
+    res.push_back(in);
+  return res;
+}
+
 void QMLHandler::onExit() {
   write(outpipefd[1], "disconnect\n", strlen("disconnect\n"));
 }
@@ -39,9 +53,11 @@ void QMLHandler::handleJSON(string buffer) {
   Json::CharReader *reader = builder.newCharReader();
   string jsonError;
 
+  qInfo() << QString::fromStdString("Using buffer: \"" + buffer + "\"");
   bool parsingSuccessful = reader->parse(
       buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
 
+  qInfo() << "Parsing successful " << parsingSuccessful;
   if (!parsingSuccessful) {
     return;
   }
@@ -69,11 +85,7 @@ void QMLHandler::handleJSON(string buffer) {
   }
 
   else if (cmd == "connect") {
-    if(root["accept"].asBool()) {
-      write(outpipefd[1], (_USERNAME + "\n").toUtf8().constData(),
-            strlen((_USERNAME + "\n").toUtf8().constData()));
-      write(outpipefd[1], (_PASSWORD + "\n").toUtf8().constData(),
-            strlen((_PASSWORD + "\n").toUtf8().constData()));
+    if (root["accept"].asBool()) {
     } else {
       emit ipPopupSetStatus(root["error"].asString().c_str());
       close(outpipefd[1]);
@@ -89,6 +101,12 @@ void QMLHandler::handleJSON(string buffer) {
       emit ipPopupSetStatus(errorMessage);
       close(outpipefd[1]);
       close(inpipefd[0]);
+    } else {
+
+      write(outpipefd[1], (_USERNAME + "\n").toUtf8().constData(),
+            strlen((_USERNAME + "\n").toUtf8().constData()));
+      write(outpipefd[1], (_PASSWORD + "\n").toUtf8().constData(),
+            strlen((_PASSWORD + "\n").toUtf8().constData()));
     }
   }
 
@@ -109,8 +127,10 @@ void QMLHandler::readPipeLoop() {
   struct pollfd inPipeStatus;
   inPipeStatus.fd = inpipefd[0];
   inPipeStatus.events = POLLIN;
+  std::vector<std::string> inputs;
 
   while (programActive) {
+    inputs = std::vector<std::string>();
     poll(&inPipeStatus, 1, 100);
 
     if (inPipeStatus.revents & POLLIN) {
@@ -125,13 +145,15 @@ void QMLHandler::readPipeLoop() {
     if (pollCount > 9 && buf[0]) {
       buf[1024] = 0;
       buf[strlen(buf)] = 0;
-
-      string cleanBuffer = buf + strcspn(buf, "\n") + 1;
-      string receivedData = cleanBuffer.substr(0, cleanBuffer.size() - 1);
-
-      emit log(QString::fromStdString(receivedData));
-      qInfo() << QString::fromStdString(receivedData);
-      handleJSON(receivedData);
+      inputs = tokenizeByNewlines(string(buf));
+      for (string s : inputs) {
+        //~ string cleanBuffer = buf + strcspn(buf, "\n") + 1;
+        //~ string receivedData = cleanBuffer.substr(0, cleanBuffer.size() - 1);
+
+        emit log(QString::fromStdString(s));
+        qInfo() << QString::fromStdString(s);
+        handleJSON(s);
+      }
       memset(buf, 0, 1024);
       pollCount = 0;
       readOffset = 0;