ioman.h 3.9 KB

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