Browse Source

US53: CLI adjust output of status command to show username and IP/port

Sander, Paul 4 years ago
parent
commit
856ce375bf
6 changed files with 123 additions and 39 deletions
  1. 1 1
      cli/README.md
  2. 7 1
      cli/include/cmdman.h
  3. 9 9
      cli/src/batchioman.cpp
  4. 26 17
      cli/src/cmdman.cpp
  5. 9 9
      cli/src/userioman.cpp
  6. 71 2
      cli/test/cmdman_test.cpp

+ 1 - 1
cli/README.md

@@ -47,7 +47,7 @@ The following commands can be sent when the user is connected to a server.
 
 
 `status`: <br/>
-Shows wether a file transfer between server and client is running.
+Shows username, ip and port and wether a file transfer between server and client is running.
 
 `extendedstatus`: <br/>
 Shows detailed information about ongoing transfers at the server.

+ 7 - 1
cli/include/cmdman.h

@@ -124,9 +124,15 @@ private:
 	 * Vectors containing command strings that should either be usable in any
 	 * state or after connecting but not logging in
 	 */
-	vector<string> cmdAllowAlways = {"help", "keyfile", "closekey", "exit"};
+	vector<string> cmdAllowAlways = {"help", "keyfile", "closekey", "exit", "status"};
 	vector<string> cmdAllowAfterConnect = {"login", "signup", "disconnect"};
 
+	/**
+	 * Fields used for status output.
+	 */
+	string username, ip;
+	unsigned int port;
+
 	/**
 	 * Help strings and method prototypes for commands to be used by a user
 	 */

+ 9 - 9
cli/src/batchioman.cpp

