cmdman.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 {
  28. print = (1 << 0),
  29. send = (1 << 1),
  30. error = (1 << 2),
  31. close = (1 << 3),
  32. seton = (1 << 4)
  33. };
  34. /**
  35. * Response to user or command input
  36. *
  37. * msg is to be handled depending on type
  38. * if nextcommand isnt an empty string it should be handled;
  39. */
  40. struct CmdRet {
  41. unsigned int type;
  42. string nextcommand;
  43. Json::Value msg;
  44. };
  45. /**
  46. * Constructor and destructor
  47. */
  48. CmdMan(FileMan &fm, void (*dpf)(string));
  49. ~CmdMan();
  50. /**
  51. * Executes a user provided command with optional arguments
  52. */
  53. CmdRet execute(string cmd, vector<string> args);
  54. /**
  55. * Handles a server provided response json
  56. */
  57. CmdRet handle(Json::Value root);
  58. /**
  59. * Internal json reader
  60. */
  61. Json::CharReader *reader;
  62. private:
  63. /**
  64. * internal json writer and error string member
  65. */
  66. Json::StreamWriterBuilder wbuilder;
  67. string jsonerror;
  68. /**
  69. * FileMan instance used to file commands
  70. */
  71. FileMan &fileman;
  72. void (*debugprintfunc)(string);
  73. /**
  74. * Maps containing pointers to the appropriate member functions for executing
  75. * or handling commands
  76. */
  77. map<string, CmdRet (CmdMan::*)(vector<string>)> execmap;
  78. map<string, CmdRet (CmdMan::*)(Json::Value)> handlemap;
  79. /**
  80. * Map containing help strings to show to a user
  81. */
  82. map<string, string> helpmap;
  83. /**
  84. * State used to internally format received json to allow easy handling using
  85. * handlemap and to disallow the use of certain commands when logged in/not
  86. * logged in.
  87. */
  88. bool doversion, loginpossible, dologin, dosignup;
  89. /**
  90. * Help strings and method prototypes for commands to be used by a user
  91. */
  92. /* execute command descriptions and methods go here */
  93. const string descHelp = "print available commands";
  94. CmdRet cmdHelp(vector<string> args);
  95. const string descStatus = "request status from server";
  96. CmdRet cmdStatus(vector<string> args);
  97. const string descDisconnect = "disconnect from server";
  98. CmdRet cmdDisconnect(vector<string> args);
  99. const string descPut = "upload file to server and add to queue";
  100. CmdRet cmdPut(vector<string> args);
  101. const string descGet = "retrieve file from server";
  102. CmdRet cmdGet(vector<string> args);
  103. const string descList = "list files available on server";
  104. CmdRet cmdList(vector<string> args);
  105. const string descHead =
  106. "request the first four bytes of a file from the server";
  107. CmdRet cmdHead(vector<string> args);
  108. const string descLogin = "login to the server";
  109. CmdRet cmdLogin(vector<string> args);
  110. const string descSignup = "sign up and login to the server";
  111. CmdRet cmdSignup(vector<string> args);
  112. /**
  113. * Method prototypes for commands used internally
  114. */
  115. /* internal execute commands */
  116. CmdRet cmdVersion(vector<string> args);
  117. CmdRet cmdPutdata(vector<string> args);
  118. CmdRet cmdGetdata(vector<string> args);
  119. CmdRet cmdListdata(vector<string> args);
  120. /**
  121. * Method prototypes for handling json responses
  122. */
  123. /* handle commands go here */
  124. CmdRet handleStatus(Json::Value);
  125. CmdRet handleClose(Json::Value);
  126. CmdRet handlePut(Json::Value);
  127. CmdRet handleGet(Json::Value);
  128. CmdRet handleList(Json::Value);
  129. CmdRet handlePutdata(Json::Value);
  130. CmdRet handleGetdata(Json::Value);
  131. CmdRet handleListdata(Json::Value);
  132. CmdRet handleVersion(Json::Value);
  133. CmdRet handleLogin(Json::Value);
  134. CmdRet handleSignup(Json::Value);
  135. CmdRet handleHead(Json::Value);
  136. };
  137. #endif