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 <condition_variable>
  7. #include <mutex>
  8. #include <string>
  9. #include <thread>
  10. #include <vector>
  11. using boost::asio::ip::tcp;
  12. /**
  13. * @class IoMan
  14. *
  15. * Input/Output manager
  16. *
  17. * Provides the glue logic for getting user and network input
  18. * as well as providing user and network output
  19. */
  20. class IoMan {
  21. /* this is technically private and protected stuff which needs to be public
  22. * for the readline callback */
  23. public:
  24. /**
  25. * Type of message to be output
  26. */
  27. enum OutMsgType { normal, error, debug };
  28. /**
  29. * Output msg to the user, treating it as type
  30. */
  31. virtual void printMessage(std::string msg, OutMsgType type);
  32. /**
  33. * Flag wether to keep the main thread running
  34. * Matching mutex to provide syncthonizes access
  35. */
  36. bool runmain;
  37. std::mutex mainmutex;
  38. /**
  39. * Vector to hold input generated/fetched locally (e.g. from the user)
  40. * Matching condition variable to wake up waiting threads
  41. * Matching mutex to provide synchronized access
  42. */
  43. std::mutex localmutex;
  44. std::vector<std::string> localinput;
  45. std::condition_variable localcv;
  46. private:
  47. /**
  48. * Internal state to provide class-wide asio networking functionality
  49. */
  50. boost::asio::io_service ios;
  51. boost::asio::streambuf recvbuf;
  52. boost::system::error_code errcode;
  53. tcp::socket *tcpsock;
  54. /**
  55. * The IP and port to connect to
  56. * Flag telling wether one is connected
  57. */
  58. std::string ipstring;
  59. int port;
  60. bool connected;
  61. /**
  62. * Instances of CmdMan and FileMan to process user input and handle File I/O
  63. */
  64. CmdMan cmdman;
  65. FileMan fileman;
  66. /**
  67. * Thread handles for processing local and network input as well as generating
  68. * responses to both Matching mutexes for the flags wether the threads should
  69. * keep running Function prototypes for the main thread functions
  70. */
  71. std::thread tinput, tnetwork, tresponse;
  72. std::mutex inputmutex, networkmutex, responsemutex;
  73. bool runinput, runnetwork, runresponse;
  74. void networkMain();
  75. void inputMain();
  76. void responseMain();
  77. /**
  78. * Vector to hold preprocessed input from the network
  79. * Matching condition variable to wake up waiting threads
  80. * Matching mutex to provide synchronized access
  81. */
  82. std::vector<Json::Value> netinput;
  83. std::mutex netmutex;
  84. std::condition_variable netcv;
  85. /**
  86. * Class-wide json functionality
  87. */
  88. Json::CharReader *reader;
  89. Json::StreamWriterBuilder wbuilder;
  90. string jsonerror;
  91. /**
  92. * Enum for internal login and version state
  93. * Variables for keeping said state
  94. * Matching mutex and condition variable
  95. */
  96. enum Status { off, on, err };
  97. Status versionstatus;
  98. Status loginstatus;
  99. std::mutex initmutex;
  100. std::condition_variable initcv;
  101. protected:
  102. /**
  103. * mutex
  104. */
  105. std::mutex msgmutex;
  106. /**
  107. * Prompt messages for readline prompt
  108. */
  109. virtual void printWelcomeMessage() = 0;
  110. virtual std::string getCmdPrompt() = 0;
  111. virtual std::string getUserPrompt() = 0;
  112. virtual std::string getPassPrompt() = 0;
  113. public:
  114. /**
  115. * Constructor and destructor
  116. */
  117. IoMan(char *ipcstring);
  118. ~IoMan();
  119. /**
  120. * Establish connection to server and perform vesion check
  121. * Return true if successful, false otherwise
  122. */
  123. bool init();
  124. /**
  125. * Main loop, call init first
  126. */
  127. void run();
  128. /**
  129. * Establish connection to server
  130. * Return true if successful, false otherwise
  131. */
  132. bool connect();
  133. /**
  134. * Disconnect from a connected server
  135. */
  136. void disconnect();
  137. };
  138. #endif