Missingmew 5 years ago
parent
commit
6149d216eb
4 changed files with 132 additions and 21 deletions
  1. 3 1
      cli/include/cmdman.h
  2. 27 9
      cli/include/fileman.h
  3. 56 8
      cli/src/cmdman.cpp
  4. 46 3
      cli/src/fileman.cpp

+ 3 - 1
cli/include/cmdman.h

@@ -65,15 +65,17 @@ private:
 	CmdRet cmdLogin(vector<string> args);
 	CmdRet cmdPutdata(vector<string> args);
 	CmdRet cmdGetdata(vector<string> args);
+	CmdRet cmdListdata(vector<string> args);
 	
 	/* handle commands go here */
 	CmdRet handleStatus(Json::Value);
 	CmdRet handleClose(Json::Value);
 	CmdRet handlePut(Json::Value);
 	CmdRet handleGet(Json::Value);
+	CmdRet handleList(Json::Value);
 	CmdRet handlePutdata(Json::Value);
 	CmdRet handleGetdata(Json::Value);
-	CmdRet handleList(Json::Value);
+	CmdRet handleListdata(Json::Value);
 	CmdRet handleVersion(Json::Value);
 	CmdRet handleLogin(Json::Value);
 };

+ 27 - 9
cli/include/fileman.h

@@ -11,42 +11,60 @@ class FileMan {
 private:
 	std::ifstream putfile;
 	std::ofstream getfile;
+	std::vector<std::string> listdata;
 	std::string getname, putname;
 	const unsigned int max_read_len = 8;
-	int putchunks;
 	int putsize;
+	int putchunks;
 	int putchunksRemaining;
 	int getchunks;
 	int getchunksRemaining;
+	int listchunks;
+	int listchunksRemaining;
+
+	bool islisting;
 public:
 	FileMan();
 	~FileMan();
 	
 	bool isGetting();
 	bool isPutting();
+	bool isListing();
 
 	bool openPut(const std::string &name);
 	bool openGet(const std::string &name);
+	bool openList();
+
 	void closePut();
 	void closeGet();
+	void closeList();
+
 	std::string getPutName();
 	std::string getGetName();
+
 	void cancelPut();
-	// closes and deletes getfile
 	void cancelGet();
+	void cancelList();
 
+	std::vector<char> readPut();
 	void writeGet(std::vector<char> data);
+	
+	std::string readBase64();
 	void writeBase64(std::string data);
+	void putListData(std::string name);
+	std::vector<std::string> getListData();
+	
+	int getPutChunks();
 	int getGetChunks();
+	int getListChunks();
+	
+	int getPutRemainingChunks();
 	int getGetRemainingChunks();
-	void setGetChunks(int chunks);
-
-	std::vector<char> readPut();
-	std::string readBase64();
-	int getPutChunks();
-	int getPutChunksRemaining();
-	int getPutSize();
+	int getListRemainingChunks();
 	
+	int getPutSize();
+	void setGetChunks(int chunks);
+	void setListChunks(int chunks);
 };
 
 #endif

+ 56 - 8
cli/src/cmdman.cpp

@@ -116,7 +116,7 @@ CmdMan::CmdRet CmdMan::cmdPutdata(vector<string> args) {
 	root["file"] = fileman.getPutName();
 	root["cancel"] = false;
 	root["data"] = fileman.readBase64();
-	root["remaining"] = fileman.getPutChunksRemaining(); // number already decremented by readBase64
+	root["remaining"] = fileman.getPutRemainingChunks(); // number already decremented by readBase64
 	retval.type = send;
 	retval.msg = Json::writeString(wbuilder, root);
 		
@@ -169,6 +169,18 @@ CmdMan::CmdRet CmdMan::cmdList(vector<string> args) {
 	return retval;
 }
 
+CmdMan::CmdRet CmdMan::cmdListdata(vector<string> args) {
+	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
+	Json::Value root;
+	root["command"] = "listdata";
+	root["chunk"] = fileman.getListRemainingChunks();
+	root["cancel"] = false;
+	retval.type = send;
+	retval.msg = Json::writeString(wbuilder, root);
+	
+	return retval;
+}
 
 CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
 	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
@@ -178,7 +190,7 @@ CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
 	map<string,CmdRet(CmdMan::*)(vector<string>)>::iterator it = execmap.find(cmd);
 	string retmsg;
 	if(it == execmap.end()) {
-		return { error, "unknown command \"" + cmd + "\".\ntype help to list available commands." };
+		return { error, string(__PRETTY_FUNCTION__) + " unknown command \"" + cmd + "\".\ntype help to list available commands." };
 	}
 	return (this->*(execmap[cmd]))(args);
 }
@@ -231,7 +243,7 @@ CmdMan::CmdRet CmdMan::handle(Json::Value root) {
 	string retmsg;
 	map<string,CmdRet(CmdMan::*)(Json::Value)>::iterator it = handlemap.find(root["command"].asString());
 	if(it == handlemap.end()) {
-		return { error, "unknown command \"" + root["command"].asString() + "\".\nensure code is implemented." };
+		return { error, string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString() + "\".\nensure code is implemented." };
 	}
 	return (this->*(handlemap[root["command"].asString()]))(root);
 }
@@ -277,10 +289,10 @@ CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
 	CmdRet retval;
 	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	
