Browse Source

added signup command

Denys 4 years ago
parent
commit
499dff0760
5 changed files with 73 additions and 17 deletions
  1. 7 2
      cli/include/cmdman.h
  2. 3 2
      cli/include/ioman.h
  3. 56 8
      cli/src/cmdman.cpp
  4. 3 2
      cli/src/machineioman.cpp
  5. 4 3
      cli/src/userioman.cpp

+ 7 - 2
cli/include/cmdman.h

@@ -96,7 +96,7 @@ private:
    * State used to internally format received json to allow easy handling using
    * handlemap
    */
-  bool dologin, doversion;
+  bool dologin, dosignup, doversion;
 
   /**
    * Help strings and method prototypes for commands to be used by a user
@@ -114,13 +114,17 @@ private:
   CmdRet cmdGet(vector<string> args);
   const string descList = "list files available on server";
   CmdRet cmdList(vector<string> args);
+  
+  const string descLogin = "login to the server";
+  CmdRet cmdLogin(vector<string> args);
+  const string descSignup = "sign up and login to the server";
+  CmdRet cmdSignup(vector<string> args);
 
   /**
    * Method prototypes for commands used internally
    */
   /* internal execute commands */
   CmdRet cmdVersion(vector<string> args);
-  CmdRet cmdLogin(vector<string> args);
   CmdRet cmdPutdata(vector<string> args);
   CmdRet cmdGetdata(vector<string> args);
   CmdRet cmdListdata(vector<string> args);
@@ -139,6 +143,7 @@ private:
   CmdRet handleListdata(Json::Value);
   CmdRet handleVersion(Json::Value);
   CmdRet handleLogin(Json::Value);
+  CmdRet handleSignup(Json::Value);
 };
 
 #endif

+ 3 - 2
cli/include/ioman.h

@@ -122,8 +122,9 @@ protected:
    */
   virtual void printWelcomeMessage() = 0;
   virtual std::string getCmdPrompt() = 0;
-  virtual std::string getUserPrompt() = 0;
-  virtual std::string getPassPrompt() = 0;
+  // following prompts currently not in use because login happens by entering command
+//~  virtual std::string getUserPrompt() = 0;
+//~  virtual std::string getPassPrompt() = 0;
 
 public:
   /**

+ 56 - 8
cli/src/cmdman.cpp

@@ -15,6 +15,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
   reader = rbuilder.newCharReader();
 
   dologin = false;
+  dosignup = false;
   doversion = false;
 
   /* initialize execute command map */
@@ -26,6 +27,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
   execmap["list"] = &CmdMan::cmdList;
   execmap["version"] = &CmdMan::cmdVersion;
   execmap["login"] = &CmdMan::cmdLogin;
+  execmap["signup"] = &CmdMan::cmdSignup;
   execmap["putdata"] = &CmdMan::cmdPutdata;
   execmap["getdata"] = &CmdMan::cmdGetdata;
 
@@ -47,6 +49,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
   handlemap["list"] = &CmdMan::handleList;
   handlemap["version"] = &CmdMan::handleVersion;
   handlemap["login"] = &CmdMan::handleLogin;
+  handlemap["signup"] = &CmdMan::handleSignup;
 
   debugprintfunc = dpf;
 }
@@ -241,32 +244,52 @@ CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
   return (this->*(execmap[cmd]))(args);
 }
 
-/* internal commands */
-CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
+/* login and signup commands */
+CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
   CmdRet retval;
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
-  root["version"] = protocolVersion;
+  root["user"] = args[0];
+  root["pass"] = args[1];
+  root["login"] = true;
+  root["cancel"] = false;
   retval.type = send;
   retval.msg = root;
 
-  doversion = true;
+  // dologin already set by handleVersion, so no need here
 
   return retval;
 }
 
-CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
+CmdMan::CmdRet CmdMan::cmdSignup(vector<string> args) {
   CmdRet retval;
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
   root["user"] = args[0];
   root["pass"] = args[1];
-  root["login"] = true;
+  root["login"] = false;
   root["cancel"] = false;
   retval.type = send;
   retval.msg = root;
 
-  dologin = true;
+  // set dosignin to differentiate from only logging in
+  // note: dologin was also set (by handleVersion)
+  dosignup = true;
+
+  return retval;
+}
+
+
+/* internal commands */
+CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
+  CmdRet retval;
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
+  Json::Value root;
+  root["version"] = protocolVersion;
+  retval.type = send;
+  retval.msg = root;
+
+  doversion = true;
 
   return retval;
 }
@@ -277,6 +300,8 @@ CmdMan::CmdRet CmdMan::handle(Json::Value root) {
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   if (doversion)
     root["command"] = "version";
+  else if (dosignup) 
+    root["command"] = "signup";
   else if (dologin)
     root["command"] = "login";
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " using json\n" +
@@ -288,7 +313,7 @@ CmdMan::CmdRet CmdMan::handle(Json::Value root) {
   if (it == handlemap.end()) {
 	  retval.type = error;
 	  output["command"] = "error";
-	  output["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString(); + "\".\nensure code is implemented.";
+	  output["error"] = string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString() + "\".\nEnsure code is implemented.";
 	  retval.msg = output;
 	  return retval;
   }
@@ -572,3 +597,26 @@ CmdMan::CmdRet CmdMan::handleLogin(Json::Value root) {
 
   return retval;
 }
+
+CmdMan::CmdRet CmdMan::handleSignup(Json::Value root) {
+  CmdRet retval;
+  Json::Value output; // LOCALOUTPUT
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
+
+  output["command"] = "signup";
+
+  if (!root["accept"].asBool()) {
+    retval.type = error;
+    output["error"] = root["error"].asString();
+    output["accept"] = false;
+  } else {
+    retval.type = print | seton;
+    output["error"] = "";
+    output["accept"] = true;
+    dologin = false;
+	dosignup = false;
+  }
+  retval.msg = output;
+
+  return retval;
+}

+ 3 - 2
cli/src/machineioman.cpp

@@ -33,5 +33,6 @@ void MachineIoMan::printWelcomeMessage() {
 
 std::string MachineIoMan::getCmdPrompt() { return ""; }
 
-std::string MachineIoMan::getUserPrompt() { return ""; }
-std::string MachineIoMan::getPassPrompt() { return ""; }
+// currently not in use:
+//~ std::string MachineIoMan::getUserPrompt() { return ""; }
+//~ std::string MachineIoMan::getPassPrompt() { return ""; }

+ 4 - 3
cli/src/userioman.cpp

@@ -65,13 +65,14 @@ void UserIoMan::printMessage(std::string msg, OutMsgType type) {
 }
 
 void UserIoMan::printWelcomeMessage() {
-  std::cout << "please enter user and password" << std::endl;
+  std::cout << "please login by entering \"login <username> <password>\" \nor sign up and log in with \"signup <username> <password>\"" << std::endl;
 }
 
 std::string UserIoMan::getCmdPrompt() { return "ccats> "; }
 
-std::string UserIoMan::getUserPrompt() { return "User: "; }
-std::string UserIoMan::getPassPrompt() { return "Pass: "; }
+// currently not in use:
+//~ std::string UserIoMan::getUserPrompt() { return "User: "; }
+//~ std::string UserIoMan::getPassPrompt() { return "Pass: "; }
 
 void UserIoMan::printJson(Json::Value root) {
   map<string, void (UserIoMan::*)(Json::Value)>::iterator it =