Ver Fonte

make cmdman use printing from ioman, rework internal return value structure, use separate field for possible next command to be executed

Missingmew há 5 anos atrás
pai
commit
1e1de39366
6 ficheiros alterados com 74 adições e 58 exclusões
  1. 5 1
      cli/include/cmdman.h
  2. 41 37
      cli/src/cmdman.cpp
  3. 8 7
      cli/src/fileman.cpp
  4. 10 4
      cli/src/ioman.cpp
  5. 1 1
      cli/src/main.cpp
  6. 9 8
      cli/src/userioman.cpp

+ 5 - 1
cli/include/cmdman.h

@@ -40,15 +40,17 @@ public:
    * Response to user or command input
    *
    * msg is to be handled depending on type
+   * if nextcommand isnt an empty string it should be handled;
    */
   struct CmdRet {
     unsigned int type;
+    string nextcommand;
     Json::Value msg;
   };
   /**
    * Constructor and destructor
    */
-  CmdMan(FileMan &fm);
+  CmdMan(FileMan &fm, void (*dpf)(string));
   ~CmdMan();
 
   /**
@@ -77,6 +79,8 @@ private:
    */
   FileMan &fileman;
 
+  void (*debugprintfunc)(string);
+
   /**
    * Maps containing pointers to the appropriate member functions for executing
    * or handling commands

+ 41 - 37
cli/src/cmdman.cpp

@@ -3,10 +3,11 @@
 
 #include <iostream>
 
+#define DEBUGPRINT(x) debugprintfunc(x)
 //~ #define DEBUGPRINT(x) std::cerr << x
-#define DEBUGPRINT(x)
+//~ #define DEBUGPRINT(x)
 
-CmdMan::CmdMan(FileMan &fm) : fileman(fm) {
+CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
 
   /* setup json stuff */
   Json::CharReaderBuilder rbuilder;
@@ -46,6 +47,8 @@ CmdMan::CmdMan(FileMan &fm) : fileman(fm) {
   handlemap["list"] = &CmdMan::handleList;
   handlemap["version"] = &CmdMan::handleVersion;
   handlemap["login"] = &CmdMan::handleLogin;
+
+  debugprintfunc = dpf;
 }
 
 CmdMan::~CmdMan() { delete reader; }
