Cleaned up Dictionary/Message classes.

This commit is contained in:
Gunnar Beutner 2012-05-16 11:30:54 +02:00
parent 77bec95ec5
commit 877c7a09b2
23 changed files with 197 additions and 332 deletions

View File

@ -372,7 +372,7 @@ string Application::GetExeDirectory(void) const
#else /* _WIN32 */
char FullExePath[MAXPATHLEN];
GetModuleFileName(NULL, FullExePath, MAXPATHLEN);
GetModuleFileName(NULL, FullExePath, sizeof(FullExePath));
PathRemoveFileSpec(FullExePath);

View File

@ -31,11 +31,13 @@ DEFINE_EXCEPTION_CLASS(ComponentLoadException);
*/
class I2_BASE_API Application : public Object {
private:
bool m_ShuttingDown;
ConfigHive::Ptr m_ConfigHive;
map< string, shared_ptr<Component> > m_Components;
vector<string> m_Arguments;
bool m_Debugging;
bool m_ShuttingDown; /**< Whether the application is in the process of
shutting down. */
ConfigHive::Ptr m_ConfigHive; /**< The application's configuration. */
map< string, shared_ptr<Component> > m_Components; /**< Components that
were loaded by the application. */
vector<string> m_Arguments; /**< Command-line arguments */
bool m_Debugging; /**< Whether debugging is enabled. */
protected:
void RunEventLoop(void);

View File

@ -59,128 +59,27 @@ void Dictionary::SetProperty(string key, const Variant& value)
}
/**
* Retrieves a value from the dictionary and converts it to a string.
* Retrieves a value from the dictionary.
*
* @param key The key.
* @param value Pointer to the value.
* @returns true if the value was retrieved, false otherwise.
*/
bool Dictionary::GetPropertyString(string key, string *value)
bool Dictionary::GetProperty(string key, Dictionary::Ptr *value) const
{
Variant data;
Object::Ptr object;
if (!GetProperty(key, &data))
if (!GetProperty(key, &object))
return false;
*value = static_cast<string>(data);
return true;
}
/**
* Sets a value in the dictionary.
*
* @param key The key.
* @param value The value.
*/
void Dictionary::SetPropertyString(string key, const string& value)
{
SetProperty(key, Variant(value));
}
/**
* Retrieves a value from the dictionary and converts it to an integer.
*
* @param key The key.
* @param value Pointer to the value.
* @returns true if the value was retrieved, false otherwise.
*/
bool Dictionary::GetPropertyInteger(string key, long *value)
{
Variant data;
if (!GetProperty(key, &data))
return false;
*value = static_cast<long>(data);
return true;
}
/**
* Sets a value in the dictionary.
*
* @param key The key.
* @param value The value.
*/
void Dictionary::SetPropertyInteger(string key, long value)
{
SetProperty(key, Variant(value));
}
/**
* Retrieves a value from the dictionary and converts it to a dictionary.
*
* @param key The key.
* @param value Pointer to the value.
* @returns true if the value was retrieved, false otherwise.
*/
bool Dictionary::GetPropertyDictionary(string key, Dictionary::Ptr *value)
{
Dictionary::Ptr dictionary;
Variant data;
if (!GetProperty(key, &data))
return false;
dictionary = dynamic_pointer_cast<Dictionary>(data.GetObject());
if (dictionary == NULL)
throw InvalidArgumentException("Property is not a dictionary.");
Dictionary::Ptr dictionary = dynamic_pointer_cast<Dictionary>(object);
if (!dictionary)
throw InvalidArgumentException();
*value = dictionary;
return true;
}
/**
* Sets a value in the dictionary.
*
* @param key The key.
* @param value The value.
*/
void Dictionary::SetPropertyDictionary(string key, const Dictionary::Ptr& value)
{
SetProperty(key, Variant(value));
}
/**
* Retrieves a value from the dictionary and converts it to an object.
*
* @param key The key.
* @param value Pointer to the value.
* @returns true if the value was retrieved, false otherwise.
*/
bool Dictionary::GetPropertyObject(string key, Object::Ptr *value)
{
Variant data;
if (!GetProperty(key, &data))
return false;
*value = data;
return true;
}
/**
* Sets a value in the dictionary.
*
* @param key The key.
* @param value The value.
*/
void Dictionary::SetPropertyObject(string key, const Object::Ptr& value)
{
SetProperty(key, Variant(value));
}
/**
* Returns an iterator to the beginning of the dictionary.
*
@ -232,43 +131,3 @@ void Dictionary::AddUnnamedProperty(const Variant& value)
m_Data[key] = value;
}
/**
* Adds an unnamed value to the dictionary.
*
* @param value The value.
*/
void Dictionary::AddUnnamedPropertyString(const string& value)
{
AddUnnamedProperty(Variant(value));
}
/**
* Adds an unnamed value to the dictionary.
*
* @param value The value.
*/
void Dictionary::AddUnnamedPropertyInteger(long value)
{
AddUnnamedProperty(Variant(value));
}
/**
* Adds an unnamed value to the dictionary.
*
* @param value The value.
*/
void Dictionary::AddUnnamedPropertyDictionary(const Dictionary::Ptr& value)
{
AddUnnamedProperty(Variant(value));
}
/**
* Adds an unnamed value to the dictionary.
*
* @param value The value.
*/
void Dictionary::AddUnnamedPropertyObject(const Object::Ptr& value)
{
AddUnnamedProperty(Variant(value));
}

