ioman.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. class IoMan {
  13. /* this is technically private and protected stuff which needs to be public
  14. * for the readline callback */
  15. public:
  16. std::mutex localmutex;
  17. enum OutMsgType { normal, error, debug };
  18. virtual void printMessage(std::string msg, OutMsgType type);
  19. bool runmain;
  20. std::vector<std::string> localinput;
  21. std::condition_variable localcv;
  22. std::mutex mainmutex;
  23. private:
  24. boost::asio::io_service ios;
  25. boost::asio::streambuf sendbuf;
  26. boost::asio::streambuf recvbuf;
  27. boost::system::error_code errcode;
  28. tcp::socket *tcpsock;
  29. std::string ipstring;
  30. int port;
  31. bool connected;
  32. CmdMan cmdman;
  33. FileMan fileman;
  34. /*
  35. 3 threads: handleinput, handlenetwork, handleresponse
  36. */
  37. std::thread tinput, tnetwork, tresponse;
  38. std::mutex inputmutex, networkmutex, responsemutex;
  39. bool runinput, runnetwork, runresponse;
  40. std::vector<Json::Value> netinput;
  41. std::mutex netmutex;
  42. std::condition_variable netcv;
  43. /*
  44. to be put elsewhere
  45. */
  46. Json::CharReader *reader;
  47. Json::StreamWriterBuilder wbuilder;
  48. string jsonerror;
  49. void networkMain();
  50. void inputMain();
  51. void responseMain();
  52. enum Status { off, on, err };
  53. Status versionstatus;
  54. Status loginstatus;
  55. std::mutex initmutex;
  56. std::condition_variable initcv;
  57. bool startlist;
  58. protected:
  59. std::mutex msgmutex;
  60. virtual void printWelcomeMessage() = 0;
  61. virtual std::string getCmdPrompt() = 0;
  62. virtual std::string getUserPrompt() = 0;
  63. virtual std::string getPassPrompt() = 0;
  64. public:
  65. // Basic constructor
  66. IoMan(char *ipcstring);
  67. // destructor to clean up all generic stuff
  68. ~IoMan();
  69. // enters loop to handle further interaction based on derived class
  70. void run();
  71. // setup stuff
  72. bool init();
  73. // tries to establish connection, returns error string if applicable
  74. bool connect();
  75. // disconnect from server
  76. void disconnect();
  77. };
  78. #endif