cmServerProtocol.h 3.33 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
4
5
#pragma once

#include "cmListFileCache.h"
6
#include "cmake.h"
7
8
9
10
11
12
13
14
15

#if defined(CMAKE_BUILD_WITH_CMAKE)
#include "cm_jsoncpp_writer.h"
#endif

#include <memory>
#include <string>

class cmake;
16
class cmFileMonitor;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class cmServer;

class cmServerRequest;

class cmServerResponse
{
public:
  explicit cmServerResponse(const cmServerRequest& request);

  void SetData(const Json::Value& data);
  void SetError(const std::string& message);

  bool IsComplete() const;
  bool IsError() const;
  std::string ErrorMessage() const;
  Json::Value Data() const;

  const std::string Type;
  const std::string Cookie;

private:
  enum PayLoad
  {
    PAYLOAD_UNKNOWN,
    PAYLOAD_ERROR,
    PAYLOAD_DATA
  };
  PayLoad m_Payload = PAYLOAD_UNKNOWN;
  std::string m_ErrorMessage;
  Json::Value m_Data;
};

class cmServerRequest
{
public:
  cmServerResponse Reply(const Json::Value& data) const;
  cmServerResponse ReportError(const std::string& message) const;

  const std::string Type;
  const std::string Cookie;
  const Json::Value Data;

private:
  cmServerRequest(cmServer* server, const std::string& t, const std::string& c,
                  const Json::Value& d);

63
64
  void ReportProgress(int min, int current, int max,
                      const std::string& message) const;
65
66
  void ReportMessage(const std::string& message,
                     const std::string& title) const;
67

68
69
70
71
72
73
74
75
76
77
78
  cmServer* m_Server;

  friend class cmServer;
};

class cmServerProtocol
{
public:
  virtual ~cmServerProtocol() {}

  virtual std::pair<int, int> ProtocolVersion() const = 0;
79
  virtual bool IsExperimental() const = 0;
80
81
  virtual const cmServerResponse Process(const cmServerRequest& request) = 0;

82
83
  bool Activate(cmServer* server, const cmServerRequest& request,
                std::string* errorMessage);
84

85
  cmFileMonitor* FileMonitor() const;
86
87
  void SendSignal(const std::string& name, const Json::Value& data) const;

88
89
90
91
92
93
94
95
protected:
  cmake* CMakeInstance() const;
  // Implement protocol specific activation tasks here. Called from Activate().
  virtual bool DoActivate(const cmServerRequest& request,
                          std::string* errorMessage);

private:
  std::unique_ptr<cmake> m_CMakeInstance;
96
  cmServer* m_Server = nullptr; // not owned!
97
98

  friend class cmServer;
99
};
100
101
102
103
104

class cmServerProtocol1_0 : public cmServerProtocol
{
public:
  std::pair<int, int> ProtocolVersion() const override;
105
  bool IsExperimental() const override;
106
107
108
109
110
111
  const cmServerResponse Process(const cmServerRequest& request) override;

private:
  bool DoActivate(const cmServerRequest& request,
                  std::string* errorMessage) override;

112
  // Handle requests:
113
  cmServerResponse ProcessCache(const cmServerRequest& request);
114
  cmServerResponse ProcessCMakeInputs(const cmServerRequest& request);
115
  cmServerResponse ProcessCodeModel(const cmServerRequest& request);
116
  cmServerResponse ProcessCompute(const cmServerRequest& request);
117
  cmServerResponse ProcessConfigure(const cmServerRequest& request);
118
  cmServerResponse ProcessGlobalSettings(const cmServerRequest& request);
119
  cmServerResponse ProcessSetGlobalSettings(const cmServerRequest& request);
120

121
122
123
  enum State
  {
    STATE_INACTIVE,
124
    STATE_ACTIVE,
125
126
    STATE_CONFIGURED,
    STATE_COMPUTED
127
128
129
  };
  State m_State = STATE_INACTIVE;
};