qmlhandler.cpp 7.1 KB

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