瀏覽代碼

implemented and fixed put, implement correct semantics prepare get

Missingmew 5 年之前
父節點
當前提交
3107f89684
共有 4 個文件被更改,包括 94 次插入18 次删除
  1. 2 2
      cli/include/cmdman.h
  2. 11 2
      cli/include/fileman.h
  3. 56 10
      cli/src/cmdman.cpp
  4. 25 4
      cli/src/fileman.cpp

+ 2 - 2
cli/include/cmdman.h

@@ -64,7 +64,7 @@ private:
 	CmdRet cmdVersion(vector<string> args);
 	CmdRet cmdLogin(vector<string> args);
 	CmdRet cmdPutdata(vector<string> args);
-	CmdRet cmdGetData(vector<string> args);
+	CmdRet cmdGetdata(vector<string> args);
 	
 	/* handle commands go here */
 	CmdRet handleStatus(Json::Value);
@@ -72,7 +72,7 @@ private:
 	CmdRet handlePut(Json::Value);
 	CmdRet handleGet(Json::Value);
 	CmdRet handlePutdata(Json::Value);
-//	CmdRet handleGetdata(Json::Value);
+	CmdRet handleGetdata(Json::Value);
 	CmdRet handleList(Json::Value);
 	CmdRet handleVersion(Json::Value);
 	CmdRet handleLogin(Json::Value);

+ 11 - 2
cli/include/fileman.h

@@ -14,7 +14,10 @@ private:
 	std::string getname, putname;
 	const unsigned int max_read_len = 8;
 	unsigned int putchunks;
+	unsigned int putsize;
 	unsigned int putchunksRemaining;
+	unsigned int getchunks;
+	unsigned int getchunksReceived;
 public:
 	FileMan();
 	~FileMan();
@@ -32,10 +35,16 @@ public:
 	void cancelGet();
 
 	void writeGet(std::vector<char> data);
+	void writeBase64(std::string data);
+	unsigned int getGetChunks();
+	unsigned int getGetReceivedChunks();
 
 	std::vector<char> readPut();
-	int getPutChunks();
-	int getPutChunksRemaining();
+	std::string readBase64();
+	unsigned int getPutChunks();
+	unsigned int getPutChunksRemaining();
+	unsigned int getPutSize();
+	
 };
 
 #endif

+ 56 - 10
cli/src/cmdman.cpp

