|
@@ -22,13 +22,54 @@ int inpipefd[2];
|
|
int outpipefd[2];
|
|
int outpipefd[2];
|
|
char buf[1025];
|
|
char buf[1025];
|
|
QUrl sendFileUrl = QUrl("");
|
|
QUrl sendFileUrl = QUrl("");
|
|
-QString _USERNAME;
|
|
|
|
-QString _PASSWORD;
|
|
|
|
|
|
+QString _IP = "";
|
|
|
|
+bool _CLI_RUNNING = false;
|
|
|
|
|
|
bool programActive = true;
|
|
bool programActive = true;
|
|
|
|
|
|
QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
|
|
QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
|
|
|
|
|
|
|
|
+void QMLHandler::closeCLI() {
|
|
|
|
+ close(outpipefd[1]);
|
|
|
|
+ close(inpipefd[0]);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void QMLHandler::reopenCLI(QString ip) {
|
|
|
|
+ if (_CLI_RUNNING) {
|
|
|
|
+ closeCLI();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _IP = ip;
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ _CLI_RUNNING = true;
|
|
|
|
+
|
|
|
|
+ exit(1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ close(outpipefd[0]);
|
|
|
|
+ close(inpipefd[1]);
|
|
|
|
+
|
|
|
|
+ std::thread(&QMLHandler::readPipeLoop, this).detach();
|
|
|
|
+}
|
|
|
|
+
|
|
std::vector<std::string> tokenizeByNewlines(std::string in) {
|
|
std::vector<std::string> tokenizeByNewlines(std::string in) {
|
|
vector<string> res;
|
|
vector<string> res;
|
|
size_t index;
|
|
size_t index;
|
|
@@ -69,15 +110,15 @@ void QMLHandler::handleJSON(string buffer) {
|
|
string cmd = command.asString();
|
|
string cmd = command.asString();
|
|
qInfo() << QString::fromStdString("Received command " + cmd);
|
|
qInfo() << QString::fromStdString("Received command " + cmd);
|
|
|
|
|
|
- if (cmd.compare("status")) {
|
|
|
|
|
|
+ if (!cmd.compare("status")) {
|
|
emit footerSetStatus(root["response"].asString().c_str());
|
|
emit footerSetStatus(root["response"].asString().c_str());
|
|
}
|
|
}
|
|
|
|
|
|
- else if (cmd.compare("close")) {
|
|
|
|
|
|
+ else if (!cmd.compare("close")) {
|
|
programActive = false;
|
|
programActive = false;
|
|
}
|
|
}
|
|
|
|
|
|
- else if (cmd.compare("list")) {
|
|
|
|
|
|
+ else if (!cmd.compare("list")) {
|
|
if (root["accept"] == true) {
|
|
if (root["accept"] == true) {
|
|
emit receivingClearFileList();
|
|
emit receivingClearFileList();
|
|
|
|
|
|
@@ -92,43 +133,46 @@ void QMLHandler::handleJSON(string buffer) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- else if (cmd == "connect") {
|
|
|
|
|
|
+ else if (!cmd.compare("connect")) {
|
|
if (root["accept"].asBool()) {
|
|
if (root["accept"].asBool()) {
|
|
} else {
|
|
} else {
|
|
emit ipPopupSetStatus(root["error"].asString().c_str());
|
|
emit ipPopupSetStatus(root["error"].asString().c_str());
|
|
- close(outpipefd[1]);
|
|
|
|
- close(inpipefd[0]);
|
|
|
|
|
|
+ reopenCLI(_IP);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- else if (cmd == "version") {
|
|
|
|
- if (root["accept"].asBool() == false) {
|
|
|
|
|
|
+ else if (!cmd.compare("version")) {
|
|
|
|
+ if (root["accept"] == true) {
|
|
|
|
+ emit ipPopupClose();
|
|
|
|
+ emit loginSignupPopupOpen();
|
|
|
|
+ } else {
|
|
QString errorMessage = QString::fromStdString(string(
|
|
QString errorMessage = QString::fromStdString(string(
|
|
"Version mismatch: \nClient: " + root["clientversion"].asString() +
|
|
"Version mismatch: \nClient: " + root["clientversion"].asString() +
|
|
"\nServer: " + root["serverversion"].asString()));
|
|
"\nServer: " + root["serverversion"].asString()));
|
|
emit ipPopupSetStatus(errorMessage);
|
|
emit ipPopupSetStatus(errorMessage);
|
|
- close(outpipefd[1]);
|
|
|
|
- close(inpipefd[0]);
|
|
|
|
- } else {
|
|
|
|
|
|
+ reopenCLI(_IP);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- write(outpipefd[1], (_USERNAME + "\n").toUtf8().constData(),
|
|
|
|
- strlen((_USERNAME + "\n").toUtf8().constData()));
|
|
|
|
- write(outpipefd[1], (_PASSWORD + "\n").toUtf8().constData(),
|
|
|
|
- strlen((_PASSWORD + "\n").toUtf8().constData()));
|
|
|
|
|
|
+ else if (!cmd.compare("login")) {
|
|
|
|
+ if (root["accept"] == true) {
|
|
|
|
+ emit loginSignupPopupClose();
|
|
|
|
+ } else {
|
|
|
|
+ emit loginSetStatus(root["error"].asString().c_str());
|
|
|
|
+ reopenCLI(_IP);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- else if (cmd == "login") {
|
|
|
|
- if (root["accept"].asBool() == true) {
|
|
|
|
- emit ipPopupClose();
|
|
|
|
|
|
+ else if (!cmd.compare("signup")) {
|
|
|
|
+ if (root["accept"] == true) {
|
|
|
|
+ emit loginSignupPopupClose();
|
|
} else {
|
|
} else {
|
|
- emit ipPopupSetStatus(root["error"].asString().c_str());
|
|
|
|
- close(outpipefd[1]);
|
|
|
|
- close(inpipefd[0]);
|
|
|
|
|
|
+ emit loginSetStatus(root["error"].asString().c_str());
|
|
|
|
+ reopenCLI(_IP);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- else if (cmd.compare("put")) {
|
|
|
|
|
|
+ else if (!cmd.compare("put")) {
|
|
if (root["accept"] == false) {
|
|
if (root["accept"] == false) {
|
|
QString errorMessage = QString::fromStdString(
|
|
QString errorMessage = QString::fromStdString(
|
|
string("Error when uploading file " + root["file"].asString() +
|
|
string("Error when uploading file " + root["file"].asString() +
|
|
@@ -137,11 +181,11 @@ void QMLHandler::handleJSON(string buffer) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- else if (cmd.compare("putdata")) {
|
|
|
|
|
|
+ else if (!cmd.compare("putdata")) {
|
|
// TODO: Show speed and handle Error
|
|
// TODO: Show speed and handle Error
|
|
}
|
|
}
|
|
|
|
|
|
- else if (cmd.compare("get")) {
|
|
|
|
|
|
+ else if (!cmd.compare("get")) {
|
|
if (root["accept"] == false) {
|
|
if (root["accept"] == false) {
|
|
QString errorMessage = QString::fromStdString(
|
|
QString errorMessage = QString::fromStdString(
|
|
string("Error when downloading file " + root["file"].asString() +
|
|
string("Error when downloading file " + root["file"].asString() +
|
|
@@ -150,7 +194,7 @@ void QMLHandler::handleJSON(string buffer) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- else if (cmd.compare("getdata")) {
|
|
|
|
|
|
+ else if (!cmd.compare("getdata")) {
|
|
// TODO: Show speed and handle Error
|
|
// TODO: Show speed and handle Error
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -173,13 +217,6 @@ void QMLHandler::readPipeLoop() {
|
|
|
|
|
|
if (inPipeStatus.revents & POLLIN) {
|
|
if (inPipeStatus.revents & POLLIN) {
|
|
readOffset += read(inpipefd[0], buf + readOffset, 1);
|
|
readOffset += read(inpipefd[0], buf + readOffset, 1);
|
|
-
|
|
|
|
- if (buf[readOffset - 1] == '\n') {
|
|
|
|
- pollCount = 10;
|
|
|
|
- } else {
|
|
|
|
- pollCount = 0;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
pollCount = 0;
|
|
pollCount = 0;
|
|
|
|
|
|
} else {
|
|
} else {
|
|
@@ -256,45 +293,25 @@ void QMLHandler::onSettingsSwitchServerButton() {
|
|
}
|
|
}
|
|
|
|
|
|
// Ip Popup
|
|
// 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);
|
|
|
|
|
|
+void QMLHandler::onIpPopupConnectButton(QString ip) { reopenCLI(ip); }
|
|
|
|
|
|
- // 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();
|
|
|
|
|
|
+// Login
|
|
|
|
+void QMLHandler::onLoginLoginButton(QString username, QString password) {
|
|
|
|
+ QString command = "login " + username + " " + password + "\n";
|
|
|
|
+ write(outpipefd[1], command.toUtf8().constData(),
|
|
|
|
+ strlen(command.toUtf8().constData()));
|
|
}
|
|
}
|
|
|
|
|
|
-// Switch Popup
|
|
|
|
-void QMLHandler::onSwitchPopupEnterIp(QString ip) {
|
|
|
|
- qInfo() << "Switching to " << ip;
|
|
|
|
|
|
+// Signup
|
|
|
|
+void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne,
|
|
|
|
+ QString passwordTwo) {
|
|
|
|
+ if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
|
|
|
|
+ emit signupSetStatus("Passwords don't match");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ QString command = "signup " + username + " " + passwordOne + "\n";
|
|
|
|
+ write(outpipefd[1], command.toUtf8().constData(),
|
|
|
|
+ strlen(command.toUtf8().constData()));
|
|
}
|
|
}
|
|
|
|
|
|
// Footer
|
|
// Footer
|