Implemented support for sending reply messages.

This commit is contained in:
Gunnar Beutner 2012-03-28 15:56:39 +02:00
parent ed402c45c5
commit 265a8231c8
6 changed files with 66 additions and 14 deletions

View File

@ -32,7 +32,9 @@ FIFO::RefType TCPClient::GetRecvQueue(void)
int TCPClient::ReadableEventHandler(EventArgs::RefType ea) int TCPClient::ReadableEventHandler(EventArgs::RefType ea)
{ {
char buffer[4096]; char buffer[4096];
int rc; int read_total, rc;
read_total = 0;
while (true) { while (true) {
rc = recv(GetFD(), buffer, sizeof(buffer), 0); rc = recv(GetFD(), buffer, sizeof(buffer), 0);
@ -50,8 +52,10 @@ int TCPClient::ReadableEventHandler(EventArgs::RefType ea)
} }
m_RecvQueue->Write(buffer, rc); m_RecvQueue->Write(buffer, rc);
read_total += rc;
if (m_RecvQueue->GetSize() > 1024 * 1024) /* make sure we don't starve other sockets */
if (read_total > 128 * 1024)
break; break;
} }

View File

@ -21,11 +21,27 @@ JsonRpcMessage::RefType JsonRpcMessage::FromNetstring(Netstring::RefType ns)
Netstring::RefType JsonRpcMessage::ToNetstring(void) 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) string JsonRpcMessage::GetFieldString(const char *field)
{ {
if (m_JSON == NULL)
m_JSON = cJSON_CreateObject();
cJSON *idObject = cJSON_GetObjectItem(m_JSON, field); cJSON *idObject = cJSON_GetObjectItem(m_JSON, field);
if (idObject == NULL || idObject->type != cJSON_String) if (idObject == NULL || idObject->type != cJSON_String)
@ -34,8 +50,19 @@ string JsonRpcMessage::GetFieldString(const char *field)
return string(idObject->valuestring); 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) string JsonRpcMessage::GetID(void)
@ -43,8 +70,9 @@ string JsonRpcMessage::GetID(void)
return GetFieldString("id"); return GetFieldString("id");
} }
void JsonRpcMessage::SetMethod(string method) void JsonRpcMessage::SetMethod(const string& method)
{ {
SetFieldString("method", method);
} }
string JsonRpcMessage::GetMethod(void) string JsonRpcMessage::GetMethod(void)
@ -52,8 +80,9 @@ string JsonRpcMessage::GetMethod(void)
return GetFieldString("method"); return GetFieldString("method");
} }
void JsonRpcMessage::SetParams(string params) void JsonRpcMessage::SetParams(const string& params)
{ {
SetFieldString("params", params);
} }
string JsonRpcMessage::GetParams(void) string JsonRpcMessage::GetParams(void)
@ -61,8 +90,9 @@ string JsonRpcMessage::GetParams(void)
return GetFieldString("params"); return GetFieldString("params");
} }
void JsonRpcMessage::SetResult(string result) void JsonRpcMessage::SetResult(const string& result)
{ {
SetFieldString("result", result);
} }
string JsonRpcMessage::GetResult(void) string JsonRpcMessage::GetResult(void)
@ -70,8 +100,9 @@ string JsonRpcMessage::GetResult(void)
return GetFieldString("result"); return GetFieldString("result");
} }
void JsonRpcMessage::SetError(string error) void JsonRpcMessage::SetError(const string& error)
{ {
SetFieldString("error", error);
} }
string JsonRpcMessage::GetError(void) string JsonRpcMessage::GetError(void)

View File

@ -9,6 +9,7 @@ class JsonRpcMessage : public Object
private: private:
cJSON *m_JSON; cJSON *m_JSON;
void SetFieldString(const char *field, const string& value);
string GetFieldString(const char *field); string GetFieldString(const char *field);
public: public:
@ -21,19 +22,22 @@ public:
static JsonRpcMessage::RefType FromNetstring(Netstring::RefType ns); static JsonRpcMessage::RefType FromNetstring(Netstring::RefType ns);
Netstring::RefType ToNetstring(void); Netstring::RefType ToNetstring(void);
void SetID(string id); void SetVersion(const string& version);
string GetVersion(void);
void SetID(const string& id);
string GetID(void); string GetID(void);
void SetMethod(string method); void SetMethod(const string& method);
string GetMethod(void); string GetMethod(void);
void SetParams(string params); void SetParams(const string& params);
string GetParams(void); string GetParams(void);
void SetResult(string result); void SetResult(const string& result);
string GetResult(void); string GetResult(void);
void SetError(string error); void SetError(const string& error);
string GetError(void); string GetError(void);
}; };

View File

@ -85,6 +85,12 @@ const void *Netstring::GetData(void) const
return m_Data; return m_Data;
} }
void Netstring::SetString(char *str)
{
m_Data = str;
m_Length = strlen(str);
}
const char *Netstring::ToString(void) const char *Netstring::ToString(void)
{ {
/* our implementation already guarantees that there's a NUL char at /* our implementation already guarantees that there's a NUL char at

View File

@ -23,6 +23,7 @@ public:
size_t GetSize(void) const; size_t GetSize(void) const;
const void *GetData(void) const; const void *GetData(void) const;
void SetString(char *str);
const char *ToString(void); const char *ToString(void);
}; };

View File

@ -50,9 +50,15 @@ public:
int MessageHandler(NewMessageEventArgs::RefType nea) int MessageHandler(NewMessageEventArgs::RefType nea)
{ {
JsonRpcClient::RefType client = static_pointer_cast<JsonRpcClient>(nea->Source);
JsonRpcMessage::RefType msg = nea->Message; 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; return 0;
} }