Ver Fonte

Put now requires to send amount of chunks

Jonas Pflanzer há 5 anos atrás
pai
commit
d45e06fc78
2 ficheiros alterados com 19 adições e 7 exclusões
  1. 8 4
      daemon/src/JsonCommander.cpp
  2. 11 3
      daemon/test/JsonCommanderTest.cpp

+ 8 - 4
daemon/src/JsonCommander.cpp

@@ -85,7 +85,8 @@ JsonCommander::Response JsonCommander::executePut(const Json::Value &message) {
   response.json["command"] = "put";
   response.json["file"] = message["file"].asString();
 
-  if (!message["file"].isString() || !message["size"].isInt()) {
+  if (!message["file"].isString() || !message["size"].isInt() ||
+      !message["chunks"].isInt()) {
     // if request is incomplete close connection
     response.action = closeAndSend;
     response.json["accept"] = false;
@@ -95,6 +96,10 @@ JsonCommander::Response JsonCommander::executePut(const Json::Value &message) {
     response.action = send;
     response.json["accept"] = false;
     response.json["error"] = "upload already running";
+  } else if (message["chunks"].asInt() <= 0) {
+    response.action = send;
+    response.json["accept"] = false;
+    response.json["error"] = "there must be at least one chunk";
   } else if (fileManager.checkFilename(message["file"].asString())) {
     // accept request
     response.action = send;
@@ -103,7 +108,7 @@ JsonCommander::Response JsonCommander::executePut(const Json::Value &message) {
     if (opened) {
       response.json["accept"] = true;
       response.json["error"] = "";
-      this->putFileReceived = -1;
+      this->putFileReceived = message["chunks"].asInt();
     } else {
       response.json["accept"] = false;
       response.json["error"] = "file already exists";
@@ -150,8 +155,7 @@ JsonCommander::executePutdata(const Json::Value &message) {
   } else if (message["file"].asString().compare(
                  fileManager.getPutBaseFileName()) == 0) {
 
-    if (this->putFileReceived == -1 ||
-        --this->putFileReceived == message["remaining"].asInt()) {
+    if (--this->putFileReceived == message["remaining"].asInt()) {
       // accept request
       response.action = send;
       response.json["cancel"] = false;

+ 11 - 3
daemon/test/JsonCommanderTest.cpp

@@ -138,6 +138,7 @@ TEST(Put, Positive) {
   message["command"] = command;
   message["file"] = filename;
   message["size"] = 1337;
+  message["chunks"] = 1;
 
   ON_CALL(fileManager, openPutFile(testing::_))
       .WillByDefault(testing::Return(true));
@@ -162,6 +163,7 @@ TEST(Put, Negative) {
   message["command"] = command;
   message["file"] = filename;
   message["size"] = 1337;
+  message["chunks"] = 1;
 
   ON_CALL(fileManager, openPutFile(testing::_))
       .WillByDefault(testing::Return(false));
@@ -188,6 +190,8 @@ TEST(Putdata, Positive) {
   message["command"] = command;
   message["file"] = filename;
   message["size"] = 1337;
+  const int chunks = 3;
+  message["chunks"] = chunks;
 
   ON_CALL(fileManager, openPutFile(testing::_))
       .WillByDefault(testing::Return(true));
@@ -207,7 +211,7 @@ TEST(Putdata, Positive) {
   ON_CALL(fileManager, getPutBaseFileName())
       .WillByDefault(testing::Return(filename));
 
-  for (int remaining = 2; remaining >= 0; remaining--) {
+  for (int remaining = chunks - 1; remaining >= 0; remaining--) {
     message = Json::Value();
     message["command"] = command;
     message["file"] = filename;
@@ -238,6 +242,8 @@ TEST(Putdata, Cancel) {
   message["command"] = command;
   message["file"] = filename;
   message["size"] = 1337;
+  const int chunks = 3;
+  message["chunks"] = chunks;
 
   ON_CALL(fileManager, openPutFile(testing::_))
       .WillByDefault(testing::Return(true));
@@ -257,7 +263,7 @@ TEST(Putdata, Cancel) {
   ON_CALL(fileManager, getPutBaseFileName())
       .WillByDefault(testing::Return(filename));
 
-  int remaining = 2;
+  int remaining = chunks - 1;
   message = Json::Value();
   message["command"] = command;
   message["file"] = filename;
@@ -304,6 +310,8 @@ TEST(Putdata, WrongRemaining) {
   message["command"] = command;
   message["file"] = filename;
   message["size"] = 1337;
+  const int chunks = 3;
+  message["chunks"] = chunks;
 
   ON_CALL(fileManager, openPutFile(testing::_))
       .WillByDefault(testing::Return(true));
@@ -323,7 +331,7 @@ TEST(Putdata, WrongRemaining) {
   ON_CALL(fileManager, getPutBaseFileName())
       .WillByDefault(testing::Return(filename));
 
-  int remaining = 2;
+  int remaining = chunks - 1;
   message = Json::Value();
   message["command"] = command;
   message["file"] = filename;