cmServerProtocol.h 4.38 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
#pragma once

5
#include "cmConfigure.h" // IWYU pragma: keep
6

7 8
#include "cm_jsoncpp_value.h"
#include "cmake.h"
9 10 11

#include <memory>
#include <string>
12
#include <utility>
13

14
class cmConnection;
15
class cmFileMonitor;
16 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
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;
56
  cmConnection* Connection;
57 58

private:
wahikihiki's avatar
wahikihiki committed
59 60
  cmServerRequest(cmServer* server, cmConnection* connection, std::string t,
                  std::string c, Json::Value d);
61

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

67 68 69 70 71 72 73 74
  cmServer* m_Server;

  friend class cmServer;
};

class cmServerProtocol
{
public:
75 76
  cmServerProtocol() = default;
  virtual ~cmServerProtocol() = default;
77

wahikihiki's avatar
wahikihiki committed
78 79 80
  cmServerProtocol(cmServerProtocol const&) = delete;
  cmServerProtocol& operator=(cmServerProtocol const&) = delete;

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

85 86
  bool Activate(cmServer* server, const cmServerRequest& request,
                std::string* errorMessage);
87

88
  cmFileMonitor* FileMonitor() const;
89 90
  void SendSignal(const std::string& name, const Json::Value& data) const;

91 92 93 94 95 96 97 98
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;
99
  cmServer* m_Server = nullptr; // not owned!
100 101

  friend class cmServer;
102
};
103

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

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

115 116
  void HandleCMakeFileChanges(const std::string& path, int event, int status);

117
  // Handle requests:
118
  cmServerResponse ProcessCache(const cmServerRequest& request);
119
  cmServerResponse ProcessCMakeInputs(const cmServerRequest& request);
120
  cmServerResponse ProcessCodeModel(const cmServerRequest& request);
121
  cmServerResponse ProcessCompute(const cmServerRequest& request);
122
  cmServerResponse ProcessConfigure(const cmServerRequest& request);
123
  cmServerResponse ProcessGlobalSettings(const cmServerRequest& request);
124
  cmServerResponse ProcessSetGlobalSettings(const cmServerRequest& request);
125
  cmServerResponse ProcessFileSystemWatchers(const cmServerRequest& request);
126
  cmServerResponse ProcessCTests(const cmServerRequest& request);
127

128 129 130
  enum State
  {
    STATE_INACTIVE,
131
    STATE_ACTIVE,
132 133
    STATE_CONFIGURED,
    STATE_COMPUTED
134 135
  };
  State m_State = STATE_INACTIVE;
136 137

  bool m_isDirty = false;
138 139 140 141 142

  struct GeneratorInformation
  {
  public:
    GeneratorInformation() = default;
wahikihiki's avatar
wahikihiki committed
143 144 145 146
    GeneratorInformation(std::string generatorName,
                         std::string extraGeneratorName, std::string toolset,
                         std::string platform, std::string sourceDirectory,
                         std::string buildDirectory);
147 148 149 150 151 152 153 154 155 156 157 158 159

    void SetupGenerator(cmake* cm, std::string* errorMessage);

    std::string GeneratorName;
    std::string ExtraGeneratorName;
    std::string Toolset;
    std::string Platform;

    std::string SourceDirectory;
    std::string BuildDirectory;
  };

  GeneratorInformation GeneratorInfo;
160
};