Application.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef __APPLICATION__H_
  2. #define __APPLICATION__H_
  3. #include <string>
  4. #include <vector>
  5. #include <json/json.h>
  6. #include "Command.h"
  7. class IAbsServer;
  8. class Pad;
  9. class ISocket;
  10. #define SEND(__sock__,__msg__) {std::ostringstream os; os << __msg__ <<"\n"; (__sock__).write(os.str());}
  11. class Application;
  12. class Pad;
  13. class CommandLog : public std::ostringstream
  14. {
  15. public:
  16. CommandLog() : std::ostringstream(){ error=0; message="success"; }
  17. virtual ~CommandLog(){}
  18. void set(int err=0, const std::string& msg=""){
  19. error=err;
  20. message=msg;
  21. }
  22. void write(ISocket& sock, const std::string& append="");
  23. int error;
  24. std::string message;
  25. };
  26. typedef Command::CommandReturn (*CommandCallback)(Application&, Pad&, std::vector<Json::Value>&, CommandLog&);
  27. class CommandHandler
  28. {
  29. public:
  30. CommandHandler(const std::string& s, CommandCallback c, const std::string& h){
  31. name=s;
  32. command=c;
  33. help=h;
  34. }
  35. virtual ~CommandHandler(){}
  36. std::string name;
  37. std::string help;
  38. CommandCallback command;
  39. };
  40. class Application
  41. {
  42. public:
  43. Application();
  44. virtual ~Application();
  45. void process_next_command(int=1);
  46. void process();
  47. Pad& operator[](int);
  48. Pad& operator[](const std::string&);
  49. Pad& get_pad(int=-1);
  50. Pad& get_pad(const std::string& = "");
  51. void remove_pad(int);
  52. void remove_pad(const std::string&);
  53. int add_pad(Pad*);
  54. int add_pad(const std::string&);
  55. bool has_pad(const std::string&);
  56. Pad& set_pad(int);
  57. Pad& set_pad(const std::string&);
  58. void add_command(const std::string&, CommandCallback, const std::string&);
  59. void help(CommandLog&);
  60. int pad_count() const {return m_pads.size();}
  61. bool load_pad_configuration(const std::string&);
  62. bool load_pad_definition(const std::string&);
  63. int get_id(const std::string&);
  64. protected:
  65. Command::CommandReturn _call(Command&, ISocket&);
  66. void _process_one();
  67. void _auto_load_pad();
  68. std::vector<std::pair<int, Pad*> > m_pads;
  69. int m_pad_last_id;
  70. IAbsServer* m_server;
  71. Pad* m_current;
  72. int m_current_index;
  73. std::vector<CommandHandler*> m_callbacks;
  74. };
  75. #endif