Browse Source

adding put/putdata/get (todo: getdata), not completely working yet

Denys 5 years ago
parent
commit
a9ae3cdc76
4 changed files with 76 additions and 5 deletions
  1. 5 1
      cli/include/cmdman.h
  2. 2 0
      cli/include/fileman.h
  3. 63 4
      cli/src/cmdman.cpp
  4. 6 0
      cli/src/fileman.cpp

+ 5 - 1
cli/include/cmdman.h

@@ -63,15 +63,19 @@ private:
 	/* internal execute commands */
 	CmdRet cmdVersion(vector<string> args);
 	CmdRet cmdLogin(vector<string> args);
+	CmdRet cmdPutdata(vector<string> args);
+	CmdRet cmdGetData(vector<string> args);
 	
 	/* handle commands go here */
 	CmdRet handleStatus(Json::Value);
 	CmdRet handleClose(Json::Value);
 	CmdRet handlePut(Json::Value);
 	CmdRet handleGet(Json::Value);
+	CmdRet handlePutdata(Json::Value);
+//	CmdRet handleGetdata(Json::Value);
 	CmdRet handleList(Json::Value);
 	CmdRet handleVersion(Json::Value);
 	CmdRet handleLogin(Json::Value);
 };
 
-#endif
+#endif

+ 2 - 0
cli/include/fileman.h

@@ -14,6 +14,7 @@ private:
 	std::string getname, putname;
 	const unsigned int max_read_len = 8;
 	unsigned int putchunks;
+	unsigned int putchunksRemaining;
 public:
 	FileMan();
 	~FileMan();
@@ -34,6 +35,7 @@ public:
 
 	std::vector<char> readPut();
 	int getPutChunks();
+	int getPutChunksRemaining();
 };
 
 #endif

+ 63 - 4
cli/src/cmdman.cpp

@@ -22,6 +22,8 @@ CmdMan::CmdMan(FileMan &fm) : fileman(fileman) {
 	execmap["list"] = &CmdMan::cmdList;
 	execmap["version"] = &CmdMan::cmdVersion;
 	execmap["login"] = &CmdMan::cmdLogin;
+	execmap["putdata"] = &CmdMan::cmdPutdata;
+//	execmap["getdata"] = &CmdMan::cmdGetdata;
 	
 	/* initialize description map */
 	helpmap["help"] = descHelp;
@@ -36,6 +38,8 @@ CmdMan::CmdMan(FileMan &fm) : fileman(fileman) {
 	handlemap["close"] = &CmdMan::handleClose;
 	handlemap["put"] = &CmdMan::handlePut;
 	handlemap["get"] = &CmdMan::handleGet;
+	handlemap["putdata"] = &CmdMan::handlePutdata;
+//	handlemap["getdata"] = &CmdMan::handleGetdata;
 	handlemap["list"] = &CmdMan::handleList;
 	handlemap["version"] = &CmdMan::handleVersion;
 	handlemap["login"] = &CmdMan::handleLogin;
@@ -102,6 +106,21 @@ CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
 	return retval;
 }
 
+CmdMan::CmdRet CmdMan::cmdPutdata(vector<string> args) {
+	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
+	Json::Value root;
+	
+	root["command"] = "putdata";
+	root["file"] = fileman.getPutName();
+	root["remaining"] = fileman.getPutChunksRemaining();
+	root["cancel"] = false;
+	retval.type = send;
+	retval.msg = Json::writeString(wbuilder, root);
+		
+	return retval;
+}
+
 CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
 	CmdRet retval;
 	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
@@ -224,17 +243,57 @@ CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
 	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	
 	retval.type = send;
-	retval.msg = Json::writeString(wbuilder, root);
+	if(!root["accept"].asBool()) {
+		retval.type = error;
+		retval.msg = "File upload request failed: Server reports: " + root["error"].asString();
+	} else if(root["file"].asString() != fileman.getPutName()) {
+		retval.type = error;
+		retval.msg = "File upload request failed: Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getPutName();
+	} else {
+		retval.type = send;
+		retval.msg = "putdata";
+	}
 	
 	return retval;
 }
 
-CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
+CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
 	CmdRet retval;
 	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
+	// TODO
 	
-	retval.type = send;
-	retval.msg = Json::writeString(wbuilder, root);
+	// the passed number of recieved chunks should equal the number of sent chunks
+	unsigned int sentChunks = fileman.getPutChunks() - fileman.getPutChunksRemaining();
+	if(root["received"].asInt() != sentChunks) {
+		retval.type = error;
+		retval.msg = std::string("File upload failed: Server reports number of sent chunks as ") + std::to_string (root["received"].asInt()) + " but actual number is " + std::to_string (sentChunks);
+	} else if(!root["accept"].asBool()) {
+		retval.type = error;
+		retval.msg = "File upload failed: Server reports: " + root["error"].asString();
+	} else if(root["file"].asString() != fileman.getPutName()) {
+		retval.type = error;
+		retval.msg = "File upload request failed: Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getPutName();
+	} else {
+		// sent successfully
+		if(root["received"].asInt() == fileman.getPutChunks()) {
+			// everything sent
+			retval.type = notsend;
+			retval.msg = "succesfully uploaded file " + fileman.getPutName();
+			fileman.closePut();
+		} else {
+			retval.type = send;
+			retval.msg = "putdata";
+		}
+	}
+	return retval;
+}
+
+CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
+	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
+	// TODO
+	retval.type = error;
+	retval.msg = "Get handler not implemented yet";
 	
 	return retval;
 }

+ 6 - 0
cli/src/fileman.cpp

@@ -24,6 +24,7 @@ bool FileMan::openPut(const std::string &name) {
 	if(putfile.is_open()) {
 		size_t size = putfile.tellg();
 		putchunks = size / max_read_len + ((size % max_read_len) ? 1 : 0);
+		putchunksRemaining = putchunks;
 		putfile.seekg(std::ios::beg);
 		return true;
 	}
@@ -67,6 +68,7 @@ std::vector<char> FileMan::readPut() {
 	std::vector<char> data;
 	int read = putfile.readsome(buf, max_read_len);
 	data.assign(buf, buf+read);
+	putchunksRemaining--;
 	return data;
 }
 
@@ -81,3 +83,7 @@ std::string FileMan::getGetName() {
 int FileMan::getPutChunks() {
 	return putchunks;
 }
+
+int FileMan::getPutChunksRemaining() {
+	return putchunksRemaining;
+}