Browse Source

rework output message system, todo: get readline prompt fully working after output

Missingmew 4 years ago
parent
commit
8cc3aa6081
6 changed files with 79 additions and 720 deletions
  1. 3 3
      cli/include/ioman.h
  2. 1 3
      cli/include/machineioman.h
  3. 1 3
      cli/include/userioman.h
  4. 41 47
      cli/src/ioman.cpp
  5. 15 267
      cli/src/machineioman.cpp
  6. 18 397
      cli/src/userioman.cpp

+ 3 - 3
cli/include/ioman.h

@@ -56,9 +56,9 @@ private:
 	bool startlist;
 
 protected:
-	virtual void printNormalMessage(std::string msg);
-	virtual void printErrorMessage(std::string msg);
-	virtual void printDebugMessage(std::string msg);
+	enum OutMsgType { normal, error, debug };
+	std::mutex msgmutex;
+	virtual void printMessage(std::string msg, OutMsgType type);
 	virtual void printWelcomeMessage() = 0;
 	virtual std::string getCmdPrompt() = 0;
 	virtual std::string getUserPrompt() = 0;

+ 1 - 3
cli/include/machineioman.h

@@ -9,9 +9,7 @@ public:
 
 	bool parseJson(Json::Value *root, string jsonstring);
 	bool handleJson(Json::Value root);
-	void printNormalMessage(std::string msg);
-	void printErrorMessage(std::string msg);
-	void printDebugMessage(std::string msg);
+	void printMessage(std::string msg, OutMsgType type);
 	void printWelcomeMessage();
 
 	std::string getCmdPrompt();

+ 1 - 3
cli/include/userioman.h

@@ -9,9 +9,7 @@ public:
 
 	bool parseJson(Json::Value *root, string jsonstring);
 	bool handleJson(Json::Value root);
-	void printNormalMessage(std::string msg);
-	void printErrorMessage(std::string msg);
-	void printDebugMessage(std::string msg);
+	void printMessage(std::string msg, OutMsgType type);
 	void printWelcomeMessage();
 
 	std::string getCmdPrompt();

+ 41 - 47
cli/src/ioman.cpp

@@ -54,13 +54,7 @@ IoMan::~IoMan() {
 	delete reader;
 }
 