@@ -23,7 +23,7 @@ CmdMan::CmdMan(FileMan &fm) : fileman(fm) {
 	execmap["version"] = &CmdMan::cmdVersion;
 	execmap["login"] = &CmdMan::cmdLogin;
 	execmap["putdata"] = &CmdMan::cmdPutdata;
-//	execmap["getdata"] = &CmdMan::cmdGetdata;
+	execmap["getdata"] = &CmdMan::cmdGetdata;
 	
 	/* initialize description map */
 	helpmap["help"] = descHelp;
@@ -39,7 +39,7 @@ CmdMan::CmdMan(FileMan &fm) : fileman(fm) {
 	handlemap["put"] = &CmdMan::handlePut;
 	handlemap["get"] = &CmdMan::handleGet;
 	handlemap["putdata"] = &CmdMan::handlePutdata;
-//	handlemap["getdata"] = &CmdMan::handleGetdata;
+	handlemap["getdata"] = &CmdMan::handleGetdata;
 	handlemap["list"] = &CmdMan::handleList;
 	handlemap["version"] = &CmdMan::handleVersion;
 	handlemap["login"] = &CmdMan::handleLogin;
@@ -94,7 +94,8 @@ CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
 	if(opened) {
 		root["command"] = "put";
 		root["file"] = fileman.getPutName();
-		root["size"] = fileman.getPutChunks();
+		root["size"] = fileman.getPutSize();
+		root["chunks"] = fileman.getPutChunks();
 		retval.type = send;
 		retval.msg = Json::writeString(wbuilder, root);
 	}
@@ -115,6 +116,7 @@ CmdMan::CmdRet CmdMan::cmdPutdata(vector<string> args) {
 	root["file"] = fileman.getPutName();
 	root["remaining"] = fileman.getPutChunksRemaining();
 	root["cancel"] = false;
+	root["data"] = fileman.readBase64();
 	retval.type = send;
 	retval.msg = Json::writeString(wbuilder, root);
 		
@@ -141,6 +143,21 @@ CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
 	return retval;
 }
 
+CmdMan::CmdRet CmdMan::cmdGetdata(vector<string> args) {
+	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
+	Json::Value root;
+	
+	root["command"] = "getdata";
+	root["file"] = fileman.getGetName();
+	root["chunk"] = fileman.getGetReceivedChunks();
+	root["cancel"] = false;
+	retval.type = send;
+	retval.msg = Json::writeString(wbuilder, root);
+		
+	return retval;
+}
+
 CmdMan::CmdRet CmdMan::cmdList(vector<string> args) {
 	CmdRet retval;
 	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
@@ -260,22 +277,21 @@ CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
 CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
 	CmdRet retval;
 	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
-	// TODO
 	
 	// 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) {
+	unsigned int sentChunks = fileman.getPutChunksRemaining()+1;
+	if(root["received"].asUInt() != 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.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["cancel"].asBool()) {
 		retval.type = error;
-		retval.msg = "File upload failed: Server reports: " + root["error"].asString();
+		retval.msg = "File upload cancelles: 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()) {
+		if(!root["received"].asUInt()) {
 			// everything sent
 			retval.type = notsend;
 			retval.msg = "succesfully uploaded file " + fileman.getPutName();
@@ -288,6 +304,36 @@ CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
 	return retval;
 }
 
+CmdMan::CmdRet CmdMan::handleGetdata(Json::Value root) {
+	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
+	
+	// the passed number of recieved chunks should equal the number of sent chunks
+	if(root["received"].asUInt() != fileman.getGetReceivedChunks()) {
+		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(fileman.getGetReceivedChunks());
+	} else if(root["cancel"].asBool()) {
+		retval.type = error;
+		retval.msg = "File download cancelled: 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 {
+		fileman.writeBase64(root["data"].asString());
+		// loaded successfully
+		if(fileman.getGetChunks() == fileman.getGetReceivedChunks()) {
+			// everything sent
+			retval.type = notsend;
+			retval.msg = "succesfully downloaded file " + fileman.getGetName();
+			fileman.closePut();
+		} else {
+			retval.type = send;
+			retval.msg = "getdata";
+		}
+	}
+	return retval;
+}
+
 CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
 	CmdRet retval;
 	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;

+ 25 - 4
cli/src/fileman.cpp

@@ -1,4 +1,5 @@
 #include "../include/fileman.h"
+#include "../include/base64.h"
 
 FileMan::FileMan() {
 	
@@ -23,8 +24,9 @@ bool FileMan::openPut(const std::string &name) {
 	putfile.open(name, std::ios::ate | std::ios::binary);
 	if(putfile.is_open()) {
 		size_t size = putfile.tellg();
+		putsize = size;
 		putchunks = size / max_read_len + ((size % max_read_len) ? 1 : 0);
-		putchunksRemaining = putchunks;
+		putchunksRemaining = putchunks-1;
 		putfile.seekg(std::ios::beg);
 		return true;
 	}
@@ -57,11 +59,22 @@ void FileMan::cancelGet() {
 	}
 }
 
-
 void FileMan::writeGet(const std::vector<char> data) {
 	getfile.write(data.data(), data.size());
+	getchunks++;
 }
 
+void FileMan::writeBase64(std::string data) {
+	writeGet(base64::decodeVector(data));
+}
+
+unsigned int FileMan::getGetChunks() {
+	return getchunks;
+}
+
+unsigned int FileMan::getGetReceivedChunks() {
+	return getchunksReceived;
+}
 
 std::vector<char> FileMan::readPut() {
 	char buf[max_read_len];
@@ -72,6 +85,10 @@ std::vector<char> FileMan::readPut() {
 	return data;
 }
 
+std::string FileMan::readBase64() {
+	return base64::encodeVector(readPut());
+}
+
 std::string FileMan::getPutName() {
 	return putname;
 }
@@ -80,10 +97,14 @@ std::string FileMan::getGetName() {
 	return getname;
 }
 
-int FileMan::getPutChunks() {
+unsigned int FileMan::getPutChunks() {
 	return putchunks;
 }
 
-int FileMan::getPutChunksRemaining() {
+unsigned int FileMan::getPutChunksRemaining() {
 	return putchunksRemaining;
 }
+
+unsigned int FileMan::getPutSize() {
+	return putsize;
+}