Browse Source

list command: some annotations and fixing stuff

Jonas Pflanzer 5 years ago
parent
commit
54a62b8ac9
4 changed files with 78 additions and 57 deletions
  1. 31 31
      daemon/include/FileManager.h
  2. 14 14
      daemon/src/FileManager.cpp
  3. 32 11
      daemon/src/JsonCommander.cpp
  4. 1 1
      daemon/test/CMakeLists.txt

+ 31 - 31
daemon/include/FileManager.h

@@ -42,12 +42,12 @@ private:
   /**
    * list vector for list command
    */
-   std::vector<std::string> list;
+  std::vector<std::string> list;
 
-   /**
-    * The number of remainig chunks in vector list.
-    */
-   int remainingListChunks;
+  /**
+   * The number of remainig chunks in vector list.
+   */
+  int remainingListChunks;
 
 public:
   enum { max_data_length = 512 };
@@ -135,32 +135,32 @@ public:
    *
    * @return vector of all files in the current directory
    */
-   std::vector<std::string> listFiles();
-
-   /**
-    * Open list command. Set list vector and claculate chunks
-    *
-    * @return chunks of the resulting list.
-    */
-    int openList();
-
-    /**
-     * @return remaining chunks to be send with listdata command
-     */
-     int getRemainingListChunks();
-
-     /**
-      * @return size of the list vector
-      */
-      int getListSize();
-
-    /**
-     * Return next chunk for listdata command.
-     * Increases remainingListChunks. You need to run openList before.
-     *
-     * @return next chnuk vector
-     */
-    std::vector<std::string> getNextChunkFromList();
+  std::vector<std::string> listFiles();
+
+  /**
+   * Open list command. Set list vector and claculate chunks
+   *
+   * @return chunks of the resulting list.
+   */
+  int openList();
+
+  /**
+   * @return remaining chunks to be send with listdata command
+   */
+  int getRemainingListChunks();
+
+  /**
+   * @return size of the list vector
+   */
+  int getListSize();
+
+  /**
+   * Return next chunk for listdata command.
+   * Increases remainingListChunks. You need to run openList before.
+   *
+   * @return next chnuk vector
+   */
+  std::vector<std::string> getNextChunkFromList();
 };
 
 #endif

+ 14 - 14
daemon/src/FileManager.cpp

@@ -99,45 +99,45 @@ int FileManager::openList() {
   list = listFiles();
   int chunks = 0;
   int cursize = 0;
-  for(int i=0; i<list.size(); i++) {
-    if(list.at(i).length() > max_data_length)
+  for (int i = 0; i < list.size(); i++) {
+    if (list.at(i).length() > max_data_length)
       return -1;
     cursize += list.at(i).length();
-    if(cursize > max_data_length) {
+    if (cursize > max_data_length) {
       chunks++;
       i--;
       cursize = 0;
     }
   }
-  if(list.size() > 0)
+  if (list.size() > 0)
     chunks++;
   remainingListChunks = chunks;
   return chunks;
 }
 
-int FileManager::getRemainingListChunks() {
-  return remainingListChunks;
-}
+int FileManager::getRemainingListChunks() { return remainingListChunks; }
 
-int FileManager::getListSize() {
-  return list.size();
-}
+int FileManager::getListSize() { return list.size(); }
 
 std::vector<std::string> FileManager::getNextChunkFromList() {
+  if (remainingListChunks == 0) { // This should never happen!!!
+    return std::vector<std::string>();
+  }
+
   std::vector<std::string> ret;
   int cursize = 0;
   std::string tmp;
-  while(!list.empty()) {
+  while (!list.empty()) {
     tmp = list.back();
     cursize += tmp.length();
-    if(cursize>max_data_length) {
+    if (cursize > max_data_length) {
       break;
     } else {
       ret.push_back(tmp);
       list.pop_back();
     }
   }
-  if(remainingListChunks != 0)
-    remainingListChunks--;
+
+  remainingListChunks--;
   return ret;
 }

+ 32 - 11
daemon/src/JsonCommander.cpp

@@ -69,14 +69,24 @@ JsonCommander::Response JsonCommander::executeList(const Json::Value &message) {
   response.action = send;
   response.json["command"] = "list";
 
-  if(fileManager.getRemainingListChunks()!=0) {
+  // 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";
-  } else if (fileManager.openList() == -1){
+    // 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;
-    response.json["error"] = "there is a filename which is to long";
+    response.json["chunks"] = -1;
+    response.json["items"] = -1;
+    response.json["error"] = "there is a filename which is too long";
   } else {
-    response.json["chunks"] = fileManager.getRemainingListChunks();
+    response.json["accept"] = true;
+    response.json["chunks"] = chunks;
     response.json["items"] = fileManager.getListSize();
     response.json["error"] = "";
   }
@@ -84,23 +94,34 @@ JsonCommander::Response JsonCommander::executeList(const Json::Value &message) {
   return response;
 }
 
-JsonCommander::Response JsonCommander::executeListData(const Json::Value &message) {
+JsonCommander::Response
+JsonCommander::executeListData(const Json::Value &message) {
   JsonCommander::Response response;
   response.action = send;
   response.json["command"] = "listdata";
   Json::Value array;
 
-  if(fileManager.getRemainingListChunks()==0) {
-    response.json["accept"] = false;
+  // TODO test validity of message request
+
+  // TODO execute request and add the ability to CANCEL the download
+
+  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(fileManager.getRemainingListChunks()-1!=message["chunk"].asInt()) {
-    response.json["accept"] = false;
+  } 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++)
+    for (int i = 0; i < v.size(); i++)
       array.append(v.at(i));
-    response.json["remaining"] = fileManager.getRemainingListChunks();
+    response.json["remaining"] = message["chunk"].asInt();
     response.json["cancel"] = false;
     response.json["names"] = array;
     response.json["error"] = "";

+ 1 - 1
daemon/test/CMakeLists.txt

@@ -11,7 +11,7 @@ pkg_check_modules(JSONCPP REQUIRED jsoncpp)
 
 
 find_package(Threads REQUIRED)
-find_package(Boost 1.67 REQUIRED COMPONENTS system)
+find_package(Boost 1.67 REQUIRED COMPONENTS system filesystem)
 
 # Setup testing
 enable_testing()