-void IoMan::printNormalMessage(string nouse) {
-}
-
-void IoMan::printErrorMessage(string nouse) {
-}
-
-void IoMan::printDebugMessage(string nouse) {
+void IoMan::printMessage(string nouse, OutMsgType nouse2) {
 }
 
 bool IoMan::connect() {
@@ -69,11 +63,11 @@ bool IoMan::connect() {
   ep = new tcp::endpoint(boost::asio::ip::address::from_string(ipstring), 1234);
 
   // establish connection
-  printDebugMessage("IoMan::connect() connecting to " + ipstring);
+  printMessage("IoMan::connect() connecting to " + ipstring, debug);
   tcpsock->connect(*ep, errcode);
   if (errcode) {
 	  delete ep;
-	  printErrorMessage("IoMan::connect() couldnt connect to " + ipstring + "\n" + errcode.message());
+	  printMessage("IoMan::connect() couldnt connect to " + ipstring + "\n" + errcode.message(), error);
 	return false;
   }
   connected = true;
@@ -83,7 +77,7 @@ bool IoMan::connect() {
 
 
 void IoMan::disconnect() {
-  printDebugMessage("IoMan::disconnect()");
+  printMessage("IoMan::disconnect()", debug);
 	tcpsock->close();
 	connected = false;
 }
@@ -94,19 +88,19 @@ bool IoMan::init() {
 	string work;
 	Json::Value root;
 	
-	printDebugMessage("IoMan::Init() begin");
+	printMessage("IoMan::Init() begin", debug);
 	
 	if(!connect()) return false;
 	
-	printDebugMessage("IoMan::Init() versioncheck");
+	printMessage("IoMan::Init() versioncheck", debug);
 	
 	localmutex.lock();
-	printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"));
+	printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), debug);
 	localinput.push_back("version " + protocolVersion);
 	localmutex.unlock();
-	printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
+	printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
 	
-	printDebugMessage("IoMan::Init() begin");
+	printMessage("IoMan::Init() begin", debug);
 	
 	runnetwork = true;
 	runinput = true;
@@ -134,7 +128,7 @@ void IoMan::networkMain() {
 	Json::Value root;
 	unsigned int jsonsize, readsize;
 	
-	printDebugMessage("IoMan::networkMain() begin");
+	printMessage("IoMan::networkMain() begin", debug);
 	while(runnetwork) {
 		std::this_thread::yield();
 		std::this_thread::sleep_for(std::chrono::milliseconds(10));
@@ -151,10 +145,10 @@ void IoMan::networkMain() {
 		
 		// read from network
 		readsize = boost::asio::read_until(*tcpsock, recvbuf, '\n', errcode);
-		printDebugMessage(string(__PRETTY_FUNCTION__) + string(" asio::read() ok ") + std::to_string(readsize));
-		// printDebugMessage(string("have ") + std::to_string(toprocess.size()) + string(" commands"));
+		printMessage(string(__PRETTY_FUNCTION__) + string(" asio::read() ok ") + std::to_string(readsize), debug);
+		// printMessage(string("have ") + std::to_string(toprocess.size()) + string(" commands"), debug);
 		if (errcode && errcode != boost::asio::error::eof) {
-			printErrorMessage("IoMan::networkMain() couldnt read json data\n" + errcode.message());
+			printMessage("IoMan::networkMain() couldnt read json data\n" + errcode.message(), error);
 			continue;
 		}
 		if(!readsize) break;
@@ -164,17 +158,17 @@ void IoMan::networkMain() {
 			// parse
 			jsonsize = strchr(recvjson, '\n') - recvjson + 1;
 			
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" found jsondata ") + string(recvjson));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" found jsondata ") + string(recvjson), debug);
 			
 			if (!reader->parse(recvjson, recvjson + jsonsize, &root, &jsonerror)) {
-				printErrorMessage("IoMan::networkMain() couldnt parse json data: " + jsonerror);
+				printMessage("IoMan::networkMain() couldnt parse json data: " + jsonerror, error);
 				recvbuf.consume(jsonsize);
 				continue;
 			}
 			recvbuf.consume(jsonsize);
 			readsize -= jsonsize;
 			
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" remaining recvbuf ") + string(boost::asio::buffer_cast<const char *>(recvbuf.data())));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" remaining recvbuf ") + string(boost::asio::buffer_cast<const char *>(recvbuf.data())), debug);
 			
 			for(int i = 0; i < jsonsize; i++) recvjson++;
 			// store locally
@@ -184,10 +178,10 @@ void IoMan::networkMain() {
 		if(toput.size()){
 			// put into global vector
 			netmutex.lock();
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get netmutex"));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" get netmutex"), debug);
 			netinput.insert(netinput.end(), toput.begin(), toput.end());
 			netmutex.unlock();
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release netmutex"));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" release netmutex"), debug);
 		}
 		
 		// clean up local stuff
@@ -204,7 +198,7 @@ void IoMan::inputMain() {
 	
 	size_t prev, index, quot;
 	
-	printDebugMessage("IoMan::inputMain() begin");
+	printMessage("IoMan::inputMain() begin", debug);
 	while(runinput) {
 		std::this_thread::yield();
 		std::this_thread::sleep_for(std::chrono::milliseconds(10));
@@ -220,14 +214,14 @@ void IoMan::inputMain() {
 		// read into local vector
 		if(localinput.size()) {
 			localmutex.lock();
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" has localmutex"));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" has localmutex"), debug);
 			toprocess = vector<string>(localinput);
 			localinput = vector<string>();
 			localmutex.unlock();
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
 		}
 		
