Documentation updates.

Renamed classes to match style guide.
This commit is contained in:
Gunnar Beutner 2012-05-19 10:27:41 +02:00
parent 8b0c102cac
commit ce60fbd6ba
17 changed files with 317 additions and 108 deletions

View File

@ -22,11 +22,11 @@
using namespace icinga;
/**
* Constructor for the TCPClient class.
* Constructor for the TcpClient class.
*
* @param role The role of the TCP client socket.
*/
TCPClient::TCPClient(TCPClientRole role)
TcpClient::TcpClient(TcpClientRole role)
{
m_Role = role;
@ -39,7 +39,7 @@ TCPClient::TCPClient(TCPClientRole role)
*
* @returns The role.
*/
TCPClientRole TCPClient::GetRole(void) const
TcpClientRole TcpClient::GetRole(void) const
{
return m_Role;
}
@ -47,12 +47,12 @@ TCPClientRole TCPClient::GetRole(void) const
/**
* Registers the socket and starts processing events for it.
*/
void TCPClient::Start(void)
void TcpClient::Start(void)
{
TCPSocket::Start();
TcpSocket::Start();
OnReadable += bind_weak(&TCPClient::ReadableEventHandler, shared_from_this());
OnWritable += bind_weak(&TCPClient::WritableEventHandler, shared_from_this());
OnReadable += bind_weak(&TcpClient::ReadableEventHandler, shared_from_this());
OnWritable += bind_weak(&TcpClient::WritableEventHandler, shared_from_this());
}
/**
@ -61,7 +61,7 @@ void TCPClient::Start(void)
* @param node The node.
* @param service The service.
*/
void TCPClient::Connect(const string& node, const string& service)
void TcpClient::Connect(const string& node, const string& service)
{
m_Role = RoleOutbound;
@ -114,7 +114,7 @@ void TCPClient::Connect(const string& node, const string& service)
*
* @returns The send queue.
*/
FIFO::Ptr TCPClient::GetSendQueue(void)
FIFO::Ptr TcpClient::GetSendQueue(void)
{
return m_SendQueue;
}
@ -124,7 +124,7 @@ FIFO::Ptr TCPClient::GetSendQueue(void)
*
* @returns The recv queue.
*/
FIFO::Ptr TCPClient::GetRecvQueue(void)
FIFO::Ptr TcpClient::GetRecvQueue(void)
{
return m_RecvQueue;
}
@ -135,7 +135,7 @@ FIFO::Ptr TCPClient::GetRecvQueue(void)
* @param - Event arguments.
* @returns 0
*/
int TCPClient::ReadableEventHandler(const EventArgs&)
int TcpClient::ReadableEventHandler(const EventArgs&)
{
int rc;
@ -170,7 +170,7 @@ int TCPClient::ReadableEventHandler(const EventArgs&)
* @param - Event arguments.
* @returns 0
*/
int TCPClient::WritableEventHandler(const EventArgs&)
int TcpClient::WritableEventHandler(const EventArgs&)
{
int rc;
@ -191,7 +191,7 @@ int TCPClient::WritableEventHandler(const EventArgs&)
*
* @returns true
*/
bool TCPClient::WantsToRead(void) const
bool TcpClient::WantsToRead(void) const
{
return true;
}
@ -201,7 +201,7 @@ bool TCPClient::WantsToRead(void) const
*
* @returns true if data should be written, false otherwise.
*/
bool TCPClient::WantsToWrite(void) const
bool TcpClient::WantsToWrite(void) const
{
return (m_SendQueue->GetSize() > 0);
}
@ -212,7 +212,7 @@ bool TCPClient::WantsToWrite(void) const
* @param role The role of the new client.
* @returns The new client.
*/
TCPClient::Ptr icinga::TCPClientFactory(TCPClientRole role)
TcpClient::Ptr icinga::TcpClientFactory(TcpClientRole role)
{
return make_shared<TCPClient>(role);
return make_shared<TcpClient>(role);
}

View File

@ -28,7 +28,7 @@ namespace icinga
*
* @ingroup base
*/
enum TCPClientRole
enum TcpClientRole
{
RoleInbound, /**< Inbound socket, i.e. one that was returned
from accept(). */
@ -41,10 +41,10 @@ enum TCPClientRole
*
* @ingroup base
*/
class I2_BASE_API TCPClient : public TCPSocket
class I2_BASE_API TcpClient : public TcpSocket
{
private:
TCPClientRole m_Role;
TcpClientRole m_Role;
FIFO::Ptr m_SendQueue;
FIFO::Ptr m_RecvQueue;
@ -53,12 +53,12 @@ private:
virtual int WritableEventHandler(const EventArgs& ea);
public:
typedef shared_ptr<TCPClient> Ptr;
typedef weak_ptr<TCPClient> WeakPtr;
typedef shared_ptr<TcpClient> Ptr;
typedef weak_ptr<TcpClient> WeakPtr;
TCPClient(TCPClientRole role);
TcpClient(TcpClientRole role);
TCPClientRole GetRole(void) const;
TcpClientRole GetRole(void) const;
virtual void Start(void);
@ -74,13 +74,13 @@ public:
};
/**
* Returns a new unconnected TCPClient object that has the specified
* Returns a new unconnected TcpClient object that has the specified
* connection role.
*
* @param role The role of the new object.
* @returns A new TCPClient object.
* @returns A new TcpClient object.
*/
TCPClient::Ptr TCPClientFactory(TCPClientRole role);
TcpClient::Ptr TcpClientFactory(TcpClientRole role);
}

View File

@ -22,11 +22,11 @@
using namespace icinga;
/**
* Constructor for the TCPServer class.
* Constructor for the TcpServer class.
*/
TCPServer::TCPServer(void)
TcpServer::TcpServer(void)
{
m_ClientFactory = bind(&TCPClientFactory, RoleInbound);
m_ClientFactory = bind(&TcpClientFactory, RoleInbound);
}
/**
@ -34,7 +34,7 @@ TCPServer::TCPServer(void)
*
* @param clientFactory The client factory function.
*/
void TCPServer::SetClientFactory(function<TCPClient::Ptr()> clientFactory)
void TcpServer::SetClientFactory(function<TcpClient::Ptr()> clientFactory)
{
m_ClientFactory = clientFactory;
}
@ -44,7 +44,7 @@ void TCPServer::SetClientFactory(function<TCPClient::Ptr()> clientFactory)
*
* @returns The client factory function.
*/
function<TCPClient::Ptr()> TCPServer::GetFactoryFunction(void) const
function<TcpClient::Ptr()> TcpServer::GetFactoryFunction(void) const
{
return m_ClientFactory;
}
@ -52,17 +52,17 @@ function<TCPClient::Ptr()> TCPServer::GetFactoryFunction(void) const
/**
* Registers the TCP server and starts processing events for it.
*/
void TCPServer::Start(void)
void TcpServer::Start(void)
{
TCPSocket::Start();
TcpSocket::Start();
OnReadable += bind_weak(&TCPServer::ReadableEventHandler, shared_from_this());
OnReadable += bind_weak(&TcpServer::ReadableEventHandler, shared_from_this());
}
/**
* Starts listening for incoming client connections.
*/
void TCPServer::Listen(void)
void TcpServer::Listen(void)
{
int rc = listen(GetFD(), SOMAXCONN);
@ -79,7 +79,7 @@ void TCPServer::Listen(void)
* @param - Event arguments.
* @returns 0
*/
int TCPServer::ReadableEventHandler(const EventArgs&)
int TcpServer::ReadableEventHandler(const EventArgs&)
{
int fd;
sockaddr_storage addr;
@ -94,7 +94,7 @@ int TCPServer::ReadableEventHandler(const EventArgs&)
NewClientEventArgs nea;
nea.Source = shared_from_this();
nea.Client = static_pointer_cast<TCPSocket>(m_ClientFactory());
nea.Client = static_pointer_cast<TcpSocket>(m_ClientFactory());
nea.Client->SetFD(fd);
nea.Client->Start();
OnNewClient(nea);
@ -107,7 +107,7 @@ int TCPServer::ReadableEventHandler(const EventArgs&)
*
* @returns true
*/
bool TCPServer::WantsToRead(void) const
bool TcpServer::WantsToRead(void) const
{
return true;
}

View File

@ -30,7 +30,7 @@ namespace icinga
*/
struct I2_BASE_API NewClientEventArgs : public EventArgs
{
TCPSocket::Ptr Client; /**< The new client object. */
TcpSocket::Ptr Client; /**< The new client object. */
};
/**
@ -39,21 +39,21 @@ struct I2_BASE_API NewClientEventArgs : public EventArgs
*
* @ingroup base
*/
class I2_BASE_API TCPServer : public TCPSocket
class I2_BASE_API TcpServer : public TcpSocket
{
private:
int ReadableEventHandler(const EventArgs& ea);
function<TCPClient::Ptr()> m_ClientFactory;
function<TcpClient::Ptr()> m_ClientFactory;
public:
typedef shared_ptr<TCPServer> Ptr;
typedef weak_ptr<TCPServer> WeakPtr;
typedef shared_ptr<TcpServer> Ptr;
typedef weak_ptr<TcpServer> WeakPtr;
TCPServer(void);
TcpServer(void);
void SetClientFactory(function<TCPClient::Ptr()> function);
function<TCPClient::Ptr()> GetFactoryFunction(void) const;
void SetClientFactory(function<TcpClient::Ptr()> function);
function<TcpClient::Ptr()> GetFactoryFunction(void) const;
virtual void Start();

View File

@ -26,7 +26,7 @@ using namespace icinga;
*
* @param family The socket family for the new socket.
*/
void TCPSocket::MakeSocket(int family)
void TcpSocket::MakeSocket(int family)
{
assert(GetFD() == INVALID_SOCKET);
@ -47,7 +47,7 @@ void TCPSocket::MakeSocket(int family)
* @param service The service.
* @param family The address family for the socket.
*/
void TCPSocket::Bind(string service, int family)
void TcpSocket::Bind(string service, int family)
{
Bind(string(), service, family);
}
@ -58,7 +58,7 @@ void TCPSocket::Bind(string service, int family)
* @param service The service.
* @param family The address family for the socket.
*/
void TCPSocket::Bind(string node, string service, int family)
void TcpSocket::Bind(string node, string service, int family)
{
addrinfo hints;
addrinfo *result;

View File

@ -28,14 +28,14 @@ namespace icinga
*
* @ingroup base
*/
class I2_BASE_API TCPSocket : public Socket
class I2_BASE_API TcpSocket : public Socket
{
private:
void MakeSocket(int family);
public:
typedef shared_ptr<TCPSocket> Ptr;
typedef weak_ptr<TCPSocket> WeakPtr;
typedef shared_ptr<TcpSocket> Ptr;
typedef weak_ptr<TcpSocket> WeakPtr;
void Bind(string service, int family);
void Bind(string node, string service, int family);

View File

@ -21,16 +21,16 @@
using namespace icinga;
int I2_EXPORT TLSClient::m_SSLIndex;
bool I2_EXPORT TLSClient::m_SSLIndexInitialized = false;
int I2_EXPORT TlsClient::m_SSLIndex;
bool I2_EXPORT TlsClient::m_SSLIndexInitialized = false;
/**
* Constructor for the TLSClient class.
* Constructor for the TlsClient class.
*
* @param role The role of the client.
* @param sslContext The SSL context for the client.
*/
TLSClient::TLSClient(TCPClientRole role, shared_ptr<SSL_CTX> sslContext) : TCPClient(role)
TlsClient::TlsClient(TcpClientRole role, shared_ptr<SSL_CTX> sslContext) : TcpClient(role)
{
m_SSLContext = sslContext;
m_BlockRead = false;
@ -42,7 +42,7 @@ TLSClient::TLSClient(TCPClientRole role, shared_ptr<SSL_CTX> sslContext) : TCPCl
*
* @param certificate An X509 certificate.
*/
void TLSClient::NullCertificateDeleter(X509 *certificate)
void TlsClient::NullCertificateDeleter(X509 *certificate)
{
/* Nothing to do here. */
}
@ -52,9 +52,9 @@ void TLSClient::NullCertificateDeleter(X509 *certificate)
*
* @returns The X509 certificate.
*/
shared_ptr<X509> TLSClient::GetClientCertificate(void) const
shared_ptr<X509> TlsClient::GetClientCertificate(void) const
{
return shared_ptr<X509>(SSL_get_certificate(m_SSL.get()), &TLSClient::NullCertificateDeleter);
return shared_ptr<X509>(SSL_get_certificate(m_SSL.get()), &TlsClient::NullCertificateDeleter);
}
/**
@ -62,7 +62,7 @@ shared_ptr<X509> TLSClient::GetClientCertificate(void) const
*
* @returns The X509 certificate.
*/
shared_ptr<X509> TLSClient::GetPeerCertificate(void) const
shared_ptr<X509> TlsClient::GetPeerCertificate(void) const
{
return shared_ptr<X509>(SSL_get_peer_certificate(m_SSL.get()), X509_free);
}
@ -70,9 +70,9 @@ shared_ptr<X509> TLSClient::GetPeerCertificate(void) const
/**
* Registers the TLS socket and starts processing events for it.
*/
void TLSClient::Start(void)
void TlsClient::Start(void)
{
TCPClient::Start();
TcpClient::Start();
m_SSL = shared_ptr<SSL>(SSL_new(m_SSLContext.get()), SSL_free);
@ -83,14 +83,14 @@ void TLSClient::Start(void)
throw InvalidArgumentException("No X509 client certificate was specified.");
if (!m_SSLIndexInitialized) {
m_SSLIndex = SSL_get_ex_new_index(0, (void *)"TLSClient", NULL, NULL, NULL);
m_SSLIndex = SSL_get_ex_new_index(0, (void *)"TlsClient", NULL, NULL, NULL);
m_SSLIndexInitialized = true;
}
SSL_set_ex_data(m_SSL.get(), m_SSLIndex, this);
SSL_set_verify(m_SSL.get(), SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
&TLSClient::SSLVerifyCertificate);
&TlsClient::SSLVerifyCertificate);
BIO *bio = BIO_new_socket(GetFD(), 0);
SSL_set_bio(m_SSL.get(), bio, bio);
@ -109,7 +109,7 @@ void TLSClient::Start(void)
* @param - Event arguments.
* @returns 0
*/
int TLSClient::ReadableEventHandler(const EventArgs&)
int TlsClient::ReadableEventHandler(const EventArgs&)
{
int rc;
@ -153,7 +153,7 @@ int TLSClient::ReadableEventHandler(const EventArgs&)
* @param - Event arguments.
* @returns 0
*/
int TLSClient::WritableEventHandler(const EventArgs&)
int TlsClient::WritableEventHandler(const EventArgs&)
{
int rc;
@ -190,7 +190,7 @@ int TLSClient::WritableEventHandler(const EventArgs&)
*
* @returns true if data should be read, false otherwise.
*/
bool TLSClient::WantsToRead(void) const
bool TlsClient::WantsToRead(void) const
{
if (SSL_want_read(m_SSL.get()))
return true;
@ -198,7 +198,7 @@ bool TLSClient::WantsToRead(void) const
if (m_BlockRead)
return false;
return TCPClient::WantsToRead();
return TcpClient::WantsToRead();
}
/**
@ -206,7 +206,7 @@ bool TLSClient::WantsToRead(void) const
*
* @returns true if data should be written, false otherwise.
*/
bool TLSClient::WantsToWrite(void) const
bool TlsClient::WantsToWrite(void) const
{
if (SSL_want_write(m_SSL.get()))
return true;
@ -214,7 +214,7 @@ bool TLSClient::WantsToWrite(void) const
if (m_BlockWrite)
return false;
return TCPClient::WantsToWrite();
return TcpClient::WantsToWrite();
}
/**
@ -222,17 +222,17 @@ bool TLSClient::WantsToWrite(void) const
*
* @param from_dtor Whether this method was invoked from the destructor.
*/
void TLSClient::CloseInternal(bool from_dtor)
void TlsClient::CloseInternal(bool from_dtor)
{
SSL_shutdown(m_SSL.get());
TCPClient::CloseInternal(from_dtor);
TcpClient::CloseInternal(from_dtor);
}
/**
* Handles an OpenSSL error.
*/
void TLSClient::HandleSSLError(void)
void TlsClient::HandleSSLError(void)
{
int code = ERR_get_error();
@ -248,15 +248,15 @@ void TLSClient::HandleSSLError(void)
}
/**
* Factory function for the TLSClient class.
* Factory function for the TlsClient class.
*
* @param role The role of the TLS socket.
* @param sslContext The SSL context for the socket.
* @returns A new TLS socket.
*/
TCPClient::Ptr icinga::TLSClientFactory(TCPClientRole role, shared_ptr<SSL_CTX> sslContext)
TcpClient::Ptr icinga::TlsClientFactory(TcpClientRole role, shared_ptr<SSL_CTX> sslContext)
{
return make_shared<TLSClient>(role, sslContext);
return make_shared<TlsClient>(role, sslContext);
}
/**
@ -266,10 +266,10 @@ TCPClient::Ptr icinga::TLSClientFactory(TCPClientRole role, shared_ptr<SSL_CTX>
* @param x509Context X509 context for the certificate.
* @returns 1 if the verification was successful, 0 otherwise.
*/
int TLSClient::SSLVerifyCertificate(int ok, X509_STORE_CTX *x509Context)
int TlsClient::SSLVerifyCertificate(int ok, X509_STORE_CTX *x509Context)
{
SSL *ssl = (SSL *)X509_STORE_CTX_get_ex_data(x509Context, SSL_get_ex_data_X509_STORE_CTX_idx());
TLSClient *client = (TLSClient *)SSL_get_ex_data(ssl, m_SSLIndex);
TlsClient *client = (TlsClient *)SSL_get_ex_data(ssl, m_SSLIndex);
if (client == NULL)
return 0;
@ -278,7 +278,7 @@ int TLSClient::SSLVerifyCertificate(int ok, X509_STORE_CTX *x509Context)
vcea.Source = client->shared_from_this();
vcea.ValidCertificate = (ok != 0);
vcea.Context = x509Context;
vcea.Certificate = shared_ptr<X509>(x509Context->cert, &TLSClient::NullCertificateDeleter);
vcea.Certificate = shared_ptr<X509>(x509Context->cert, &TlsClient::NullCertificateDeleter);
client->OnVerifyCertificate(vcea);
return (int)vcea.ValidCertificate;

View File

@ -42,7 +42,7 @@ struct I2_BASE_API VerifyCertificateEventArgs : public EventArgs
*
* @ingroup base
*/
class I2_BASE_API TLSClient : public TCPClient
class I2_BASE_API TlsClient : public TcpClient
{
private:
shared_ptr<SSL_CTX> m_SSLContext;
@ -67,7 +67,7 @@ protected:
void HandleSSLError(void);
public:
TLSClient(TCPClientRole role, shared_ptr<SSL_CTX> sslContext);
TlsClient(TcpClientRole role, shared_ptr<SSL_CTX> sslContext);
shared_ptr<X509> GetClientCertificate(void) const;
shared_ptr<X509> GetPeerCertificate(void) const;
@ -80,7 +80,7 @@ public:
Observable<VerifyCertificateEventArgs> OnVerifyCertificate;
};
TCPClient::Ptr TLSClientFactory(TCPClientRole role, shared_ptr<SSL_CTX> sslContext);
TcpClient::Ptr TlsClientFactory(TcpClientRole role, shared_ptr<SSL_CTX> sslContext);
}

View File

@ -30,9 +30,6 @@ namespace icinga
*/
struct I2_ICINGA_API NewRequestEventArgs : public EventArgs
{
typedef shared_ptr<NewRequestEventArgs> Ptr;
typedef weak_ptr<NewRequestEventArgs> WeakPtr;
Endpoint::Ptr Sender;
RpcRequest Request;
};

View File

@ -21,21 +21,38 @@
using namespace icinga;
JsonRpcClient::JsonRpcClient(TCPClientRole role, shared_ptr<SSL_CTX> sslContext)
: TLSClient(role, sslContext) { }
/**
* Constructor for the JsonRpcClient class.
*
* @param role The role of the underlying TCP client.
* @param sslContext SSL context for the TLS connection.
*/
JsonRpcClient::JsonRpcClient(TcpClientRole role, shared_ptr<SSL_CTX> sslContext)
: TlsClient(role, sslContext) { }
void JsonRpcClient::Start(void)
{
TLSClient::Start();
TlsClient::Start();
OnDataAvailable += bind_weak(&JsonRpcClient::DataAvailableHandler, shared_from_this());
}
/**
* Sends a message to the connected peer.
*
* @param message The message.
*/
void JsonRpcClient::SendMessage(const MessagePart& message)
{
Netstring::WriteStringToFIFO(GetSendQueue(), message.ToJsonString());
}
/**
* Processes inbound data.
*
* @param - Event arguments for the event.
* @returns 0
*/
int JsonRpcClient::DataAvailableHandler(const EventArgs&)
{
for (;;) {
@ -56,14 +73,21 @@ int JsonRpcClient::DataAvailableHandler(const EventArgs&)
Application::Log("Exception while processing message from JSON-RPC client: " + string(ex.GetMessage()));
Close();
return 1;
return 0;
}
}
return 0;
}
TCPClient::Ptr icinga::JsonRpcClientFactory(TCPClientRole role, shared_ptr<SSL_CTX> sslContext)
/**
* Factory function for JSON-RPC clients.
*
* @param role The role of the underlying TCP client.
* @param sslContext SSL context for the TLS connection.
* @returns A new JSON-RPC client.
*/
JsonRpcClient::Ptr icinga::JsonRpcClientFactory(TcpClientRole role, shared_ptr<SSL_CTX> sslContext)
{
return make_shared<JsonRpcClient>(role, sslContext);
}

View File

@ -30,9 +30,6 @@ namespace icinga
*/
struct I2_JSONRPC_API NewMessageEventArgs : public EventArgs
{
typedef shared_ptr<NewMessageEventArgs> Ptr;
typedef weak_ptr<NewMessageEventArgs> WeakPtr;
icinga::MessagePart Message;
};
@ -41,16 +38,16 @@ struct I2_JSONRPC_API NewMessageEventArgs : public EventArgs
*
* @ingroup jsonrpc
*/
class I2_JSONRPC_API JsonRpcClient : public TLSClient
class I2_JSONRPC_API JsonRpcClient : public TlsClient
{
private:
int DataAvailableHandler(const EventArgs& ea);
int DataAvailableHandler(const EventArgs&);
public:
typedef shared_ptr<JsonRpcClient> Ptr;
typedef weak_ptr<JsonRpcClient> WeakPtr;
JsonRpcClient(TCPClientRole role, shared_ptr<SSL_CTX> sslContext);
JsonRpcClient(TcpClientRole role, shared_ptr<SSL_CTX> sslContext);
void SendMessage(const MessagePart& message);
@ -59,7 +56,7 @@ public:
Observable<NewMessageEventArgs> OnNewMessage;
};
TCPClient::Ptr JsonRpcClientFactory(TCPClientRole role, shared_ptr<SSL_CTX> sslContext);
JsonRpcClient::Ptr JsonRpcClientFactory(TcpClientRole role, shared_ptr<SSL_CTX> sslContext);
}

View File

@ -21,6 +21,11 @@
using namespace icinga;
/**
* Constructor for the JsonRpcServer class.
*
* @param sslContext SSL context that should be used for client connections.
*/
JsonRpcServer::JsonRpcServer(shared_ptr<SSL_CTX> sslContext)
{
SetClientFactory(bind(&JsonRpcClientFactory, RoleInbound, sslContext));

View File

@ -28,7 +28,7 @@ namespace icinga
*
* @ingroup jsonrpc
*/
class I2_JSONRPC_API JsonRpcServer : public TCPServer
class I2_JSONRPC_API JsonRpcServer : public TcpServer
{
public:
typedef shared_ptr<JsonRpcServer> Ptr;

View File

@ -22,11 +22,20 @@
using namespace icinga;
/**
* Constructor for the MessagePart class.
*/
MessagePart::MessagePart(void)
{
m_Dictionary = make_shared<Dictionary>();
}
/**
* Constructor for the MessagePart class.
*
* @param jsonString The JSON string that should be used to initialize
* the message.
*/
MessagePart::MessagePart(string jsonString)
{
json_t *json = cJSON_Parse(jsonString.c_str());
@ -39,16 +48,32 @@ MessagePart::MessagePart(string jsonString)
cJSON_Delete(json);
}
/**
* Constructor for the MessagePart class.
*
* @param dictionary The dictionary that this MessagePart object should wrap.
*/
MessagePart::MessagePart(const Dictionary::Ptr& dictionary)
{
m_Dictionary = dictionary;
}
/**
* Copy-constructor for the MessagePart class.
*
* @param message The message that should be copied.
*/
MessagePart::MessagePart(const MessagePart& message)
{
m_Dictionary = message.GetDictionary();
}
/**
* Converts a JSON object to a dictionary.
*
* @param json The JSON object.
* @returns A dictionary that is equivalent to the JSON object.
*/
Dictionary::Ptr MessagePart::GetDictionaryFromJson(json_t *json)
{
Dictionary::Ptr dictionary = make_shared<Dictionary>();
@ -72,6 +97,13 @@ Dictionary::Ptr MessagePart::GetDictionaryFromJson(json_t *json)
return dictionary;
}
/**
* Converts a dictionary to a JSON object.
*
* @param dictionary The dictionary.
* @returns A JSON object that is equivalent to the dictionary. Values that
* cannot be represented in JSON are omitted.
*/
json_t *MessagePart::GetJsonFromDictionary(const Dictionary::Ptr& dictionary)
{
cJSON *json;
@ -102,6 +134,11 @@ json_t *MessagePart::GetJsonFromDictionary(const Dictionary::Ptr& dictionary)
return json;
}
/**
* Converts a message into a JSON string.
*
* @returns A JSON string representing the message.
*/
string MessagePart::ToJsonString(void) const
{
json_t *json = GetJsonFromDictionary(m_Dictionary);
@ -123,11 +160,23 @@ string MessagePart::ToJsonString(void) const
return result;
}
/**
* Retrieves the underlying dictionary for this message.
*
* @returns A dictionary.
*/
Dictionary::Ptr MessagePart::GetDictionary(void) const
{
return m_Dictionary;
}
/**
* Retrieves a property's value.
*
* @param key The name of the property.
* @param[out] The value.
* @returns true if the value was retrieved, false otherwise.
*/
bool MessagePart::GetProperty(string key, MessagePart *value) const
{
Object::Ptr object;
@ -142,27 +191,45 @@ bool MessagePart::GetProperty(string key, MessagePart *value) const
return true;
}
/**
* Sets a property's value.
*
* @param key The name of the property.
* @param value The value.
*/
void MessagePart::SetProperty(string key, const MessagePart& value)
{
GetDictionary()->SetProperty(key, value.GetDictionary());
}
/**
* Adds an item to the message using an automatically generated property name.
*
* @param value The value.
*/
void MessagePart::AddUnnamedProperty(const MessagePart& value)
{
GetDictionary()->AddUnnamedProperty(value.GetDictionary());
}
/**
* Returns an iterator that points to the first element of the dictionary
* which holds the properties for the message.
*
* @returns An iterator.
*/
DictionaryIterator MessagePart::Begin(void)
{
return GetDictionary()->Begin();
}
/**
* Returns an iterator that points past the last element of the dictionary
* which holds the properties for the message.
*
* @returns An iterator.
*/
DictionaryIterator MessagePart::End(void)
{
return GetDictionary()->End();
}
MessagePart::operator Dictionary::Ptr(void)
{
return GetDictionary();
}

View File

@ -50,12 +50,25 @@ public:
Dictionary::Ptr GetDictionary(void) const;
/**
* Retrieves a property's value.
*
* @param key The name of the property.
* @param[out] The value.
* @returns true if the value was retrieved, false otherwise.
*/
template<typename T>
bool GetProperty(string key, T *value) const
{
return GetDictionary()->GetProperty(key, value);
}
/**
* Sets a property's value.
*
* @param key The name of the property.
* @param value The value.
*/
template<typename T>
void SetProperty(string key, const T& value)
{
@ -65,6 +78,11 @@ public:
bool GetProperty(string key, MessagePart *value) const;
void SetProperty(string key, const MessagePart& value);
/**
* Adds an item to the message using an automatically generated property name.
*
* @param value The value.
*/
template<typename T>
void AddUnnamedProperty(const T& value)
{
@ -75,8 +93,6 @@ public:
DictionaryIterator Begin(void);
DictionaryIterator End(void);
operator Dictionary::Ptr(void);
};
}

View File

@ -30,49 +30,100 @@ namespace icinga
*/
class I2_JSONRPC_API RpcRequest : public MessagePart
{
public:
/**
* Constructor for the RpcRequest class.
*/
RpcRequest(void) : MessagePart() {
SetVersion("2.0");
}
/**
* Copy-constructor for the RpcRequest class.
*
* @param message The message that is to be copied.
*/
RpcRequest(const MessagePart& message) : MessagePart(message) { }
/**
* Retrieves the version of the JSON-RPC protocol.
*
* @param[out] value The value.
* @returns true if the value was retrieved, false otherwise.
*/
inline bool GetVersion(string *value) const
{
return GetProperty("jsonrpc", value);
}
/**
* Sets the version of the JSON-RPC protocol that should be used.
*
* @param value The version.
*/
inline void SetVersion(const string& value)
{
SetProperty("jsonrpc", value);
}
/**
* Retrieves the method of the JSON-RPC call.
*
* @param[out] value The method.
* @returns true if the value was retrieved, false otherwise.
*/
inline bool GetMethod(string *value) const
{
return GetProperty("method", value);
}
/**
* Sets the method for the JSON-RPC call.
*
* @param value The method.
*/
inline void SetMethod(const string& value)
{
SetProperty("method", value);
}
/**
* Retrieves the parameters of the JSON-RPC call.
*
* @param[out] value The parameters.
* @returns true if the value was retrieved, false otherwise.
*/
inline bool GetParams(MessagePart *value) const
{
return GetProperty("params", value);
}
/**
* Sets the parameters for the JSON-RPC call.
*
* @param value The parameters.
*/
inline void SetParams(const MessagePart& value)
{
SetProperty("params", value);
}
/**
* Retrieves the ID of the JSON-RPC call.
*
* @param[out] value The ID.
* @return true if the value was retrieved, false otherwise.
*/
inline bool GetID(string *value) const
{
return GetProperty("id", value);
}
/**
* Sets the ID for the JSON-RPC call.
*
* @param value The ID.
*/
inline void SetID(const string& value)
{
SetProperty("id", value);

View File

@ -31,47 +31,99 @@ namespace icinga
class I2_JSONRPC_API RpcResponse : public MessagePart
{
public:
/**
* Constructor for the RpcResponse class.
*/
RpcResponse(void) : MessagePart() {
SetVersion("2.0");
}
/**
* Copy-constructor for the RpcResponse class.
*
* @param message The message that should be copied.
*/
RpcResponse(const MessagePart& message) : MessagePart(message) { }
/**
* Retrieves the version of the JSON-RPC protocol.
*
* @param[out] value The value.
* @returns true if the value was retrieved, false otherwise.
*/
inline bool GetVersion(string *value) const
{
return GetProperty("jsonrpc", value);
}
/**
* Sets the version of the JSON-RPC protocol that should be used.
*
* @param value The version.
*/
inline void SetVersion(const string& value)
{
SetProperty("jsonrpc", value);
}
bool GetResult(string *value) const
/**
* Retrieves the result of the JSON-RPC call.
*
* @param[out] value The result.
* @returns true if the value was retrieved, false otherwise.
*/
bool GetResult(MessagePart *value) const
{
return GetProperty("result", value);
}
void SetResult(const string& value)
/**
* Sets the result for the JSON-RPC call.
*
* @param value The result.
*/
void SetResult(const MessagePart& value)
{
SetProperty("result", value);
}
/**
* Retrieves the error message of the JSON-RPC call.
*
* @param[out] value The error message.
* @returns true if the value was retrieved, false otherwise.
*/
bool GetError(string *value) const
{
return GetProperty("error", value);
}
/**
* Sets the error message for the JSON-RPC call.
*
* @param value The error message.
*/
void SetError(const string& value)
{
SetProperty("error", value);
}
/**
* Retrieves the ID of the JSON-RPC call.
*
* @param[out] value The ID.
* @return true if the value was retrieved, false otherwise.
*/
bool GetID(string *value) const
{
return GetProperty("id", value);
}
/**
* Sets the ID for the JSON-RPC call.
*
* @param value The ID.
*/
void SetID(const string& value)
{
SetProperty("id", value);