123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- #include <QGuiApplication>
- #include <csignal>
- #include <cstdio>
- #include <cstdlib>
- #include <iostream>
- #include <poll.h>
- #include <sys/prctl.h>
- #include <sys/wait.h>
- #include <thread>
- #include <unistd.h>
- #include "qmlhandler.h"
- #include <boost/asio.hpp>
- #include <iostream>
- #include <json/json.h>
- using boost::asio::buffer;
- using namespace std;
- int inpipefd[2];
- int outpipefd[2];
- char buf[1025];
- QUrl sendFileUrl = QUrl("");
- QString _USERNAME;
- QString _PASSWORD;
- bool programActive = true;
- QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
- void QMLHandler::onExit() {
- write(outpipefd[1], "disconnect\n", strlen("disconnect\n"));
- }
- void QMLHandler::handleJSON(string buffer) {
- Json::Value root;
- Json::CharReaderBuilder builder;
- Json::CharReader *reader = builder.newCharReader();
- string jsonError;
- bool parsingSuccessful = reader->parse(
- buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
- if (!parsingSuccessful) {
- return;
- }
- const Json::Value command = root["command"];
- string cmd = command.asString();
- if (cmd == "status") {
- emit footerSetStatus(root["response"].asString().c_str());
- }
- else if (cmd == "close") {
- programActive = false;
- }
- else if (cmd == "list") {
- emit receivingClearFileList();
- // Get the array of file Names
- auto fileNames = root["names"];
- for (int i = 0; i < fileNames.size(); i++) {
- emit receivingListFile(
- QString::fromStdString(fileNames[i].asString().c_str()));
- }
- }
- else if (cmd == "connect") {
- if (root["accept"] == false) {
- emit ipPopupSetStatus(root["error"].asString().c_str());
- close(outpipefd[1]);
- close(inpipefd[0]);
- }
- }
- else if (cmd == "version") {
- if (root["accept"] == true) {
- write(outpipefd[1], _USERNAME.toUtf8().constData(),
- strlen(_USERNAME.toUtf8().constData()));
- write(outpipefd[1], _PASSWORD.toUtf8().constData(),
- strlen(_PASSWORD.toUtf8().constData()));
- } else {
- QString errorMessage = QString::fromStdString(string(
- "Version mismatch: \nClient: " + root["clientversion"].asString() +
- "\nServer: " + root["serverversion"].asString()));
- emit ipPopupSetStatus(errorMessage);
- close(outpipefd[1]);
- close(inpipefd[0]);
- }
- }
- else if (cmd == "login") {
- if (root["accept"] == true) {
- emit ipPopupClose();
- } else {
- emit ipPopupSetStatus(root["error"].asString().c_str());
- close(outpipefd[1]);
- close(inpipefd[0]);
- }
- }
- }
- void QMLHandler::readPipeLoop() {
- unsigned int readOffset = 0;
- unsigned int pollCount = 0;
- struct pollfd inPipeStatus;
- inPipeStatus.fd = inpipefd[0];
- inPipeStatus.events = POLLIN;
- while (programActive) {
- poll(&inPipeStatus, 1, 100);
- if (inPipeStatus.revents & POLLIN) {
- readOffset += read(inpipefd[0], buf + readOffset, 1);
- pollCount = 0;
- } else {
- pollCount++;
- }
- if (pollCount > 9 && buf[0]) {
- buf[1024] = 0;
- buf[strlen(buf)] = 0;
- string cleanBuffer = buf + strcspn(buf, "\n") + 1;
- string receivedData = cleanBuffer.substr(0, cleanBuffer.size() - 1);
- emit log(QString::fromStdString(receivedData));
- qInfo() << QString::fromStdString(receivedData);
- handleJSON(receivedData);
- memset(buf, 0, 1024);
- pollCount = 0;
- readOffset = 0;
- }
- }
- }
- // ### QML Handlers ###
- // Sending
- void QMLHandler::onSendingSelectFileButton(QUrl url) {
- sendFileUrl = url.toLocalFile();
- emit log("File Selected: " + sendFileUrl.toString());
- emit sendingSetFileUrlText("Selected File: " + sendFileUrl.toString());
- emit sendingEnableSendButton();
- }
- void QMLHandler::onSendingSendFileButton() {
- QString command = "put " + sendFileUrl.toString() + "\n";
- write(outpipefd[1], command.toUtf8().constData(),
- strlen(command.toUtf8().constData()));
- }
- void QMLHandler::onSendingClearSelectionButton() {
- sendFileUrl = QUrl("");
- emit log("Cleared Selection");
- emit sendingSetFileUrlText("Selected File: None");
- emit sendingDisableSendButton();
- }
- // Receiving
- void QMLHandler::onReceivingListFilesButton() {
- write(outpipefd[1], "list\n", strlen("list\n"));
- }
- void QMLHandler::onReceivingGetFileButton(QString fileName) {
- QString command = "get " + fileName + "\n";
- write(outpipefd[1], command.toUtf8().constData(),
- strlen(command.toUtf8().constData()));
- }
- // Messages
- void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
- // Settings
- void QMLHandler::onSettingsSwitchServerButton() {
- emit settingsOpenSwitchServerPopup();
- }
- // Ip Popup
- void QMLHandler::onIpPopupConnectButton(QString ip, QString username,
- QString password) {
- pid_t pid = 0;
- pipe(inpipefd);
- pipe(outpipefd);
- pid = fork();
- if (pid == 0) {
- // Child
- dup2(outpipefd[0], STDIN_FILENO);
- dup2(inpipefd[1], STDOUT_FILENO);
- // dup2(inpipefd[1], STDERR_FILENO);
- // ask kernel to deliver SIGTERM in case the parent dies
- prctl(PR_SET_PDEATHSIG, SIGTERM);
- // Set the path to the CLI - pass argument h (help) for now
- // TODO: Change hardcoded path
- execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(),
- "--machine", (char *)NULL);
- exit(1);
- }
- // TODO: Not hardcoded
- emit footerSetStatus("Connected to " + ip);
- _USERNAME = username;
- _PASSWORD = password;
- close(outpipefd[0]);
- close(inpipefd[1]);
- std::thread(&QMLHandler::readPipeLoop, this).detach();
- }
- // Switch Popup
- void QMLHandler::onSwitchPopupEnterIp(QString ip) {
- qInfo() << "Switching to " << ip;
- }
- // Footer
- void QMLHandler::onFooterGetStatusButton() {
- write(outpipefd[1], "status\n", strlen("status\n"));
- }
|