qmlhandler.cpp 8.3 KB

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