mirror of https://github.com/Icinga/icinga2.git
Implemented support for sending reply messages.
This commit is contained in:
parent
ed402c45c5
commit
265a8231c8
|
@ -32,7 +32,9 @@ FIFO::RefType TCPClient::GetRecvQueue(void)
|
|||
int TCPClient::ReadableEventHandler(EventArgs::RefType ea)
|
||||
{
|
||||
char buffer[4096];
|
||||
int rc;
|
||||
int read_total, rc;
|
||||
|
||||
read_total = 0;
|
||||
|
||||
while (true) {
|
||||
rc = recv(GetFD(), buffer, sizeof(buffer), 0);
|
||||
|
@ -50,8 +52,10 @@ int TCPClient::ReadableEventHandler(EventArgs::RefType ea)
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -21,11 +21,27 @@ JsonRpcMessage::RefType JsonRpcMessage::FromNetstring(Netstring::RefType ns)
|
|||
|
||||
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)
|
||||
|
@ -34,8 +50,19 @@ string JsonRpcMessage::GetFieldString(const char *field)
|
|||
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)
|
||||
|
@ -43,8 +70,9 @@ 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)
|
||||
|
@ -52,8 +80,9 @@ 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)
|
||||
|
@ -61,8 +90,9 @@ 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)
|
||||
|
@ -70,8 +100,9 @@ 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)
|
||||
|
|
|
@ -9,6 +9,7 @@ class JsonRpcMessage : public Object
|
|||
private:
|
||||
cJSON *m_JSON;
|
||||
|
||||
void SetFieldString(const char *field, const string& value);
|
||||
string GetFieldString(const char *field);
|
||||
|
||||
public:
|
||||
|
@ -21,19 +22,22 @@ 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);
|
||||
};
|
||||
|
||||
|
|
|
@ -85,6 +85,12 @@ const void *Netstring::GetData(void) const
|
|||
return m_Data;
|
||||
}
|
||||
|
||||
void Netstring::SetString(char *str)
|
||||
{
|
||||
m_Data = str;
|
||||
m_Length = strlen(str);
|
||||
}
|
||||
|
||||
const char *Netstring::ToString(void)
|
||||
{
|
||||
/* our implementation already guarantees that there's a NUL char at
|
||||
|
|
|
@ -23,6 +23,7 @@ public:
|
|||
size_t GetSize(void) const;
|
||||
const void *GetData(void) const;
|
||||
|
||||
void SetString(char *str);
|
||||
const char *ToString(void);
|
||||
};
|
||||
|
||||
|
|
|
@ -50,8 +50,14 @@ public:
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue