cmdman.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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
  86. */
  87. bool dologin, doversion;
  88. /**
  89. * Help strings and method prototypes for commands to be used by a user
  90. */
  91. /* execute command descriptions and methods go here */
  92. const string descHelp = "print available commands";
  93. CmdRet cmdHelp(vector<string> args);
  94. const string descStatus = "request status from server";
  95. CmdRet cmdStatus(vector<string> args);
  96. const string descDisconnect = "disconnect from server";
  97. CmdRet cmdDisconnect(vector<string> args);
  98. const string descPut = "upload file to server and add to queue";
  99. CmdRet cmdPut(vector<string> args);
  100. const string descGet = "retrieve file from server";
  101. CmdRet cmdGet(vector<string> args);
  102. const string descList = "list files available on server";
  103. CmdRet cmdList(vector<string> args);
  104. /**
  105. * Method prototypes for commands used internally
  106. */
  107. /* internal execute commands */
  108. CmdRet cmdVersion(vector<string> args);
  109. CmdRet cmdLogin(vector<string> args);
  110. CmdRet cmdPutdata(vector<string> args);
  111. CmdRet cmdGetdata(vector<string> args);
  112. CmdRet cmdListdata(vector<string> args);
  113. /**
  114. * Method prototypes for handling json responses
  115. */
  116. /* handle commands go here */
  117. CmdRet handleStatus(Json::Value);
  118. CmdRet handleClose(Json::Value);
  119. CmdRet handlePut(Json::Value);
  120. CmdRet handleGet(Json::Value);
  121. CmdRet handleList(Json::Value);
  122. CmdRet handlePutdata(Json::Value);
  123. CmdRet handleGetdata(Json::Value);
  124. CmdRet handleListdata(Json::Value);
  125. CmdRet handleVersion(Json::Value);
  126. CmdRet handleLogin(Json::Value);
  127. };
  128. #endif