cmServerProtocol.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. void ReportProgress(int min, int current, int max,
  47. const std::string& message) const;
  48. cmServerResponse Reply(const Json::Value& data) const;
  49. cmServerResponse ReportError(const std::string& message) const;
  50. const std::string Type;
  51. const std::string Cookie;
  52. const Json::Value Data;
  53. private:
  54. cmServerRequest(cmServer* server, const std::string& t, const std::string& c,
  55. const Json::Value& d);
  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 const cmServerResponse Process(const cmServerRequest& request) = 0;
  65. bool Activate(const cmServerRequest& request, std::string* errorMessage);
  66. protected:
  67. cmake* CMakeInstance() const;
  68. // Implement protocol specific activation tasks here. Called from Activate().
  69. virtual bool DoActivate(const cmServerRequest& request,
  70. std::string* errorMessage);
  71. private:
  72. std::unique_ptr<cmake> m_CMakeInstance;
  73. };