-	if(root["received"].asInt() != fileman.getPutChunksRemaining()) {
+	if(root["received"].asInt() != fileman.getPutRemainingChunks()) {
 		// the number of remaining chunks received from the daemon does not equal the number stored at the client side		
 		retval.type = error;
-		retval.msg = std::string("File upload failed: Server reports number of remaining chunks as ") + std::to_string(root["received"].asInt()) + " but actual number is " + std::to_string(fileman.getPutChunksRemaining());
+		retval.msg = std::string("File upload failed: Server reports number of remaining chunks as ") + std::to_string(root["received"].asInt()) + " but actual number is " + std::to_string(fileman.getPutRemainingChunks());
 		fileman.cancelPut();
 	} else if(root["cancel"].asBool()) {
 		retval.type = error;
@@ -316,7 +328,7 @@ CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
 		retval.type = error;
 		retval.msg = "File download request failed: Server reports filename " + root["file"].asString() + " but actual filename is " + fileman.getGetName();
 	} else {
-		fileman.setGetChunks(root["chunks"].asUInt());
+		fileman.setGetChunks(root["chunks"].asInt());
 		retval.type = send;
 		retval.msg = "getdata";
 	}
@@ -361,9 +373,45 @@ CmdMan::CmdRet CmdMan::handleList(Json::Value root) {
 	CmdRet retval;
 	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 listing request failed: Server reports: " + root["error"].asString();
+	} else {
+		fileman.setListChunks(root["chunks"].asInt());
+		retval.type = send;
+		retval.msg = "listdata";
+	}
+	
+	return retval;
+}
+
+CmdMan::CmdRet CmdMan::handleListdata(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["remaining"].asInt() != fileman.getListRemainingChunks()) {
+		retval.type = error;
+		retval.msg = std::string("File listing failed: Server reports number of remaining chunks as ") + std::to_string(root["remaining"].asInt()) + " but actual number is " + std::to_string(fileman.getListRemainingChunks());
+		fileman.cancelList();
+	} else if(root["cancel"].asBool()) {
+		retval.type = error;
+		retval.msg = "File listing cancelled: Server reports: " + root["error"].asString();
+		fileman.cancelList();
+	} else {
+		for(Json::Value i : root["names"]) fileman.putListData(i.asString());
+		// loaded successfully
+		if(!fileman.getListRemainingChunks()) {
+			// everything sent
+			retval.type = notsend;
+			retval.msg = "Files on server:\n";
+			for(string s : fileman.getListData()) retval.msg += s + "\n";
+			fileman.closeList();
+		} else {
+			retval.type = send;
+			retval.msg = "listdata";
+		}
+	}
 	return retval;
 }
 

+ 46 - 3
cli/src/fileman.cpp

@@ -2,7 +2,7 @@
 #include "../include/base64.h"
 
 FileMan::FileMan() {
-	
+	islisting = false;
 }
 
 FileMan::~FileMan() {
@@ -18,6 +18,9 @@ bool FileMan::isPutting() {
 	return putfile.is_open();
 }
 
+bool FileMan::isListing() {
+	return islisting;
+}
 
 bool FileMan::openPut(const std::string &name) {
 	putname = name;
@@ -45,6 +48,15 @@ bool FileMan::openGet(const std::string &name) {
 	return true;
 }
 
+bool FileMan::openList() {
+	if(isListing()) {
+		return false;
+	}
+	listdata = std::vector<std::string>();
+	islisting = true;
+	return true;
+}
+
 void FileMan::closePut() {
 	putfile.close();
 }
@@ -53,6 +65,10 @@ void FileMan::closeGet() {
 	getfile.close();
 }
 
+void FileMan::closeList() {
+	islisting = false;
+}
+
 void FileMan::cancelPut() {
 	if(isPutting()) {
 		closePut();
@@ -66,6 +82,12 @@ void FileMan::cancelGet() {
 	}
 }
 
+void FileMan::cancelList() {
+	if(isListing()) {
+		closeList();
+	}
+}
+
 void FileMan::writeGet(const std::vector<char> data) {
 	getfile.write(data.data(), data.size());
 	getchunksRemaining--;
@@ -75,6 +97,14 @@ void FileMan::writeBase64(std::string data) {
 	writeGet(base64::decodeVector(data));
 }
 
+void FileMan::putListData(std::string name) {
+	listdata.push_back(name);
+}
+
+std::vector<std::string> FileMan::getListData() {
+	return listdata;
+}
+
 int FileMan::getGetChunks() {
 	return getchunks;
 }
@@ -88,6 +118,11 @@ void FileMan::setGetChunks(int chunks) {
 	getchunksRemaining = chunks-1;
 }
 
+void FileMan::setListChunks(int chunks) {
+	listchunks = chunks;
+	getchunksRemaining = chunks-1;
+}
+
 std::vector<char> FileMan::readPut() {
 	char buf[max_read_len];
 	std::vector<char> data;
@@ -113,10 +148,18 @@ int FileMan::getPutChunks() {
 	return putchunks;
 }
 
-int FileMan::getPutChunksRemaining() {
+int FileMan::getPutRemainingChunks() {
 	return putchunksRemaining;
 }
 
 int FileMan::getPutSize() {
 	return putsize;
-}
+}
+
+int FileMan::getListRemainingChunks() {
+	return listchunksRemaining;
+}
+
+int FileMan::getListChunks() {
+	return listchunks;
+}