Browse Source

run autoformatter

Missingmew 5 years ago
parent
commit
1e64d311cf
5 changed files with 570 additions and 541 deletions
  1. 8 8
      cli/src/commands.cpp
  2. 16 10
      cli/src/iomanager.cpp
  3. 189 171
      cli/src/machineiomanager.cpp
  4. 352 348
      cli/src/useriomanager.cpp
  5. 5 4
      gui/src/qmlhandler.cpp

+ 8 - 8
cli/src/commands.cpp

@@ -2,12 +2,12 @@
 #include <cstdio>
 
 CMD commands[]{
-    {CMD_HELP,       "help", "show help"},
-    {CMD_STATUS,     "status", "query status of IP"},
+    {CMD_HELP, "help", "show help"},
+    {CMD_STATUS, "status", "query status of IP"},
     {CMD_DISCONNECT, "disconnect", "disconnect from IP"},
-    {CMD_PUT	,    "put", "upload file to IP and add to queue"},
-    {CMD_GET	,    "get", "retrieve file from deamon"},
-    {CMD_LIST   ,    "list", "list files that are downloadable from deamon"}
+    {CMD_PUT, "put", "upload file to IP and add to queue"},
+    {CMD_GET, "get", "retrieve file from deamon"},
+    {CMD_LIST, "list", "list files that are downloadable from deamon"}
 
     /* TODO
     {CMD_REMOVE	, "remove", "remove file from IP and queue (stops xfer
@@ -23,8 +23,8 @@ COMMANDID getCmdIdFromString(const char *str) {
   int len = COMMANDLEN;
   if (std::strlen(str) > COMMANDLEN) {
     return ret;
-  }
-  else len = std::strlen(str);
+  } else
+    len = std::strlen(str);
   for (int i = 0; i < len; i++)
     temp[i] = std::tolower(str[i]);
 
@@ -45,7 +45,7 @@ const char *getCmdStringFromId(COMMANDID id) {
 }
 
 void printCmds(void) {
-  for(CMD command : commands) {
+  for (CMD command : commands) {
     printf("%-12s - %s\n", command.name, command.desc);
   }
 }

+ 16 - 10
cli/src/iomanager.cpp

@@ -14,7 +14,8 @@ IoManager::IoManager(char *ipcstring) {
 }
 
 bool IoManager::sendJson(Json::Value root) {
-  boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)), errcode);
+  boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)),
+                     errcode);
   if (errcode) {
     std::cerr << "couldnt send json data" << std::endl
               << errcode.message() << std::endl;
@@ -54,8 +55,7 @@ bool IoManager::connect() {
   Json::Value root, checkok;
   bool bcheckok;
 
-  ep =
-      new tcp::endpoint(boost::asio::ip::address::from_string(ipstring), 1234);
+  ep = new tcp::endpoint(boost::asio::ip::address::from_string(ipstring), 1234);
 
   // establish connection
   std::cerr << "connecting to " << ipstring << std::endl;
@@ -70,11 +70,14 @@ bool IoManager::connect() {
   root["version"] = VERSION;
 
   std::cerr << root << std::endl;
-  
-  if(!sendJson(root)) return false;
+
+  if (!sendJson(root))
+    return false;
   // receive answer
-  if(!receiveJson(recvbuf)) return false;
-  if(!parseJson(&root, recvbuf)) return false;
+  if (!receiveJson(recvbuf))
+    return false;
+  if (!parseJson(&root, recvbuf))
+    return false;
 
   // dump for GUI
   std::cout << root << std::endl;
@@ -103,10 +106,13 @@ bool IoManager::connect() {
   std::cerr << root << std::endl;
 
   // send login
-  if(!sendJson(root)) return false;
+  if (!sendJson(root))
+    return false;
   // receive answer to login
-  if(!receiveJson(recvbuf)) return false;
-  if(!parseJson(&root, recvbuf)) return false;
+  if (!receiveJson(recvbuf))
+    return false;
+  if (!parseJson(&root, recvbuf))
+    return false;
 
   // dump for GUI
   std::cout << root << std::endl;

+ 189 - 171
cli/src/machineiomanager.cpp

@@ -1,12 +1,12 @@
 #include "../include/machineiomanager.hpp"
-#include "../include/commands.hpp"
 #include "../include/base64.hpp"
+#include "../include/commands.hpp"
 
+#include <boost/algorithm/string.hpp>
 #include <fstream>
 #include <iostream>
-#include <vector>
 #include <limits>
-#include <boost/algorithm/string.hpp>
+#include <vector>
 
 #include <readline/readline.h>
 
@@ -23,11 +23,11 @@ void MachineIoManager::run() {
   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)
@@ -35,13 +35,13 @@ void MachineIoManager::run() {
   // .seekg(offset, ios_base::beg, cur, end)
   // .tellg()
   // .close()
-  
+
   while (keep_reading) {
-	  // clean up
+    // clean up
     free(line);
     root = Json::Value();
     recv = Json::Value();
-	  recvbuf.consume(recvbuf.size());
+    recvbuf.consume(recvbuf.size());
 
     // read an input line
     line = readline("");
@@ -50,17 +50,19 @@ void MachineIoManager::run() {
     if (strlen(line) == 0) {
       continue;
     }
-    
-    boost::algorithm::split(tokens, std::string(line), boost::algorithm::is_any_of(" "), boost::algorithm::token_compress_on);
-    
+
+    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] << " ";
+    for (int i = 0; i < tokens.size(); i++) {
+      std::cerr << tokens[i] << " ";
     }
     std::cerr << "]" << std::endl;
-    
-    if(tokens.size() < 1) {
-	    continue;
+
+    if (tokens.size() < 1) {
+      continue;
     }
 
     cmd = getCmdIdFromString(tokens[0].c_str());
@@ -75,170 +77,185 @@ void MachineIoManager::run() {
       root["command"] = "status";
 
       // send request
-	sendJson(root);
+      sendJson(root);
       // receive answer
-	receiveJson(recvbuf);
+      receiveJson(recvbuf);
       //
-	    parseJson(&recv, recvbuf);
+      parseJson(&recv, recvbuf);
 
       // dump received data to stdout for gui to handle
-	    std::cout << recv;
+      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;
+      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;
+      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;
-			    /*
-			    Json::Value arraything = recv["names"];
-			    if(arraything.isArray())
-			    for(Json::Value d : recv["names"])
-			    */
-			    chunks = recv["remaining"].asInt();
-			    root["received"] = rec;
-			    if(!sendJson(root)) {
-				// couldnt ACK
-				break;
-			    }
-			    rec++;
-		    }
-	    }
-	    break;
+      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;
+          /*
+          Json::Value arraything = recv["names"];
+          if(arraything.isArray())
+          for(Json::Value d : recv["names"])
+          */
+          chunks = recv["remaining"].asInt();
+          root["received"] = rec;
+          if (!sendJson(root)) {
+            // couldnt ACK
+            break;
+          }
+          rec++;
+        }
+      }
+      break;
     }
     default: {
       std::cerr << "unknown command " << line << "\n";
@@ -246,9 +263,10 @@ void MachineIoManager::run() {
     }
     }
     std::printf("\n");
