Browse Source

added cancel option to listdata and added validation

marius.rescheleit 5 years ago
parent
commit
10af1b6ead
3 changed files with 23 additions and 9 deletions
  1. 6 0
      daemon/include/FileManager.h
  2. 5 0
      daemon/src/FileManager.cpp
  3. 12 9
      daemon/src/JsonCommander.cpp

+ 6 - 0
daemon/include/FileManager.h

@@ -161,6 +161,12 @@ public:
    * @return next chnuk vector
    */
   std::vector<std::string> getNextChunkFromList();
+
+  /**
+   * Cancel current list command.
+   * Clear list vector and set remainingListChunks zero.
+   */
+   void cancelList();
 };
 
 #endif

+ 5 - 0
daemon/src/FileManager.cpp

@@ -141,3 +141,8 @@ std::vector<std::string> FileManager::getNextChunkFromList() {
   remainingListChunks--;
   return ret;
 }
+
+void FileManager::cancelList() {
+  remainingListChunks = 0;
+  list.clear();
+}

+ 12 - 9
daemon/src/JsonCommander.cpp

@@ -69,15 +69,12 @@ JsonCommander::Response JsonCommander::executeList(const Json::Value &message) {
   response.action = send;
   response.json["command"] = "list";
 
-  // TODO test validity of message request
-
   int chunks;
   if (fileManager.getRemainingListChunks() > 0) {
     response.json["accept"] = false;
     response.json["chunks"] = -1;
     response.json["items"] = -1;
     response.json["error"] = "there is already an open list command";
-    // TODO cancel last list command???
   } else if ((chunks = fileManager.openList()) ==
              -1) { // TODO do we need to check? maybe. Think about it
     response.json["accept"] = false;
@@ -101,22 +98,28 @@ JsonCommander::executeListData(const Json::Value &message) {
   response.json["command"] = "listdata";
   Json::Value array;
 
-  // TODO test validity of message request
-
-  // TODO execute request and add the ability to CANCEL the download
-
-  if (fileManager.getRemainingListChunks() == 0) {
+  if (!message["chunk"].isInt() || !message["cancel"].isBool()) {
+    response.json["cancel"] = true;
+    response.json["remaining"] = -1;
+    response.json["names"] = Json::arrayValue;
+    response.json["error"] = "incorrect listdata command request";
+  } else if (fileManager.getRemainingListChunks() == 0) {
     response.json["cancel"] = true;
     response.json["remaining"] = -1;
     response.json["names"] = Json::arrayValue;
     response.json["error"] = "there are no chunks to send";
+  } else if (message["cancel"].asBool()) {
+    response.json["cancel"] = true;
+    response.json["remaining"] = 0;
+    response.json["names"] = Json::arrayValue;
+    response.json["error"] = "";
+    fileManager.cancelList();
   } else if (fileManager.getRemainingListChunks() - 1 !=
              message["chunk"].asInt()) {
     response.json["cancel"] = true;
     response.json["remaining"] = -1;
     response.json["names"] = Json::arrayValue;
     response.json["error"] = "wrong chunk number";
-    // TODO discard file list and cancel list download
   } else {
     std::vector<std::string> v = fileManager.getNextChunkFromList();
     for (int i = 0; i < v.size(); i++)