View File

@ -41,26 +41,25 @@ public:
bool GetProperty(string key, Variant *value) const;
void SetProperty(string key, const Variant& value);
bool GetPropertyString(string key, string *value);
void SetPropertyString(string key, const string& value);
template<typename T>
bool GetProperty(string key, T *value) const
{
Variant data;
bool GetPropertyInteger(string key, long *value);
void SetPropertyInteger(string key, long value);
if (!GetProperty(key, &data))
return false;
bool GetPropertyDictionary(string key, Dictionary::Ptr *value);
void SetPropertyDictionary(string key, const Dictionary::Ptr& value);
*value = data;
bool GetPropertyObject(string key, Object::Ptr *value);
void SetPropertyObject(string key, const Object::Ptr& value);
return true;
}
bool GetProperty(string key, Dictionary::Ptr *value) const;
DictionaryIterator Begin(void);
DictionaryIterator End(void);
void AddUnnamedProperty(const Variant& value);
void AddUnnamedPropertyString(const string& value);
void AddUnnamedPropertyInteger(long value);
void AddUnnamedPropertyDictionary(const Dictionary::Ptr& value);
void AddUnnamedPropertyObject(const Object::Ptr& value);
long GetLength(void) const;
};

View File

@ -54,6 +54,9 @@ private:
public:
inline Variant(void) : m_Type(VariantEmpty) { }
inline Variant(int value)
: m_Type(VariantInteger), m_IntegerValue(value) { }
inline Variant(long value)
: m_Type(VariantInteger), m_IntegerValue(value) { }

View File

@ -35,7 +35,7 @@ void ConfigFileComponent::Start(void)
FIFO::Ptr fifo = make_shared<FIFO>();
string filename;
if (!GetConfig()->GetPropertyString("configFilename", &filename))
if (!GetConfig()->GetProperty("configFilename", &filename))
throw InvalidArgumentException("Missing 'configFilename' property");
fp.open(filename.c_str(), ifstream::in);
@ -78,7 +78,7 @@ void ConfigFileComponent::Start(void)
if (property->type == cJSON_String) {
string value = property->valuestring;
cfgobj->SetPropertyString(key, value);
cfgobj->SetProperty(key, value);
} else if (property->type == cJSON_Array) {
Dictionary::Ptr items = make_shared<Dictionary>();
@ -86,10 +86,10 @@ void ConfigFileComponent::Start(void)
if (item->type != cJSON_String)
continue;
items->AddUnnamedPropertyString(item->valuestring);
items->AddUnnamedProperty(item->valuestring);
}
cfgobj->SetPropertyDictionary(key, items);
cfgobj->SetProperty(key, items);
}
}

View File

