Netstring::RefType JsonRpcMessage::ToNetstring(void)
{
- return Netstring::RefType();
+ 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);
}
string JsonRpcMessage::GetFieldString(const char *field)
{
+ if (m_JSON == NULL)
+ m_JSON = cJSON_CreateObject();
+
cJSON *idObject = cJSON_GetObjectItem(m_JSON, field);
if (idObject == NULL || idObject->type != cJSON_String)
return string(idObject->valuestring);
}
-void JsonRpcMessage::SetID(string id)
+void JsonRpcMessage::SetVersion(const string& version)
+{
+ SetFieldString("version", version);
+}
+
+string JsonRpcMessage::GetVersion(void)
+{
+ return GetFieldString("jsonrpc");
+}
+
+void JsonRpcMessage::SetID(const string& id)
{
+ SetFieldString("id", id);
}
string JsonRpcMessage::GetID(void)
return GetFieldString("id");
}
-void JsonRpcMessage::SetMethod(string method)
+void JsonRpcMessage::SetMethod(const string& method)
{
+ SetFieldString("method", method);
}
string JsonRpcMessage::GetMethod(void)
return GetFieldString("method");
}
-void JsonRpcMessage::SetParams(string params)
+void JsonRpcMessage::SetParams(const string& params)
{
+ SetFieldString("params", params);
}
string JsonRpcMessage::GetParams(void)
return GetFieldString("params");
}
-void JsonRpcMessage::SetResult(string result)
+void JsonRpcMessage::SetResult(const string& result)
{
+ SetFieldString("result", result);
}
string JsonRpcMessage::GetResult(void)
return GetFieldString("result");
}
-void JsonRpcMessage::SetError(string error)
+void JsonRpcMessage::SetError(const string& error)
{
+ SetFieldString("error", error);
}
string JsonRpcMessage::GetError(void)
private:
cJSON *m_JSON;
+ void SetFieldString(const char *field, const string& value);
string GetFieldString(const char *field);
public:
static JsonRpcMessage::RefType FromNetstring(Netstring::RefType ns);
Netstring::RefType ToNetstring(void);
- void SetID(string id);
+ void SetVersion(const string& version);
+ string GetVersion(void);
+
+ void SetID(const string& id);
string GetID(void);
- void SetMethod(string method);
+ void SetMethod(const string& method);
string GetMethod(void);
- void SetParams(string params);
+ void SetParams(const string& params);
string GetParams(void);
- void SetResult(string result);
+ void SetResult(const string& result);
string GetResult(void);
- void SetError(string error);
+ void SetError(const string& error);
string GetError(void);
};
int MessageHandler(NewMessageEventArgs::RefType nea)
{
+ JsonRpcClient::RefType client = static_pointer_cast<JsonRpcClient>(nea->Source);
JsonRpcMessage::RefType msg = nea->Message;
- //cout << "Message received: " << msg->GetID() << ": " << msg->GetMethod() << endl;
+ JsonRpcMessage::RefType response = new_object<JsonRpcMessage>();
+ response->SetVersion("2.0");
+ response->SetID(msg->GetID());
+ response->SetResult("moo");
+ client->SendMessage(response);
+
return 0;
}