Browse Source

externalize commands, switch useriomanager to use commands

Missingmew 5 years ago
parent
commit
e52d9b5452

+ 1 - 1
cli/CMakeLists.txt

@@ -5,7 +5,7 @@ set (CMAKE_BUILD_TYPE debug)
 
 project(ccats-cli)
 
-add_executable(ccats-cli src/main.cpp src/iomanager.cpp src/machineiomanager.cpp src/useriomanager.cpp)
+add_executable(ccats-cli src/main.cpp src/iomanager.cpp src/machineiomanager.cpp src/useriomanager.cpp src/commands.cpp)
 
 # use pkg-config to find readline as it doesnt provide cmake files
 find_package(PkgConfig REQUIRED)

+ 36 - 0
cli/include/commands.hpp

@@ -0,0 +1,36 @@
+#ifndef COMMANDS_HPP
+#define COMMANDS_HPP
+
+#include <cstring>
+#include <cctype>
+
+#define COMMANDLEN 10
+#define sizeofarr(a) (sizeof(a) / sizeof(a[0]))
+
+typedef enum {
+  CMD_HELP,
+  CMD_CONNECT,
+  CMD_DISCONNECT,
+  CMD_PUT,
+  CMD_REMOVE,
+  CMD_GET,
+  CMD_STATUS,
+  CMD_SETUP,
+  CMD_LOG,
+  CMD_UNKNOWN
+} COMMANDID;
+
+typedef struct {
+  COMMANDID cmd;
+  const char *name;
+  const char *desc;
+} CMD;
+
+extern CMD commands[];
+
+COMMANDID getCmdIdFromString(const char *str);
+const char *getCmdStringFromId(COMMANDID id);
+// TODO
+void printCmds(void);
+
+#endif

+ 6 - 0
cli/include/global.hpp

@@ -0,0 +1,6 @@
+#ifndef GLOBAL_HPP
+#define GLOBAL_HPP
+
+#define VERSION "0.1"
+
+#endif

+ 2 - 0
cli/include/iomanager.hpp

@@ -1,6 +1,8 @@
 #ifndef IOMANAGER_HPP
 #define IOMANAGER_HPP
 
+#include "global.hpp"
+
 #include <string>
 #include <boost/asio.hpp>
 

+ 42 - 0
cli/src/commands.cpp

@@ -0,0 +1,42 @@
+#include "../include/commands.hpp"
+
+CMD commands[]{
+	{CMD_HELP	, "help", "show help"},
+        {CMD_STATUS	, "status", "query status of IP"},
+	{CMD_DISCONNECT	, "disconnect", "disconnect from IP"},
+	
+	/* TODO
+        {CMD_PUT	, "put", "upload file to IP and add to queue"},
+        {CMD_REMOVE	, "remove", "remove file from IP and queue (stops xfer if required)"},
+        {CMD_GET	, "get", "retrieve file from IP"},
+        {CMD_SETUP	, "setup", "configure server at IP"},
+        {CMD_LOG	, "log", "show log from IP"}
+	*/
+};
+
+COMMANDID getCmdIdFromString(const char *str) {
+  COMMANDID ret = CMD_UNKNOWN;
+  char temp[COMMANDLEN + 1] = {0};
+  if (std::strlen(str) > COMMANDLEN)
+    return ret;
+  for (int i = 0; i < COMMANDLEN; i++)
+      temp[i] = std::tolower(str[i]);
+
+  for (int i = 0; i < sizeofarr(commands); i++) {
+    if (!std::strncmp(temp, commands[i].name, COMMANDLEN))
+        ret = commands[i].cmd;
+  }
+  return ret;
+}
+
+const char *getCmdStringFromId(COMMANDID id) {
+	const char *ret = NULL;
+	for(int i = 0; i < sizeofarr(commands); i++) {
+		if(commands[i].cmd == id) ret = commands[i].name;
+	}
+	return ret;
+}
+
+void printCmds(void) {
+	// TODO
+}

+ 3 - 3
cli/src/iomanager.cpp

@@ -1,4 +1,4 @@
-#include <iomanager.hpp>
+#include "../include/iomanager.hpp"
 
 #include <iostream>
 #include <jsoncpp/json/json.h>