@ -34,7 +34,7 @@ void ConfigRpcComponent::Start(void)
m_ConfigRpcEndpoint = make_shared<VirtualEndpoint>();
long configSource;
if (GetConfig()->GetPropertyInteger("configSource", &configSource) && configSource != 0) {
if (GetConfig()->GetProperty("configSource", &configSource) && configSource != 0) {
m_ConfigRpcEndpoint->RegisterTopicHandler("config::FetchObjects",
bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this()));
@ -87,14 +87,14 @@ RpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object
RpcRequest msg;
msg.SetMethod(method);
Message params;
MessagePart params;
msg.SetParams(params);
params.GetDictionary()->SetPropertyString("name", object->GetName());
params.GetDictionary()->SetPropertyString("type", object->GetType());
params.SetProperty("name", object->GetName());
params.SetProperty("type", object->GetType());
if (includeProperties)
params.SetPropertyMessage("properties", Message(object));
params.SetProperty("properties", object);
return msg;
}
@ -102,7 +102,7 @@ RpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object
bool ConfigRpcComponent::ShouldReplicateObject(const ConfigObject::Ptr& object)
{
long replicate;
if (!object->GetPropertyInteger("replicate", &replicate))
if (!object->GetProperty("replicate", &replicate))
return true;
return (replicate != 0);
}
@ -161,16 +161,16 @@ int ConfigRpcComponent::RemoteObjectCommittedHandler(const NewRequestEventArgs&
RpcRequest message = ea.Request;
bool was_null = false;
Message params;
MessagePart params;
if (!message.GetParams(&params))
return 0;
string name;
if (!params.GetDictionary()->GetPropertyString("name", &name))
if (!params.GetProperty("name", &name))
return 0;
string type;
if (!params.GetDictionary()->GetPropertyString("type", &type))
if (!params.GetProperty("type", &type))
return 0;
ConfigHive::Ptr configHive = GetConfigHive();
@ -181,12 +181,12 @@ int ConfigRpcComponent::RemoteObjectCommittedHandler(const NewRequestEventArgs&
object = make_shared<ConfigObject>(type, name);
}
Dictionary::Ptr properties;
if (!params.GetDictionary()->GetPropertyDictionary("properties", &properties))
MessagePart properties;
if (!params.GetProperty("properties", &properties))
return 0;
for (DictionaryIterator i = properties->Begin(); i != properties->End(); i++) {
object->SetPropertyString(i->first, i->second);
for (DictionaryIterator i = properties.Begin(); i != properties.End(); i++) {
object->SetProperty(i->first, i->second);
}
if (was_null) {
@ -201,16 +201,16 @@ int ConfigRpcComponent::RemoteObjectRemovedHandler(const NewRequestEventArgs& ea
{
RpcRequest message = ea.Request;
Message params;
MessagePart params;
if (!message.GetParams(&params))
return 0;
string name;
if (!params.GetDictionary()->GetPropertyString("name", &name))
if (!params.GetProperty("name", &name))
return 0;
string type;
if (!params.GetDictionary()->GetPropertyString("type", &type))
if (!params.GetProperty("type", &type))
return 0;
ConfigHive::Ptr configHive = GetConfigHive();

View File

@ -314,12 +314,6 @@ void DiscoveryComponent::SendDiscoveryMessage(string method, string identity, En
params.SetIdentity(identity);
Message subscriptions;
params.SetSubscriptions(subscriptions);
Message publications;
params.SetPublications(publications);
ComponentDiscoveryInfo::Ptr info;
if (!GetComponentDiscoveryInfo(identity, &info))
@ -331,11 +325,17 @@ void DiscoveryComponent::SendDiscoveryMessage(string method, string identity, En
}
set<string>::iterator i;
for (i = info->Publications.begin(); i != info->Publications.end(); i++)
publications.AddUnnamedPropertyString(*i);
MessagePart subscriptions;
for (i = info->Subscriptions.begin(); i != info->Subscriptions.end(); i++)
subscriptions.AddUnnamedPropertyString(*i);
subscriptions.AddUnnamedProperty(*i);
params.SetSubscriptions(subscriptions);
MessagePart publications;
for (i = info->Publications.begin(); i != info->Publications.end(); i++)
publications.AddUnnamedProperty(*i);
params.SetPublications(publications);
if (recipient)
GetEndpointManager()->SendUnicastMessage(m_DiscoveryEndpoint, recipient, request);
@ -358,7 +358,7 @@ bool DiscoveryComponent::HasMessagePermission(Dictionary::Ptr roles, string mess
continue;
Dictionary::Ptr permissions;
if (!role->GetPropertyDictionary(messageType, &permissions))
if (!role->GetProperty(messageType, &permissions))
continue;
for (DictionaryIterator is = permissions->Begin(); is != permissions->End(); is++) {
@ -397,14 +397,14 @@ void DiscoveryComponent::ProcessDiscoveryMessage(string identity, DiscoveryMessa
ConfigObject::Ptr endpointConfig = endpointCollection->GetObject(identity);
Dictionary::Ptr roles;
if (endpointConfig)
endpointConfig->GetPropertyDictionary("roles", &roles);
endpointConfig->GetProperty("roles", &roles);
Endpoint::Ptr endpoint = GetEndpointManager()->GetEndpointByIdentity(identity);
Message publications;
MessagePart publications;
if (message.GetPublications(&publications)) {
DictionaryIterator i;
for (i = publications.GetDictionary()->Begin(); i != publications.GetDictionary()->End(); i++) {
for (i = publications.Begin(); i != publications.End(); i++) {
if (trusted || HasMessagePermission(roles, "publications", i->second)) {
info->Publications.insert(i->second);
if (endpoint)
@ -413,10 +413,10 @@ void DiscoveryComponent::ProcessDiscoveryMessage(string identity, DiscoveryMessa
}
}
Message subscriptions;
MessagePart subscriptions;
if (message.GetSubscriptions(&subscriptions)) {
DictionaryIterator i;
for (i = subscriptions.GetDictionary()->Begin(); i != subscriptions.GetDictionary()->End(); i++) {
for (i = subscriptions.Begin(); i != subscriptions.End(); i++) {
if (trusted || HasMessagePermission(roles, "subscriptions", i->second)) {
info->Subscriptions.insert(i->second);
if (endpoint)
@ -492,7 +492,7 @@ int DiscoveryComponent::EndpointConfigHandler(const EventArgs& ea)
return 0;
string node, service;
if (object->GetPropertyString("node", &node) && object->GetPropertyString("service", &service)) {
if (object->GetProperty("node", &node) && object->GetProperty("service", &service)) {
/* reconnect to this endpoint */
endpointManager->AddConnection(node, service);
}

View File

@ -4,61 +4,61 @@
namespace icinga
{
class DiscoveryMessage : public Message
class DiscoveryMessage : public MessagePart
{
public:
DiscoveryMessage(void) : Message() { }
DiscoveryMessage(const Message& message) : Message(message) { }
DiscoveryMessage(void) : MessagePart() { }
DiscoveryMessage(const MessagePart& message) : MessagePart(message) { }
inline bool GetIdentity(string *value) const
{
return GetPropertyString("identity", value);
return GetProperty("identity", value);
}
inline void SetIdentity(const string& value)
{
SetPropertyString("identity", value);
SetProperty("identity", value);
}
inline bool GetNode(string *value) const
{
return GetPropertyString("node", value);
return GetProperty("node", value);
}
inline void SetNode(const string& value)
{
SetPropertyString("node", value);
SetProperty("node", value);
}
inline bool GetService(string *value) const
{
return GetPropertyString("service", value);
return GetProperty("service", value);
}
inline void SetService(const string& value)
{
SetPropertyString("service", value);
SetProperty("service", value);
}
inline bool GetSubscriptions(Message *value) const
inline bool GetSubscriptions(MessagePart *value) const
{
return GetPropertyMessage("subscriptions", value);
return GetProperty("subscriptions", value);
}
inline void SetSubscriptions(Message value)
inline void SetSubscriptions(MessagePart value)
{
SetPropertyMessage("subscriptions", value);
SetProperty("subscriptions", value);
}
inline bool GetPublications(Message *value) const
inline bool GetPublications(MessagePart *value) const
{
return GetPropertyMessage("publications", value);
return GetProperty("publications", value);
}
inline void SetPublications(Message value)
inline void SetPublications(MessagePart value)
{
SetPropertyMessage("publications", value);
SetProperty("publications", value);
}
};

View File

@ -109,7 +109,8 @@ void EndpointManager::AddConnection(string node, string service)
void EndpointManager::RegisterServer(JsonRpcServer::Ptr server)
{
m_Servers.push_back(server);
server->OnNewClient += bind_weak(&EndpointManager::NewClientHandler, shared_from_this());
server->OnNewClient += bind_weak(&EndpointManager::NewClientHandler,
shared_from_this());
}
/**
@ -180,7 +181,8 @@ void EndpointManager::UnregisterEndpoint(Endpoint::Ptr endpoint)
* @param recipient The recipient of the message.
* @param message The request.
*/
void EndpointManager::SendUnicastMessage(Endpoint::Ptr sender, Endpoint::Ptr recipient, const Message& message)
void EndpointManager::SendUnicastMessage(Endpoint::Ptr sender,
Endpoint::Ptr recipient, const MessagePart& message)
{
/* don't forward messages back to the sender */
if (sender == recipient)
@ -200,7 +202,8 @@ void EndpointManager::SendUnicastMessage(Endpoint::Ptr sender, Endpoint::Ptr rec
* @param sender The sender of the message.
* @param message The message.
*/
void EndpointManager::SendAnycastMessage(Endpoint::Ptr sender, const RpcRequest& message)
void EndpointManager::SendAnycastMessage(Endpoint::Ptr sender,
const RpcRequest& message)
{
throw NotImplementedException();
}
@ -212,7 +215,8 @@ void EndpointManager::SendAnycastMessage(Endpoint::Ptr sender, const RpcRequest&
* @param sender The sender of the message.
* @param message The message.
*/
void EndpointManager::SendMulticastMessage(Endpoint::Ptr sender, const RpcRequest& message)
void EndpointManager::SendMulticastMessage(Endpoint::Ptr sender,
const RpcRequest& message)
{
string id;
if (message.GetID(&id))

View File

@ -63,7 +63,7 @@ public:
void RegisterEndpoint(Endpoint::Ptr endpoint);
void UnregisterEndpoint(Endpoint::Ptr endpoint);
void SendUnicastMessage(Endpoint::Ptr sender, Endpoint::Ptr recipient, const Message& message);
void SendUnicastMessage(Endpoint::Ptr sender, Endpoint::Ptr recipient, const MessagePart& message);
void SendAnycastMessage(Endpoint::Ptr sender, const RpcRequest& message);
void SendMulticastMessage(Endpoint::Ptr sender, const RpcRequest& message);

View File

@ -68,8 +68,8 @@ int IcingaApplication::Main(const vector<string>& args)
/* load config file */
ConfigObject::Ptr fileComponentConfig = make_shared<ConfigObject>("component", "configfile");
fileComponentConfig->SetPropertyString("configFilename", args[1]);
fileComponentConfig->SetPropertyInteger("replicate", 0);
fileComponentConfig->SetProperty("configFilename", args[1]);
fileComponentConfig->SetProperty("replicate", 0);
GetConfigHive()->AddObject(fileComponentConfig);
if (!GetPrivateKeyFile().empty() && !GetPublicKeyFile().empty() && !GetCAKeyFile().empty()) {
@ -112,7 +112,7 @@ int IcingaApplication::NewComponentHandler(const EventArgs& ea)
return 0;
string path;
if (!object->GetPropertyString("path", &path)) {
if (!object->GetProperty("path", &path)) {
#ifdef _WIN32
path = object->GetName() + ".dll";
#else /* _WIN32 */
@ -144,23 +144,23 @@ int IcingaApplication::NewIcingaConfigHandler(const EventArgs& ea)
return 0;
string privkey;
if (object->GetPropertyString("privkey", &privkey))
if (object->GetProperty("privkey", &privkey))
SetPrivateKeyFile(privkey);
string pubkey;
if (object->GetPropertyString("pubkey", &pubkey))
if (object->GetProperty("pubkey", &pubkey))
SetPublicKeyFile(pubkey);
string cakey;
if (object->GetPropertyString("cakey", &cakey))
if (object->GetProperty("cakey", &cakey))
SetCAKeyFile(cakey);
string node;
if (object->GetPropertyString("node", &node))
if (object->GetProperty("node", &node))
SetNode(node);
string service;
if (object->GetPropertyString("service", &service))
if (object->GetProperty("service", &service))
SetService(service);
return 0;

View File

@ -83,11 +83,11 @@ void JsonRpcEndpoint::ProcessResponse(Endpoint::Ptr sender, const RpcResponse& m
int JsonRpcEndpoint::NewMessageHandler(const NewMessageEventArgs& nmea)
{
const Message& message = nmea.Message;
const MessagePart& message = nmea.Message;
Endpoint::Ptr sender = static_pointer_cast<Endpoint>(shared_from_this());
string method;
if (message.GetPropertyString("method", &method)) {
if (message.GetProperty("method", &method)) {
if (!HasPublication(method))
return 0;

View File

@ -10,8 +10,8 @@ libjsonrpc_la_SOURCES = \
jsonrpcclient.h \
jsonrpcserver.cpp \
jsonrpcserver.h \
message.cpp \
message.h \
messagepart.cpp \
messagepart.h \
netstring.cpp \
netstring.h \
rpcrequest.cpp \

View File

@ -29,9 +29,7 @@
# define I2_JSONRPC_API I2_IMPORT
#endif /* I2_JSONRPC_BUILD */
#include "variant.h"
#include "dictionary.h"
#include "message.h"
#include "messagepart.h"
#include "rpcrequest.h"
#include "rpcresponse.h"
#include "netstring.h"

View File

@ -16,7 +16,7 @@
<ClInclude Include="rpcrequest.h" />
<ClInclude Include="rpcresponse.h" />
<ClInclude Include="jsonrpcserver.h" />
<ClInclude Include="message.h" />
<ClInclude Include="messagepart.h" />
<ClInclude Include="netstring.h" />
</ItemGroup>
<ItemGroup>
@ -24,7 +24,7 @@
<ClCompile Include="rpcrequest.cpp" />
<ClCompile Include="rpcresponse.cpp" />
<ClCompile Include="jsonrpcserver.cpp" />
<ClCompile Include="message.cpp" />
<ClCompile Include="messagepart.cpp" />
<ClCompile Include="netstring.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">

View File

@ -3,18 +3,18 @@
<ItemGroup>
<ClCompile Include="jsonrpcclient.cpp" />
<ClCompile Include="jsonrpcserver.cpp" />
<ClCompile Include="message.cpp" />
<ClCompile Include="netstring.cpp" />
<ClCompile Include="rpcrequest.cpp" />
<ClCompile Include="rpcresponse.cpp" />
<ClCompile Include="messagepart.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="i2-jsonrpc.h" />
<ClInclude Include="jsonrpcclient.h" />
<ClInclude Include="jsonrpcserver.h" />
<ClInclude Include="message.h" />
<ClInclude Include="netstring.h" />
<ClInclude Include="rpcrequest.h" />
<ClInclude Include="rpcresponse.h" />
<ClInclude Include="messagepart.h" />
</ItemGroup>
</Project>

View File

@ -31,7 +31,7 @@ void JsonRpcClient::Start(void)
OnDataAvailable += bind_weak(&JsonRpcClient::DataAvailableHandler, shared_from_this());
}
void JsonRpcClient::SendMessage(const Message& message)
void JsonRpcClient::SendMessage(const MessagePart& message)
{
Netstring::WriteStringToFIFO(GetSendQueue(), message.ToJsonString());
}
@ -41,12 +41,12 @@ int JsonRpcClient::DataAvailableHandler(const EventArgs&)
for (;;) {
try {
string jsonString;
Message message;
MessagePart message;
if (!Netstring::ReadStringFromFIFO(GetRecvQueue(), &jsonString))
break;
message = Message(jsonString);
message = MessagePart(jsonString);
NewMessageEventArgs nea;
nea.Source = shared_from_this();

View File

@ -28,7 +28,7 @@ struct I2_JSONRPC_API NewMessageEventArgs : public EventArgs
typedef shared_ptr<NewMessageEventArgs> Ptr;
typedef weak_ptr<NewMessageEventArgs> WeakPtr;
icinga::Message Message;
icinga::MessagePart Message;
};
class I2_JSONRPC_API JsonRpcClient : public TLSClient
@ -42,7 +42,7 @@ public:
JsonRpcClient(TCPClientRole role, shared_ptr<SSL_CTX> sslContext);
void SendMessage(const Message& message);
void SendMessage(const MessagePart& message);
virtual void Start(void);

View File

@ -22,12 +22,12 @@
using namespace icinga;
Message::Message(void)
MessagePart::MessagePart(void)
{
m_Dictionary = make_shared<Dictionary>();
}
Message::Message(string jsonString)
MessagePart::MessagePart(string jsonString)
{
json_t *json = cJSON_Parse(jsonString.c_str());
@ -39,17 +39,17 @@ Message::Message(string jsonString)
cJSON_Delete(json);
}
Message::Message(const Dictionary::Ptr& dictionary)
MessagePart::MessagePart(const Dictionary::Ptr& dictionary)
{
m_Dictionary = dictionary;
}
Message::Message(const Message& message)
MessagePart::MessagePart(const MessagePart& message)
{
m_Dictionary = message.GetDictionary();
}
Dictionary::Ptr Message::GetDictionaryFromJson(json_t *json)
Dictionary::Ptr MessagePart::GetDictionaryFromJson(json_t *json)
{
Dictionary::Ptr dictionary = make_shared<Dictionary>();
@ -72,7 +72,7 @@ Dictionary::Ptr Message::GetDictionaryFromJson(json_t *json)
return dictionary;
}
json_t *Message::GetJsonFromDictionary(const Dictionary::Ptr& dictionary)
json_t *MessagePart::GetJsonFromDictionary(const Dictionary::Ptr& dictionary)
{
cJSON *json;
string valueString;
@ -102,7 +102,7 @@ json_t *Message::GetJsonFromDictionary(const Dictionary::Ptr& dictionary)
return json;
}
string Message::ToJsonString(void) const
string MessagePart::ToJsonString(void) const
{
json_t *json = GetJsonFromDictionary(m_Dictionary);
char *jsonString;
@ -123,57 +123,42 @@ string Message::ToJsonString(void) const
return result;
}
Dictionary::Ptr Message::GetDictionary(void) const
Dictionary::Ptr MessagePart::GetDictionary(void) const
{
return m_Dictionary;
}
bool Message::GetPropertyString(string key, string *value) const
{
return GetDictionary()->GetPropertyString(key, value);
}
bool Message::GetPropertyInteger(string key, long *value) const
{
return GetDictionary()->GetPropertyInteger(key, value);
}
bool Message::GetPropertyMessage(string key, Message *value) const
bool MessagePart::GetProperty(string key, MessagePart *value) const
{
Dictionary::Ptr dictionary;
if (!GetDictionary()->GetPropertyDictionary(key, &dictionary))
if (!GetDictionary()->GetProperty(key, &dictionary))
return false;
*value = Message(dictionary);
*value = MessagePart(dictionary);
return true;
}
void Message::SetPropertyString(string key, const string& value)
void MessagePart::SetProperty(string key, const MessagePart& value)
{
GetDictionary()->SetProperty(key, value);
GetDictionary()->SetProperty(key, value.GetDictionary());
}
void Message::SetPropertyInteger(string key, long value)
void MessagePart::AddUnnamedProperty(const MessagePart& value)
{
GetDictionary()->SetProperty(key, value);
GetDictionary()->AddUnnamedProperty(value.GetDictionary());
}
void Message::SetPropertyMessage(string key, const Message& value)
DictionaryIterator MessagePart::Begin(void)
{
GetDictionary()->SetProperty(key, Variant(value.GetDictionary()));
return GetDictionary()->Begin();
}
void Message::AddUnnamedPropertyString(const string& value)
DictionaryIterator MessagePart::End(void)
{
GetDictionary()->AddUnnamedPropertyString(value);
return GetDictionary()->End();
}
void Message::AddUnnamedPropertyInteger(long value)
MessagePart::operator Dictionary::Ptr(void)
{
GetDictionary()->AddUnnamedPropertyInteger(value);
}
void Message::AddUnnamedPropertyMessage(const Message& value)
{
GetDictionary()->AddUnnamedPropertyDictionary(value.GetDictionary());
return GetDictionary();
}

View File

@ -17,8 +17,8 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef MESSAGE_H
#define MESSAGE_H
#ifndef MESSAGEPART_H
#define MESSAGEPART_H
struct cJSON;
@ -27,7 +27,7 @@ namespace icinga
typedef ::cJSON json_t;
class I2_JSONRPC_API Message
class I2_JSONRPC_API MessagePart
{
private:
Dictionary::Ptr m_Dictionary;
@ -36,29 +36,44 @@ private:
static json_t *GetJsonFromDictionary(const Dictionary::Ptr& dictionary);
public:
Message(void);
Message(string json);
Message(const Dictionary::Ptr& dictionary);
Message(const Message& message);
MessagePart(void);
MessagePart(string json);
MessagePart(const Dictionary::Ptr& dictionary);
MessagePart(const MessagePart& message);
string ToJsonString(void) const;
Dictionary::Ptr GetDictionary(void) const;
bool GetPropertyString(string key, string *value) const;
void SetPropertyString(string key, const string& value);
template<typename T>
bool GetProperty(string key, T *value) const
{
return GetDictionary()->GetProperty(key, value);
}
bool GetPropertyInteger(string key, long *value) const;
void SetPropertyInteger(string key, long value);
template<typename T>
void SetProperty(string key, const T& value)
{
GetDictionary()->SetProperty(key, value);
}
bool GetPropertyMessage(string key, Message *value) const;
void SetPropertyMessage(string key, const Message& value);
bool GetProperty(string key, MessagePart *value) const;
void SetProperty(string key, const MessagePart& value);
void AddUnnamedPropertyString(const string& value);
void AddUnnamedPropertyInteger(long value);
void AddUnnamedPropertyMessage(const Message& value);
template<typename T>
void AddUnnamedProperty(const T& value)
{
GetDictionary()->AddUnnamedProperty(value);
}
void AddUnnamedProperty(const MessagePart& value);
DictionaryIterator Begin(void);
DictionaryIterator End(void);
operator Dictionary::Ptr(void);
};
}
#endif /* MESSAGE_H */
#endif /* MESSAGEPART_H */

View File

@ -17,63 +17,63 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef RpcRequest_H
#define RpcRequest_H
#ifndef RPCREQUEST_H
#define RPCREQUEST_H
namespace icinga
{
class I2_JSONRPC_API RpcRequest : public Message
class I2_JSONRPC_API RpcRequest : public MessagePart
{
public:
RpcRequest(void) : Message() {
RpcRequest(void) : MessagePart() {
SetVersion("2.0");
}
RpcRequest(const Message& message) : Message(message) { }
RpcRequest(const MessagePart& message) : MessagePart(message) { }
inline bool GetVersion(string *value) const
{
return GetPropertyString("jsonrpc", value);
return GetProperty("jsonrpc", value);
}
inline void SetVersion(const string& value)
{
SetPropertyString("jsonrpc", value);
SetProperty("jsonrpc", value);
}
inline bool GetMethod(string *value) const
{
return GetPropertyString("method", value);
return GetProperty("method", value);
}
inline void SetMethod(const string& value)
{
SetPropertyString("method", value);
SetProperty("method", value);
}
inline bool GetParams(Message *value) const
inline bool GetParams(MessagePart *value) const
{
return GetPropertyMessage("params", value);
return GetProperty("params", value);
}
inline void SetParams(const Message& value)
inline void SetParams(const MessagePart& value)
{
SetPropertyMessage("params", value);
SetProperty("params", value);
}
inline bool GetID(string *value) const
{
return GetPropertyString("id", value);
return GetProperty("id", value);
}
inline void SetID(const string& value)
{
SetPropertyString("id", value);
SetProperty("id", value);
}
};
}
#endif /* RpcRequest_H */
#endif /* RPCREQUEST_H */

View File

@ -17,62 +17,62 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef RpcResponse_H
#define RpcResponse_H
#ifndef RPCRESPONSE_H
#define RPCRESPONSE_H
namespace icinga
{
class I2_JSONRPC_API RpcResponse : public Message
class I2_JSONRPC_API RpcResponse : public MessagePart
{
public:
RpcResponse(void) : Message() {
RpcResponse(void) : MessagePart() {
SetVersion("2.0");
}
RpcResponse(const Message& message) : Message(message) { }
RpcResponse(const MessagePart& message) : MessagePart(message) { }
inline bool GetVersion(string *value) const
{
return GetPropertyString("jsonrpc", value);
return GetProperty("jsonrpc", value);
}
inline void SetVersion(const string& value)
{
SetPropertyString("jsonrpc", value);
SetProperty("jsonrpc", value);
}
bool GetResult(string *value) const
{
return GetPropertyString("result", value);
return GetProperty("result", value);
}
void SetResult(const string& value)
{
SetPropertyString("result", value);
SetProperty("result", value);
}
bool GetError(string *value) const
{
return GetPropertyString("error", value);
return GetProperty("error", value);
}
void SetError(const string& value)
{
SetPropertyString("error", value);
SetProperty("error", value);
}
bool GetID(string *value) const
{
return GetPropertyString("id", value);
return GetProperty("id", value);
}
void SetID(const string& value)
{
SetPropertyString("id", value);
SetProperty("id", value);
}
};
}
#endif /* RpcResponse_H */
#endif /* RPCRESPONSE_H */