Pārlūkot izejas kodu

minor cleanup, add documentation to headers

Missingmew 4 gadi atpakaļ
vecāks
revīzija
7b83a2dc39

+ 59 - 6
cli/include/cmdman.h

@@ -13,37 +13,84 @@ using std::map;
 using std::string;
 using std::vector;
 
+/**
+ * @class CmdMan
+ *
+ * A class that provides handling of user provided commands as well as
+ * commands as provided by json responses from a server.
+ */
 class CmdMan {
 public:
-  enum rettype { notsend, send, error, startlist, stoplist, close, seton };
+  /**
+   * Type of message returned in CmdRet.
+   * notsend	- do not send anything to the server
+   * send		- send something to the server
+   * error	- an error occured, do not send to the server
+   * close	- terminate the connection
+   * seton	- contextually change state, used for version check and login
+   */
+  enum rettype { notsend, send, error, close, seton };
+  /**
+   * Response to user or command input
+   *
+   * string is to be handled depending on type
+   */
   struct CmdRet {
     rettype type;
     string msg;
   };
-
+  /**
+   * Constructor and destructor
+   */
   CmdMan(FileMan &fm);
   ~CmdMan();
 
-  // execute cmd with optional argument args, returns answer string
-  // if cmd unknown, returns error string
+  /**
+   * Executes a user provided command with optional arguments
+   */
   CmdRet execute(string cmd, vector<string> args);
+  /**
+   * Handles a server provided response json
+   */
   CmdRet handle(Json::Value root);
 
+  /**
+   * Internal json reader
+   */
   Json::CharReader *reader;
 
 private:
+  /**
+   * internal json writer and error string member
+   */
   Json::StreamWriterBuilder wbuilder;
-
   string jsonerror;
 
+  /**
+   * FileMan instance used to file commands
+   */
   FileMan &fileman;
 
+  /**
+   * Maps containing pointers to the appropriate member functions for executing
+   * or handling commands
+   */
   map<string, CmdRet (CmdMan::*)(vector<string>)> execmap;
-  map<string, string> helpmap;
   map<string, CmdRet (CmdMan::*)(Json::Value)> handlemap;
+  /**
+   * Map containing help strings to show to a user
+   */
+  map<string, string> helpmap;
 
+  /**
+   * State used to internally format received json to allow easy handling using
+   * handlemap
+   */
   bool dologin, doversion;
 
+  /**
+   * Help strings and method prototypes for commands to be used by a user
+   */
   /* execute command descriptions and methods go here */
   const string descHelp = "print available commands";
   CmdRet cmdHelp(vector<string> args);
@@ -58,6 +105,9 @@ private:
   const string descList = "list files available on server";
   CmdRet cmdList(vector<string> args);
 
+  /**
+   * Method prototypes for commands used internally
+   */
   /* internal execute commands */
   CmdRet cmdVersion(vector<string> args);
   CmdRet cmdLogin(vector<string> args);
@@ -65,6 +115,9 @@ private:
   CmdRet cmdGetdata(vector<string> args);
   CmdRet cmdListdata(vector<string> args);
 
+  /**
+   * Method prototypes for handling json responses
+   */
   /* handle commands go here */
   CmdRet handleStatus(Json::Value);
   CmdRet handleClose(Json::Value);

+ 61 - 2
cli/include/fileman.h

@@ -7,8 +7,24 @@
 
 #define BLOCKSIZE 8
 
+/**
+ * @class FileMan
+ *
+ * Provides File I/O abstraction
+ */
 class FileMan {
 private:
+  /**
+   * Internal state
+   *
+   * Filestreams for put and get
+   * Vector for holding received filenames from listing
+   * Names for put and get
+   * Size for internal read
+   * Total and Remaining chunks for put, get and list
+   * Boolean replacement for filestreams being open for list
+   *
+   */
   std::ifstream putfile;
   std::ofstream getfile;
   std::vector<std::string> listdata;
@@ -25,45 +41,88 @@ private:
   bool islisting;
 
 public:
+  /**
+   * Constructor and Destructor
+   */
   FileMan();
   ~FileMan();
 
+  /**
+   * Query internal state
+   *
+   * Return true if the corresponding action is being performed, false otherwise
+   */
   bool isGetting();
   bool isPutting();
   bool isListing();
 
+  /**
+   * Check for and prepare state and streams for reading/writing
+   *
+   * Return true if successful, false otherwise
+   */
   bool openPut(const std::string &name);
   bool openGet(const std::string &name);
   bool openList();
 
+  /**
+   * Close the respective filestream
+   */
   void closePut();
   void closeGet();
   void closeList();
 
+  /**
+   * Query the names of the file currently being put or get
+   */
   std::string getPutName();
   std::string getGetName();
 
+  /**
+   * Cancel a put, get or list, depreparing internal state (closing streams if
+   * required)
+   */
   void cancelPut();
   void cancelGet();
   void cancelList();
 
+  /**
+   * Read max_rea_len bytes from the current file opened for put
+   */
   std::vector<char> readPut();
+  /**
+   * Write the provided vector to the current file opened for get
+   */
   void writeGet(std::vector<char> data);
 
+  /**
+   * Wrapper methods for reading and writing base64 encoded data instead of raw
+   * bytes
+   */
   std::string readBase64();
   void writeBase64(std::string data);
+
+  /**
+   * read and write emulating methods for list
+   */
   void putListData(std::vector<std::string> names);
   std::vector<std::string> getListData();
 
+  /**
+   * Query internal state, requesting the corresponding size
+   */
   int getPutChunks();
   int getGetChunks();
   int getListChunks();
-
   int getPutRemainingChunks();
   int getGetRemainingChunks();
   int getListRemainingChunks();
-
   int getPutSize();
+
+  /**
+   * Set internal state, adjusting the chunks as well as chunks remaining for
+   * get and list
+   */
   void setGetChunks(int chunks);
   void setListChunks(int chunks);
 };

+ 82 - 23
cli/include/ioman.h

@@ -13,82 +13,141 @@
 
 using boost::asio::ip::tcp;
 
+/**
+ * @class IoMan
+ *
+ * Input/Output manager
+ *
+ * Provides the glue logic for getting user and network input
+ * as well as providing user and network output
+ */
 class IoMan {
   /* this is technically private and protected stuff which needs to be public
    * for the readline callback */
 public:
-  std::mutex localmutex;
+  /**
+   * Type of message to be output
+   */
   enum OutMsgType { normal, error, debug };
+  /**
+   * Output msg to the user, treating it as type
+   */
   virtual void printMessage(std::string msg, OutMsgType type);
+  /**
+   * Flag wether to keep the main thread running
+   * Matching mutex to provide syncthonizes access
+   */
   bool runmain;
+  std::mutex mainmutex;
+  /**
+   * Vector to hold input generated/fetched locally (e.g. from the user)
+   * Matching condition variable to wake up waiting threads
+   * Matching mutex to provide synchronized access
+   */
+  std::mutex localmutex;
   std::vector<std::string> localinput;
   std::condition_variable localcv;
-  std::mutex mainmutex;
 
 private:
+  /**
+   * Internal state to provide class-wide asio networking functionality
+   */
   boost::asio::io_service ios;
-  boost::asio::streambuf sendbuf;
   boost::asio::streambuf recvbuf;
   boost::system::error_code errcode;
   tcp::socket *tcpsock;
+  /**
+   * The IP and port to connect to
+   * Flag telling wether one is connected
+   */
   std::string ipstring;
   int port;
   bool connected;
+  /**
+   * Instances of CmdMan and FileMan to process user input and handle File I/O
+   */
   CmdMan cmdman;
   FileMan fileman;
 
-  /*
-  3 threads: handleinput, handlenetwork, handleresponse
-  */
+  /**
+   * Thread handles for processing local and network input as well as generating
+   * responses to both Matching mutexes for the flags wether the threads should
+   * keep running Function prototypes for the main thread functions
+   */
   std::thread tinput, tnetwork, tresponse;
   std::mutex inputmutex, networkmutex, responsemutex;
   bool runinput, runnetwork, runresponse;
+  void networkMain();
+  void inputMain();
+  void responseMain();
 
+  /**
+   * Vector to hold preprocessed input from the network
+   * Matching condition variable to wake up waiting threads
+   * Matching mutex to provide synchronized access
+   */
   std::vector<Json::Value> netinput;
   std::mutex netmutex;
   std::condition_variable netcv;
 
-  /*
-  to be put elsewhere
-  */
+  /**
+   * Class-wide json functionality
+   */
   Json::CharReader *reader;
   Json::StreamWriterBuilder wbuilder;
   string jsonerror;
 
-  void networkMain();
-  void inputMain();
-  void responseMain();
-
+  /**
+   * Enum for internal login and version state
+   * Variables for keeping said state
+   * Matching mutex and condition variable
+   */
   enum Status { off, on, err };
-
   Status versionstatus;
   Status loginstatus;
   std::mutex initmutex;
   std::condition_variable initcv;
 
-  bool startlist;
-
 protected:
+  /**
+   * mutex
+   */
   std::mutex msgmutex;
 
+  /**
+   * Prompt messages for readline prompt
+   */
   virtual void printWelcomeMessage() = 0;
   virtual std::string getCmdPrompt() = 0;
   virtual std::string getUserPrompt() = 0;
   virtual std::string getPassPrompt() = 0;
 
 public:
-  // Basic constructor
+  /**
+   * Constructor and destructor
+   */
   IoMan(char *ipcstring);
-  // destructor to clean up all generic stuff
   ~IoMan();
-  // enters loop to handle further interaction based on derived class
-  void run();
-  // setup stuff
+
+  /**
+   * Establish connection to server and perform vesion check
+   * Return true if successful, false otherwise
+   */
   bool init();
 
-  // tries to establish connection, returns error string if applicable
+  /**
+   * Main loop, call init first
+   */
+  void run();
+
+  /**
+   * Establish connection to server
+   * Return true if successful, false otherwise
+   */
   bool connect();
-  // disconnect from server
+  /**
+   * Disconnect from a connected server
+   */
   void disconnect();
 };
 

+ 12 - 2
cli/include/machineioman.h

@@ -3,15 +3,25 @@
 
 #include "ioman.h"
 
+/**
+ * @class MachineIoMan
+ *
+ * Provides specific implementations of IoMan outputs and prompts
+ * for interactive non-user sessions
+ */
 class MachineIoMan : public IoMan {
 public:
   using IoMan::IoMan;
 
-  bool parseJson(Json::Value *root, string jsonstring);
-  bool handleJson(Json::Value root);
+  /**
+   * Specific implementations for printing messages
+   */
   void printMessage(std::string msg, OutMsgType type);
   void printWelcomeMessage();
 
+  /**
+   * Return the specific prompt strings for IoMan prompts
+   */
   std::string getCmdPrompt();
   std::string getUserPrompt();
   std::string getPassPrompt();

+ 12 - 2
cli/include/userioman.h

@@ -3,15 +3,25 @@
 
 #include "ioman.h"
 
+/**
+ * @class UserIoMan
+ *
+ * Provides specific implementations of IoMan outputs and prompts
+ * for interactive user sessions
+ */
 class UserIoMan : public IoMan {
 public:
   using IoMan::IoMan;
 
-  bool parseJson(Json::Value *root, string jsonstring);
-  bool handleJson(Json::Value root);
+  /**
+   * Specific implementations for printing messages
+   */
   void printMessage(std::string msg, OutMsgType type);
   void printWelcomeMessage();
 
+  /**
+   * Return the specific prompt strings for IoMan prompts
+   */
   std::string getCmdPrompt();
   std::string getUserPrompt();
   std::string getPassPrompt();

+ 1 - 10
cli/src/ioman.cpp

@@ -37,7 +37,6 @@ IoMan::IoMan(char *ipcstring) : cmdman(fileman) {
   runnetwork = false;
   runinput = false;
   runresponse = false;
-  startlist = false;
   versionstatus = off;
   loginstatus = off;
 }
@@ -415,14 +414,6 @@ void IoMan::responseMain() {
     for (Json::Value root : toprocess) {
       cmdret = cmdman.handle(root);
       switch (cmdret.type) {
-      case CmdMan::rettype::startlist: {
-        startlist = true;
-        break;
-      }
-      case CmdMan::rettype::stoplist: {
-        startlist = false;
-        break;
-      }
       case CmdMan::rettype::close: {
         /* TODO i dunno */
         mainmutex.lock();
@@ -487,7 +478,7 @@ void ioman_readlineHandler(char *line) {
   if (!line) {
     printf("\nNULLBURGER\n");
     gIOMAN->mainmutex.lock();
-    gIOMAN->runmain = 0;
+    gIOMAN->runmain = false;
     gIOMAN->mainmutex.unlock();
   } else {
     // split input line into tokens

+ 0 - 6
cli/src/machineioman.cpp

@@ -10,12 +10,6 @@
 
 using boost::asio::buffer;
 
-bool MachineIoMan::parseJson(Json::Value *root, string jsonstring) {
-  return false;
-}
-
-bool MachineIoMan::handleJson(Json::Value root) { return false; }
-
 void MachineIoMan::printMessage(std::string msg, OutMsgType type) {
   switch (type) {
   case normal: {

+ 1 - 1
cli/src/main.cpp

@@ -8,7 +8,7 @@
 namespace bpo = boost::program_options;
 
 /* this is provided for the readline callback in IoMan */
-IoMan *gIOMAN;
+IoMan *gIOMAN = NULL;
 
 void show_help(char *exec) {
   std::printf("ccats command line interface\n");

+ 0 - 6
cli/src/userioman.cpp

@@ -10,12 +10,6 @@
 
 using boost::asio::buffer;
 
-bool UserIoMan::parseJson(Json::Value *root, string jsonstring) {
-  return false;
-}
-
-bool UserIoMan::handleJson(Json::Value root) { return false; }
-
 void UserIoMan::printMessage(std::string msg, OutMsgType type) {
   msgmutex.lock();
   switch (type) {