@@ -53,7 +56,7 @@ CmdMan::~CmdMan() { delete reader; }
 CmdMan::CmdRet CmdMan::cmdHelp(vector<string> args) {
   CmdRet retval;
   Json::Value root, arr;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   map<string, string>::iterator it;
   root["command"] = "help";
   for (it = helpmap.begin(); it != helpmap.end(); it++) {
@@ -67,7 +70,7 @@ CmdMan::CmdRet CmdMan::cmdHelp(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdStatus(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
   root["command"] = "status";
   retval.type = send;
@@ -78,7 +81,7 @@ CmdMan::CmdRet CmdMan::cmdStatus(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdDisconnect(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
   root["command"] = "close";
   retval.type = send;
@@ -89,7 +92,7 @@ CmdMan::CmdRet CmdMan::cmdDisconnect(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
 
   bool opened = fileman.openPut(args[0]);
@@ -111,7 +114,7 @@ CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdPutdata(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
 
   root["command"] = "putdata";
@@ -129,7 +132,7 @@ CmdMan::CmdRet CmdMan::cmdPutdata(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
 
   bool opened = fileman.openGet(args[0]);
@@ -149,7 +152,7 @@ CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdGetdata(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
 
   root["command"] = "getdata";
@@ -164,7 +167,7 @@ CmdMan::CmdRet CmdMan::cmdGetdata(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdList(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
 
   bool opened = fileman.openList();
@@ -184,7 +187,7 @@ CmdMan::CmdRet CmdMan::cmdList(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdListdata(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
   root["command"] = "listdata";
   root["chunk"] = fileman.getListRemainingChunks();
@@ -196,12 +199,12 @@ CmdMan::CmdRet CmdMan::cmdListdata(vector<string> args) {
 }
 
 CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " using command \"" << cmd
-                                 << "\" with arguments [ ");
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using command \"" + cmd +
+             "\" with arguments [ ");
   for (string s : args)
-    DEBUGPRINT(s << " ");
-  DEBUGPRINT("]" << std::endl);
+    DEBUGPRINT(s + " ");
+  DEBUGPRINT("]\n");
   map<string, CmdRet (CmdMan::*)(vector<string>)>::iterator it =
       execmap.find(cmd);
   string retmsg;
@@ -215,7 +218,7 @@ CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
 /* internal commands */
 CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
   root["version"] = protocolVersion;
   retval.type = send;
@@ -228,7 +231,7 @@ CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
 
 CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
   root["user"] = args[0];
   root["pass"] = args[1];
@@ -253,13 +256,13 @@ CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
 */
 
 CmdMan::CmdRet CmdMan::handle(Json::Value root) {
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   if (doversion)
     root["command"] = "version";
   else if (dologin)
     root["command"] = "login";
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " using json" << std::endl
-                                 << root << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using json\n" +
+             Json::writeString(wbuilder, root) + "\n");
   string retmsg;
   map<string, CmdRet (CmdMan::*)(Json::Value)>::iterator it =
       handlemap.find(root["command"].asString());
@@ -273,7 +276,7 @@ CmdMan::CmdRet CmdMan::handle(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handleStatus(Json::Value root) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   retval.type = print;
   retval.msg = root;
 
@@ -282,7 +285,7 @@ CmdMan::CmdRet CmdMan::handleStatus(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handleClose(Json::Value root) {
   CmdRet retval;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   retval.type = close;
   retval.msg = root;
 
@@ -295,7 +298,7 @@ CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
   output["command"] = "put";
   output["file"] = fileman.getPutName();
   output["accept"] = false;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 
   if (!root["accept"].asBool()) {
     retval.type = error;
@@ -310,7 +313,7 @@ CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
     output["accept"] = true;
     output["error"] = "";
     retval.type = print | send;
-    output["nextcommand"] = "putdata";
+    retval.nextcommand = "putdata";
   }
   retval.msg = output;
 
@@ -320,7 +323,7 @@ CmdMan::CmdRet CmdMan::handlePut(Json::Value root) {
 CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
   CmdRet retval;
   Json::Value output;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   output["command"] = "putdata";
   output["file"] = fileman.getPutName();
   output["speed"] = 0.0f; // TODO
@@ -357,7 +360,7 @@ CmdMan::CmdRet CmdMan::handlePutdata(Json::Value root) {
       fileman.closePut();
     } else {
       retval.type = print | send;
-      output["nextcommand"] = "putdata";
+      retval.nextcommand = "putdata";
     }
   }
   retval.msg = output;
@@ -370,7 +373,7 @@ CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
   output["command"] = "get";
   output["file"] = fileman.getGetName();
   output["accept"] = false;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 
   if (!root["accept"].asBool()) {
     retval.type = error;
@@ -384,7 +387,7 @@ CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
     output["accept"] = true;
     output["error"] = "";
     retval.type = print | send;
-    output["nextcommand"] = "getdata";
+    retval.nextcommand = "getdata";
   }
   retval.msg = output;
 
@@ -394,7 +397,7 @@ CmdMan::CmdRet CmdMan::handleGet(Json::Value root) {
 CmdMan::CmdRet CmdMan::handleGetdata(Json::Value root) {
   CmdRet retval;
   Json::Value output;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   output["command"] = "getdata";
   output["file"] = fileman.getGetName();
   output["speed"] = 0.0f; // TODO
@@ -429,7 +432,7 @@ CmdMan::CmdRet CmdMan::handleGetdata(Json::Value root) {
       fileman.closeGet();
     } else {
       retval.type = print | send;
-      output["nextcommand"] = "getdata";
+      retval.nextcommand = "getdata";
     }
   }
   retval.msg = output;
@@ -439,7 +442,7 @@ CmdMan::CmdRet CmdMan::handleGetdata(Json::Value root) {
 CmdMan::CmdRet CmdMan::handleList(Json::Value root) {
   CmdRet retval;
   Json::Value output; // LOCALOUTPUT
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 
   output["command"] = "list";
   output["names"] = "";
@@ -452,7 +455,8 @@ CmdMan::CmdRet CmdMan::handleList(Json::Value root) {
   } else {
     fileman.setListChunks(root["chunks"].asInt());
     retval.type = send;
-    output["nextcommand"] = "listdata";
+    output["accept"] = true;
+    retval.nextcommand = "listdata";
   }
 
   retval.msg = output;
@@ -462,7 +466,7 @@ CmdMan::CmdRet CmdMan::handleList(Json::Value root) {
 CmdMan::CmdRet CmdMan::handleListdata(Json::Value root) {
   CmdRet retval;
   Json::Value output, arr;
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   vector<string> toadd;
 
   output["command"] = "list";
@@ -496,7 +500,7 @@ CmdMan::CmdRet CmdMan::handleListdata(Json::Value root) {
       fileman.closeList();
     } else {
       retval.type = print | send;
-      output["nextcommand"] = "listdata";
+      retval.nextcommand = "listdata";
     }
   }
   retval.msg = output;
@@ -506,7 +510,7 @@ CmdMan::CmdRet CmdMan::handleListdata(Json::Value root) {
 CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
   CmdRet retval;
   Json::Value output; // LOCALOUTPUT
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 
   output["command"] = "version";
   output["serverversion"] = root["version"].asString();
@@ -528,7 +532,7 @@ CmdMan::CmdRet CmdMan::handleVersion(Json::Value root) {
 CmdMan::CmdRet CmdMan::handleLogin(Json::Value root) {
   CmdRet retval;
   Json::Value output; // LOCALOUTPUT
-  DEBUGPRINT(__PRETTY_FUNCTION__ << " begin" << std::endl);
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
 
   output["command"] = "login";
 

+ 8 - 7
cli/src/fileman.cpp

@@ -151,13 +151,14 @@ std::string FileMan::pathToFilename(std::string path) {
     if (currentIndex > 0 && path[currentIndex - 1] == '\\')
       continue;
 
-/*  // check unnecessary, because error occurs when trying to open file anyways
-
-    // check if the "/" is at the end of path name
-    if (currentIndex + 1 == path.length()) {
-      // ERROR: INVALID FILENAME, BECAUSE ENDS WITH "/"
-    }
-*/
+    /*  // check unnecessary, because error occurs when trying to open file
+       anyways
+
+        // check if the "/" is at the end of path name
+        if (currentIndex + 1 == path.length()) {
+          // ERROR: INVALID FILENAME, BECAUSE ENDS WITH "/"
+        }
+    */
     // otherwise we found a valid "/"
     lastFoundIndex = currentIndex;
   }

+ 10 - 4
cli/src/ioman.cpp

@@ -23,7 +23,11 @@ using boost::asio::ip::tcp;
 /* this will be provided by main.cpp for the readline callback */
 extern IoMan *gIOMAN;
 
-IoMan::IoMan(char *ipcstring) : cmdman(fileman) {
+void ioman_externalDebugPrint(string msg) {
+  gIOMAN->printMessage(msg, gIOMAN->OutMsgType::debug);
+}
+
+IoMan::IoMan(char *ipcstring) : cmdman(fileman, &ioman_externalDebugPrint) {
   ipstring = std::string(ipcstring);
   port = 1234;
   tcpsock = new tcp::socket(ios);
@@ -455,7 +459,9 @@ void IoMan::responseMain() {
         printMessage(Json::writeString(wbuilder, cmdret.msg), normal);
       }
       if (cmdret.type & CmdMan::rettype::send) {
-        toput.push_back(cmdret.msg["nextcommand"].asString());
+        if (cmdret.nextcommand.size()) {
+          toput.push_back(cmdret.nextcommand);
+        }
       }
     }
 
@@ -528,11 +534,11 @@ void IoMan::run() {
 
   while (!user) {
     user = readline(getUserPrompt().c_str());
-    printMessage("Using user: " + string(user), error);
+    printMessage("Using user: " + string(user), debug);
   }
   while (!pass) {
     pass = readline(getPassPrompt().c_str());
-    printMessage("Using pass: " + string(pass), error);
+    printMessage("Using pass: " + string(pass), debug);
   }
 
   printMessage("IoMan::run() login", debug);

+ 1 - 1
cli/src/main.cpp

@@ -63,8 +63,8 @@ int main(int argc, char **argv) {
   } else {
     ioman = new UserIoMan(argv[1]);
   }
+  gIOMAN = ioman;
   if (ioman->init()) {
-    gIOMAN = ioman;
     ioman->run();
   }
   delete ioman;

+ 9 - 8
cli/src/userioman.cpp

@@ -37,24 +37,25 @@ void UserIoMan::printMessage(std::string msg, OutMsgType type) {
   Json::Value root;
   msgmutex.lock();
   switch (type) {
-  case normal: {
+  case normal:
+  case error: {
     // this should never happen outside of development
     if (!reader->parse(msg.c_str(), msg.c_str() + msg.size(), &root,
                        &jsonerror)) {
       printMessage(string(__PRETTY_FUNCTION__) +
                        " couldnt parse json data: " + jsonerror,
-                   error);
+                   debug);
     } else {
       printJson(root);
     }
     break;
   }
-  case error: {
-    std::cout << msg << std::endl;
-    break;
-  }
+  //~ case error: {
+  //~ std::cout << msg << std::endl;
+  //~ break;
+  //~ }
   case debug: {
-    //~ std::cerr << msg << std::endl;
+    std::cerr << msg << std::endl;
     break;
   }
   }
@@ -79,7 +80,7 @@ void UserIoMan::printJson(Json::Value root) {
     printMessage(string(__PRETTY_FUNCTION__) + " unknown command \"" +
                      root["command"].asString() +
                      "\".\nensure code is implemented.",
-                 error);
+                 debug);
     return;
   }
   (this->*(printmap[root["command"].asString()]))(root);