Browse Source

added CMD_LIST to commands and useriomanager and bugfixes

Denys 5 years ago
parent
commit
b98c551bd9
3 changed files with 77 additions and 7 deletions
  1. 2 1
      cli/include/commands.hpp
  2. 5 4
      cli/src/commands.cpp
  3. 70 2
      cli/src/useriomanager.cpp

+ 2 - 1
cli/include/commands.hpp

@@ -14,6 +14,7 @@ typedef enum {
   CMD_PUT,
   CMD_REMOVE,
   CMD_GET,
+  CMD_LIST,
   CMD_STATUS,
   CMD_SETUP,
   CMD_LOG,
@@ -33,4 +34,4 @@ const char *getCmdStringFromId(COMMANDID id);
 // TODO
 void printCmds(void);
 
-#endif
+#endif

+ 5 - 4
cli/src/commands.cpp

@@ -2,11 +2,12 @@
 #include <cstdio>
 
 CMD commands[]{
-    {CMD_HELP, "help", "show help"},
-    {CMD_STATUS, "status", "query status of IP"},
+    {CMD_HELP,       "help", "show help"},
+    {CMD_STATUS,     "status", "query status of IP"},
     {CMD_DISCONNECT, "disconnect", "disconnect from IP"},
-    {CMD_PUT	, "put", "upload file to IP and add to queue"},
-    {CMD_GET	, "get", "retrieve file from IP"},
+    {CMD_PUT	,    "put", "upload file to IP and add to queue"},
+    {CMD_GET	,    "get", "retrieve file from deamon"},
+    {CMD_LIST   ,    "list", "list files that are downloadable from deamon"}
 
     /* TODO
     {CMD_REMOVE	, "remove", "remove file from IP and queue (stops xfer

+ 70 - 2
cli/src/useriomanager.cpp

@@ -110,12 +110,21 @@ void UserIoManager::run() {
 	    }
 	    case CMD_PUT: {
 	    	
+	    	if(tokens.size() < 2) {
+			std::cerr << "no file specified" << std::endl;
+			continue;
+	   	}
+	    	
 	    	boost::asio::streambuf recvbuf;
 	    	Json::Value root, ackRecv;
 	    	
 	    	std::fstream fs;
 	    	fs.open(tokens[1].c_str(), std::fstream::in | std::fstream::binary);
 	    	
+	    	if(!fs.is_open()) {
+			std::cerr << "couldnt open file " << tokens[1] << std::endl;
+			break;
+	    	}
 	    	// determine file size
 	    	fs.seekg(0, fs.end);
 	    	int filelen = fs.tellg();
@@ -211,6 +220,10 @@ void UserIoManager::run() {
 	    }
 	    case CMD_GET: {
 	    	
+	    	if(tokens.size() < 2) {
+			std::cerr << "no file specified" << std::endl;
+			continue;
+	   	}
 	    	
 	    	boost::asio::streambuf recvbuf;
 	    	Json::Value root, recvAck;
@@ -233,8 +246,7 @@ void UserIoManager::run() {
 	    	}
 	    	if(!parseJson(&root, recvbuf)){
 	    		std::cerr << "received invalid JSON from server" << std::endl;
-	    		keep_reading = false;
-			continue;
+	    		continue;
 	    	}
 	    	
 	    	std::cout << root << std::endl;
@@ -305,6 +317,62 @@ void UserIoManager::run() {
 	    	break;
 	    	
 	    }
+	case CMD_LIST: {
+        	boost::asio::streambuf recvbuf;
+	        Json::Value root, recv;
+	 //       const char *recvjson;
+        	
+        	int chunks, rec = 0;
+        	root["command"] = "list";
+        	if(!sendJson(root)) {
+			std::cerr << "couldn't send json" << std::endl;
+            		continue;
+        	}
+		if(!receiveJson(recvbuf)) {
+			std::cerr << "couldn't receive json" << std::endl;
+            		continue;
+        	}
+		if(!parseJson(&recv, recvbuf)) {
+			std::cerr << "couldn't parse json" << std::endl;
+            		continue;
+        	}
+
+		if(recv["accept"].asBool()) {
+			// ACK?
+			root["command"] = "list";
+			root["cancel"] = false;
+			chunks = std::numeric_limits<int>::max();
+			while(chunks) {
+				if(!receiveJson(recvbuf)) {
+					std::cerr << "couldn't receive json" << std::endl;
+            				break;
+        			}
+				if(!parseJson(&recv, recvbuf)) {
+					std::cerr << "couldn't parse json" << std::endl;
+            				break;
+        			}
+				// dump received data
+				if(recv["names"].isArray()) {
+					for(Json::Value name : recv["names"]){
+						std::cout << name.asString();
+					}
+				} else {
+					std::cerr << "should have recieved an array of files, but did not receive an array" << std::endl;				
+				}
+				chunks = recv["remaining"].asInt();
+				root["received"] = rec;
+				if(!sendJson(root)) {
+				// couldnt ACK
+				break;
+				}
+				rec++;
+			}
+		} else {
+			std::cerr << "server refused listing" << std::endl;		
+		}
+		break;
+		
+	    }
 	    case CMD_DISCONNECT: {
 	      std::printf("disconnecting\n");
 	      keep_reading = false;