Просмотр исходного кода

get actual working code, add more debugging output. to investigate: trailing buffer content

Missingmew 4 лет назад
Родитель
Сommit
90acb7ccdf
4 измененных файлов с 110 добавлено и 20 удалено
  1. 2 0
      cli/include/cmdman.h
  2. 3 3
      cli/include/ioman.h
  3. 35 1
      cli/src/cmdman.cpp
  4. 70 16
      cli/src/ioman.cpp

+ 2 - 0
cli/include/cmdman.h

@@ -42,6 +42,8 @@ private:
 	map<string,CmdRet(CmdMan::*)(vector<string>)> execmap;
 	map<string,string> helpmap;
 	map<string,CmdRet(CmdMan::*)(Json::Value)> handlemap;
+
+	bool dologin, doversion;
 	
 
 	/* execute command descriptions and methods go here */

+ 3 - 3
cli/include/ioman.h

@@ -56,9 +56,9 @@ private:
 	bool startlist;
 
 protected:
-	virtual void printNormalMessage(std::string msg) = 0;
-	virtual void printErrorMessage(std::string msg) = 0;
-	virtual void printDebugMessage(std::string msg) = 0;
+	virtual void printNormalMessage(std::string msg);
+	virtual void printErrorMessage(std::string msg);
+	virtual void printDebugMessage(std::string msg);
 	virtual void printWelcomeMessage() = 0;
 	virtual std::string getCmdPrompt() = 0;
 	virtual std::string getUserPrompt() = 0;

+ 35 - 1
cli/src/cmdman.cpp

@@ -1,6 +1,8 @@
 #include "../include/global.h"
 #include "../include/cmdman.h"
 
