Sfoglia il codice sorgente

Merge branch 'us-19-2-head-command-cli' into 'develop'

US19.2 head command (CLI)

See merge request tobias.wach/ccats!34
Sander, Paul 4 anni fa
parent
commit
583e5e2f17
4 ha cambiato i file con 58 aggiunte e 0 eliminazioni
  1. 4 0
      cli/include/cmdman.h
  2. 1 0
      cli/include/userioman.h
  3. 43 0
      cli/src/cmdman.cpp
  4. 10 0
      cli/src/userioman.cpp

+ 4 - 0
cli/include/cmdman.h

@@ -115,6 +115,9 @@ private:
   CmdRet cmdGet(vector<string> args);
   const string descList = "list files available on server";
   CmdRet cmdList(vector<string> args);
+  const string descHead =
+      "request the first four bytes of a file from the server";
+  CmdRet cmdHead(vector<string> args);
 
   const string descLogin = "login to the server";
   CmdRet cmdLogin(vector<string> args);
@@ -145,6 +148,7 @@ private:
   CmdRet handleVersion(Json::Value);
   CmdRet handleLogin(Json::Value);
   CmdRet handleSignup(Json::Value);
+  CmdRet handleHead(Json::Value);
 };
 
 #endif

+ 1 - 0
cli/include/userioman.h

@@ -47,6 +47,7 @@ private:
   void printPutdata(Json::Value root);
   void printGetdata(Json::Value root);
   void printListdata(Json::Value root);
+  void printHead(Json::Value root);
 
   /**
    * Mutex for synchronized message output

+ 43 - 0
cli/src/cmdman.cpp

@@ -32,6 +32,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
   execmap["putdata"] = &CmdMan::cmdPutdata;
   execmap["getdata"] = &CmdMan::cmdGetdata;
   execmap["listdata"] = &CmdMan::cmdListdata;
+  execmap["head"] = &CmdMan::cmdHead;
 
   /* initialize description map */
   helpmap["help"] = descHelp;
@@ -40,6 +41,9 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
   helpmap["put"] = descPut;
   helpmap["get"] = descGet;
   helpmap["list"] = descList;
+  helpmap["head"] = descHead;
+  helpmap["login"] = descLogin;
+  helpmap["signup"] = descSignup;
 
   /* initialize handle command map */
   handlemap["status"] = &CmdMan::handleStatus;
@@ -53,6 +57,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
   handlemap["login"] = &CmdMan::handleLogin;
   handlemap["signup"] = &CmdMan::handleSignup;
   handlemap["listdata"] = &CmdMan::handleListdata;
+  handlemap["head"] = &CmdMan::handleHead;
 
   debugprintfunc = dpf;
 }
@@ -225,6 +230,25 @@ CmdMan::CmdRet CmdMan::cmdListdata(vector<string> args) {
   return retval;
 }
 
+CmdMan::CmdRet CmdMan::cmdHead(vector<string> args) {
+  CmdRet retval;
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
+  Json::Value root;
+  root["command"] = "head";
+
+  if (args.size() < 1) {
+    retval.type = error;
+    root["accept"] = false;
+    root["error"] = "not enough arguments, at least 1 argument required";
+  } else {
+    root["file"] = args[0];
+    retval.type = send;
+  }
+  retval.msg = root;
+
+  return retval;
+}
+
 CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using command \"" + cmd +
@@ -676,3 +700,22 @@ CmdMan::CmdRet CmdMan::handleSignup(Json::Value root) {
 
   return retval;
 }
+
+CmdMan::CmdRet CmdMan::handleHead(Json::Value root) {
+  CmdRet retval;
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
+
+  if (!root["accept"].asBool()) {
+    Json::Value output;
+    output["command"] = "head";
+    output["file"] = root["file"];
+    output["error"] = "Server reports: " + root["error"].asString();
+    retval.type = error;
+    retval.msg = output;
+  } else {
+    retval.type = print;
+    retval.msg = root;
+  }
+
+  return retval;
+}

+ 10 - 0
cli/src/userioman.cpp

@@ -31,6 +31,7 @@ UserIoMan::UserIoMan(char *ipcstring) : IoMan(ipcstring) {
   printmap["signup"] = &UserIoMan::printSignup;
   printmap["putdata"] = &UserIoMan::printPutdata;
   printmap["getdata"] = &UserIoMan::printGetdata;
+  printmap["head"] = &UserIoMan::printHead;
 }
 
 UserIoMan::~UserIoMan() { delete reader; }
@@ -190,3 +191,12 @@ void UserIoMan::printPutdata(Json::Value root) {}
 void UserIoMan::printGetdata(Json::Value root) {}
 
 void UserIoMan::printListdata(Json::Value root) {}
+
+void UserIoMan::printHead(Json::Value root) {
+  if (!root["accept"].asBool())
+    std::cout << "Request of the first four bytes failed, server reports: "
+              << root["error"].asString() << std::endl;
+  else
+    std::cout << "First four bytes of file " << root["file"].asString()
+              << " are: " << root["data"].asString() << std::endl;
+}