cmdman.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef CMDMAN_H
  2. #define CMDMAN_H
  3. #include "fileman.h"
  4. #include <json/json.h>
  5. #include <map>
  6. #include <string>
  7. #include <vector>
  8. using std::map;
  9. using std::string;
  10. using std::vector;
  11. /**
  12. * @class CmdMan
  13. *
  14. * A class that provides handling of user provided commands as well as
  15. * commands as provided by json responses from a server.
  16. */
  17. class CmdMan {
  18. public:
  19. /**
  20. * Flags for type of message returned in CmdRet.
  21. * print - print something to the user
  22. * send - send something to the server
  23. * error - an error occured, do not send to the server
  24. * close - terminate the connection
  25. * seton - contextually change state, used for version check and login
  26. */
  27. enum rettype { none = 0, print = (1 << 1), send = (1 << 2), error = (1 << 3), close = (1 << 4), connect = (1 << 5), exit = (1 << 6) };
  28. /**
  29. * Response to user or command input
  30. *
  31. * msg is to be handled depending on type
  32. * if nextcommand isnt an empty string it should be handled;
  33. */
  34. struct CmdRet {
  35. unsigned int type;
  36. string nextcommand;
  37. Json::Value msg;
  38. };
  39. /**
  40. * Constructor and destructor
  41. */
  42. CmdMan(FileMan &fm, void (*dpf)(string));
  43. ~CmdMan();
  44. /**
  45. * Executes a user provided command with optional arguments
  46. */
  47. CmdRet execute(string cmd, vector<string> args);
  48. /**
  49. * Handles a server provided response json
  50. */
  51. CmdRet handle(Json::Value root);
  52. /**
  53. * Internal json reader
  54. */
  55. Json::CharReader *reader;
  56. void stateSetConnectionOk();
  57. protected:
  58. /**
  59. * State used to internally format received json to allow easy handling using
  60. * handlemap and to disallow the use of certain commands when logged in/not
  61. * logged in.
  62. */
  63. enum state { connectionpossible, versionpossible, doversion, loginpossible, dologin, dosignup, normal, disconnecttoexit, disconnecttoexitearly };
  64. state currentState;
  65. private:
  66. /**
  67. * internal json writer and error string member
  68. */
  69. Json::StreamWriterBuilder wbuilder;
  70. string jsonerror;
  71. /**
  72. * FileMan instance used to file commands
  73. */
  74. FileMan &fileman;
  75. void (*debugprintfunc)(string);
  76. /**
  77. * Maps containing pointers to the appropriate member functions for executing
  78. * or handling commands
  79. */
  80. map<string, CmdRet (CmdMan::*)(vector<string>)> execmap;
  81. map<string, CmdRet (CmdMan::*)(Json::Value)> handlemap;
  82. /**
  83. * Map containing help strings to show to a user
  84. */
  85. map<string, string> helpmap;
  86. /**
  87. * Help strings and method prototypes for commands to be used by a user
  88. */
  89. /* execute command descriptions and methods go here */
  90. const string descHelp = "print available commands";
  91. CmdRet cmdHelp(vector<string> args);
  92. const string descStatus = "request status from server";
  93. CmdRet cmdStatus(vector<string> args);
  94. const string descDisconnect = "disconnect from server";
  95. CmdRet cmdDisconnect(vector<string> args);
  96. const string descPut = "upload file to server and add to queue";
  97. CmdRet cmdPut(vector<string> args);
  98. const string descGet = "retrieve file from server";
  99. CmdRet cmdGet(vector<string> args);
  100. const string descList = "list files available on server";
  101. CmdRet cmdList(vector<string> args);
  102. const string descHead = "request the first four bytes of a file from the server";
  103. CmdRet cmdHead(vector<string> args);
  104. const string descDeletefile = "delete a file from the server";
  105. CmdRet cmdDeletefile(vector<string> args);
  106. const string descKeyfile = "set keyfile to use";
  107. CmdRet cmdKeyfile(vector<string> args);
  108. const string descClosekey = "stop using the previously selected keyfile";
  109. CmdRet cmdClosekey(vector<string> args);
  110. const string descConnect = "connect to server";
  111. CmdRet cmdConnect(vector<string> args);
  112. const string descExit = "exit the application";
  113. CmdRet cmdExit(vector<string> args);
  114. const string descLogin = "login to the server";
  115. CmdRet cmdLogin(vector<string> args);
  116. const string descSignup = "sign up and login to the server";
  117. CmdRet cmdSignup(vector<string> args);
  118. const string descDeleteme = "delete the user you are currently logged in as "
  119. "(needs to be confirmed with the password)";
  120. CmdRet cmdDeleteme(vector<string> args);
  121. /**
  122. * Method prototypes for commands used internally
  123. */
  124. /* internal execute commands */
  125. CmdRet cmdVersion(vector<string> args);
  126. CmdRet cmdPutdata(vector<string> args);
  127. CmdRet cmdGetdata(vector<string> args);
  128. CmdRet cmdListdata(vector<string> args);
  129. /**
  130. * Method prototypes for handling json responses
  131. */
  132. /* handle commands go here */
  133. CmdRet handleStatus(Json::Value);
  134. CmdRet handleClose(Json::Value);
  135. CmdRet handlePut(Json::Value);
  136. CmdRet handleGet(Json::Value);
  137. CmdRet handleList(Json::Value);
  138. CmdRet handlePutdata(Json::Value);
  139. CmdRet handleGetdata(Json::Value);
  140. CmdRet handleListdata(Json::Value);
  141. CmdRet handleVersion(Json::Value);
  142. CmdRet handleLogin(Json::Value);
  143. CmdRet handleSignup(Json::Value);
  144. CmdRet handleHead(Json::Value);
  145. CmdRet handleDeletefile(Json::Value);
  146. CmdRet handleDeleteme(Json::Value);
  147. };
  148. #endif