ioman.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. virtual void handleInCmdResponse(CmdMan::CmdRet cmdret);
  79. virtual void handleOutCmdResponse(CmdMan::CmdRet cmdret, vector<string> &toput);
  80. tcp::socket *tcpsock;
  81. boost::asio::ssl::stream<tcp::socket &> *sslsock;
  82. bool usessl;
  83. private:
  84. /**
  85. * Internal state to provide class-wide asio networking functionality
  86. */
  87. boost::asio::io_service ios;
  88. boost::asio::streambuf recvbuf;
  89. boost::asio::ssl::context *sslctx;
  90. /**
  91. * Class-wide json functionality
  92. */
  93. Json::CharReader *reader;
  94. Json::StreamWriterBuilder wbuilder;
  95. string jsonerror;
  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