-    
+
     // if a command used fs, close it now
-    if(fs.is_open()) fs.close();
+    if (fs.is_open())
+      fs.close();
   }
   free(line);
 

+ 352 - 348
cli/src/useriomanager.cpp

@@ -1,11 +1,11 @@
 #include "../include/useriomanager.hpp"
-#include "../include/commands.hpp"
 #include "../include/base64.hpp"
+#include "../include/commands.hpp"
 
-#include <iostream>
+#include <boost/algorithm/string.hpp>
 #include <fstream>
+#include <iostream>
 #include <vector>
-#include <boost/algorithm/string.hpp>
 
 #include <readline/history.h>
 #include <readline/readline.h>
@@ -19,10 +19,10 @@ void UserIoManager::run() {
 
   bool keep_reading = true;
   COMMANDID cmd;
-  
+
   char *line = NULL;
   std::vector<std::string> tokens;
-  
+
   while (keep_reading) {
     free(line);
 
@@ -33,360 +33,364 @@ void UserIoManager::run() {
     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
+    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;
-	    }
+    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");
   }

+ 5 - 4
gui/src/qmlhandler.cpp

@@ -1,3 +1,4 @@
+#include <QGuiApplication>
 #include <csignal>
 #include <cstdio>
 #include <cstdlib>
@@ -7,7 +8,6 @@
 #include <sys/wait.h>
 #include <thread>
 #include <unistd.h>
-#include <QGuiApplication>
 
 #include "qmlhandler.h"
 #include <boost/asio.hpp>
@@ -31,10 +31,11 @@ void QMLHandler::onExit() {
 void QMLHandler::handleJSON(string buffer) {
   Json::Value root;
   Json::CharReaderBuilder builder;
-  Json::CharReader * reader = builder.newCharReader();
+  Json::CharReader *reader = builder.newCharReader();
   string jsonError;
 
-  bool parsingSuccessful = reader->parse(buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
+  bool parsingSuccessful = reader->parse(
+      buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
 
   if (!parsingSuccessful) {
     return;
@@ -117,7 +118,7 @@ void QMLHandler::onIpPopupEnterIp(QString ip) {
     // Child
     dup2(outpipefd[0], STDIN_FILENO);
     dup2(inpipefd[1], STDOUT_FILENO);
-    //dup2(inpipefd[1], STDERR_FILENO);
+    // dup2(inpipefd[1], STDERR_FILENO);
 
     // ask kernel to deliver SIGTERM in case the parent dies
     prctl(PR_SET_PDEATHSIG, SIGTERM);