ioman.h 3.7 KB

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