qmlhandler.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.compare("status")) {
  41. emit footerSetStatus(root["response"].asString().c_str());
  42. }
  43. else if (cmd.compare("close")) {
  44. programActive = false;
  45. }
  46. else if (cmd.compare("list")) {
  47. if (root["accept"] == true) {
  48. emit receivingClearFileList();
  49. // Get the array of file Names
  50. auto fileNames = root["names"];
  51. for (int i = 0; i < fileNames.size(); i++) {
  52. emit receivingListFile(
  53. QString::fromStdString(fileNames[i].asString().c_str()));
  54. }
  55. } else {
  56. emit log(root["error"].asString().c_str());
  57. }
  58. }
  59. else if (cmd.compare("connect")) {
  60. if (root["accept"] == false) {
  61. emit ipPopupSetStatus(root["error"].asString().c_str());
  62. close(outpipefd[1]);
  63. close(inpipefd[0]);
  64. }
  65. }
  66. else if (cmd.compare("version")) {
  67. if (root["accept"] == true) {
  68. write(outpipefd[1], _USERNAME.toUtf8().constData(),
  69. strlen(_USERNAME.toUtf8().constData()));
  70. write(outpipefd[1], _PASSWORD.toUtf8().constData(),
  71. strlen(_PASSWORD.toUtf8().constData()));
  72. } else {
  73. QString errorMessage = QString::fromStdString(string(
  74. "Version mismatch: \nClient: " + root["clientversion"].asString() +
  75. "\nServer: " + root["serverversion"].asString()));
  76. emit ipPopupSetStatus(errorMessage);
  77. close(outpipefd[1]);
  78. close(inpipefd[0]);
  79. }
  80. }
  81. else if (cmd.compare("login")) {
  82. if (root["accept"] == true) {
  83. emit ipPopupClose();
  84. } else {
  85. emit ipPopupSetStatus(root["error"].asString().c_str());
  86. close(outpipefd[1]);
  87. close(inpipefd[0]);
  88. }
  89. }
  90. else if (cmd.compare("put")) {
  91. if (root["accept"] == false) {
  92. QString errorMessage = QString::fromStdString(
  93. string("Error when uploading file " + root["file"].asString() +
  94. ":\n" + root["error"].asString()));
  95. emit log(errorMessage);
  96. }
  97. }
  98. else if (cmd.compare("putdata")) {
  99. // TODO: Show speed and handle Error
  100. }
  101. else if (cmd.compare("get")) {
  102. if (root["accept"] == false) {
  103. QString errorMessage = QString::fromStdString(
  104. string("Error when downloading file " + root["file"].asString() +
  105. ":\n" + root["error"].asString()));
  106. emit log(errorMessage);
  107. }
  108. }
  109. else if (cmd.compare("getdata")) {
  110. // TODO: Show speed and handle Error
  111. }
  112. }
  113. void QMLHandler::readPipeLoop() {
  114. unsigned int readOffset = 0;
  115. unsigned int pollCount = 0;
  116. struct pollfd inPipeStatus;
  117. inPipeStatus.fd = inpipefd[0];
  118. inPipeStatus.events = POLLIN;
  119. while (programActive) {
  120. poll(&inPipeStatus, 1, 100);
  121. if (inPipeStatus.revents & POLLIN) {
  122. readOffset += read(inpipefd[0], buf + readOffset, 1);
  123. pollCount = 0;
  124. } else {
  125. pollCount++;
  126. }
  127. if (pollCount > 9 && buf[0]) {
  128. buf[1024] = 0;
  129. buf[strlen(buf)] = 0;
  130. string cleanBuffer = buf + strcspn(buf, "\n") + 1;
  131. string receivedData = cleanBuffer.substr(0, cleanBuffer.size() - 1);
  132. emit log(QString::fromStdString(receivedData));
  133. qInfo() << QString::fromStdString(receivedData);
  134. handleJSON(receivedData);
  135. memset(buf, 0, 1024);
  136. pollCount = 0;
  137. readOffset = 0;
  138. }
  139. }
  140. }
  141. // ### QML Handlers ###
  142. // Sending
  143. void QMLHandler::onSendingSelectFileButton(QUrl url) {
  144. sendFileUrl = url.toLocalFile();
  145. emit log("File Selected: " + sendFileUrl.toString());
  146. emit sendingSetFileUrlText("Selected File: " + sendFileUrl.toString());
  147. emit sendingEnableSendButton();
  148. }
  149. void QMLHandler::onSendingSendFileButton() {
  150. QString command = "put " + sendFileUrl.toString() + "\n";
  151. write(outpipefd[1], command.toUtf8().constData(),
  152. strlen(command.toUtf8().constData()));
  153. }
  154. void QMLHandler::onSendingClearSelectionButton() {
  155. sendFileUrl = QUrl("");
  156. emit log("Cleared Selection");
  157. emit sendingSetFileUrlText("Selected File: None");
  158. emit sendingDisableSendButton();
  159. }
  160. // Receiving
  161. void QMLHandler::onReceivingListFilesButton() {
  162. write(outpipefd[1], "list\n", strlen("list\n"));
  163. }
  164. void QMLHandler::onReceivingGetFileButton(QString fileName) {
  165. QString command = "get " + fileName + "\n";
  166. write(outpipefd[1], command.toUtf8().constData(),
  167. strlen(command.toUtf8().constData()));
  168. }
  169. // Messages
  170. void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
  171. // Settings
  172. void QMLHandler::onSettingsSwitchServerButton() {
  173. emit settingsOpenSwitchServerPopup();
  174. }
  175. // Ip Popup
  176. void QMLHandler::onIpPopupConnectButton(QString ip, QString username,
  177. QString password) {
  178. pid_t pid = 0;
  179. pipe(inpipefd);
  180. pipe(outpipefd);
  181. pid = fork();
  182. if (pid == 0) {
  183. // Child
  184. dup2(outpipefd[0], STDIN_FILENO);
  185. dup2(inpipefd[1], STDOUT_FILENO);
  186. // dup2(inpipefd[1], STDERR_FILENO);
  187. // ask kernel to deliver SIGTERM in case the parent dies
  188. prctl(PR_SET_PDEATHSIG, SIGTERM);
  189. // Set the path to the CLI - pass argument h (help) for now
  190. // TODO: Change hardcoded path
  191. execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(),
  192. "--machine", (char *)NULL);
  193. exit(1);
  194. }
  195. // TODO: Not hardcoded
  196. emit footerSetStatus("Connected to " + ip);
  197. _USERNAME = username;
  198. _PASSWORD = password;
  199. close(outpipefd[0]);
  200. close(inpipefd[1]);
  201. std::thread(&QMLHandler::readPipeLoop, this).detach();
  202. }
  203. // Switch Popup
  204. void QMLHandler::onSwitchPopupEnterIp(QString ip) {
  205. qInfo() << "Switching to " << ip;
  206. }
  207. // Footer
  208. void QMLHandler::onFooterGetStatusButton() {
  209. write(outpipefd[1], "status\n", strlen("status\n"));
  210. }