mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-20 20:24:33 +02:00
Refactored jsonrpc lib to use the new TcpClient interface.
This commit is contained in:
parent
52f4bd7a72
commit
8e118983be
@ -3,11 +3,11 @@ local object application "icinga" {
|
|||||||
cert = "icinga-c1.pem",
|
cert = "icinga-c1.pem",
|
||||||
|
|
||||||
node = "10.0.10.14",
|
node = "10.0.10.14",
|
||||||
service = 7777
|
service = 7778
|
||||||
}
|
}
|
||||||
|
|
||||||
local object component "configrpc" {
|
local object component "cibsync" {
|
||||||
configSource = 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
local object component "demo" {
|
local object component "demo" {
|
||||||
|
@ -40,9 +40,7 @@ JsonRpcClient::JsonRpcClient(TcpClientRole role, shared_ptr<SSL_CTX> sslContext)
|
|||||||
*/
|
*/
|
||||||
void JsonRpcClient::SendMessage(const MessagePart& message)
|
void JsonRpcClient::SendMessage(const MessagePart& message)
|
||||||
{
|
{
|
||||||
mutex::scoped_lock lock(GetMutex());
|
Netstring::WriteStringToIOQueue(this, message.ToJsonString());
|
||||||
|
|
||||||
Netstring::WriteStringToFIFO(GetSendQueue(), message.ToJsonString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,12 +52,8 @@ void JsonRpcClient::DataAvailableHandler(void)
|
|||||||
string jsonString;
|
string jsonString;
|
||||||
MessagePart message;
|
MessagePart message;
|
||||||
|
|
||||||
{
|
if (!Netstring::ReadStringFromIOQueue(this, &jsonString))
|
||||||
mutex::scoped_lock lock(GetMutex());
|
|
||||||
|
|
||||||
if (!Netstring::ReadStringFromFIFO(GetRecvQueue(), &jsonString))
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
message = MessagePart(jsonString);
|
message = MessagePart(jsonString);
|
||||||
|
@ -22,54 +22,86 @@
|
|||||||
using namespace icinga;
|
using namespace icinga;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads data from a FIFO in netstring format.
|
* Reads data from an IOQueue in netstring format.
|
||||||
*
|
*
|
||||||
* @param fifo The FIFO to read from.
|
* @param fifo The IOQueue to read from.
|
||||||
* @param[out] str The string that has been read from the FIFO.
|
* @param[out] str The string that has been read from the FIFO.
|
||||||
* @returns true if a complete string was read from the FIFO, false otherwise.
|
* @returns true if a complete string was read from the FIFO, false otherwise.
|
||||||
* @exception InvalidNetstringException The input stream is invalid.
|
* @exception invalid_argument The input stream is invalid.
|
||||||
* @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c
|
* @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c
|
||||||
*/
|
*/
|
||||||
bool Netstring::ReadStringFromFIFO(const FIFO::Ptr& fifo, string *str)
|
bool Netstring::ReadStringFromIOQueue(IOQueue *queue, string *str)
|
||||||
{
|
{
|
||||||
size_t buffer_length = fifo->GetSize();
|
size_t buffer_length = queue->GetAvailableBytes();
|
||||||
char *buffer = (char *)fifo->GetReadBuffer();
|
|
||||||
|
|
||||||
/* minimum netstring length is 3 */
|
/* minimum netstring length is 3 */
|
||||||
if (buffer_length < 3)
|
if (buffer_length < 3)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
/* limit the number of bytes we're reading for the header */
|
||||||
|
if (buffer_length > 16)
|
||||||
|
buffer_length = 16;
|
||||||
|
|
||||||
|
char *buffer = (char *)malloc(buffer_length);
|
||||||
|
queue->Peek(buffer, buffer_length);
|
||||||
|
|
||||||
/* no leading zeros allowed */
|
/* no leading zeros allowed */
|
||||||
if (buffer[0] == '0' && isdigit(buffer[1]))
|
if (buffer[0] == '0' && isdigit(buffer[1])) {
|
||||||
|
free(buffer);
|
||||||
throw invalid_argument("Invalid netstring (leading zero)");
|
throw invalid_argument("Invalid netstring (leading zero)");
|
||||||
|
}
|
||||||
|
|
||||||
size_t len, i;
|
size_t len, i;
|
||||||
|
|
||||||
len = 0;
|
len = 0;
|
||||||
for (i = 0; i < buffer_length && isdigit(buffer[i]); i++) {
|
for (i = 0; i < buffer_length && isdigit(buffer[i]); i++) {
|
||||||
/* length specifier must have at most 9 characters */
|
/* length specifier must have at most 9 characters */
|
||||||
if (i >= 9)
|
if (i >= 9) {
|
||||||
|
free(buffer);
|
||||||
throw invalid_argument("Length specifier must not exceed 9 characters");
|
throw invalid_argument("Length specifier must not exceed 9 characters");
|
||||||
|
}
|
||||||
|
|
||||||
len = len * 10 + (buffer[i] - '0');
|
len = len * 10 + (buffer[i] - '0');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buffer_length = queue->GetAvailableBytes();
|
||||||
|
|
||||||
/* make sure the buffer is large enough */
|
/* make sure the buffer is large enough */
|
||||||
if (i + len + 1 >= buffer_length)
|
if (i + len + 1 >= buffer_length)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
/* limit the number of bytes we're reading to this message */
|
||||||
|
buffer_length = i + 1 + len + 1;
|
||||||
|
|
||||||
|
char *new_buffer = (char *)realloc(buffer, buffer_length);
|
||||||
|
|
||||||
|
if (new_buffer == NULL) {
|
||||||
|
free(buffer);
|
||||||
|
throw std::bad_alloc();
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = new_buffer;
|
||||||
|
|
||||||
|
queue->Peek(buffer, buffer_length);
|
||||||
|
|
||||||
/* check for the colon delimiter */
|
/* check for the colon delimiter */
|
||||||
if (buffer[i++] != ':')
|
if (buffer[i] != ':') {
|
||||||
|
free(buffer);
|
||||||
throw invalid_argument("Invalid Netstring (missing :)");
|
throw invalid_argument("Invalid Netstring (missing :)");
|
||||||
|
}
|
||||||
|
|
||||||
/* check for the comma delimiter after the string */
|
/* check for the comma delimiter after the string */
|
||||||
if (buffer[i + len] != ',')
|
if (buffer[i + 1 + len] != ',') {
|
||||||
|
free(buffer);
|
||||||
throw invalid_argument("Invalid Netstring (missing ,)");
|
throw invalid_argument("Invalid Netstring (missing ,)");
|
||||||
|
}
|
||||||
|
|
||||||
*str = string(&buffer[i], &buffer[i + len]);
|
*str = string(&buffer[i + 1], &buffer[i + 1 + len]);
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
/* remove the data from the fifo */
|
/* remove the data from the fifo */
|
||||||
fifo->Read(NULL, i + len + 1);
|
queue->Read(NULL, buffer_length);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -80,14 +112,13 @@ bool Netstring::ReadStringFromFIFO(const FIFO::Ptr& fifo, string *str)
|
|||||||
* @param fifo The FIFO.
|
* @param fifo The FIFO.
|
||||||
* @param str The string that is to be written.
|
* @param str The string that is to be written.
|
||||||
*/
|
*/
|
||||||
void Netstring::WriteStringToFIFO(const FIFO::Ptr& fifo, const string& str)
|
void Netstring::WriteStringToIOQueue(IOQueue *queue, const string& str)
|
||||||
{
|
{
|
||||||
stringstream prefixbuf;
|
stringstream prefixbuf;
|
||||||
prefixbuf << str.size() << ":";
|
prefixbuf << str.size() << ":";
|
||||||
|
|
||||||
string prefix = prefixbuf.str();
|
string prefix = prefixbuf.str();
|
||||||
fifo->Write(prefix.c_str(), prefix.size());
|
queue->Write(prefix.c_str(), prefix.size());
|
||||||
fifo->Write(str.c_str(), str.size());
|
queue->Write(str.c_str(), str.size());
|
||||||
|
queue->Write(",", 1);
|
||||||
fifo->Write(",", 1);
|
|
||||||
}
|
}
|
||||||
|
@ -23,13 +23,6 @@
|
|||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* Thrown when an invalid netstring was encountered while reading from a FIFO.
|
|
||||||
*
|
|
||||||
* @ingroup jsonrpc
|
|
||||||
*/
|
|
||||||
DEFINE_EXCEPTION_CLASS(InvalidNetstringException);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper functions for reading/writing messages in the netstring format.
|
* Helper functions for reading/writing messages in the netstring format.
|
||||||
*
|
*
|
||||||
@ -40,8 +33,8 @@ DEFINE_EXCEPTION_CLASS(InvalidNetstringException);
|
|||||||
class I2_JSONRPC_API Netstring
|
class I2_JSONRPC_API Netstring
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static bool ReadStringFromFIFO(const FIFO::Ptr& fifo, string *message);
|
static bool ReadStringFromIOQueue(IOQueue *queue, string *message);
|
||||||
static void WriteStringToFIFO(const FIFO::Ptr& fifo, const string& message);
|
static void WriteStringToIOQueue(IOQueue *queue, const string& message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Netstring(void);
|
Netstring(void);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user