Browse Source

implemented listdata command

marius.rescheleit 5 years ago
parent
commit
7def9ab76a
3 changed files with 38 additions and 11 deletions
  1. 17 3
      daemon/include/FileManager.h
  2. 9 2
      daemon/src/FileManager.cpp
  3. 12 6
      daemon/src/JsonCommander.cpp

+ 17 - 3
daemon/include/FileManager.h

@@ -11,7 +11,7 @@
  */
 class FileManager {
 private:
-  const std::string fileDirectory = ".";
+  const std::string fileDirectory = "./files";
 
   /**
    * file stream for get command
@@ -44,6 +44,11 @@ private:
    */
    std::vector<std::string> list;
 
+   /**
+    * The number of remainig chunks in vector list.
+    */
+   int remainingListChunks;
+
 public:
   enum { max_data_length = 512 };
 
@@ -139,10 +144,19 @@ public:
     */
     int openList();
 
-    int getListSize();
+    /**
+     * @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
      */

+ 9 - 2
daemon/src/FileManager.cpp

@@ -10,7 +10,7 @@ FileManager::~FileManager() {
   closeGetFile();
 }
 
-bool FileManager::isUploading() { return this->putFile.is_open() || !list.empty(); }
+bool FileManager::isUploading() { return this->putFile.is_open(); }
 
 bool FileManager::isDownloading() { return this->getFile.is_open(); }
 
@@ -90,7 +90,7 @@ std::vector<std::string> FileManager::listFiles() {
        boost::filesystem::directory_iterator(this->fileDirectory)) {
     if (boost::filesystem::is_directory(entry.path()))
       continue;
-    res.push_back(entry.path().string());
+    res.push_back(entry.path().filename().string());
   }
   return res;
 }
@@ -111,9 +111,14 @@ int FileManager::openList() {
   }
   if(list.size() > 0)
     chunks++;
+  remainingListChunks = chunks;
   return chunks;
 }
 
+int FileManager::getRemainingListChunks() {
+  return remainingListChunks;
+}
+
 int FileManager::getListSize() {
   return list.size();
 }
@@ -132,5 +137,7 @@ std::vector<std::string> FileManager::getNextChunkFromList() {
       list.pop_back();
     }
   }
+  if(remainingListChunks != 0)
+    remainingListChunks--;
   return ret;
 }

+ 12 - 6
daemon/src/JsonCommander.cpp

@@ -69,11 +69,14 @@ JsonCommander::Response JsonCommander::executeList(const Json::Value &message) {
   response.action = send;
   response.json["command"] = "list";
 
-  if(fileManager.isUploading()) {
+  if(fileManager.getRemainingListChunks()!=0) {
     response.json["accept"] = false;
-    response.json["error"] = "there is an upload already running";
+    response.json["error"] = "there is already an open list command";
+  } else if (fileManager.openList() == -1){
+    response.json["accept"] = false;
+    response.json["error"] = "there is a filename which is to long";
   } else {
-    response.json["chunks"] = fileManager.openList();
+    response.json["chunks"] = fileManager.getRemainingListChunks();
     response.json["items"] = fileManager.getListSize();
     response.json["error"] = "";
   }
@@ -87,14 +90,17 @@ JsonCommander::Response JsonCommander::executeListData(const Json::Value &messag
   response.json["command"] = "listdata";
   Json::Value array;
 
-  if(fileManager.getListSize()==0) {
+  if(fileManager.getRemainingListChunks()==0) {
+    response.json["accept"] = false;
+    response.json["error"] = "there are no chunks to send";
+  } else if(fileManager.getRemainingListChunks()-1!=message["chunk"].asInt()) {
     response.json["accept"] = false;
-    response.json["error"] = "nothing to upload";
+    response.json["error"] = "wrong chunk number";
   } else {
     std::vector<std::string> v = fileManager.getNextChunkFromList();
     for(int i=0; i<v.size(); i++)
       array.append(v.at(i));
-    response.json["remaining"] = message["chunk"].asInt()-1;
+    response.json["remaining"] = fileManager.getRemainingListChunks();
     response.json["cancel"] = false;
     response.json["names"] = array;
     response.json["error"] = "";