Command.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef COMMAND_H
  2. #define COMMAND_H
  3. #include "Socket.h"
  4. #include <json/json.h>
  5. #include <iostream>
  6. #include <vector>
  7. #include "error.h"
  8. class Command
  9. {
  10. public:
  11. enum CommandReturn {CONTINUE, EXIT, CLOSE};
  12. Command(ISocket* sock);
  13. Command();
  14. Command(const Command& sock);
  15. virtual ~Command(){}
  16. virtual const Command& operator=(const Command&);
  17. virtual Json::Value& operator[](int i){return m_args[i];}
  18. void print();
  19. const std::string& get_name() const {return m_command;}
  20. std::vector<Json::Value>& get_args() {return m_args;}
  21. static Command parse(ISocket* s);
  22. std::string str() const;
  23. protected:
  24. std::string m_command;
  25. std::vector<Json::Value> m_args;
  26. ISocket* m_io;
  27. friend class CommandParser;
  28. };
  29. class CommandParser
  30. {
  31. public:
  32. enum Token {
  33. PV,
  34. IDENT,
  35. STRING,
  36. INT,
  37. FLOAT,
  38. BOOL
  39. };
  40. CommandParser(ISocket* sock) : m_io(sock){}
  41. virtual ~CommandParser(){}
  42. static Command parse(ISocket* sock)
  43. {
  44. CommandParser cp(sock);
  45. return cp._parse();
  46. }
  47. protected:
  48. Command _parse();
  49. Token _next();
  50. char _nc(){ return m_c=m_io->readc();}
  51. Token _set(Token t);
  52. ISocket* m_io;
  53. Token m_token;
  54. std::string m_current;
  55. Json::Value m_val;
  56. char m_c;
  57. };
  58. #endif // COMMAND_H