@@ -299,7 +299,7 @@ std::string BatchIoMan::printError(Json::Value root) { return std::string("Error
 
 std::string BatchIoMan::printConnect(Json::Value root) {
 	if (!root["accept"].asBool()) {
-		return std::string("Couldnt connect to ") + root["address"].asString() + ":" + std::to_string(root["port"].asUInt()) + "\n" +
+		return std::string("Couldnt connect to ") + root["address"].asString() + ":" + std::to_string(root["port"].asUInt()) + ".\n" +
 		       "Reason: " + root["error"].asString();
 	}
 	return "";
@@ -312,7 +312,7 @@ std::string BatchIoMan::printHelp(Json::Value root) {
 	return ret;
 }
 
-std::string BatchIoMan::printStatus(Json::Value root) { return std::string("Server reports status: ") + root["response"].asString(); }
+std::string BatchIoMan::printStatus(Json::Value root) { return root["response"].asString(); }
 
 std::string BatchIoMan::printExtendedstatus(Json::Value root) {
 	if (!root["accept"].asBool()) {
@@ -393,7 +393,7 @@ std::string BatchIoMan::printPut(Json::Value root) {
 			return std::string("Upload request failed: ") + root["error"].asString();
 		}
 	} else
-		return std::string("Begin uploading file ") + root["file"].asString();
+		return std::string("Begin uploading file ") + root["file"].asString() + ".";
 }
 
 std::string BatchIoMan::printGet(Json::Value root) {
@@ -466,7 +466,7 @@ std::string BatchIoMan::printExtendedlist(Json::Value root) {
 std::string BatchIoMan::printVersion(Json::Value root) {
 	if (!root["accept"].asBool()) {
 		return std::string("Version check failed. Server reports version ") + root["serverversion"].asString() + " but client is " +
-		       root["clientversion"].asString();
+		       root["clientversion"].asString() + ".";
 	} else
 		return "Version check ok.";
 }
@@ -507,16 +507,16 @@ std::string BatchIoMan::printHead(Json::Value root) {
 
 std::string BatchIoMan::printDeletefile(Json::Value root) {
 	if (!root["accept"].asBool())
-		return std::string("Deletion of file ") + root["file"].asString() + " failed. " + root["error"].asString();
+		return std::string("Deletion of file ") + root["file"].asString() + " failed: " + root["error"].asString();
 	else
-		return std::string("File ") + root["file"].asString() + " deleted succesfully";
+		return std::string("File ") + root["file"].asString() + " deleted succesfully.";
 }
 
 std::string BatchIoMan::printKeyfile(Json::Value root) {
 	if (!root["accept"].asBool())
 		return std::string("Couldnt select keyfile ") + root["file"].asString() + ": " + root["error"].asString();
 	else
-		return std::string("Using keyfile ") + root["file"].asString();
+		return std::string("Using keyfile ") + root["file"].asString() + ".";
 }
 
 std::string BatchIoMan::printClosekey(Json::Value root) {
@@ -528,14 +528,14 @@ std::string BatchIoMan::printClosekey(Json::Value root) {
 
 std::string BatchIoMan::printQueue(Json::Value root) {
 	if (!root["accept"].asBool())
-		return std::string("Queueing of file ") + root["file"].asString() + " failed. " + root["error"].asString();
+		return std::string("Queueing of file ") + root["file"].asString() + " failed: " + root["error"].asString();
 	else
 		return std::string("File ") + root["file"].asString() + " queued succesfully.";
 }
 
 std::string BatchIoMan::printDequeue(Json::Value root) {
 	if (!root["accept"].asBool())
-		return std::string("Dequeueing of file ") + root["file"].asString() + " failed. " + root["error"].asString();
+		return std::string("Dequeueing of file ") + root["file"].asString() + " failed: " + root["error"].asString();
 	else
 		return std::string("File ") + root["file"].asString() + " dequeued succesfully.";
 }

+ 26 - 17
cli/src/cmdman.cpp

@@ -121,7 +121,15 @@ CmdMan::CmdRet CmdMan::cmdStatus(vector<string> args) {
 	DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 	Json::Value root;
 	root["command"] = "status";
-	retval.type = send;
+	if (currentState == normal) {
+		retval.type = send;
+	} else if (currentState == connectionpossible || currentState == disconnecttoexit || currentState == disconnecttoexitearly) {
+		retval.type = print;
+		root["response"] = "You are not connected.";
+	} else {
+		retval.type = print;
+		root["response"] = "Connected to " + ip + ":" + std::to_string(port) + ". Not logged in.";
+	}
 	retval.msg = root;
 
 	return retval;
@@ -406,16 +414,19 @@ CmdMan::CmdRet CmdMan::cmdConnect(vector<string> args) {
 		retval.type = error;
 		root["command"] = "error"; // analogous to execute() method when command unavailable
 		root["error"] = "Connecting not possible, you are already connected to a server.";
-	} else if (args.size() < 2) {
-		retval.type = connect;
-		root["command"] = "connect";
-		root["address"] = args[0];
-		root["port"] = 1234;
 	} else {
+		// set internal ip and port fields first
+		ip = args[0];
+		if (args.size() < 2) {
+			port = 1234;
+		} else {
+			port = (unsigned int)stoul(args[1]);
+		}
+		// construct json
 		retval.type = connect;
 		root["command"] = "connect";
-		root["address"] = args[0];
-		root["port"] = (unsigned int)stoul(args[1]);
+		root["address"] = ip;
+		root["port"] = port;
 	}
 	retval.msg = root;
 
@@ -485,7 +496,7 @@ CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
 		if (cmd.compare("version") && cmd.compare("connect")) {
 			retval.type = error;
 			root["command"] = "error";
-			root["error"] = string("Not connected. Connect using \"connect ip [port]\".");
+			root["error"] = "Not connected. Please connect.";
 			retval.msg = root;
 			cmdmutex.unlock();
 			return retval;
@@ -594,7 +605,7 @@ CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
 	} else {
 		if (currentState == loginpossible) {
 			currentState = dologin;
-			root["user"] = args[0];
+			root["user"] = username = args[0];
 			root["pass"] = args[1];
 			root["login"] = true;
 			root["cancel"] = false;
@@ -624,7 +635,7 @@ CmdMan::CmdRet CmdMan::cmdSignup(vector<string> args) {
 	} else {
 		if (currentState == loginpossible) {
 			currentState = dosignup;
-			root["user"] = args[0];
+			root["user"] = username = args[0];
 			root["pass"] = args[1];
 			root["login"] = false;
 			root["cancel"] = false;
@@ -741,9 +752,12 @@ CmdMan::CmdRet CmdMan::handle(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handleStatus(Json::Value root) {
 	CmdRet retval;
+	Json::Value output;
 	DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 	retval.type = print;
-	retval.msg = root;
+	output["command"] = "status";
+	output["response"] = username + "@" + ip + ":" + std::to_string(port) + " - Status: " + root["response"].asString();
+	retval.msg = output;
 
 	return retval;
 }
@@ -814,7 +828,6 @@ CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
 	DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 	output["command"] = "putdata";
 	output["file"] = fileman.getPutName();
-	output["speed"] = 0.0f; // TODO
 	output["cancel"] = true;
 
 	if (root["cancel"].asBool()) {
@@ -843,8 +856,6 @@ CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
 		if (!root["received"].asInt()) {
 			// everything sent
 			retval.type = print;
-			// TODO
-			//~ retval.msg = "succesfully uploaded file " + fileman.getPutName();
 			fileman.closePut();
 		} else {
 			retval.type = print | send;
@@ -892,7 +903,6 @@ CmdMan::CmdRet CmdMan::handleGetdata(Json::Value root) {
 	DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 	output["command"] = "getdata";
 	output["file"] = fileman.getGetName();
-	output["speed"] = 0.0f; // TODO
 	output["cancel"] = true;
 
 	if (root["cancel"].asBool()) {
@@ -919,7 +929,6 @@ CmdMan::CmdRet CmdMan::handleGetdata(Json::Value root) {
 		if (!root["remaining"].asInt()) {
 			// everything received
 			retval.type = print;
-			//~ retval.msg = "succesfully downloaded file " + fileman.getGetName();
 			fileman.closeGet();
 		} else {
 			retval.type = print | send;

+ 9 - 9
cli/src/userioman.cpp

@@ -120,7 +120,7 @@ void UserIoMan::printError(Json::Value root) { std::cout << "Error: " << root["e
 
 void UserIoMan::printConnect(Json::Value root) {
 	if (!root["accept"].asBool()) {
-		std::cout << "Couldnt connect to " << root["address"].asString() << ":" << root["port"].asUInt() << std::endl
+		std::cout << "Couldnt connect to " << root["address"].asString() << ":" << root["port"].asUInt() << "." << std::endl
 		          << "Reason: " << root["error"].asString() << std::endl;
 	}
 }
@@ -131,7 +131,7 @@ void UserIoMan::printHelp(Json::Value root) {
 		std::cout << i.asString() << std::endl;
 }
 
-void UserIoMan::printStatus(Json::Value root) { std::cout << "Server reports status: " << root["response"].asString() << std::endl; }
+void UserIoMan::printStatus(Json::Value root) { std::cout << root["response"].asString() << std::endl; }
 
 void UserIoMan::printExtendedstatus(Json::Value root) {
 
@@ -221,7 +221,7 @@ void UserIoMan::printGet(Json::Value root) {
 			std::cout << "Download request failed: " << root["error"].asString() << std::endl;
 		}
 	} else
-		std::cout << "Begin downloading file " << root["file"].asString() << std::endl;
+		std::cout << "Begin downloading file " << root["file"].asString() << "." << std::endl;
 }
 
 void UserIoMan::printList(Json::Value root) {
@@ -279,7 +279,7 @@ void UserIoMan::printExtendedlist(Json::Value root) {
 void UserIoMan::printVersion(Json::Value root) {
 	if (!root["accept"].asBool()) {
 		std::cout << "Version check failed. Server reports version " << root["serverversion"].asString() << " but client is "
-		          << root["clientversion"].asString() << std::endl;
+		          << root["clientversion"].asString() << "." << std::endl;
 	} else
 		std::cout << "Version check ok." << std::endl;
 }
@@ -320,16 +320,16 @@ void UserIoMan::printHead(Json::Value root) {
 
 void UserIoMan::printDeletefile(Json::Value root) {
 	if (!root["accept"].asBool())
-		std::cout << "Deletion of file " << root["file"] << " failed. " << root["error"].asString() << std::endl;
+		std::cout << "Deletion of file " << root["file"] << " failed: " << root["error"].asString() << std::endl;
 	else
-		std::cout << "File " << root["file"] << " deleted succesfully" << std::endl;
+		std::cout << "File " << root["file"] << " deleted succesfully." << std::endl;
 }
 
 void UserIoMan::printKeyfile(Json::Value root) {
 	if (!root["accept"].asBool())
 		std::cout << "Couldnt select keyfile " << root["file"].asString() << ": " << root["error"].asString() << std::endl;
 	else
-		std::cout << "Using keyfile " << root["file"].asString() << std::endl;
+		std::cout << "Using keyfile " << root["file"].asString() << "." << std::endl;
 }
 
 void UserIoMan::printClosekey(Json::Value root) {
@@ -341,14 +341,14 @@ void UserIoMan::printClosekey(Json::Value root) {
 
 void UserIoMan::printQueue(Json::Value root) {
 	if (!root["accept"].asBool())
-		std::cout << "Queueing of file " << root["file"] << " failed. " << root["error"].asString() << std::endl;
+		std::cout << "Queueing of file " << root["file"] << " failed: " << root["error"].asString() << std::endl;
 	else
 		std::cout << "File " << root["file"] << " queued succesfully." << std::endl;
 }
 
 void UserIoMan::printDequeue(Json::Value root) {
 	if (!root["accept"].asBool())
-		std::cout << "Dequeueing of file " << root["file"] << " failed. " << root["error"].asString() << std::endl;
+		std::cout << "Dequeueing of file " << root["file"] << " failed: " << root["error"].asString() << std::endl;
 	else
 		std::cout << "File " << root["file"] << " dequeued succesfully." << std::endl;
 }

+ 71 - 2
cli/test/cmdman_test.cpp

@@ -2553,10 +2553,9 @@ TEST(testDeletefile, Negative) {
 /* =====================================
  * test for status
  */
-TEST(testStatus, Test) {
+TEST(testStatus, LoggedIn) {
 	FileManMock fm;
 	CmdManForTest cm(fm, dummyDebugPrint);
-	cm.initLoggedIn();
 
 	std::string cmd;
 	std::vector<std::string> args;
@@ -2564,6 +2563,12 @@ TEST(testStatus, Test) {
 	CmdMan::CmdRet retvalCmd;
 	CmdMan::CmdRet retvalHdl;
 
+	// set ip, port and user name fields of the cmdman
+	cm.execute("connect", {"1.2.3.4", "1337"});
+	cm.initVersionChecked();
+	cm.execute("login", {"usernem", "paswod"});
+	cm.initLoggedIn();
+
 	// prepare cmd/args/root
 	cmd = "status";
 	args = {};
@@ -2581,10 +2586,74 @@ TEST(testStatus, Test) {
 	EXPECT_EQ(retvalHdl.type, CmdMan::print);
 	EXPECT_EQ(retvalHdl.msg["command"].asString(), "status");
 	EXPECT_THAT(retvalHdl.msg["response"].asString(), testing::HasSubstr("fancy response"));
+	EXPECT_THAT(retvalHdl.msg["response"].asString(), testing::HasSubstr("1.2.3.4"));
+	EXPECT_THAT(retvalHdl.msg["response"].asString(), testing::HasSubstr("1337"));
+	EXPECT_THAT(retvalHdl.msg["response"].asString(), testing::HasSubstr("usernem"));
 
 	EXPECT_TRUE(cm.isLoggedIn());
 }
 
+TEST(testStatus, NotConnected) {
+	FileManMock fm;
+	CmdManForTest cm(fm, dummyDebugPrint);
+
+	std::string cmd;
+	std::vector<std::string> args;
+	CmdMan::CmdRet retvalCmd;
+
+	// set ip and port fields of the cmdman
+	cm.execute("connect", {"1.2.3.4"});
+	cm.initNotConnected();
+
+	// prepare cmd/args
+	cmd = "status";
+	args = {};
+
+	// stick into cmdman
+	retvalCmd = cm.execute(cmd, args);
+
+	// check things
+	EXPECT_EQ(retvalCmd.type, CmdMan::print);
+	EXPECT_EQ(retvalCmd.msg["command"].asString(), "status");
+	EXPECT_NE(retvalCmd.msg["response"].asString(), "");
+	EXPECT_THAT(retvalCmd.msg["response"].asString(), testing::Not(testing::HasSubstr("1.2.3.4")));
+
+	EXPECT_TRUE(cm.isNotConnected());
+}
+
+TEST(testStatus, NotLoggedIn) {
+	FileManMock fm;
+	CmdManForTest cm(fm, dummyDebugPrint);
+
+	std::string cmd;
+	std::vector<std::string> args;
+	CmdMan::CmdRet retvalCmd;
+	CmdMan::CmdRet retvalHdl;
+
+	// set ip, port and user name fields of the cmdman
+	cm.initConnected();
+	cm.execute("login", {"usernem", "paswod"});
+	cm.initNotConnected();
+	cm.execute("connect", {"1.2.3.4", "1337"});
+	cm.initVersionChecked(); // not logged in
+
+	// prepare cmd/args
+	cmd = "status";
+	args = {};
+
+	// stick into cmdman
+	retvalCmd = cm.execute(cmd, args);
+
+	// check things
+	EXPECT_EQ(retvalCmd.type, CmdMan::print);
+	EXPECT_EQ(retvalCmd.msg["command"].asString(), "status");
+	EXPECT_THAT(retvalCmd.msg["response"].asString(), testing::HasSubstr("1.2.3.4"));
+	EXPECT_THAT(retvalCmd.msg["response"].asString(), testing::HasSubstr("1337"));
+	EXPECT_THAT(retvalCmd.msg["response"].asString(), testing::Not(testing::HasSubstr("usernem")));
+
+	EXPECT_TRUE(cm.isVersionChecked());
+}
+
 /* =====================================
  * test for extendedstatus
  */