cmdman.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. * Type of message returned in CmdRet.
  21. * notsend - do not send anything to the server
  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 { notsend, send, error, close, seton };
  28. /**
  29. * Response to user or command input
  30. *
  31. * string is to be handled depending on type
  32. */
  33. struct CmdRet {
  34. rettype type;
  35. string msg;
  36. };
  37. /**
  38. * Constructor and destructor
  39. */
  40. CmdMan(FileMan &fm);
  41. ~CmdMan();
  42. /**
  43. * Executes a user provided command with optional arguments
  44. */
  45. CmdRet execute(string cmd, vector<string> args);
  46. /**
  47. * Handles a server provided response json
  48. */
  49. CmdRet handle(Json::Value root);
  50. /**
  51. * Internal json reader
  52. */
  53. Json::CharReader *reader;
  54. private:
  55. /**
  56. * internal json writer and error string member
  57. */
  58. Json::StreamWriterBuilder wbuilder;
  59. string jsonerror;
  60. /**
  61. * FileMan instance used to file commands
  62. */
  63. FileMan &fileman;
  64. /**
  65. * Maps containing pointers to the appropriate member functions for executing
  66. * or handling commands
  67. */
  68. map<string, CmdRet (CmdMan::*)(vector<string>)> execmap;
  69. map<string, CmdRet (CmdMan::*)(Json::Value)> handlemap;
  70. /**
  71. * Map containing help strings to show to a user
  72. */
  73. map<string, string> helpmap;
  74. /**
  75. * State used to internally format received json to allow easy handling using
  76. * handlemap
  77. */
  78. bool dologin, doversion;
  79. /**
  80. * Help strings and method prototypes for commands to be used by a user
  81. */
  82. /* execute command descriptions and methods go here */
  83. const string descHelp = "print available commands";
  84. CmdRet cmdHelp(vector<string> args);
  85. const string descStatus = "request status from server";
  86. CmdRet cmdStatus(vector<string> args);
  87. const string descDisconnect = "disconnect from server";
  88. CmdRet cmdDisconnect(vector<string> args);
  89. const string descPut = "upload file to server and add to queue";
  90. CmdRet cmdPut(vector<string> args);
  91. const string descGet = "retrieve file from server";
  92. CmdRet cmdGet(vector<string> args);
  93. const string descList = "list files available on server";
  94. CmdRet cmdList(vector<string> args);
  95. /**
  96. * Method prototypes for commands used internally
  97. */
  98. /* internal execute commands */
  99. CmdRet cmdVersion(vector<string> args);
  100. CmdRet cmdLogin(vector<string> args);
  101. CmdRet cmdPutdata(vector<string> args);
  102. CmdRet cmdGetdata(vector<string> args);
  103. CmdRet cmdListdata(vector<string> args);
  104. /**
  105. * Method prototypes for handling json responses
  106. */
  107. /* handle commands go here */
  108. CmdRet handleStatus(Json::Value);
  109. CmdRet handleClose(Json::Value);
  110. CmdRet handlePut(Json::Value);
  111. CmdRet handleGet(Json::Value);
  112. CmdRet handleList(Json::Value);
  113. CmdRet handlePutdata(Json::Value);
  114. CmdRet handleGetdata(Json::Value);
  115. CmdRet handleListdata(Json::Value);
  116. CmdRet handleVersion(Json::Value);
  117. CmdRet handleLogin(Json::Value);
  118. };
  119. #endif