qmlhandler.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <QGuiApplication>
  2. #include <csignal>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <poll.h>
  7. #include <sys/prctl.h>
  8. #include <sys/wait.h>
  9. #include <thread>
  10. #include <unistd.h>
  11. #include "qmlhandler.h"
  12. #include <boost/asio.hpp>
  13. #include <iostream>
  14. #include <json/json.h>
  15. using boost::asio::buffer;
  16. using namespace std;
  17. int inpipefd[2];
  18. int outpipefd[2];
  19. char buf[1025];
  20. QUrl sendFileUrl = QUrl("");
  21. QString _USERNAME;
  22. QString _PASSWORD;
  23. bool programActive = true;
  24. QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
  25. void QMLHandler::onExit() {
  26. write(outpipefd[1], "disconnect\n", strlen("disconnect\n"));
  27. }
  28. void QMLHandler::handleJSON(string buffer) {
  29. Json::Value root;
  30. Json::CharReaderBuilder builder;
  31. Json::CharReader *reader = builder.newCharReader();
  32. string jsonError;
  33. bool parsingSuccessful = reader->parse(
  34. buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
  35. if (!parsingSuccessful) {
  36. return;
  37. }
  38. const Json::Value command = root["command"];
  39. string cmd = command.asString();
  40. if (cmd == "status") {
  41. emit footerSetStatus(root["response"].asString().c_str());
  42. }
  43. else if (cmd == "close") {
  44. programActive = false;
  45. }
  46. else if (cmd == "list") {
  47. emit receivingClearFileList();
  48. // Get the array of file Names
  49. auto fileNames = root["names"];
  50. for (int i = 0; i < fileNames.size(); i++) {
  51. emit receivingListFile(
  52. QString::fromStdString(fileNames[i].asString().c_str()));
  53. }
  54. }
  55. else if (cmd == "connect") {
  56. if (root["accept"] == false) {
  57. emit ipPopupSetStatus(root["error"].asString().c_str());
  58. close(outpipefd[1]);
  59. close(inpipefd[0]);
  60. }
  61. }
  62. else if (cmd == "version") {
  63. if (root["accept"] == true) {
  64. write(outpipefd[1], _USERNAME.toUtf8().constData(),
  65. strlen(_USERNAME.toUtf8().constData()));
  66. write(outpipefd[1], _PASSWORD.toUtf8().constData(),
  67. strlen(_PASSWORD.toUtf8().constData()));
  68. } else {
  69. QString errorMessage = QString::fromStdString(string(
  70. "Version mismatch: \nClient: " + root["clientversion"].asString() +
  71. "\nServer: " + root["serverversion"].asString()));
  72. emit ipPopupSetStatus(errorMessage);
  73. close(outpipefd[1]);
  74. close(inpipefd[0]);
  75. }
  76. }
  77. else if (cmd == "login") {
  78. if (root["accept"] == true) {
  79. emit ipPopupClose();
  80. } else {
  81. emit ipPopupSetStatus(root["error"].asString().c_str());
  82. close(outpipefd[1]);
  83. close(inpipefd[0]);
  84. }
  85. }
  86. }
  87. void QMLHandler::readPipeLoop() {
  88. unsigned int readOffset = 0;
  89. unsigned int pollCount = 0;
  90. struct pollfd inPipeStatus;
  91. inPipeStatus.fd = inpipefd[0];
  92. inPipeStatus.events = POLLIN;
  93. while (programActive) {
  94. poll(&inPipeStatus, 1, 100);
  95. if (inPipeStatus.revents & POLLIN) {
  96. readOffset += read(inpipefd[0], buf + readOffset, 1);
  97. pollCount = 0;
  98. } else {
  99. pollCount++;
  100. }
  101. if (pollCount > 9 && buf[0]) {
  102. buf[1024] = 0;
  103. buf[strlen(buf)] = 0;
  104. string cleanBuffer = buf + strcspn(buf, "\n") + 1;
  105. string receivedData = cleanBuffer.substr(0, cleanBuffer.size() - 1);
  106. emit log(QString::fromStdString(receivedData));
  107. qInfo() << QString::fromStdString(receivedData);
  108. handleJSON(receivedData);
  109. memset(buf, 0, 1024);
  110. pollCount = 0;
  111. readOffset = 0;
  112. }
  113. }
  114. }
  115. // ### QML Handlers ###
  116. // Sending
  117. void QMLHandler::onSendingSelectFileButton(QUrl url) {
  118. sendFileUrl = url.toLocalFile();
  119. emit log("File Selected: " + sendFileUrl.toString());
  120. emit sendingSetFileUrlText("Selected File: " + sendFileUrl.toString());
  121. emit sendingEnableSendButton();
  122. }
  123. void QMLHandler::onSendingSendFileButton() {
  124. QString command = "put " + sendFileUrl.toString() + "\n";
  125. write(outpipefd[1], command.toUtf8().constData(),
  126. strlen(command.toUtf8().constData()));
  127. }
  128. void QMLHandler::onSendingClearSelectionButton() {
  129. sendFileUrl = QUrl("");
  130. emit log("Cleared Selection");
  131. emit sendingSetFileUrlText("Selected File: None");
  132. emit sendingDisableSendButton();
  133. }
  134. // Receiving
  135. void QMLHandler::onReceivingListFilesButton() {
  136. write(outpipefd[1], "list\n", strlen("list\n"));
  137. }
  138. void QMLHandler::onReceivingGetFileButton(QString fileName) {
  139. QString command = "get " + fileName + "\n";
  140. write(outpipefd[1], command.toUtf8().constData(),
  141. strlen(command.toUtf8().constData()));
  142. }
  143. // Messages
  144. void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
  145. // Settings
  146. void QMLHandler::onSettingsSwitchServerButton() {
  147. emit settingsOpenSwitchServerPopup();
  148. }
  149. // Ip Popup
  150. void QMLHandler::onIpPopupConnectButton(QString ip, QString username,
  151. QString password) {
  152. pid_t pid = 0;
  153. pipe(inpipefd);
  154. pipe(outpipefd);
  155. pid = fork();
  156. if (pid == 0) {
  157. // Child
  158. dup2(outpipefd[0], STDIN_FILENO);
  159. dup2(inpipefd[1], STDOUT_FILENO);
  160. // dup2(inpipefd[1], STDERR_FILENO);
  161. // ask kernel to deliver SIGTERM in case the parent dies
  162. prctl(PR_SET_PDEATHSIG, SIGTERM);
  163. // Set the path to the CLI - pass argument h (help) for now
  164. // TODO: Change hardcoded path
  165. execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(),
  166. "--machine", (char *)NULL);
  167. exit(1);
  168. }
  169. // TODO: Not hardcoded
  170. emit footerSetStatus("Connected to " + ip);
  171. _USERNAME = username;
  172. _PASSWORD = password;
  173. close(outpipefd[0]);
  174. close(inpipefd[1]);
  175. std::thread(&QMLHandler::readPipeLoop, this).detach();
  176. }
  177. // Switch Popup
  178. void QMLHandler::onSwitchPopupEnterIp(QString ip) {
  179. qInfo() << "Switching to " << ip;
  180. }
  181. // Footer
  182. void QMLHandler::onFooterGetStatusButton() {
  183. write(outpipefd[1], "status\n", strlen("status\n"));
  184. }