#ifndef IOMAN_H #define IOMAN_H #include "cmdman.h" #include "fileman.h" #include #include #include #include #include #include using boost::asio::ip::tcp; /** * @class IoMan * * Input/Output manager * * Provides the glue logic for getting user and network input * as well as providing user and network output */ class IoMan { /* this is technically private and protected stuff which needs to be public * for the readline callback */ public: /** * Type of message to be output */ enum OutMsgType { normal, error, debug }; /** * Output msg to the user, treating it as type */ virtual void printMessage(std::string msg, OutMsgType type); /** * Flag wether to keep the main thread running * Matching mutex to provide syncthonizes access */ bool runmain; std::mutex mainmutex; /** * Vector to hold input generated/fetched locally (e.g. from the user) * Matching condition variable to wake up waiting threads * Matching mutex to provide synchronized access */ std::mutex localmutex; std::vector localinput; std::condition_variable localcv; private: /** * Internal state to provide class-wide asio networking functionality */ boost::asio::io_service ios; boost::asio::streambuf recvbuf; boost::system::error_code errcode; tcp::socket *tcpsock; /** * The IP and port to connect to * Flag telling wether one is connected */ std::string ipstring; int port; bool connected; /** * Instances of CmdMan and FileMan to process user input and handle File I/O */ CmdMan cmdman; FileMan fileman; /** * Thread handles for processing local and network input as well as generating * responses to both Matching mutexes for the flags wether the threads should * keep running Function prototypes for the main thread functions */ std::thread tinput, tnetwork, tresponse; std::mutex inputmutex, networkmutex, responsemutex; bool runinput, runnetwork, runresponse; void networkMain(); void inputMain(); void responseMain(); /** * Vector to hold preprocessed input from the network * Matching condition variable to wake up waiting threads * Matching mutex to provide synchronized access */ std::vector netinput; std::mutex netmutex; std::condition_variable netcv; /** * Class-wide json functionality */ Json::CharReader *reader; Json::StreamWriterBuilder wbuilder; string jsonerror; /** * Enum for internal login and version state * Variables for keeping said state * Matching mutex and condition variable */ enum Status { off, on, err }; Status versionstatus; Status loginstatus; std::mutex initmutex; std::condition_variable initcv; protected: /** * mutex */ std::mutex msgmutex; /** * Prompt messages for readline prompt */ virtual void printWelcomeMessage() = 0; virtual std::string getCmdPrompt() = 0; virtual std::string getUserPrompt() = 0; virtual std::string getPassPrompt() = 0; public: /** * Constructor and destructor */ IoMan(char *ipcstring); ~IoMan(); /** * Establish connection to server and perform vesion check * Return true if successful, false otherwise */ bool init(); /** * Main loop, call init first */ void run(); /** * Establish connection to server * Return true if successful, false otherwise */ bool connect(); /** * Disconnect from a connected server */ void disconnect(); }; #endif