2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2016-09-13 09:26:34 +00:00
|
|
|
#include "cmServer.h"
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
#include "cmServerConnection.h"
|
2016-09-09 08:01:44 +00:00
|
|
|
#include "cmServerDictionary.h"
|
2016-09-13 09:26:34 +00:00
|
|
|
#include "cmServerProtocol.h"
|
2016-09-09 08:01:44 +00:00
|
|
|
#include "cmSystemTools.h"
|
2016-09-13 09:26:34 +00:00
|
|
|
#include "cm_jsoncpp_reader.h"
|
2017-05-05 21:21:39 +00:00
|
|
|
#include "cm_jsoncpp_writer.h"
|
|
|
|
#include "cmake.h"
|
|
|
|
#include "cmsys/FStream.hxx"
|
2016-09-13 09:26:34 +00:00
|
|
|
|
2016-11-05 20:40:14 +00:00
|
|
|
#include <algorithm>
|
2017-05-05 21:21:39 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <utility>
|
2016-09-09 08:01:44 +00:00
|
|
|
|
|
|
|
class cmServer::DebugInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DebugInfo()
|
|
|
|
: StartTime(uv_hrtime())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PrintStatistics = false;
|
|
|
|
|
|
|
|
std::string OutputFile;
|
|
|
|
uint64_t StartTime;
|
|
|
|
};
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
cmServer::cmServer(cmServerConnection* conn, bool supportExperimental)
|
|
|
|
: Connection(conn)
|
|
|
|
, SupportExperimental(supportExperimental)
|
2016-09-13 09:26:34 +00:00
|
|
|
{
|
2016-09-09 08:01:44 +00:00
|
|
|
this->Connection->SetServer(this);
|
2016-09-13 09:39:24 +00:00
|
|
|
// Register supported protocols:
|
|
|
|
this->RegisterProtocol(new cmServerProtocol1_0);
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmServer::~cmServer()
|
|
|
|
{
|
2016-09-09 08:01:44 +00:00
|
|
|
if (!this->Protocol) { // Server was never fully started!
|
2016-09-13 09:26:34 +00:00
|
|
|
return;
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
|
|
|
|
for (cmServerProtocol* p : this->SupportedProtocols) {
|
|
|
|
delete p;
|
|
|
|
}
|
2016-09-09 08:01:44 +00:00
|
|
|
|
|
|
|
delete this->Connection;
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmServer::PopOne()
|
|
|
|
{
|
|
|
|
if (this->Queue.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Reader reader;
|
|
|
|
Json::Value value;
|
|
|
|
const std::string input = this->Queue.front();
|
|
|
|
this->Queue.erase(this->Queue.begin());
|
|
|
|
|
|
|
|
if (!reader.parse(input, value)) {
|
|
|
|
this->WriteParseError("Failed to parse JSON input.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
std::unique_ptr<DebugInfo> debug;
|
|
|
|
Json::Value debugValue = value["debug"];
|
|
|
|
if (!debugValue.isNull()) {
|
|
|
|
debug = std::make_unique<DebugInfo>();
|
|
|
|
debug->OutputFile = debugValue["dumpToFile"].asString();
|
|
|
|
debug->PrintStatistics = debugValue["showStats"].asBool();
|
|
|
|
}
|
|
|
|
|
2016-09-13 09:26:34 +00:00
|
|
|
const cmServerRequest request(this, value[kTYPE_KEY].asString(),
|
|
|
|
value[kCOOKIE_KEY].asString(), value);
|
|
|
|
|
|
|
|
if (request.Type == "") {
|
|
|
|
cmServerResponse response(request);
|
|
|
|
response.SetError("No type given in request.");
|
2016-09-09 08:01:44 +00:00
|
|
|
this->WriteResponse(response, nullptr);
|
2016-09-13 09:26:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
cmSystemTools::SetMessageCallback(reportMessage,
|
|
|
|
const_cast<cmServerRequest*>(&request));
|
2016-09-09 08:01:44 +00:00
|
|
|
if (this->Protocol) {
|
|
|
|
this->Protocol->CMakeInstance()->SetProgressCallback(
|
|
|
|
reportProgress, const_cast<cmServerRequest*>(&request));
|
2016-09-09 08:01:44 +00:00
|
|
|
this->WriteResponse(this->Protocol->Process(request), debug.get());
|
2016-09-09 08:01:44 +00:00
|
|
|
} else {
|
2016-09-09 08:01:44 +00:00
|
|
|
this->WriteResponse(this->SetProtocolVersion(request), debug.get());
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmServer::RegisterProtocol(cmServerProtocol* protocol)
|
|
|
|
{
|
2016-09-09 08:01:44 +00:00
|
|
|
if (protocol->IsExperimental() && !this->SupportExperimental) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
auto version = protocol->ProtocolVersion();
|
|
|
|
assert(version.first >= 0);
|
|
|
|
assert(version.second >= 0);
|
|
|
|
auto it = std::find_if(this->SupportedProtocols.begin(),
|
|
|
|
this->SupportedProtocols.end(),
|
|
|
|
[version](cmServerProtocol* p) {
|
|
|
|
return p->ProtocolVersion() == version;
|
|
|
|
});
|
2016-09-23 20:43:36 +00:00
|
|
|
if (it == this->SupportedProtocols.end()) {
|
2016-09-13 09:26:34 +00:00
|
|
|
this->SupportedProtocols.push_back(protocol);
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmServer::PrintHello() const
|
|
|
|
{
|
|
|
|
Json::Value hello = Json::objectValue;
|
|
|
|
hello[kTYPE_KEY] = "hello";
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
Json::Value& protocolVersions = hello[kSUPPORTED_PROTOCOL_VERSIONS] =
|
2016-09-13 09:26:34 +00:00
|
|
|
Json::arrayValue;
|
|
|
|
|
|
|
|
for (auto const& proto : this->SupportedProtocols) {
|
|
|
|
auto version = proto->ProtocolVersion();
|
|
|
|
Json::Value tmp = Json::objectValue;
|
2016-09-09 08:01:44 +00:00
|
|
|
tmp[kMAJOR_KEY] = version.first;
|
|
|
|
tmp[kMINOR_KEY] = version.second;
|
2016-09-09 08:01:44 +00:00
|
|
|
if (proto->IsExperimental()) {
|
2016-09-09 08:01:44 +00:00
|
|
|
tmp[kIS_EXPERIMENTAL_KEY] = true;
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
protocolVersions.append(tmp);
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
this->WriteJsonObject(hello, nullptr);
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
void cmServer::QueueRequest(const std::string& request)
|
|
|
|
{
|
|
|
|
this->Queue.push_back(request);
|
|
|
|
this->PopOne();
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
void cmServer::reportProgress(const char* msg, float progress, void* data)
|
|
|
|
{
|
|
|
|
const cmServerRequest* request = static_cast<const cmServerRequest*>(data);
|
|
|
|
assert(request);
|
2017-05-24 14:55:45 +00:00
|
|
|
if (progress < 0.0f || progress > 1.0f) {
|
2016-09-09 08:01:44 +00:00
|
|
|
request->ReportMessage(msg, "");
|
2016-09-09 08:01:44 +00:00
|
|
|
} else {
|
|
|
|
request->ReportProgress(0, static_cast<int>(progress * 1000), 1000, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
void cmServer::reportMessage(const char* msg, const char* title,
|
|
|
|
bool& /* cancel */, void* data)
|
|
|
|
{
|
|
|
|
const cmServerRequest* request = static_cast<const cmServerRequest*>(data);
|
|
|
|
assert(request);
|
|
|
|
assert(msg);
|
|
|
|
std::string titleString;
|
|
|
|
if (title) {
|
|
|
|
titleString = title;
|
|
|
|
}
|
|
|
|
request->ReportMessage(std::string(msg), titleString);
|
|
|
|
}
|
|
|
|
|
2016-09-13 09:26:34 +00:00
|
|
|
cmServerResponse cmServer::SetProtocolVersion(const cmServerRequest& request)
|
|
|
|
{
|
2016-09-23 20:43:36 +00:00
|
|
|
if (request.Type != kHANDSHAKE_TYPE) {
|
2016-09-09 08:01:44 +00:00
|
|
|
return request.ReportError("Waiting for type \"" + kHANDSHAKE_TYPE +
|
|
|
|
"\".");
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
Json::Value requestedProtocolVersion = request.Data[kPROTOCOL_VERSION_KEY];
|
2016-09-23 20:43:36 +00:00
|
|
|
if (requestedProtocolVersion.isNull()) {
|
2016-09-09 08:01:44 +00:00
|
|
|
return request.ReportError("\"" + kPROTOCOL_VERSION_KEY +
|
|
|
|
"\" is required for \"" + kHANDSHAKE_TYPE +
|
|
|
|
"\".");
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
|
2016-09-23 20:43:36 +00:00
|
|
|
if (!requestedProtocolVersion.isObject()) {
|
2016-09-09 08:01:44 +00:00
|
|
|
return request.ReportError("\"" + kPROTOCOL_VERSION_KEY +
|
|
|
|
"\" must be a JSON object.");
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
Json::Value majorValue = requestedProtocolVersion[kMAJOR_KEY];
|
2016-09-23 20:43:36 +00:00
|
|
|
if (!majorValue.isInt()) {
|
2016-09-09 08:01:44 +00:00
|
|
|
return request.ReportError("\"" + kMAJOR_KEY +
|
|
|
|
"\" must be set and an integer.");
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
Json::Value minorValue = requestedProtocolVersion[kMINOR_KEY];
|
2016-09-23 20:43:36 +00:00
|
|
|
if (!minorValue.isNull() && !minorValue.isInt()) {
|
2016-09-09 08:01:44 +00:00
|
|
|
return request.ReportError("\"" + kMINOR_KEY +
|
|
|
|
"\" must be unset or an integer.");
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
|
|
|
|
const int major = majorValue.asInt();
|
|
|
|
const int minor = minorValue.isNull() ? -1 : minorValue.asInt();
|
2016-09-23 20:43:36 +00:00
|
|
|
if (major < 0) {
|
2016-09-09 08:01:44 +00:00
|
|
|
return request.ReportError("\"" + kMAJOR_KEY + "\" must be >= 0.");
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
|
|
|
if (!minorValue.isNull() && minor < 0) {
|
2016-09-09 08:01:44 +00:00
|
|
|
return request.ReportError("\"" + kMINOR_KEY +
|
|
|
|
"\" must be >= 0 when set.");
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
|
|
|
|
this->Protocol =
|
|
|
|
this->FindMatchingProtocol(this->SupportedProtocols, major, minor);
|
|
|
|
if (!this->Protocol) {
|
|
|
|
return request.ReportError("Protocol version not supported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string errorMessage;
|
2016-09-09 10:50:23 +00:00
|
|
|
if (!this->Protocol->Activate(this, request, &errorMessage)) {
|
2016-09-13 09:26:34 +00:00
|
|
|
this->Protocol = CM_NULLPTR;
|
|
|
|
return request.ReportError("Failed to activate protocol version: " +
|
|
|
|
errorMessage);
|
|
|
|
}
|
|
|
|
return request.Reply(Json::objectValue);
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
bool cmServer::Serve(std::string* errorMessage)
|
2016-09-13 09:26:34 +00:00
|
|
|
{
|
2016-09-09 08:01:44 +00:00
|
|
|
if (this->SupportedProtocols.empty()) {
|
2016-09-09 08:01:44 +00:00
|
|
|
*errorMessage =
|
|
|
|
"No protocol versions defined. Maybe you need --experimental?";
|
2016-09-09 08:01:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
assert(!this->Protocol);
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
return Connection->ProcessEvents(errorMessage);
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:46 +00:00
|
|
|
cmFileMonitor* cmServer::FileMonitor() const
|
|
|
|
{
|
|
|
|
return Connection->FileMonitor();
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
void cmServer::WriteJsonObject(const Json::Value& jsonValue,
|
|
|
|
const DebugInfo* debug) const
|
2016-09-13 09:26:34 +00:00
|
|
|
{
|
|
|
|
Json::FastWriter writer;
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
auto beforeJson = uv_hrtime();
|
|
|
|
std::string result = writer.write(jsonValue);
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
Json::Value copy = jsonValue;
|
|
|
|
if (debug->PrintStatistics) {
|
|
|
|
Json::Value stats = Json::objectValue;
|
|
|
|
auto endTime = uv_hrtime();
|
|
|
|
|
|
|
|
stats["jsonSerialization"] = double(endTime - beforeJson) / 1000000.0;
|
|
|
|
stats["totalTime"] = double(endTime - debug->StartTime) / 1000000.0;
|
|
|
|
stats["size"] = static_cast<int>(result.size());
|
|
|
|
if (!debug->OutputFile.empty()) {
|
|
|
|
stats["dumpFile"] = debug->OutputFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy["zzzDebug"] = stats;
|
|
|
|
|
|
|
|
result = writer.write(copy); // Update result to include debug info
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!debug->OutputFile.empty()) {
|
2017-05-05 21:21:39 +00:00
|
|
|
cmsys::ofstream myfile(debug->OutputFile.c_str());
|
2016-09-09 08:01:44 +00:00
|
|
|
myfile << result;
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
Connection->WriteData(std::string("\n") + kSTART_MAGIC + std::string("\n") +
|
|
|
|
result + kEND_MAGIC + std::string("\n"));
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmServerProtocol* cmServer::FindMatchingProtocol(
|
|
|
|
const std::vector<cmServerProtocol*>& protocols, int major, int minor)
|
|
|
|
{
|
|
|
|
cmServerProtocol* bestMatch = nullptr;
|
|
|
|
for (auto protocol : protocols) {
|
|
|
|
auto version = protocol->ProtocolVersion();
|
2016-09-23 20:43:36 +00:00
|
|
|
if (major != version.first) {
|
2016-09-13 09:26:34 +00:00
|
|
|
continue;
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
|
|
|
if (minor == version.second) {
|
2016-09-13 09:26:34 +00:00
|
|
|
return protocol;
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
|
|
|
if (!bestMatch || bestMatch->ProtocolVersion().second < version.second) {
|
2016-09-13 09:26:34 +00:00
|
|
|
bestMatch = protocol;
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
return minor < 0 ? bestMatch : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmServer::WriteProgress(const cmServerRequest& request, int min,
|
|
|
|
int current, int max,
|
|
|
|
const std::string& message) const
|
|
|
|
{
|
|
|
|
assert(min <= current && current <= max);
|
|
|
|
assert(message.length() != 0);
|
|
|
|
|
|
|
|
Json::Value obj = Json::objectValue;
|
2016-09-20 13:29:33 +00:00
|
|
|
obj[kTYPE_KEY] = kPROGRESS_TYPE;
|
|
|
|
obj[kREPLY_TO_KEY] = request.Type;
|
2016-09-13 09:26:34 +00:00
|
|
|
obj[kCOOKIE_KEY] = request.Cookie;
|
2016-09-09 08:01:44 +00:00
|
|
|
obj[kPROGRESS_MESSAGE_KEY] = message;
|
|
|
|
obj[kPROGRESS_MINIMUM_KEY] = min;
|
|
|
|
obj[kPROGRESS_MAXIMUM_KEY] = max;
|
|
|
|
obj[kPROGRESS_CURRENT_KEY] = current;
|
2016-09-13 09:26:34 +00:00
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
this->WriteJsonObject(obj, nullptr);
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
void cmServer::WriteMessage(const cmServerRequest& request,
|
|
|
|
const std::string& message,
|
|
|
|
const std::string& title) const
|
|
|
|
{
|
2016-09-23 20:43:36 +00:00
|
|
|
if (message.empty()) {
|
2016-09-09 08:01:44 +00:00
|
|
|
return;
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2016-09-09 08:01:44 +00:00
|
|
|
|
|
|
|
Json::Value obj = Json::objectValue;
|
|
|
|
obj[kTYPE_KEY] = kMESSAGE_TYPE;
|
|
|
|
obj[kREPLY_TO_KEY] = request.Type;
|
|
|
|
obj[kCOOKIE_KEY] = request.Cookie;
|
2016-09-09 08:01:44 +00:00
|
|
|
obj[kMESSAGE_KEY] = message;
|
2016-09-09 08:01:44 +00:00
|
|
|
if (!title.empty()) {
|
2016-09-09 08:01:44 +00:00
|
|
|
obj[kTITLE_KEY] = title;
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
WriteJsonObject(obj, nullptr);
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 09:26:34 +00:00
|
|
|
void cmServer::WriteParseError(const std::string& message) const
|
|
|
|
{
|
|
|
|
Json::Value obj = Json::objectValue;
|
2016-09-20 13:29:33 +00:00
|
|
|
obj[kTYPE_KEY] = kERROR_TYPE;
|
|
|
|
obj[kERROR_MESSAGE_KEY] = message;
|
|
|
|
obj[kREPLY_TO_KEY] = "";
|
2016-09-13 09:26:34 +00:00
|
|
|
obj[kCOOKIE_KEY] = "";
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
this->WriteJsonObject(obj, nullptr);
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:45 +00:00
|
|
|
void cmServer::WriteSignal(const std::string& name,
|
|
|
|
const Json::Value& data) const
|
|
|
|
{
|
|
|
|
assert(data.isObject());
|
|
|
|
Json::Value obj = data;
|
|
|
|
obj[kTYPE_KEY] = kSIGNAL_TYPE;
|
|
|
|
obj[kREPLY_TO_KEY] = "";
|
|
|
|
obj[kCOOKIE_KEY] = "";
|
|
|
|
obj[kNAME_KEY] = name;
|
|
|
|
|
|
|
|
WriteJsonObject(obj, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
void cmServer::WriteResponse(const cmServerResponse& response,
|
|
|
|
const DebugInfo* debug) const
|
2016-09-13 09:26:34 +00:00
|
|
|
{
|
|
|
|
assert(response.IsComplete());
|
|
|
|
|
|
|
|
Json::Value obj = response.Data();
|
|
|
|
obj[kCOOKIE_KEY] = response.Cookie;
|
2016-09-20 13:29:33 +00:00
|
|
|
obj[kTYPE_KEY] = response.IsError() ? kERROR_TYPE : kREPLY_TYPE;
|
|
|
|
obj[kREPLY_TO_KEY] = response.Type;
|
2016-09-13 09:26:34 +00:00
|
|
|
if (response.IsError()) {
|
2016-09-20 13:29:33 +00:00
|
|
|
obj[kERROR_MESSAGE_KEY] = response.ErrorMessage();
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
this->WriteJsonObject(obj, debug);
|
2016-09-13 09:26:34 +00:00
|
|
|
}
|