cmdman.h 3.4 KB

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