Browse Source

move boilerplate to seperate methods

Missingmew 5 years ago
parent
commit
9b3f7990ec
4 changed files with 69 additions and 130 deletions
  1. 13 1
      cli/include/iomanager.hpp
  2. 56 123
      cli/src/iomanager.cpp
  3. 0 1
      cli/src/machineiomanager.cpp
  4. 0 5
      cli/src/useriomanager.cpp

+ 13 - 1
cli/include/iomanager.hpp

@@ -5,6 +5,7 @@
 
 #include <string>
 #include <boost/asio.hpp>
+#include <jsoncpp/json/json.h>
 
 using boost::asio::ip::tcp;
 
@@ -13,8 +14,19 @@ protected:
 	boost::asio::io_service ios;
 	tcp::socket *tcpsock;
 	boost::system::error_code errcode;
-	std::string *ipstring;
+	std::string ipstring;
 	int port;
+
+	      std::string jsonerror;
+
+	      Json::CharReaderBuilder rbuilder;
+	      Json::CharReader *reader;
+	      Json::StreamWriterBuilder wbuilder;
+
+	bool sendJson(Json::Value root);
+	bool receiveJson(boost::asio::streambuf &recvbuf);
+	bool parseJson(Json::Value *root, boost::asio::streambuf &recvbuf);
+	
 public:
 	// Basic constructor
 	IoManager(char *ipcstring);

+ 56 - 123
cli/src/iomanager.cpp

@@ -1,85 +1,80 @@
 #include "../include/iomanager.hpp"
 
 #include <iostream>
-#include <jsoncpp/json/json.h>
 
 using boost::asio::buffer;
 
 IoManager::IoManager(char *ipcstring) {
-  ipstring = new std::string(ipcstring);
+  ipstring = std::string(ipcstring);
   port = 1234;
   tcpsock = new tcp::socket(ios);
+  reader = rbuilder.newCharReader();
+}
+
+bool IoManager::sendJson(Json::Value root) {
+  boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)), errcode);
+  if (errcode) {
+    std::cerr << "couldnt send json data" << std::endl
+              << errcode.message() << std::endl;
+    return false;
+  }
+  return true;
+}
+
+bool IoManager::receiveJson(boost::asio::streambuf &recvbuf) {
+  // use transfer_at_least(1) to avoid deadlock with transfer_all()
+  boost::asio::read(*tcpsock, recvbuf, boost::asio::transfer_at_least(1),
+                    errcode);
+  if (errcode && errcode != boost::asio::error::eof) {
+    std::cerr << "couldnt recieve json data" << std::endl
+              << errcode.message() << std::endl;
+    return false;
+  }
+  return true;
+}
+
+bool IoManager::parseJson(Json::Value *root, boost::asio::streambuf &recvbuf) {
+  const char *recvjson;
+  std::string jsonerror;
+  recvjson = boost::asio::buffer_cast<const char *>(recvbuf.data());
+  if (!reader->parse(recvjson, recvjson + recvbuf.size(), root, &jsonerror)) {
+    std::cerr << "couldnt parse json data" << std::endl
+              << jsonerror << std::endl;
+    return false;
+  }
+  return true;
 }
 
 bool IoManager::connect() {
   boost::asio::streambuf recvbuf;
   tcp::endpoint *ep;
   Json::Value root, checkok;
-  const char *recvjson;
-  std::string jsonerror;
   bool bcheckok;
 
-  Json::CharReaderBuilder rbuilder;
-  const std::unique_ptr<Json::CharReader> reader(rbuilder.newCharReader());
-  Json::StreamWriterBuilder wbuilder;
   // builder["indentation"] = ""; // If you want whitespace-less output
   // const std::string output = Json::writeString(wbuilder, root);
 
   ep =
-      new tcp::endpoint(boost::asio::ip::address::from_string(*ipstring), 1234);
+      new tcp::endpoint(boost::asio::ip::address::from_string(ipstring), 1234);
 
   // establish connection
-  std::cerr << "connecting to " << *ipstring << std::endl;
+  std::cerr << "connecting to " << ipstring << std::endl;
   tcpsock->connect(*ep, errcode);
   if (errcode) {
-    std::cerr << "couldnt connect to " << *ipstring << std::endl
+    std::cerr << "couldnt connect to " << ipstring << std::endl
               << errcode.message() << std::endl;
     return false;
   }
 
-  printf("connect ok\n");
-  fflush(stdout);
-
   // send version check
   root["version"] = VERSION;
 
   std::cout << root << std::endl;
-
-  boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)),
-                     errcode);
-  if (errcode) {
-    std::cerr << "couldnt send version check" << std::endl
-              << errcode.message() << std::endl;
-    return false;
-  }
-
-  printf("send ok\n");
-  fflush(stdout);
-
-  // recieve answer to version check
-  // using transfer_at_least(1) to avoid lockup with transfer_all()
-  boost::asio::read(*tcpsock, recvbuf, boost::asio::transfer_at_least(1),
-                    errcode);
-  //~ boost::asio::read(*tcpsock, recvbuf, errcode);
-  if (errcode && errcode != boost::asio::error::eof) {
-    std::cerr << "couldnt recieve version check" << std::endl
-              << errcode.message() << std::endl;
-    return false;
-  }
-
-  printf("recieve ok\n");
-  fflush(stdout);
-
-  // 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;
-    return false;
-  }
-
-  printf("parse ok\n");
-  fflush(stdout);
+  
+  if(!sendJson(root)) return false;
+  // receive answer
+  if(!receiveJson(recvbuf)) return false;
+  if(!parseJson(&root, recvbuf)) return false;
 
   // remove processed data from recvbuf
   recvbuf.consume(recvbuf.size());
