2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-jsonrpc.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
JsonRpcMessage::JsonRpcMessage(void)
|
|
|
|
{
|
|
|
|
m_JSON = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonRpcMessage::~JsonRpcMessage(void)
|
|
|
|
{
|
|
|
|
cJSON_Delete(m_JSON);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonRpcMessage::RefType JsonRpcMessage::FromNetstring(Netstring::RefType ns)
|
|
|
|
{
|
|
|
|
JsonRpcMessage::RefType msg = new_object<JsonRpcMessage>();
|
|
|
|
msg->m_JSON = cJSON_Parse(ns->ToString());
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
Netstring::RefType JsonRpcMessage::ToNetstring(void)
|
|
|
|
{
|
2012-03-28 15:56:39 +02:00
|
|
|
Netstring::RefType ns = new_object<Netstring>();
|
|
|
|
char *msg = cJSON_Print(m_JSON);
|
|
|
|
ns->SetString(msg);
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonRpcMessage::SetFieldString(const char *field, const string& value)
|
|
|
|
{
|
|
|
|
if (m_JSON == NULL)
|
|
|
|
m_JSON = cJSON_CreateObject();
|
|
|
|
|
|
|
|
cJSON *object = cJSON_CreateString(value.c_str());
|
|
|
|
cJSON_DeleteItemFromObject(m_JSON, field);
|
|
|
|
cJSON_AddItemToObject(m_JSON, field, object);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string JsonRpcMessage::GetFieldString(const char *field)
|
|
|
|
{
|
2012-03-28 15:56:39 +02:00
|
|
|
if (m_JSON == NULL)
|
|
|
|
m_JSON = cJSON_CreateObject();
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
cJSON *idObject = cJSON_GetObjectItem(m_JSON, field);
|
|
|
|
|
|
|
|
if (idObject == NULL || idObject->type != cJSON_String)
|
|
|
|
return string();
|
|
|
|
|
|
|
|
return string(idObject->valuestring);
|
|
|
|
}
|
|
|
|
|
2012-03-28 15:56:39 +02:00
|
|
|
void JsonRpcMessage::SetVersion(const string& version)
|
|
|
|
{
|
|
|
|
SetFieldString("version", version);
|
|
|
|
}
|
|
|
|
|
|
|
|
string JsonRpcMessage::GetVersion(void)
|
|
|
|
{
|
|
|
|
return GetFieldString("jsonrpc");
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonRpcMessage::SetID(const string& id)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-28 15:56:39 +02:00
|
|
|
SetFieldString("id", id);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string JsonRpcMessage::GetID(void)
|
|
|
|
{
|
|
|
|
return GetFieldString("id");
|
|
|
|
}
|
|
|
|
|
2012-03-28 15:56:39 +02:00
|
|
|
void JsonRpcMessage::SetMethod(const string& method)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-28 15:56:39 +02:00
|
|
|
SetFieldString("method", method);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string JsonRpcMessage::GetMethod(void)
|
|
|
|
{
|
|
|
|
return GetFieldString("method");
|
|
|
|
}
|
|
|
|
|
2012-03-28 15:56:39 +02:00
|
|
|
void JsonRpcMessage::SetParams(const string& params)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-28 15:56:39 +02:00
|
|
|
SetFieldString("params", params);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string JsonRpcMessage::GetParams(void)
|
|
|
|
{
|
|
|
|
return GetFieldString("params");
|
|
|
|
}
|
|
|
|
|
2012-03-28 15:56:39 +02:00
|
|
|
void JsonRpcMessage::SetResult(const string& result)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-28 15:56:39 +02:00
|
|
|
SetFieldString("result", result);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string JsonRpcMessage::GetResult(void)
|
|
|
|
{
|
|
|
|
return GetFieldString("result");
|
|
|
|
}
|
|
|
|
|
2012-03-28 15:56:39 +02:00
|
|
|
void JsonRpcMessage::SetError(const string& error)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-28 15:56:39 +02:00
|
|
|
SetFieldString("error", error);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string JsonRpcMessage::GetError(void)
|
|
|
|
{
|
|
|
|
return GetFieldString("error");
|
|
|
|
}
|