+#include <iostream>
+
 CmdMan::CmdMan(FileMan &fm) : fileman(fileman) {
 	
 	/* setup json stuff */
@@ -8,6 +10,9 @@ CmdMan::CmdMan(FileMan &fm) : fileman(fileman) {
   wbuilder.settings_["indentation"] = "";
   reader = rbuilder.newCharReader();
 	
+	dologin = false;
+	doversion = false;
+	
 	/* initialize execute command map */
 	execmap["help"] = &CmdMan::cmdHelp;
 	execmap["status"] = &CmdMan::cmdStatus;
@@ -42,6 +47,7 @@ CmdMan::~CmdMan() {
 
 CmdMan::CmdRet CmdMan::cmdHelp(vector<string> args) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	map<string,string>::iterator it;
 	for(it = helpmap.begin(); it != helpmap.end(); it++) {
 		retval.msg.append(it->first);
@@ -55,6 +61,7 @@ CmdMan::CmdRet CmdMan::cmdHelp(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdStatus(vector<string> args) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	Json::Value root;
 	root["command"] = "status";
 	retval.type = send;
@@ -65,6 +72,7 @@ CmdMan::CmdRet CmdMan::cmdStatus(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdDisconnect(vector<string> args) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	Json::Value root;
 	root["command"] = "close";
 	retval.type = send;
@@ -75,6 +83,7 @@ CmdMan::CmdRet CmdMan::cmdDisconnect(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	Json::Value root;
 	
 	bool opened = fileman.openPut(args[0]);
@@ -95,6 +104,7 @@ CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	Json::Value root;
 	
 	bool opened = fileman.openGet(args[0]);
@@ -114,6 +124,7 @@ CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdList(vector<string> args) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	Json::Value root;
 	root["command"] = "list";
 	retval.type = send;
@@ -124,6 +135,10 @@ CmdMan::CmdRet CmdMan::cmdList(vector<string> args) {
 
 
 CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
+	std::cerr << __PRETTY_FUNCTION__ << " using command \"" << cmd << "\" with arguments [ ";
+	for(string s : args) std::cerr << s << " ";
+	std::cerr << "]" << std::endl;
 	map<string,CmdRet(CmdMan::*)(vector<string>)>::iterator it = execmap.find(cmd);
 	string retmsg;
 	if(it == execmap.end()) {
@@ -135,16 +150,20 @@ CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
 /* internal commands */
 CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	Json::Value root;
 	root["version"] = protocolVersion;
 	retval.type = send;
 	retval.msg = Json::writeString(wbuilder, root);
 	
+	doversion = true;
+	
 	return retval;
 }
 
 CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	Json::Value root;
 	root["user"] = args[0];
 	root["pass"] = args[1];
@@ -153,6 +172,8 @@ CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
 	retval.type = send;
 	retval.msg = Json::writeString(wbuilder, root);
 	
+	dologin = true;
+	
 	return retval;
 }
 
@@ -167,8 +188,12 @@ CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
 */
 
 CmdMan::CmdRet CmdMan::handle(Json::Value root) {
-	map<string,CmdRet(CmdMan::*)(Json::Value)>::iterator it = handlemap.find(root["command"].asString());
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
+	if(doversion) root["command"] = "version";
+	else if (dologin) root["command"] = "login";
+	std::cerr << __PRETTY_FUNCTION__ << " using json" << std::endl << root << std::endl;
 	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." };
 	}
@@ -177,6 +202,7 @@ CmdMan::CmdRet CmdMan::handle(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handleStatus(Json::Value root) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	retval.type = notsend;
 	retval.msg = root["response"].asString();
 	
@@ -185,6 +211,7 @@ CmdMan::CmdRet CmdMan::handleStatus(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handleClose(Json::Value root) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	retval.type = close;
 	retval.msg = root["response"].asString();
 	
@@ -194,6 +221,7 @@ CmdMan::CmdRet CmdMan::handleClose(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	
 	retval.type = send;
 	retval.msg = Json::writeString(wbuilder, root);
@@ -203,6 +231,7 @@ CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	
 	retval.type = send;
 	retval.msg = Json::writeString(wbuilder, root);
@@ -212,6 +241,7 @@ CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
 
 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);
@@ -221,6 +251,7 @@ CmdMan::CmdRet CmdMan::handleList(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	
 	if(!root["accept"].asBool()) {
 		retval.type = error;
@@ -229,6 +260,7 @@ CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
 	else {
 		retval.type = seton;
 		retval.msg = "Version check ok.";
+		doversion = false;
 	}
 	
 	return retval;
@@ -236,6 +268,7 @@ CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handleLogin(Json::Value root) {
 	CmdRet retval;
+	std::cerr << __PRETTY_FUNCTION__ << " begin" << std::endl;
 	
 	if(!root["accept"].asBool()) {
 		retval.type = error;
@@ -244,6 +277,7 @@ CmdMan::CmdRet CmdMan::handleLogin(Json::Value root) {
 	else {
 		retval.type = seton;
 		retval.msg = "Login ok.";
+		dologin = false;
 	}
 	
 	return retval;

+ 70 - 16
cli/src/ioman.cpp

@@ -33,6 +33,8 @@ IoMan::IoMan(char *ipcstring) : cmdman(fileman) {
 	runinput = false;
 	runresponse = false;
 	startlist = false;
+	versionstatus = off;
+	loginstatus = off;
 }
 
 IoMan::~IoMan() {
@@ -49,6 +51,16 @@ IoMan::~IoMan() {
 	}
 	
 	delete tcpsock;
+	delete reader;
+}
+
+void IoMan::printNormalMessage(string nouse) {
+}
+
+void IoMan::printErrorMessage(string nouse) {
+}
+
+void IoMan::printDebugMessage(string nouse) {
 }
 
 bool IoMan::connect() {
@@ -89,8 +101,10 @@ bool IoMan::init() {
 	printDebugMessage("IoMan::Init() versioncheck");
 	
 	localmutex.lock();
+	printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"));
 	localinput.push_back("version " + protocolVersion);
 	localmutex.unlock();
+	printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
 	
 	printDebugMessage("IoMan::Init() begin");
 	
@@ -116,12 +130,14 @@ bool IoMan::init() {
 
 void IoMan::networkMain() {
 	vector<Json::Value> toput;
-	const char *recvjson;
+	char *recvjson;
 	Json::Value root;
-	unsigned int jsonsize;
+	unsigned int jsonsize, readsize;
 	
 	printDebugMessage("IoMan::networkMain() begin");
 	while(runnetwork) {
+		std::this_thread::yield();
+		std::this_thread::sleep_for(std::chrono::milliseconds(10));
 		/*
 		read from network until \n
 		try to parse json
@@ -134,16 +150,19 @@ void IoMan::networkMain() {
 		*/
 		
 		// read from network
-		boost::asio::read_until(*tcpsock, recvbuf, '\n', errcode);
+		readsize = boost::asio::read_until(*tcpsock, recvbuf, '\n', errcode);
+		printDebugMessage(string(__PRETTY_FUNCTION__) + string(" asio::read() ok"));
 		if (errcode && errcode != boost::asio::error::eof) {
 			printErrorMessage("IoMan::networkMain() couldnt read json data\n" + errcode.message());
 			continue;
 		}
 		
-		recvjson = boost::asio::buffer_cast<const char *>(recvbuf.data());
+		recvjson = (char *)(boost::asio::buffer_cast<const char *>(recvbuf.data()));
 		while(strchr(recvjson, '\n')) {
 			// parse
-			jsonsize = strchr(recvjson, '\n') - recvjson;
+			jsonsize = strchr(recvjson, '\n') - recvjson + 1;
+			
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" found jsondata ") + string(recvjson));
 			
 			if (!reader->parse(recvjson, recvjson + jsonsize, &root, &jsonerror)) {
 				printErrorMessage("IoMan::networkMain() couldnt parse json data: " + jsonerror);
@@ -151,15 +170,27 @@ void IoMan::networkMain() {
 				continue;
 			}
 			recvbuf.consume(jsonsize);
+			readsize -= jsonsize;
+			
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" remaining recvbuf ") + string(boost::asio::buffer_cast<const char *>(recvbuf.data())));
 			
+			for(int i = 0; i < jsonsize; i++) recvjson++;
 			// store locally
 			toput.push_back(root);
 		}
 		
-		// put into global vector
-		netmutex.lock();
-		netinput.insert(netinput.end(), toput.begin(), toput.end());
-		netmutex.unlock();
+		if(toput.size()){
+			// put into global vector
+			netmutex.lock();
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get netmutex"));
+			netinput.insert(netinput.end(), toput.begin(), toput.end());
+			netmutex.unlock();
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release netmutex"));
+		}
+		
+		// clean up local stuff
+		toput = vector<Json::Value>();
+		recvbuf.consume(readsize);
 	}
 }
 
@@ -173,6 +204,8 @@ void IoMan::inputMain() {
 	
 	printDebugMessage("IoMan::inputMain() begin");
 	while(runinput) {
+		std::this_thread::yield();
+		std::this_thread::sleep_for(std::chrono::milliseconds(10));
 		/*
 		get inputmutex
 		read all input vector into local vector
@@ -185,12 +218,14 @@ void IoMan::inputMain() {
 		// read into local vector
 		if(localinput.size()) {
 			localmutex.lock();
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" has localmutex"));
 			toprocess = vector<string>(localinput);
 			localinput = vector<string>();
 			localmutex.unlock();
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
 		}
 		
-		printDebugMessage(string("have ") + std::to_string(toprocess.size()) + string(" commands"));
+		// printDebugMessage(string("have ") + std::to_string(toprocess.size()) + string(" commands"));
 		
 		// process
 		for(string cmd : toprocess) {
@@ -202,7 +237,7 @@ void IoMan::inputMain() {
 			}
 			else {
 				command = cmd.substr(0, index);
-				index++;
+				//~ index++;
 				while(true) {
 					// find first char thats not a space
 					while(cmd[index] == ' ') {
@@ -235,7 +270,7 @@ void IoMan::inputMain() {
 							goto end_tokenizing;
 						}
 						else {
-							args.push_back(cmd.substr(1, index));
+							args.push_back(cmd.substr(0, index));
 						}
 					}
 				}
@@ -265,6 +300,9 @@ void IoMan::inputMain() {
 				}
 			}
 		}
+		
+		// clean up local stuff
+		toprocess = vector<string>();
 	}
 }
 
@@ -275,6 +313,8 @@ void IoMan::responseMain() {
 	
 	printDebugMessage("IoMan::responseMain() begin");
 	while(runresponse) {
+		std::this_thread::yield();
+		std::this_thread::sleep_for(std::chrono::milliseconds(10));
 		/*
 		get networkmutex
 		read all network vector into local vector
@@ -293,9 +333,11 @@ void IoMan::responseMain() {
 		// read into local vector
 		if(netinput.size()) {
 			netmutex.lock();
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get netmutex"));
 			toprocess = vector<Json::Value>(netinput);
 			netinput = vector<Json::Value>();
 			netmutex.unlock();
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release netmutex"));
 		}
 		
 		// process jsons
@@ -336,10 +378,18 @@ void IoMan::responseMain() {
 			}
 		}
 		
-		// put new commands into global vector
-		localmutex.lock();
-		localinput.insert(localinput.end(), toput.begin(), toput.end());
-		localmutex.unlock();
+		if(toput.size()) {
+			// put new commands into global vector
+			localmutex.lock();
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"));
+			localinput.insert(localinput.end(), toput.begin(), toput.end());
+			localmutex.unlock();
+			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
+		}
+		
+		// clean up local stuff
+		toprocess = vector<Json::Value>();
+		toput = vector<string>();
 	}
 }
 
@@ -365,8 +415,10 @@ void IoMan::run() {
 	printDebugMessage("IoMan::run() login");
 	
 	localmutex.lock();
+	printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"));
 	localinput.push_back("login " + string(user) + " " + string(pass));
 	localmutex.unlock();
+	printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
 	free(user);
 	free(pass);
 	
@@ -400,8 +452,10 @@ void IoMan::run() {
 		add_history(line);
 		
 		localmutex.lock();
+		printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"));
 		localinput.push_back(line);
 		localmutex.unlock();
+		printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
 		
 		if(!connected) break;
 	}