ioman.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef IOMAN_H
  2. #define IOMAN_H
  3. #include "cmdman.h"
  4. #include "fileman.h"
  5. #include <boost/asio.hpp>
  6. #include <string>
  7. #include <thread>
  8. #include <mutex>
  9. #include <vector>
  10. using boost::asio::ip::tcp;
  11. #define BLOCKSIZE 8
  12. class IoMan {
  13. private:
  14. boost::asio::io_service ios;
  15. boost::asio::streambuf sendbuf;
  16. boost::asio::streambuf recvbuf;
  17. boost::system::error_code errcode;
  18. tcp::socket *tcpsock;
  19. std::string ipstring;
  20. int port;
  21. bool connected;
  22. CmdMan cmdman;
  23. FileMan fileman;
  24. /*
  25. 3 threads: handleinput, handlenetwork, handleresponse
  26. */
  27. std::thread tinput, tnetwork, tresponse;
  28. bool runmain, runinput, runnetwork, runresponse;
  29. std::vector<Json::Value> netinput;
  30. std::vector<std::string> localinput;
  31. std::mutex netmutex, localmutex;
  32. /*
  33. to be put elsewhere
  34. */
  35. Json::CharReader* reader;
  36. Json::StreamWriterBuilder wbuilder;
  37. string jsonerror;
  38. void networkMain();
  39. void inputMain();
  40. void responseMain();
  41. enum Status { off, on, err };
  42. Status versionstatus;
  43. Status loginstatus;
  44. bool startlist;
  45. protected:
  46. virtual void printNormalMessage(std::string msg);
  47. virtual void printErrorMessage(std::string msg);
  48. virtual void printDebugMessage(std::string msg);
  49. virtual void printWelcomeMessage() = 0;
  50. virtual std::string getCmdPrompt() = 0;
  51. virtual std::string getUserPrompt() = 0;
  52. virtual std::string getPassPrompt() = 0;
  53. public:
  54. // Basic constructor
  55. IoMan(char *ipcstring);
  56. // destructor to clean up all generic stuff
  57. ~IoMan();
  58. // enters loop to handle further interaction based on derived class
  59. void run();
  60. // setup stuff
  61. bool init();
  62. // tries to establish connection, returns error string if applicable
  63. bool connect();
  64. // disconnect from server
  65. void disconnect();
  66. };
  67. #endif