-		// printDebugMessage(string("have ") + std::to_string(toprocess.size()) + string(" commands"));
+		// printMessage(string("have ") + std::to_string(toprocess.size()) + string(" commands"), debug);
 		
 		// process
 		for(string cmd : toprocess) {
@@ -284,20 +278,20 @@ void IoMan::inputMain() {
 			// determine wether to send something and do so if required
 			switch(cmdret.type) {
 				case CmdMan::rettype::send: {
-					printDebugMessage("IoMan::inputMain() sending json \"" + cmdret.msg + "\"");
+					printMessage("IoMan::inputMain() sending json \"" + cmdret.msg + "\"", debug);
 					boost::asio::write(*tcpsock, buffer(cmdret.msg + "\n"), errcode);
 					if (errcode) {
-						printErrorMessage("IoMan::inputMain() couldnt send json data\n" + errcode.message() + "\n");
+						printMessage("IoMan::inputMain() couldnt send json data\n" + errcode.message() + "\n", error);
 						continue;
 					}
 					break;
 				}
 				case CmdMan::rettype::notsend: {
-					printNormalMessage(cmdret.msg);
+					printMessage(cmdret.msg, normal);
 					break;
 				}
 				case CmdMan::rettype::error: {
-					printErrorMessage(cmdret.msg);
+					printMessage(cmdret.msg, error);
 					break;
 				}
 			}
@@ -313,7 +307,7 @@ void IoMan::responseMain() {
 	vector<string> toput;
 	CmdMan::CmdRet cmdret;
 	
-	printDebugMessage("IoMan::responseMain() begin");
+	printMessage("IoMan::responseMain() begin", debug);
 	while(runresponse) {
 		std::this_thread::yield();
 		std::this_thread::sleep_for(std::chrono::milliseconds(10));
@@ -335,11 +329,11 @@ void IoMan::responseMain() {
 		// read into local vector
 		if(netinput.size()) {
 			netmutex.lock();
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get netmutex"));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" get netmutex"), debug);
 			toprocess = vector<Json::Value>(netinput);
 			netinput = vector<Json::Value>();
 			netmutex.unlock();
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release netmutex"));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" release netmutex"), debug);
 		}
 		
 		// process jsons
@@ -362,7 +356,7 @@ void IoMan::responseMain() {
 				case CmdMan::rettype::error: {
 					if(versionstatus == off) versionstatus = err;
 					else if(loginstatus == off) loginstatus = err;
-					printErrorMessage(cmdret.msg);
+					printMessage(cmdret.msg, error);
 					break;
 				}
 				case CmdMan::rettype::seton: {
@@ -370,7 +364,7 @@ void IoMan::responseMain() {
 					else loginstatus = on;
 				}
 				case CmdMan::rettype::notsend: {
-					printNormalMessage(cmdret.msg);
+					printMessage(cmdret.msg, normal);
 					break;
 				}
 				case CmdMan::rettype::send: {
@@ -383,10 +377,10 @@ void IoMan::responseMain() {
 		if(toput.size()) {
 			// put new commands into global vector
 			localmutex.lock();
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), debug);
 			localinput.insert(localinput.end(), toput.begin(), toput.end());
 			localmutex.unlock();
-			printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
+			printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
 		}
 		
 		// clean up local stuff
@@ -396,7 +390,7 @@ void IoMan::responseMain() {
 }
 
 void IoMan::run() {
-	printDebugMessage("IoMan::run() begin");
+	printMessage("IoMan::run() begin", debug);
 	char *line = NULL, *user = NULL, *pass = NULL;
 	vector<string> tokens;
 	string work, command;
@@ -407,20 +401,20 @@ void IoMan::run() {
 	
 	while(!user) {
 		user = readline(getUserPrompt().c_str());
-		printErrorMessage("Using user: " + string(user));
+		printMessage("Using user: " + string(user), error);
 	}
 	while(!pass) {
 		pass = readline(getPassPrompt().c_str());
-		printErrorMessage("Using pass: " + string(pass));
+		printMessage("Using pass: " + string(pass), error);
 	}
 	
-	printDebugMessage("IoMan::run() login");
+	printMessage("IoMan::run() login", debug);
 	
 	localmutex.lock();
-	printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"));
+	printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), debug);
 	localinput.push_back("login " + string(user) + " " + string(pass));
 	localmutex.unlock();
-	printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
+	printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
 	free(user);
 	free(pass);
 	
@@ -456,10 +450,10 @@ void IoMan::run() {
 		add_history(line);
 		
 		localmutex.lock();
-		printDebugMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"));
+		printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), debug);
 		localinput.push_back(line);
 		localmutex.unlock();
-		printDebugMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"));
+		printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
 		
 		if(!connected) break;
 	}

+ 15 - 267
cli/src/machineioman.cpp

@@ -18,16 +18,21 @@ bool MachineIoMan::handleJson(Json::Value root) {
 	return false;
 }
 
-void MachineIoMan::printNormalMessage(std::string msg) {
-	std::cout << msg << std::endl;
-}
-
-void MachineIoMan::printErrorMessage(std::string msg) {
-	std::cout << msg << std::endl;
-}
-
-void MachineIoMan::printDebugMessage(std::string msg) {
-	std::cerr << msg << std::endl;
+void MachineIoMan::printMessage(std::string msg, OutMsgType type) {
+	switch(type) {
+		case normal: {
+			std::cout << msg << std::endl;
+			break;
+		}
+		case error: {
+			std::cout << msg << std::endl;
+			break;
+		}
+		case debug: {
+			std::cerr << msg << std::endl;
+			break;
+		}
+	}
 }
 
 void MachineIoMan::printWelcomeMessage() {
@@ -44,260 +49,3 @@ std::string MachineIoMan::getUserPrompt() {
 std::string MachineIoMan::getPassPrompt() {
 	return "";
 }
-
-/* void MachineIoMan::run() {
-  std::cerr << "MachineIoMan::run() says hello!" << std::endl;
-
-  boost::asio::streambuf recvbuf;
-
-  Json::Value root, recv;
-  const char *recvjson;
-
-  bool keep_reading = true;
-  COMMANDID cmd;
-  char *line = NULL;
-
-  std::vector<std::string> tokens, pathtemp;
-
-  std::fstream fs;
-
-  // .open(path, mode; mode std::fstream::in, out, binary, trunc(ate))
-  // .read(char* dest, int size)
-  // .write(char* src, int size)
-  // .eof()
-  // .seekg(offset, ios_base::beg, cur, end)
-  // .tellg()
-  // .close()
-
-  while (keep_reading) {
-    // clean up
-    free(line);
-    root = Json::Value();
-    recv = Json::Value();
-    recvbuf.consume(recvbuf.size());
-
-    // read an input line
-    line = readline("");
-
-    // if no input continue
-    if (strlen(line) == 0) {
-      continue;
-    }
-
-    boost::algorithm::split(tokens, std::string(line),
-                            boost::algorithm::is_any_of(" "),
-                            boost::algorithm::token_compress_on);
-
-    std::cerr << "tokens are: [ ";
-    for (int i = 0; i < tokens.size(); i++) {
-      std::cerr << tokens[i] << " ";
-    }
-    std::cerr << "]" << std::endl;
-
-    if (tokens.size() < 1) {
-      continue;
-    }
-
-    cmd = getCmdIdFromString(tokens[0].c_str());
-
-    switch (cmd) {
-    case CMD_DISCONNECT: {
-      std::cerr << "disconnecting\n";
-      keep_reading = false;
-      break;
-    }
-    case CMD_STATUS: {
-      root["command"] = "status";
-
-      // send request
-      sendJson(root);
-      // receive answer
-      receiveJson(recvbuf);
-      //
-      parseJson(&recv, recvbuf);
-
-      // dump received data to stdout for gui to handle
-      std::cout << recv;
-
-      break;
-    }
-    case CMD_PUT: {
-      int size, chunks, haveread;
-      char rbuf[BLOCKSIZE];
-      if (tokens.size() < 2) {
-        std::cerr << "no files specified" << std::endl;
-        break;
-      }
-      fs.open(tokens[1], std::fstream::in | std::fstream::binary);
-      if (!fs.is_open()) {
-        std::cerr << "couldnt open file " << tokens[1] << std::endl;
-        break;
-      }
-      fs.seekg(0, fs.end);
-      size = fs.tellg();
-      fs.seekg(0, fs.beg);
-      chunks = size / BLOCKSIZE + ((size % BLOCKSIZE) ? 1 : 0);
-      std::cerr << "size is " << size << " chunks are " << chunks << std::endl;
-
-      if (!size) {
-        std::cerr << "not sending empty file" << std::endl;
-        break;
-      }
-
-      boost::algorithm::split(pathtemp, tokens[1],
-                              boost::algorithm::is_any_of("/"),
-                              boost::algorithm::token_compress_on);
-
-      root["command"] = "put";
-      root["file"] = pathtemp.back();
-      root["size"] = size;
-
-      std::cerr << root;
-
-      if (!sendJson(root) | !receiveJson(recvbuf) |
-          !parseJson(&recv, recvbuf)) {
-        // error occured, stop working
-        break;
-      }
-      // dump to stdout for GUI
-      std::cout << recv;
-
-      // check correct command once asychronous
-      if (recv["accept"].asBool()) {
-        // request ok, start sending...
-        root = Json::Value();
-        root["command"] = "put";
-        root["cancel"] = false;
-
-        std::cerr << root;
-
-        while (chunks) {
-          haveread = fs.readsome(rbuf, BLOCKSIZE);
-          chunks--;
-          root["data"] = base64::encodeVector(
-              std::vector<char>(rbuf, rbuf + haveread * sizeof(char)));
-          root["remaining"] = chunks;
-          if (!sendJson(root) | !receiveJson(recvbuf) |
-              !parseJson(&recv, recvbuf)) {
-            // error occured, stop working
-            break;
-          }
-          if (recv["cancel"].asBool()) {
-            // transfer cancelled by server
-            break;
-          }
-          std::cout << root;
-        }
-        if (chunks)
-          std::cerr << "not all data sent, remain chunks " << chunks
-                    << std::endl;
-      }
-      break;
-    }
-    case CMD_GET: {
-      int chunks, rec = 0;
-      std::vector<char> wtmp;
-      char wbuf[BLOCKSIZE];
-      if (tokens.size() < 2) {
-        std::cerr << "no files specified" << std::endl;
-        break;
-      }
-      fs.open(tokens[1],
-              std::fstream::out | std::fstream::binary | std::fstream::trunc);
-      if (!fs.is_open()) {
-        std::cerr << "couldnt open file " << tokens[1] << std::endl;
-        break;
-      }
-
-      root["command"] = "get";
-      root["file"] = tokens[1];
-
-      if (!sendJson(root) | !receiveJson(recvbuf) |
-          !parseJson(&recv, recvbuf)) {
-        // error occured, stop working
-        break;
-      }
-
-      // dump received data for GUI
-      std::cout << recv << std::endl;
-
-      if (recv["accept"].asBool()) {
-        // for ACK
-        root["command"] = "get";
-        root["cancel"] = false;
-        // request ok, start receiving
-        chunks = std::numeric_limits<int>::max();
-        std::cerr << "survive" << std::endl;
-        while (chunks) {
-          std::cerr << "survive" << std::endl;
-          if (!receiveJson(recvbuf) | !parseJson(&recv, recvbuf))
-            break;
-          std::cerr << "survive" << std::endl;
-          // dump received data for GUI
-          std::cout << recv << std::endl;
-          wtmp = base64::decodeVector(recv["data"].asString());
-          fs.write(wtmp.data(), wtmp.size());
-          chunks = recv["remaining"].asInt();
-          root["received"] = rec;
-          if (!sendJson(root)) {
-            // couldnt ACK
-            break;
-          }
-          rec++;
-        }
-      }
-      if (recv["cancel"].asBool())
-        std::cerr << "transfer was cancelled" << std::endl;
-      if (recv["remaining"].asInt())
-        std::cerr << "not all data received, remain chunks "
-                  << recv["remaining"].asInt();
-      break;
-    }
-    case CMD_LIST: {
-      int chunks, rec = 0;
-      root["command"] = "list";
-      if (!sendJson(root) | !receiveJson(recvbuf) |
-          !parseJson(&recv, recvbuf)) {
-        // error occured, stop working
-        break;
-      }
-      // dump received data for GUI
-      std::cout << recv << std::endl;
-
-      if (recv["accept"].asBool()) {
-        // ACK?
-        root["command"] = "list";
-        root["cancel"] = false;
-        chunks = std::numeric_limits<int>::max();
-        while (chunks) {
-          if (!receiveJson(recvbuf) | !parseJson(&recv, recvbuf))
-            break;
-          // dump received data for GUI
-          std::cout << recv << std::endl;
-          chunks = recv["remaining"].asInt();
-          root["received"] = rec;
-          if (!sendJson(root)) {
-            // couldnt ACK
-            break;
-          }
-          rec++;
-        }
-      }
-      break;
-    }
-    default: {
-      std::cerr << "unknown command " << line << "\n";
-      break;
-    }
-    }
-    std::printf("\n");
-
-    // if a command used fs, close it now
-    if (fs.is_open())
-      fs.close();
-  }
-  free(line);
-
-  return;
-}
-*/

+ 18 - 397
cli/src/userioman.cpp

@@ -20,16 +20,24 @@ bool UserIoMan::handleJson(Json::Value root) {
 	return false;
 }
 
-void UserIoMan::printNormalMessage(std::string msg) {
-	std::cout << msg << std::endl;
-}
-
-void UserIoMan::printErrorMessage(std::string msg) {
-	std::cout << msg << std::endl;
-}
-
-void UserIoMan::printDebugMessage(std::string msg) {
-	std::cerr << msg << std::endl;
+void UserIoMan::printMessage(std::string msg, OutMsgType type) {
+	msgmutex.lock();
+	switch(type) {
+		case normal: {
+			std::cout << msg << std::endl;
+			break;
+		}
+		case error: {
+			std::cout << msg << std::endl;
+			break;
+		}
+		case debug: {
+			std::cerr << msg << std::endl;
+			break;
+		}
+	}
+	rl_redisplay();
+	msgmutex.unlock();
 }
 
 void UserIoMan::printWelcomeMessage() {
@@ -46,390 +54,3 @@ std::string UserIoMan::getUserPrompt() {
 std::string UserIoMan::getPassPrompt() {
 	return "Pass: ";
 }
-
-/* void UserIoMan::run() {
-  std::cout << "UserIoMan::run() says hello!" << std::endl;
-
-  bool keep_reading = true;
-  COMMANDID cmd;
-
-  char *line = NULL;
-  std::vector<std::string> tokens;
-
-  while (keep_reading) {
-    free(line);
-
-    // read an input line
-    line = readline("ccats> ");
-
-    // if no input, do not add to history, and go to reading next line
-    if (strlen(line) == 0) {
-      continue;
-    }
-
-    // split input line into tokens
-    boost::algorithm::split(tokens, std::string(line),
-                            boost::algorithm::is_any_of(" "),
-                            boost::algorithm::token_compress_on);
-
-    // if input contains only spaces, do not add to history, and go to reading
-    // next line
-    if (tokens.size() < 1) {
-      continue;
-    }
-
-    // add the line to history
-    add_history(line);
-
-    // check for passed command
-    cmd = getCmdIdFromString(tokens[0].c_str());
-
-    switch (cmd) {
-    case CMD_STATUS: {
-
-      boost::asio::streambuf recvbuf;
-      Json::Value root, checkok;
-      const char *recvjson;
-
-      // ask for status
-      root["command"] = "status";
-      boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)),
-                         errcode);
-      if (errcode) {
-        std::cerr << "couldnt send status query to server " << ipstring
-                  << std::endl
-                  << errcode.message() << std::endl;
-        keep_reading = false;
-        continue;
-      }
-
-      // recieve answer to status query
-      boost::asio::read(*tcpsock, recvbuf, boost::asio::transfer_at_least(1),
-                        errcode);
-      if (errcode && errcode != boost::asio::error::eof) {
-        std::cerr << "couldnt recieve status from " << ipstring << std::endl
-                  << errcode.message() << std::endl;
-        keep_reading = false;
-        continue;
-      }
-
-      // parse json
-      recvjson = boost::asio::buffer_cast<const char *>(recvbuf.data());
-      if (!reader->parse(recvjson, recvjson + recvbuf.size(), &root,
-                         &jsonerror)) {
-        std::cerr << "couldnt parse recieved json" << std::endl
-                  << jsonerror << std::endl;
-        keep_reading = false;
-        continue;
-      }
-
-      // remove processed data from recvbuf
-      recvbuf.consume(recvbuf.size());
-
-      // check if received answer to right query
-      checkok = root["command"];
-
-      if (checkok.asString().compare("status") != 0) {
-        std::cerr << "command check failed. client sent command \"status\""
-                  << std::endl
-                  << "server replied to command \"" << checkok << "\""
-                  << std::endl;
-        keep_reading = false;
-        continue;
-      } else {
-        std::cout << "server replied with status: " << root["response"]
-                  << std::endl;
-      }
-
-      break;
-    }
-    case CMD_PUT: {
-
-      if (tokens.size() < 2) {
-        std::cerr << "no file specified" << std::endl;
-        continue;
-      }
-
-      boost::asio::streambuf recvbuf;
-      Json::Value root, ackRecv;
-
-      std::fstream fs;
-      fs.open(tokens[1].c_str(), std::fstream::in | std::fstream::binary);
-
-      if (!fs.is_open()) {
-        std::cerr << "couldnt open file " << tokens[1] << std::endl;
-        break;
-      }
-      // determine file size
-      fs.seekg(0, fs.end);
-      int filelen = fs.tellg();
-      fs.seekg(0, fs.beg);
-
-      // determine file name
-      std::vector<std::string> path;
-      boost::algorithm::split(path, tokens[1], boost::algorithm::is_any_of("/"),
-                              boost::algorithm::token_compress_on);
-      std::string filename = path.back();
-
-      // send first query to server
-      root["command"] = "put";
-      root["file"] = filename;
-      root["size"] = filelen;
-
-      std::cout << root << std::endl;
-
-      if (!sendJson(root)) {
-        keep_reading = false;
-        continue;
-      }
-      std::cout << "sent request to server " << ipstring << std::endl;
-
-      if (!receiveJson(recvbuf)) {
-        keep_reading = false;
-        continue;
-      }
-      if (!parseJson(&root, recvbuf)) {
-        std::cerr << "received invalid JSON from server" << std::endl;
-        keep_reading = false;
-        continue;
-      }
-
-      std::cout << root << std::endl;
-
-      if (!root["accept"].asBool()) {
-        std::cerr << "server did not accept request" << std::endl;
-        keep_reading = false;
-        continue;
-      }
-
-      std::cout << "request successful, beginning to send file to " << ipstring
-                << std::endl;
-
-      // determine number of chunks
-      int size_remainder = filelen % BLOCKSIZE;
-      int chunks = filelen / BLOCKSIZE + ((size_remainder > 0) ? 1 : 0);
-
-      char readbuf[BLOCKSIZE];
-
-      // send file
-      root = Json::Value();
-      root["command"] = "put";
-      root["cancel"] = false;
-
-      while (chunks > 0) {
-        chunks--;
-        if (!chunks &
-            (size_remainder !=
-             0)) { // size_remainder is the size of the last chunk if it's not 0
-          fs.read(readbuf, size_remainder);
-          root["data"] = base64::encodeVector(std::vector<char>(
-              readbuf, readbuf + size_remainder * sizeof(char)));
-        } else { // not last chunk or last chunk has full length
-          fs.read(readbuf, BLOCKSIZE);
-          root["data"] = base64::encodeVector(
-              std::vector<char>(readbuf, readbuf + BLOCKSIZE * sizeof(char)));
-        }
-        root["remaining"] = chunks;
-
-        std::cout << root << std::endl;
-
-        if (!sendJson(root)) {
-          std::cerr << "couldn't send file, " << chunks + 1
-                    << " chunks not sent" << std::endl;
-          keep_reading = false;
-          continue;
-        }
-
-        if (!receiveJson(recvbuf)) {
-          continue;
-        }
-        if (!parseJson(&ackRecv, recvbuf)) {
-          std::cerr << "received invalid JSON from server" << std::endl;
-          continue;
-        }
-        // TODO: check for cancel answer
-      }
-
-      fs.close();
-      std::cout << "successfully sent file to " << ipstring << std::endl;
-
-      break;
-    }
-    case CMD_GET: {
-
-      if (tokens.size() < 2) {
-        std::cerr << "no file specified" << std::endl;
-        continue;
-      }
-
-      boost::asio::streambuf recvbuf;
-      Json::Value root, recvAck;
-
-      // send first query to server
-      root["command"] = "get";
-      root["file"] = tokens[1];
-
-      std::cout << root << std::endl;
-
-      if (!sendJson(root)) {
-        keep_reading = false;
-        continue;
-      }
-      std::cout << "sent request to server " << ipstring << std::endl;
-
-      if (!receiveJson(recvbuf)) {
-        keep_reading = false;
-        continue;
-      }
-      if (!parseJson(&root, recvbuf)) {
-        std::cerr << "received invalid JSON from server" << std::endl;
-        continue;
-      }
-
-      std::cout << root << std::endl;
-
-      if (!root["accept"].asBool()) {
-        std::cerr << "server did not accept request" << std::endl;
-        keep_reading = false;
-        continue;
-      }
-
-      std::cout << "request successful, beginning to receive file from "
-                << ipstring << std::endl;
-
-      // open file stream
-      std::fstream fs;
-      fs.open(tokens[1].c_str(),
-              std::fstream::out | std::fstream::trunc | std::fstream::binary);
-      std::vector<char> writebuf;
-
-      // number of remaining chunks, dummy value to entry loop
-      int remaining = std::numeric_limits<int>::max();
-      int recvCount = 0;
-
-      std::cout << "foobar" << std::endl;
-
-      while (remaining > 0) {
-
-        std::cout << "foobar0" << std::endl;
-
-        if (!receiveJson(recvbuf)) {
-          keep_reading = false;
-
-          std::cout << "fail1" << std::endl;
-
-          continue;
-        }
-
-        std::cout << "foobar1" << std::endl;
-
-        if (!parseJson(&root, recvbuf)) {
-          std::cerr << "received invalid JSON from server" << std::endl;
-          keep_reading = false;
-          continue;
-        }
-        std::cout << "foobar2" << std::endl;
-        std::cout << root << std::endl;
-
-        if (root["cancel"].asBool()) {
-          std::cerr << "server cancelled recieving file" << std::endl;
-          keep_reading = false;
-          continue;
-        }
-
-        writebuf = base64::decodeVector(root["data"].asString());
-        fs.write(writebuf.data(), writebuf.size());
-        remaining = root["remaining"].asInt();
-
-        std::cout << "remaining == " << remaining << std::endl;
-
-        recvAck["received"] = recvCount++;
-
-        if (!sendJson(recvAck)) {
-          continue;
-        }
-      }
-
-      fs.close();
-      break;
-    }
-    case CMD_LIST: {
-      boost::asio::streambuf recvbuf;
-      Json::Value root, recv;
-      //       const char *recvjson;
-
-      int chunks, rec = 0;
-      root["command"] = "list";
-      if (!sendJson(root)) {
-        std::cerr << "couldn't send json" << std::endl;
-        continue;
-      }
-      if (!receiveJson(recvbuf)) {
-        std::cerr << "couldn't receive json" << std::endl;
-        continue;
-      }
-      if (!parseJson(&recv, recvbuf)) {
-        std::cerr << "couldn't parse json" << std::endl;
-        continue;
-      }
-
-      if (recv["accept"].asBool()) {
-        // ACK?
-        root["command"] = "list";
-        root["cancel"] = false;
-        chunks = std::numeric_limits<int>::max();
-        while (chunks) {
-          if (!receiveJson(recvbuf)) {
-            std::cerr << "couldn't receive json" << std::endl;
-            break;
-          }
-          if (!parseJson(&recv, recvbuf)) {
-            std::cerr << "couldn't parse json" << std::endl;
-            break;
-          }
-          // dump received data
-          if (recv["names"].isArray()) {
-            for (Json::Value name : recv["names"]) {
-              std::cout << name.asString();
-            }
-          } else {
-            std::cerr << "should have recieved an array of files, but did not "
-                         "receive an array"
-                      << std::endl;
-          }
-          chunks = recv["remaining"].asInt();
-          root["received"] = rec;
-          if (!sendJson(root)) {
-            // couldnt ACK
-            break;
-          }
-          rec++;
-        }
-      } else {
-        std::cerr << "server refused listing" << std::endl;
-      }
-      break;
-    }
-    case CMD_DISCONNECT: {
-      std::printf("disconnecting\n");
-      keep_reading = false;
-      break;
-    }
-    default: {
-      std::printf("unknown command %s\n", line);
-    }
-    case CMD_HELP: {
-      std::printf("\n");
-      std::printf("AVAILABLE COMMANDS are:\n");
-      printCmds();
-      break;
-    }
-    }
-    std::printf("\n");
-  }
-  free(line);
-  rl_clear_history();
-
-  return;
-}
-*/