ioman.h 3.5 KB

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