Bladeren bron

Resolve "US23: Connecting to Servers with more functionality"

Serdyukov, Denys 4 jaren geleden
bovenliggende
commit
603c14433b

+ 6 - 4
Client-Server Protocol.md

@@ -1,6 +1,6 @@
 # Client-Server Protocol
 
-Protocol version: <b>"0.2"</b>
+Protocol version: <b>"0.3"</b>
 
 Every json message must be minimized and be followed by a newline. This rule makes receiving and parsing the messages easier because you can read until a newline comes and you will parse every json message seperately because you won't read multiple json messages from the buffer at a time.
 
@@ -14,15 +14,17 @@ You can close the connection by sending a cancel message.
 Client:
 ```
 {
-	"version": string
+	"major": int,
+	"minor": int
 }
 ```
-
+(For example, if the version number is 42.1, then major is 42, minor is 1.)
 
 Server:
 ```
 {
-	"version": string,
+	"major": int,
+	"minor": int,
 	"accept": bool
 }
 ```

+ 3 - 2
cli/include/global.h

@@ -3,6 +3,7 @@
 
 #include <string>
 
-const std::string protocolVersion = "0.2";
+const int protocolMajorVersion = 0;
+const int protocolMinorVersion = 3;
 
-#endif
+#endif

+ 4 - 3
cli/src/cmdman.cpp

@@ -457,7 +457,8 @@ CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
 	CmdRet retval;
 	DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 	Json::Value root;
-	root["version"] = protocolVersion;
+	root["major"] = protocolMajorVersion;
+	root["minor"] = protocolMinorVersion;
 	retval.type = send;
 	retval.msg = root;
 
@@ -722,8 +723,8 @@ CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
 	DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 
 	output["command"] = "version";
-	output["serverversion"] = root["version"].asString();
-	output["clientversion"] = protocolVersion;
+	output["serverversion"] = root["major"].asString() + "." + root["minor"].asString();
+	output["clientversion"] = std::to_string(protocolMajorVersion) + "." + std::to_string(protocolMinorVersion);
 
 	if (!root["accept"].asBool()) {
 		retval.type = error;

+ 1 - 1
cli/src/ioman.cpp

@@ -221,7 +221,7 @@ bool IoMan::init() {
 
 	localmutex.lock();
 	printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), debug);
-	localinput.push_back("version " + protocolVersion);
+	localinput.push_back("version");
 	localmutex.unlock();
 	printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
 	localcv.notify_all();

+ 3 - 2
daemon/include/JsonCommander.h

@@ -56,12 +56,13 @@ public:
 	 */
 	Response checkLogin(const Json::Value &message);
 
-private:
 	/**
 	 * protocol version of the client server protocol
 	 */
-	const std::string protocolVersion = "0.2";
+	const int protocolMajorVersion = 0;
+	const int protocolMinorVersion = 3;
 
+private:
 	/**
 	 * Contains the name of the user which uses the current connection.
 	 * Is set at checkLogin. Used for deleteUser.

+ 6 - 3
daemon/src/JsonCommander.cpp

@@ -422,10 +422,13 @@ JsonCommander::Response JsonCommander::executeDeleteMe(const Json::Value &messag
 
 JsonCommander::Response JsonCommander::testVersion(const Json::Value &message) {
 	JsonCommander::Response response;
-	response.json["version"] = this->protocolVersion;
+	response.json["major"] = this->protocolMajorVersion;
+	response.json["minor"] = this->protocolMinorVersion;
 
-	// check version string is the same
-	if (message["version"].asString().compare(this->protocolVersion) == 0) {
+	if (!message["major"].isInt() || !message["minor"].isInt()) {
+		response.action = closeAndSend;
+		response.json["accept"] = false;
+	} else if (message["major"].asInt() == this->protocolMajorVersion && message["minor"].asInt() <= this->protocolMinorVersion) {
 		response.action = send;
 		response.json["accept"] = true;
 	} else {

+ 61 - 11
daemon/test/JsonCommanderTest.cpp

@@ -6,34 +6,84 @@
 
 namespace {
 /* Version tests */
+TEST(testVersion, PositiveAllEqual) {
+	FileManagerMock fileManager;
+
+	JsonCommander jsonCommander(fileManager);
+	Json::Value message;
+	message["major"] = 0;
+	message["minor"] = 1;
+
+	JsonCommander::Response response = jsonCommander.testVersion(message);
+
+	EXPECT_TRUE(response.action == JsonCommander::Action::send);
+	EXPECT_TRUE(response.json["accept"].asBool());
+	EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
+	EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
+}
+
 TEST(testVersion, Positive) {
 	FileManagerMock fileManager;
 
 	JsonCommander jsonCommander(fileManager);
+	Json::Value message;
+	message["major"] = jsonCommander.protocolMajorVersion;
+	message["minor"] = jsonCommander.protocolMinorVersion - 1;
 
-	const std::string versionString = "0.2";
-	Json::Value version;
-	version["version"] = versionString;
+	JsonCommander::Response response = jsonCommander.testVersion(message);
 
-	JsonCommander::Response response = jsonCommander.testVersion(version);
 	EXPECT_TRUE(response.action == JsonCommander::Action::send);
 	EXPECT_TRUE(response.json["accept"].asBool());
-	EXPECT_EQ(response.json["version"].asString(), versionString);
+	EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
+	EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
+}
+
+TEST(testVersion, InvalidRequest) {
+	FileManagerMock fileManager;
+
+	JsonCommander jsonCommander(fileManager);
+	Json::Value message;
+	message["major"] = "def";
+	message["minor"] = "abc";
+
+	JsonCommander::Response response = jsonCommander.testVersion(message);
+
+	EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
+	EXPECT_FALSE(response.json["accept"].asBool());
+	EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
+	EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
+}
+
+TEST(testVersion, NotEqualMajorNumber) {
+	FileManagerMock fileManager;
+
+	JsonCommander jsonCommander(fileManager);
+	Json::Value message;
+	message["major"] = jsonCommander.protocolMajorVersion + 1;
+	message["minor"] = jsonCommander.protocolMinorVersion;
+
+	JsonCommander::Response response = jsonCommander.testVersion(message);
+
+	EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
+	EXPECT_FALSE(response.json["accept"].asBool());
+	EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
+	EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
 }
 
-TEST(testVersion, Negative) {
+TEST(testVersion, BiggerMinorNumber) {
 	FileManagerMock fileManager;
 
 	JsonCommander jsonCommander(fileManager);
+	Json::Value message;
+	message["major"] = jsonCommander.protocolMajorVersion;
+	message["minor"] = jsonCommander.protocolMinorVersion + 1;
 
-	const std::string versionString = "0.1";
-	Json::Value version;
-	version["version"] = versionString;
+	JsonCommander::Response response = jsonCommander.testVersion(message);
 
-	JsonCommander::Response response = jsonCommander.testVersion(version);
 	EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
 	EXPECT_FALSE(response.json["accept"].asBool());
-	EXPECT_FALSE(response.json["version"].asString().compare(versionString) == 0);
+	EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
+	EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
 }
 
 /* Status tests */