added skeleton for the json connection server

This commit is contained in:
johan
2013-08-17 15:39:29 +02:00
parent 2c97353a11
commit 16c260b3dc
9 changed files with 269 additions and 17 deletions

View File

@@ -210,16 +210,15 @@ void JsonConnection::setTransform(ColorTransformValues *threshold, ColorTransfor
Json::Value JsonConnection::sendMessage(const Json::Value & message)
{
// serialize message (FastWriter already appends a newline)
std::string serializedMessage = Json::FastWriter().write(message);
// print command if requested
if (_printJson)
{
std::cout << "Command: " << message << std::endl;
std::cout << "Command: " << serializedMessage;
}
// serialize message (FastWriter already appends a newline
Json::FastWriter jsonWriter;
std::string serializedMessage = jsonWriter.write(message);
// write message
_socket.write(serializedMessage.c_str());
if (!_socket.waitForBytesWritten())
@@ -241,6 +240,12 @@ Json::Value JsonConnection::sendMessage(const Json::Value & message)
}
int bytes = serializedReply.indexOf('\n') + 1; // Find the end of message
// print reply if requested
if (_printJson)
{
std::cout << "Reply: " << std::string(serializedReply.data(), bytes);
}
// parse reply data
Json::Reader jsonReader;
Json::Value reply;
@@ -249,12 +254,6 @@ Json::Value JsonConnection::sendMessage(const Json::Value & message)
throw std::runtime_error("Error while parsing reply: invalid json");
}
// print reply if requested
if (_printJson)
{
std::cout << "Reply:" << reply << std::endl;
}
return reply;
}
@@ -271,12 +270,12 @@ bool JsonConnection::parseReply(const Json::Value &reply)
}
catch (const std::runtime_error &)
{
// Some json paring error: ignore and set parsing error
// Some json parsing error: ignore and set parsing error
}
if (!success)
{
throw std::runtime_error("Error while paring reply: " + reason);
throw std::runtime_error("Error while executing command: " + reason);
}
return success;