@@ -40,7 +40,7 @@ bool IoManager::connect() {
 	
 	// send version check
 	// TODO make client version global
-	root["version"] = "0.1";
+	root["version"] = VERSION;
 	
 	std::cout << root << std::endl;
 	
@@ -87,7 +87,7 @@ bool IoManager::connect() {
 	checkok = root["accept"];
 	if(checkok.type() != Json::ValueType::booleanValue || !checkok) {
 		// TODO make client version global
-		std::cerr << "version check failed. client version is " << "0.1" << std::endl
+		std::cerr << "version check failed. client version is " << VERSION << std::endl
 			  << "server reports version " << root["version"] << std::endl;
 		return false;
 	}

+ 1 - 1
cli/src/machineiomanager.cpp

@@ -1,4 +1,4 @@
-#include <machineiomanager.hpp>
+#include "../include/machineiomanager.hpp"
 
 #include <iostream>
 #include <jsoncpp/json/json.h>

+ 10 - 63
cli/src/main.cpp

@@ -1,72 +1,14 @@
-#include <machineiomanager.hpp>
-#include <useriomanager.hpp>
+#include "../include/machineiomanager.hpp"
+#include "../include/useriomanager.hpp"
+#include "../include/commands.hpp"
 
 #include <boost/program_options.hpp>
 #include <boost/asio.hpp>
-#include <cctype>
 #include <iostream>
 #include <string>
 
-#include <readline/readline.h>
-
-#define COMMANDLEN 10
-#define sizeofarr(a) (sizeof(a) / sizeof(a[0]))
-
 namespace bpo = boost::program_options;
 
-typedef enum {
-  CMD_HELP,
-  CMD_CONNECT,
-  CMD_DISCONNECT,
-  CMD_PUT,
-  CMD_REMOVE,
-  CMD_GET,
-  CMD_QUERY,
-  CMD_SETUP,
-  CMD_LOG,
-  CMD_UNKNOWN
-} COMMANDTYPES;
-
-typedef struct {
-  COMMANDTYPES cmd;
-  const char *name;
-  const char *desc;
-} CMD;
-
-CMD commands[]{{CMD_HELP, "help", "show help"},
-               {CMD_CONNECT, "connect", "connect to IP"},
-               {CMD_DISCONNECT, "disconnect", "disconnect from IP"},
-               {CMD_PUT, "put", "upload file to IP and add to queue"},
-               {CMD_REMOVE, "remove",
-                "remove file from IP and queue (stops xfer if required)"},
-               {CMD_GET, "get", "retrieve file from IP"},
-               {CMD_QUERY, "query", "query status of IP"},
-               {CMD_SETUP, "setup", "configure server at IP"},
-               {CMD_LOG, "log", "show log from IP"}};
-
-COMMANDTYPES getCommand(char *str, unsigned int isshort) {
-  COMMANDTYPES ret = CMD_UNKNOWN;
-  char temp[COMMANDLEN + 1] = {0};
-  if (strlen(str) > COMMANDLEN)
-    return ret;
-  if (isshort)
-    temp[0] = std::tolower(str[0]);
-  else
-    for (int i = 0; i < COMMANDLEN; i++)
-      temp[i] = std::tolower(str[i]);
-
-  for (int i = 0; i < sizeofarr(commands); i++) {
-    if (isshort) {
-      if (tolower(str[0]) == commands[i].name[0])
-        ret = commands[i].cmd;
-    } else {
-      if (!strncmp(temp, commands[i].name, COMMANDLEN))
-        ret = commands[i].cmd;
-    }
-  }
-  return ret;
-}
-
 void show_help(char *exec) {
   std::printf("ccats command line interface\n");
   std::printf("usage: %s IP [Options]\n", exec);
@@ -118,9 +60,14 @@ int main(int argc, char **argv) {
     std::fprintf(stderr, "%s\n", ex.what());
   }
   std::printf("ip %s machine mode is %d file is %s\n", argv[1], machine, file?file:"");
-  //~ std::printf("read line %s\n", readline("fancprompt "));
   
-  ioman = new UserIoManager(argv[1]);
+  if(machine) {
+	  ioman = new MachineIoManager(argv[1]);
+  }
+  else {
+	  ioman = new UserIoManager(argv[1]);
+  }
+  
   // ‘MachineIoManager::MachineIoManager(char*&)’
   if(ioman->connect()) {
 	  ioman->run();

+ 29 - 30
cli/src/useriomanager.cpp

@@ -1,4 +1,5 @@
-#include <useriomanager.hpp>
+#include "../include/useriomanager.hpp"
+#include "../include/commands.hpp"
 
 #include <iostream>
 #include <jsoncpp/json/json.h>
@@ -9,50 +10,48 @@
 
 void UserIoManager::run() {
 	std::cout << "UserIoManager::run() says hello!" << std::endl;
-	
-	
-
 
-	
 	bool keep_reading = true;
+	COMMANDID cmd;
+	char *line = NULL;
 	while(keep_reading){
+		free(line);
 		
 		// read an input line
-		char *line = readline ("ccats> ");
+		line = readline ("ccats> ");
         
-        // if no input, do not add to history, and go to reading next line
+		// if no input, do not add to history, and go to reading next line
 		if (strlen(line) == 0) {
-			free(line);
 			continue;
 		}
 		
 		// add the line to history
 		add_history(line);
 		
-		if(!strcmp(line, "help")) {
-        
-            std::printf("ccats command line interface\n");
-            std::printf("available COMMANDs are:\n\n");
-            std::printf("help   - shows this help\n");
-            std::printf("status - asks the server for status\n");
-            std::printf("close  - closes the connection to the server\n");
-            
-        }
-        else if (!strcmp(line, "status")) {
-        
-            // do stuff
-            
-        }
-        else if (!strcmp(line, "close")) {
-        
-            // do stuff
-            
-            keep_reading = false;
-            
-        }
+		cmd = getCmdIdFromString(line);
 		
-		free(line);
+		switch(cmd) {
+			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();
+				std::printf("help        - shows this help\n");
+				std::printf("status      - asks the server for status\n");
+				std::printf("disconnect  - closes the connection to the server\n");
+				break;
+			}
+		}
+		std::printf("\n");
 	}
+	free(line);
 	
 	return;
 }