@@ -109,41 +104,11 @@ bool IoManager::connect() {
 
   std::cout << root << std::endl;
 
-  boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)),
-                     errcode);
-  if (errcode) {
-    std::cerr << "couldnt send login" << std::endl
-              << errcode.message() << std::endl;
-    return false;
-  }
-
-  printf("send ok\n");
-  fflush(stdout);
-
-  // recieve answer to version check
-  // using transfer_at_least(1) to avoid lockup with transfer_all()
-  boost::asio::read(*tcpsock, recvbuf, boost::asio::transfer_at_least(1),
-                    errcode);
-  //~ boost::asio::read(*tcpsock, recvbuf, errcode);
-  if (errcode && errcode != boost::asio::error::eof) {
-    std::cerr << "couldnt recieve login response" << std::endl
-              << errcode.message() << std::endl;
-    return false;
-  }
-
-  printf("recieve ok\n");
-  fflush(stdout);
-
-  // 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;
-    return false;
-  }
-
-  printf("parse ok\n");
-  fflush(stdout);
+  // send login
+  if(!sendJson(root)) return false;
+  // receive answer to login
+  if(!receiveJson(recvbuf)) return false;
+  if(!parseJson(&root, recvbuf)) return false;
 
   // remove processed data from recvbuf
   recvbuf.consume(recvbuf.size());
@@ -166,10 +131,6 @@ IoManager::~IoManager() {
   const char *recvjson;
   std::string jsonerror;
 
-  Json::CharReaderBuilder rbuilder;
-  const std::unique_ptr<Json::CharReader> reader(rbuilder.newCharReader());
-  Json::StreamWriterBuilder wbuilder;
-
   // TODO remove hardcoded login
   root = Json::Value();
   // send version check
@@ -178,39 +139,12 @@ IoManager::~IoManager() {
 
   std::cout << root << std::endl;
 
-  boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, root)),
-                     errcode);
-  if (errcode) {
-    std::cerr << "couldnt send close" << std::endl
-              << errcode.message() << std::endl;
-  }
-
-  printf("send ok\n");
-  fflush(stdout);
-
-  // recieve answer to version check
-  // using transfer_at_least(1) to avoid lockup with transfer_all()
-  boost::asio::read(*tcpsock, recvbuf, boost::asio::transfer_at_least(1),
-                    errcode);
-  //~ boost::asio::read(*tcpsock, recvbuf, errcode);
-  if (errcode && errcode != boost::asio::error::eof) {
-    std::cerr << "couldnt recieve close response" << std::endl
-              << errcode.message() << std::endl;
-  }
-
-  printf("recieve ok\n");
-  fflush(stdout);
-
-  // 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;
-  }
-
-  printf("parse ok\n");
-  fflush(stdout);
-
+  // send disconnect
+  sendJson(root);
+  // recieve answer to disconnect
+  receiveJson(recvbuf);
+  parseJson(&root, recvbuf);
+  
   // remove processed data from recvbuf
   recvbuf.consume(recvbuf.size());
 
@@ -218,7 +152,6 @@ IoManager::~IoManager() {
   /* */
 
   tcpsock->close();
-
-  delete ipstring;
   delete tcpsock;
+  delete reader;
 }

+ 0 - 1
cli/src/machineiomanager.cpp

@@ -2,7 +2,6 @@
 #include "../include/commands.hpp"
 
 #include <iostream>
-#include <jsoncpp/json/json.h>
 
 #include <readline/readline.h>
 

+ 0 - 5
cli/src/useriomanager.cpp

@@ -37,11 +37,6 @@ void UserIoManager::run() {
       boost::asio::streambuf recvbuf;
       Json::Value root, checkok;
       const char *recvjson;
-      std::string jsonerror;
-
-      Json::CharReaderBuilder rbuilder;
-      const std::unique_ptr<Json::CharReader> reader(rbuilder.newCharReader());
-      Json::StreamWriterBuilder wbuilder;
 
       // ask for status
       root["command"] = "status";