qmlhandler.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <QDebug>
  2. #include <QGuiApplication>
  3. #include <csignal>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <poll.h>
  8. #include <string>
  9. #include <sys/prctl.h>
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. #include <thread>
  13. #include <unistd.h>
  14. #include "qmlhandler.h"
  15. #include <boost/asio.hpp>
  16. #include <boost/filesystem.hpp>
  17. #include <iostream>
  18. #include <json/json.h>
  19. using boost::asio::buffer;
  20. using namespace std;
  21. int inpipefd[2];
  22. int outpipefd[2];
  23. char buf[1025];
  24. QUrl sendFileUrl = QUrl("");
  25. QString _IP = "";
  26. bool _CLI_RUNNING = false;
  27. bool _RESTART = false;
  28. pid_t childpid;
  29. bool programActive = true;
  30. QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
  31. void QMLHandler::closeCLI() { _CLI_RUNNING = false; }
  32. void QMLHandler::reopenCLI(QString ip) {
  33. if (_CLI_RUNNING) {
  34. waitpid(childpid, NULL, 0);
  35. closeCLI();
  36. }
  37. _IP = ip;
  38. pipe(inpipefd);
  39. pipe(outpipefd);
  40. childpid = fork();
  41. if (childpid == 0) {
  42. // Child
  43. dup2(outpipefd[0], STDIN_FILENO);
  44. dup2(inpipefd[1], STDOUT_FILENO);
  45. // dup2(inpipefd[1], STDERR_FILENO);
  46. // ask kernel to deliver SIGTERM in case the parent dies
  47. prctl(PR_SET_PDEATHSIG, SIGTERM);
  48. // Set the path to the CLI - pass argument h (help) for now
  49. // TODO: Change hardcoded path
  50. execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(), "--machine", (char *)NULL);
  51. exit(1);
  52. }
  53. _CLI_RUNNING = true;
  54. close(outpipefd[0]);
  55. close(inpipefd[1]);
  56. std::thread(&QMLHandler::readPipeLoop, this).detach();
  57. }
  58. std::vector<std::string> tokenizeByNewlines(std::string in) {
  59. vector<string> res;
  60. size_t index;
  61. for (index = in.find("\n"); index != std::string::npos; index = in.find("\n")) {
  62. if (index != 0)
  63. res.push_back(in.substr(0, index));
  64. in = in.substr(index + 1);
  65. }
  66. if (in.length() > 0)
  67. res.push_back(in);
  68. return res;
  69. }
  70. void QMLHandler::onExit() { write(outpipefd[1], "disconnect\n", strlen("disconnect\n")); }
  71. // This method gets a string and tries to read it as Json
  72. // If it fails to do so, return and do nothing, else handle the content
  73. void QMLHandler::handleJSON(string buffer) {
  74. Json::Value root;
  75. Json::CharReaderBuilder builder;
  76. Json::CharReader *reader = builder.newCharReader();
  77. string jsonError;
  78. // Try to parse the string as Json and store the result of the pasring in a
  79. // boolean
  80. bool parsingSuccessful = reader->parse(buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
  81. // If the string is not correct Json, return
  82. if (!parsingSuccessful) {
  83. return;
  84. }
  85. const Json::Value command = root["command"];
  86. string cmd = command.asString();
  87. qInfo() << QString::fromStdString("Received command " + cmd);
  88. if (!cmd.compare("status")) {
  89. emit footerSetStatus(root["response"].asString().c_str());
  90. }
  91. else if (!cmd.compare("close")) {
  92. programActive = false;
  93. }
  94. else if (!cmd.compare("list")) {
  95. if (root["accept"] == true) {
  96. emit receivingClearFileList();
  97. // Get the array of file Names
  98. auto fileNames = root["names"];
  99. for (int i = 0; i < fileNames.size(); i++) {
  100. emit receivingListFile(QString::fromStdString(fileNames[i].asString().c_str()), boost::filesystem::exists(fileNames[i].asString()));
  101. }
  102. } else {
  103. emit log(root["error"].asString().c_str());
  104. }
  105. }
  106. else if (!cmd.compare("connect")) {
  107. if (root["accept"].asBool()) {
  108. } else {
  109. emit ipPopupSetStatus(root["error"].asString().c_str());
  110. closeCLI();
  111. emit ipPopupEnableConnectButton();
  112. }
  113. }
  114. else if (!cmd.compare("version")) {
  115. if (root["accept"] == true) {
  116. emit ipPopupClose();
  117. emit loginSignupPopupOpen();
  118. } else {
  119. QString errorMessage = QString::fromStdString(
  120. string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
  121. emit ipPopupSetStatus(errorMessage);
  122. closeCLI();
  123. emit ipPopupEnableConnectButton();
  124. }
  125. }
  126. else if (!cmd.compare("login")) {
  127. if (root["accept"] == true) {
  128. emit loginSignupPopupClose();
  129. write(outpipefd[1], "list\n", strlen("list\n"));
  130. } else {
  131. emit loginSetStatus(root["error"].asString().c_str());
  132. reopenCLI(_IP);
  133. emit loginEnableLoginButton();
  134. }
  135. }
  136. else if (!cmd.compare("signup")) {
  137. if (root["accept"] == true) {
  138. emit loginSignupPopupClose();
  139. } else {
  140. emit signupSetStatus(root["error"].asString().c_str());
  141. reopenCLI(_IP);
  142. emit signupEnableRegisterButton();
  143. }
  144. }
  145. else if (!cmd.compare("put")) {
  146. if (root["accept"] == false) {
  147. QString errorMessage = QString::fromStdString(string("Error when uploading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  148. emit log(errorMessage);
  149. }
  150. }
  151. else if (!cmd.compare("putdata")) {
  152. // TODO: Show speed and handle Error
  153. }
  154. else if (!cmd.compare("get")) {
  155. if (root["accept"] == false) {
  156. QString errorMessage = QString::fromStdString(string("Error when downloading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  157. emit log(errorMessage);
  158. } else {
  159. string fileName = root["file"].asString();
  160. // TODO: Only do this in getdata when remaining is 0 (when the file is fully downloaded) - maybe set text to "downloading.." in between
  161. emit receivingDisableDownloadButton(QString::fromStdString(string(fileName)));
  162. }
  163. }
  164. else if (!cmd.compare("getdata")) {
  165. // TODO: Show speed and handle Error
  166. }
  167. else if (!cmd.compare("deleteme")) {
  168. if (root["accept"] == true) {
  169. _RESTART = true;
  170. emit closeWindow();
  171. }
  172. }
  173. }
  174. // This method is a loop which runs in a seperate thread.
  175. // If will read the Input Pipe, which containts the string that get printed
  176. // on stdout by the CLI/Server, and calls handleJSON with the read content
  177. // one it reads a newline.
  178. void QMLHandler::readPipeLoop() {
  179. unsigned int readOffset = 0;
  180. unsigned int pollCount = 0;
  181. struct pollfd inPipeStatus;
  182. inPipeStatus.fd = inpipefd[0];
  183. inPipeStatus.events = POLLIN;
  184. vector<string> inputs;
  185. string pipeInput;
  186. while (programActive && _CLI_RUNNING) {
  187. inputs = vector<string>();
  188. poll(&inPipeStatus, 1, 100);
  189. if (inPipeStatus.revents & POLLIN) {
  190. readOffset += read(inpipefd[0], buf + readOffset, 1);
  191. pollCount = 0;
  192. } else {
  193. pollCount++;
  194. }
  195. if (pollCount > 4 && (readOffset || pipeInput.size())) {
  196. pipeInput.append(buf);
  197. inputs = tokenizeByNewlines(pipeInput);
  198. for (string s : inputs) {
  199. emit log(QString::fromStdString(s));
  200. qInfo() << QString::fromStdString(s);
  201. handleJSON(s);
  202. }
  203. pipeInput = string();
  204. memset(buf, 0, 1025);
  205. pollCount = 0;
  206. readOffset = 0;
  207. if (waitpid(childpid, NULL, WNOHANG)) {
  208. // nonzero means error or childid has changed state
  209. // for us that means child has exited -> CLI is dead
  210. break;
  211. }
  212. }
  213. if (readOffset >= 1024) {
  214. pipeInput.append(buf);
  215. readOffset = 0;
  216. pollCount = 0;
  217. memset(buf, 0, 1025);
  218. }
  219. }
  220. }
  221. // ### QML Handlers ###
  222. // Sending
  223. void QMLHandler::onSendingSelectFileButton(QUrl url) {
  224. sendFileUrl = url.toLocalFile();
  225. emit log("File Selected: " + sendFileUrl.toString());
  226. emit sendingSetFileUrlText("Selected File: " + sendFileUrl.toString());
  227. emit sendingEnableSendButton();
  228. }
  229. void QMLHandler::onSendingSendFileButton() {
  230. QString command = "put " + sendFileUrl.toString() + "\n";
  231. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  232. }
  233. void QMLHandler::onSendingClearSelectionButton() {
  234. sendFileUrl = QUrl("");
  235. emit log("Cleared Selection");
  236. emit sendingSetFileUrlText("Selected File: None");
  237. emit sendingDisableSendButton();
  238. }
  239. // Receiving
  240. void QMLHandler::onReceivingListFilesButton() { write(outpipefd[1], "list\n", strlen("list\n")); }
  241. void QMLHandler::onReceivingDownloadFileButton(QString fileName) {
  242. QString command = "get " + fileName + "\n";
  243. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  244. }
  245. // Messages
  246. void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
  247. // Settings
  248. void QMLHandler::onSettingsDeleteMeButton() { write(outpipefd[1], "deleteme\n", strlen("deleteme\n")); }
  249. // Ip Popup
  250. void QMLHandler::onIpPopupConnectButton(QString ip) {
  251. reopenCLI(ip);
  252. emit ipPopupDisableConnectButton();
  253. }
  254. // Login
  255. void QMLHandler::onLoginLoginButton(QString username, QString password) {
  256. QString command = "login " + username + " " + password + "\n";
  257. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  258. emit loginDisableLoginButton();
  259. }
  260. // Signup
  261. void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo) {
  262. if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
  263. emit signupSetStatus("Passwords don't match");
  264. return;
  265. }
  266. QString command = "signup " + username + " " + passwordOne + "\n";
  267. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  268. emit signupDisableRegisterButton();
  269. }
  270. // Footer
  271. void QMLHandler::onFooterGetStatusButton() { write(outpipefd[1], "status\n", strlen("status\n")); }