ioman.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * Enum for internal login and version state
  57. * Variables for keeping said state
  58. * Matching mutex and condition variable
  59. */
  60. enum Status { off, on, err };
  61. Status loginstatus;
  62. Status versionstatus;
  63. std::mutex initmutex;
  64. std::condition_variable initcv;
  65. bool connected;
  66. virtual void handleInCmdResponse(CmdMan::CmdRet cmdret);
  67. virtual void handleOutCmdResponse(CmdMan::CmdRet cmdret, vector<string> &toput);
  68. tcp::socket *tcpsock;
  69. boost::asio::ssl::stream<tcp::socket &> *sslsock;
  70. bool usessl;
  71. private:
  72. /**
  73. * Internal state to provide class-wide asio networking functionality
  74. */
  75. boost::asio::io_service ios;
  76. boost::asio::streambuf recvbuf;
  77. boost::asio::ssl::context *sslctx;
  78. /**
  79. * The IP and port to connect to
  80. * Flag telling wether one is connected
  81. */
  82. std::string ipstring;
  83. unsigned short port;
  84. /**
  85. * Instances of CmdMan and FileMan to process user input and handle File I/O
  86. */
  87. CmdMan cmdman;
  88. FileMan fileman;
  89. /**
  90. * Class-wide json functionality
  91. */
  92. Json::CharReader *reader;
  93. Json::StreamWriterBuilder wbuilder;
  94. string jsonerror;
  95. /**
  96. * Thread handles for processing local and network input as well as generating
  97. * responses to both Matching mutexes for the flags wether the threads should
  98. * keep running Function prototypes for the main thread functions
  99. */
  100. std::thread tinput, tnetwork, tresponse;
  101. std::mutex inputmutex, networkmutex, responsemutex;
  102. bool runinput, runnetwork, runresponse;
  103. void networkMain();
  104. void inputMain();
  105. void responseMain();
  106. /**
  107. * Vector to hold preprocessed input from the network
  108. * Matching condition variable to wake up waiting threads
  109. * Matching mutex to provide synchronized access
  110. */
  111. std::vector<Json::Value> netinput;
  112. std::mutex netmutex;
  113. std::condition_variable netcv;
  114. /**
  115. * Tokenizes input based on space as seperator
  116. * Respects double-quoted tokens
  117. * Returns a vector with the tokens as elements in order
  118. */
  119. std::vector<std::string> tokenizeInput(std::string in);
  120. public:
  121. /**
  122. * Constructor and destructor
  123. */
  124. IoMan(char *ipcstring);
  125. IoMan(char *ipcstring, bool enablessl);
  126. virtual ~IoMan();
  127. /**
  128. * Establish connection to server and perform vesion check
  129. * Return true if successful, false otherwise
  130. */
  131. virtual bool init();
  132. /**
  133. * Main loop, call init first
  134. */
  135. virtual void run();
  136. /**
  137. * Establish connection to server
  138. * Return true if successful, false otherwise
  139. */
  140. bool connect();
  141. /**
  142. * Disconnect from a connected server
  143. */
  144. void disconnect();
  145. };
  146. #endif