ioman.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef IOMAN_H
  2. #define IOMAN_H
  3. #include "cmdman.h"
  4. #include "fileman.h"
  5. #include <boost/asio.hpp>
  6. #include <boost/asio/ssl.hpp>
  7. #include <condition_variable>
  8. #include <json/json.h>
  9. #include <mutex>
  10. #include <string>
  11. #include <thread>
  12. #include <vector>
  13. using boost::asio::ip::tcp;
  14. /**
  15. * @class IoMan
  16. *
  17. * Input/Output manager
  18. *
  19. * Provides the glue logic for getting user and network input
  20. * as well as providing user and network output
  21. */
  22. class IoMan {
  23. /* this is technically private and protected stuff which needs to be public
  24. * for the readline callback */
  25. public:
  26. /**
  27. * Type of message to be output
  28. */
  29. enum OutMsgType { normal, error, debug };
  30. /**
  31. * Output msg to the user, treating it as type
  32. */
  33. virtual void printMessage(std::string msg, OutMsgType type);
  34. /**
  35. * Flag wether to keep the main thread running
  36. * Matching mutex to provide syncthonizes access
  37. */
  38. bool runmain;
  39. std::mutex mainmutex;
  40. /**
  41. * Vector to hold input generated/fetched locally (e.g. from the user)
  42. * Matching condition variable to wake up waiting threads
  43. * Matching mutex to provide synchronized access
  44. */
  45. std::mutex localmutex;
  46. std::vector<std::string> localinput;
  47. std::condition_variable localcv;
  48. boost::system::error_code errcode;
  49. protected:
  50. /**
  51. * Prompt messages for readline prompt
  52. */
  53. virtual void printWelcomeMessage() = 0;
  54. virtual std::string getCmdPrompt() = 0;
  55. /**
  56. * The IP and port to connect to
  57. * Flag telling wether one is connected
  58. */
  59. std::string ipstring;
  60. unsigned short port;
  61. bool connected;
  62. /**
  63. * Thread handles for processing local and network input as well as generating
  64. * responses to both Matching mutexes for the flags wether the threads should
  65. * keep running Function prototypes for the main thread functions
  66. */
  67. std::thread tinput, tnetwork, tresponse;
  68. std::mutex inputmutex, networkmutex, responsemutex;
  69. bool runinput, runnetwork, runresponse;
  70. void networkMain();
  71. void inputMain();
  72. void responseMain();
  73. /**
  74. * Instances of CmdMan and FileMan to process user input and handle File I/O
  75. */
  76. CmdMan cmdman;
  77. FileMan fileman;
  78. /**
  79. * Functions used to handle results produced by CmdMan for user input or network input
  80. */
  81. virtual void handleInCmdResponse(CmdMan::CmdRet cmdret);
  82. virtual void handleOutCmdResponse(CmdMan::CmdRet cmdret, vector<string> &toput);
  83. /**
  84. * Boost asio sockets used for tcp and ssl communication and flag wether to use ssl or not
  85. */
  86. tcp::socket *tcpsock;
  87. boost::asio::ssl::stream<tcp::socket &> *sslsock;
  88. bool usessl;
  89. private:
  90. /**
  91. * Internal state to provide class-wide asio networking functionality
  92. */
  93. boost::asio::io_service ios;
  94. boost::asio::streambuf recvbuf;
  95. boost::asio::ssl::context *sslctx;
  96. /**
  97. * Class-wide json functionality
  98. */
  99. Json::CharReader *reader;
  100. Json::StreamWriterBuilder wbuilder;
  101. string jsonerror;
  102. /**
  103. * Vector to hold preprocessed input from the network
  104. * Matching condition variable to wake up waiting threads
  105. * Matching mutex to provide synchronized access
  106. */
  107. std::vector<Json::Value> netinput;
  108. std::mutex netmutex;
  109. std::condition_variable netcv;
  110. /**
  111. * Tokenizes input based on space as seperator
  112. * Respects double-quoted tokens
  113. * Returns a vector with the tokens as elements in order
  114. */
  115. std::vector<std::string> tokenizeInput(std::string in);
  116. /**
  117. * Timestamp saves the sending time of the oldest request to the server that was not followed by an answer.
  118. * If the timestamp is not valid, since the last request to the server, there was an answer
  119. * (or the cli is disconnected, or there was no request yet).
  120. * Mutex used for access to the timestamps.
  121. */
  122. time_t sendtimestamp;
  123. bool sendtimestampValid;
  124. std::mutex timestampmutex;
  125. public:
  126. /**
  127. * Constructor and destructor
  128. */
  129. IoMan(bool enablessl, const char *certfile);
  130. virtual ~IoMan();
  131. /**
  132. * Establish connection to server and perform vesion check
  133. * Return true if successful, false otherwise
  134. */
  135. virtual bool init();
  136. /**
  137. * Main loop, call init first
  138. */
  139. virtual void run();
  140. /**
  141. * Establish connection to server
  142. * Return true if successful, false otherwise
  143. */
  144. bool connect();
  145. /**
  146. * Disconnect from a connected server
  147. */
  148. void disconnect();
  149. };
  150. #endif