server-mode: Report Messages from cmake to clients

Pass messages sent from cmake via Error(...) or Message(...) on
to clients.
This commit is contained in:
Tobias Hunger 2016-09-09 10:01:44 +02:00
parent ca7799482c
commit 537efe0561
5 changed files with 64 additions and 1 deletions

View File

@ -132,6 +132,21 @@ a message of type "reply" or "error" that complete the request.
the request that triggered the responses was delivered. the request that triggered the responses was delivered.
Type "message"
^^^^^^^^^^^^^^
A message is triggered when the server processes a request and produces some
form of output that should be displayed to the user. A Message has a "message"
with the actual text to display as well as a "title" with a suggested dialog
box title.
Example::
[== CMake Server ==[
{"cookie":"","message":"Something happened.","title":"Title Text","inReplyTo":"handshake","type":"message"}
]== CMake Server ==]
Specific Message Types Specific Message Types
---------------------- ----------------------

View File

@ -14,6 +14,7 @@
#include "cmServer.h" #include "cmServer.h"
#include "cmServerProtocol.h" #include "cmServerProtocol.h"
#include "cmSystemTools.h"
#include "cmVersionMacros.h" #include "cmVersionMacros.h"
#include "cmake.h" #include "cmake.h"
@ -30,6 +31,7 @@ static const std::string kERROR_MESSAGE_KEY = "errorMessage";
static const std::string kERROR_TYPE = "error"; static const std::string kERROR_TYPE = "error";
static const std::string kREPLY_TYPE = "reply"; static const std::string kREPLY_TYPE = "reply";
static const std::string kPROGRESS_TYPE = "progress"; static const std::string kPROGRESS_TYPE = "progress";
static const std::string kMESSAGE_TYPE = "message";
static const std::string kSTART_MAGIC = "[== CMake Server ==["; static const std::string kSTART_MAGIC = "[== CMake Server ==[";
static const std::string kEND_MAGIC = "]== CMake Server ==]"; static const std::string kEND_MAGIC = "]== CMake Server ==]";
@ -134,6 +136,8 @@ void cmServer::PopOne()
return; return;
} }
cmSystemTools::SetMessageCallback(reportMessage,
const_cast<cmServerRequest*>(&request));
if (this->Protocol) { if (this->Protocol) {
this->Protocol->CMakeInstance()->SetProgressCallback( this->Protocol->CMakeInstance()->SetProgressCallback(
reportProgress, const_cast<cmServerRequest*>(&request)); reportProgress, const_cast<cmServerRequest*>(&request));
@ -220,12 +224,25 @@ void cmServer::reportProgress(const char* msg, float progress, void* data)
const cmServerRequest* request = static_cast<const cmServerRequest*>(data); const cmServerRequest* request = static_cast<const cmServerRequest*>(data);
assert(request); assert(request);
if (progress < 0.0 || progress > 1.0) { if (progress < 0.0 || progress > 1.0) {
request->ReportProgress(0, 0, 0, msg); request->ReportMessage(msg, "");
} else { } else {
request->ReportProgress(0, static_cast<int>(progress * 1000), 1000, msg); request->ReportProgress(0, static_cast<int>(progress * 1000), 1000, msg);
} }
} }
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);
}
cmServerResponse cmServer::SetProtocolVersion(const cmServerRequest& request) cmServerResponse cmServer::SetProtocolVersion(const cmServerRequest& request)
{ {
if (request.Type != "handshake") if (request.Type != "handshake")
@ -354,6 +371,25 @@ void cmServer::WriteProgress(const cmServerRequest& request, int min,
this->WriteJsonObject(obj); this->WriteJsonObject(obj);
} }
void cmServer::WriteMessage(const cmServerRequest& request,
const std::string& message,
const std::string& title) const
{
if (message.empty())
return;
Json::Value obj = Json::objectValue;
obj[kTYPE_KEY] = kMESSAGE_TYPE;
obj[kREPLY_TO_KEY] = request.Type;
obj[kCOOKIE_KEY] = request.Cookie;
obj["message"] = message;
if (!title.empty()) {
obj["title"] = title;
}
WriteJsonObject(obj);
}
void cmServer::WriteParseError(const std::string& message) const void cmServer::WriteParseError(const std::string& message) const
{ {
Json::Value obj = Json::objectValue; Json::Value obj = Json::objectValue;

View File

@ -44,6 +44,8 @@ private:
void RegisterProtocol(cmServerProtocol* protocol); void RegisterProtocol(cmServerProtocol* protocol);
static void reportProgress(const char* msg, float progress, void* data); static void reportProgress(const char* msg, float progress, void* data);
static void reportMessage(const char* msg, const char* title, bool& cancel,
void* data);
// Handle requests: // Handle requests:
cmServerResponse SetProtocolVersion(const cmServerRequest& request); cmServerResponse SetProtocolVersion(const cmServerRequest& request);
@ -53,6 +55,8 @@ private:
// Write responses: // Write responses:
void WriteProgress(const cmServerRequest& request, int min, int current, void WriteProgress(const cmServerRequest& request, int min, int current,
int max, const std::string& message) const; int max, const std::string& message) const;
void WriteMessage(const cmServerRequest& request, const std::string& message,
const std::string& title) const;
void WriteResponse(const cmServerResponse& response) const; void WriteResponse(const cmServerResponse& response) const;
void WriteParseError(const std::string& message) const; void WriteParseError(const std::string& message) const;

View File

@ -46,6 +46,12 @@ void cmServerRequest::ReportProgress(int min, int current, int max,
this->m_Server->WriteProgress(*this, min, current, max, message); this->m_Server->WriteProgress(*this, min, current, max, message);
} }
void cmServerRequest::ReportMessage(const std::string& message,
const std::string& title) const
{
m_Server->WriteMessage(*this, message, title);
}
cmServerResponse cmServerRequest::Reply(const Json::Value& data) const cmServerResponse cmServerRequest::Reply(const Json::Value& data) const
{ {
cmServerResponse response(*this); cmServerResponse response(*this);

View File

@ -70,6 +70,8 @@ private:
void ReportProgress(int min, int current, int max, void ReportProgress(int min, int current, int max,
const std::string& message) const; const std::string& message) const;
void ReportMessage(const std::string& message,
const std::string& title) const;
cmServer* m_Server; cmServer* m_Server;