cmServerProtocol.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2016 Tobias Hunger <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #pragma once
  11. #include "cmListFileCache.h"
  12. #if defined(CMAKE_BUILD_WITH_CMAKE)
  13. #include "cm_jsoncpp_writer.h"
  14. #endif
  15. #include <memory>
  16. #include <string>
  17. class cmake;
  18. class cmServer;
  19. class cmServerRequest;
  20. class cmServerResponse
  21. {
  22. public:
  23. explicit cmServerResponse(const cmServerRequest& request);
  24. void SetData(const Json::Value& data);
  25. void SetError(const std::string& message);
  26. bool IsComplete() const;
  27. bool IsError() const;
  28. std::string ErrorMessage() const;
  29. Json::Value Data() const;
  30. const std::string Type;
  31. const std::string Cookie;
  32. private:
  33. enum PayLoad
  34. {
  35. PAYLOAD_UNKNOWN,
  36. PAYLOAD_ERROR,
  37. PAYLOAD_DATA
  38. };
  39. PayLoad m_Payload = PAYLOAD_UNKNOWN;
  40. std::string m_ErrorMessage;
  41. Json::Value m_Data;
  42. };
  43. class cmServerRequest
  44. {
  45. public:
  46. cmServerResponse Reply(const Json::Value& data) const;
  47. cmServerResponse ReportError(const std::string& message) const;
  48. const std::string Type;
  49. const std::string Cookie;
  50. const Json::Value Data;
  51. private:
  52. cmServerRequest(cmServer* server, const std::string& t, const std::string& c,
  53. const Json::Value& d);
  54. void ReportProgress(int min, int current, int max,
  55. const std::string& message) const;
  56. cmServer* m_Server;
  57. friend class cmServer;
  58. };
  59. class cmServerProtocol
  60. {
  61. public:
  62. virtual ~cmServerProtocol() {}
  63. virtual std::pair<int, int> ProtocolVersion() const = 0;
  64. virtual bool IsExperimental() const = 0;
  65. virtual const cmServerResponse Process(const cmServerRequest& request) = 0;
  66. bool Activate(const cmServerRequest& request, std::string* errorMessage);
  67. protected:
  68. cmake* CMakeInstance() const;
  69. // Implement protocol specific activation tasks here. Called from Activate().
  70. virtual bool DoActivate(const cmServerRequest& request,
  71. std::string* errorMessage);
  72. private:
  73. std::unique_ptr<cmake> m_CMakeInstance;
  74. friend class cmServer;
  75. };
  76. class cmServerProtocol1_0 : public cmServerProtocol
  77. {
  78. public:
  79. std::pair<int, int> ProtocolVersion() const override;
  80. bool IsExperimental() const override;
  81. const cmServerResponse Process(const cmServerRequest& request) override;
  82. private:
  83. bool DoActivate(const cmServerRequest& request,
  84. std::string* errorMessage) override;
  85. enum State
  86. {
  87. STATE_INACTIVE,
  88. STATE_ACTIVE
  89. };
  90. State m_State = STATE_INACTIVE;
  91. };