diff --git a/base/application.cpp b/base/application.cpp index d854169f9..11fc30fe5 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -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); diff --git a/base/application.h b/base/application.h index f2ddff476..1bec71194 100644 --- a/base/application.h +++ b/base/application.h @@ -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 > m_Components; - vector 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 > m_Components; /**< Components that + were loaded by the application. */ + vector m_Arguments; /**< Command-line arguments */ + bool m_Debugging; /**< Whether debugging is enabled. */ protected: void RunEventLoop(void); diff --git a/base/dictionary.cpp b/base/dictionary.cpp index 0a6bc1dd8..e8e626580 100644 --- a/base/dictionary.cpp +++ b/base/dictionary.cpp @@ -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(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(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(data.GetObject()); - - if (dictionary == NULL) - throw InvalidArgumentException("Property is not a dictionary."); + Dictionary::Ptr dictionary = dynamic_pointer_cast(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)); -} diff --git a/base/dictionary.h b/base/dictionary.h index 01cb14d97..edfeda281 100644 --- a/base/dictionary.h +++ b/base/dictionary.h @@ -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 + 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; }; diff --git a/base/variant.h b/base/variant.h index b1b047127..3f10b722e 100644 --- a/base/variant.h +++ b/base/variant.h @@ -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) { } diff --git a/components/configfile/configfilecomponent.cpp b/components/configfile/configfilecomponent.cpp index 59fb080da..d939b68cc 100644 --- a/components/configfile/configfilecomponent.cpp +++ b/components/configfile/configfilecomponent.cpp @@ -35,7 +35,7 @@ void ConfigFileComponent::Start(void) FIFO::Ptr fifo = make_shared(); 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(); @@ -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); } } diff --git a/components/configrpc/configrpccomponent.cpp b/components/configrpc/configrpccomponent.cpp index 1569ef072..d59467a6e 100644 --- a/components/configrpc/configrpccomponent.cpp +++ b/components/configrpc/configrpccomponent.cpp @@ -34,7 +34,7 @@ void ConfigRpcComponent::Start(void) m_ConfigRpcEndpoint = make_shared(); 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(¶ms)) 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(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(¶ms)) 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(); diff --git a/components/discovery/discoverycomponent.cpp b/components/discovery/discoverycomponent.cpp index ce752a217..b1fa37277 100644 --- a/components/discovery/discoverycomponent.cpp +++ b/components/discovery/discoverycomponent.cpp @@ -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::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); } diff --git a/components/discovery/discoverymessage.h b/components/discovery/discoverymessage.h index f57540929..6390f530d 100644 --- a/components/discovery/discoverymessage.h +++ b/components/discovery/discoverymessage.h @@ -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); } }; diff --git a/icinga/endpointmanager.cpp b/icinga/endpointmanager.cpp index 11f88e1c2..bb2736199 100644 --- a/icinga/endpointmanager.cpp +++ b/icinga/endpointmanager.cpp @@ -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)) diff --git a/icinga/endpointmanager.h b/icinga/endpointmanager.h index 816c1f9e0..d7cd71c22 100644 --- a/icinga/endpointmanager.h +++ b/icinga/endpointmanager.h @@ -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); diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index b029c9aa8..a1ed1e4ee 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -68,8 +68,8 @@ int IcingaApplication::Main(const vector& args) /* load config file */ ConfigObject::Ptr fileComponentConfig = make_shared("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; diff --git a/icinga/jsonrpcendpoint.cpp b/icinga/jsonrpcendpoint.cpp index 9491c3497..ec1682b7b 100644 --- a/icinga/jsonrpcendpoint.cpp +++ b/icinga/jsonrpcendpoint.cpp @@ -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(shared_from_this()); string method; - if (message.GetPropertyString("method", &method)) { + if (message.GetProperty("method", &method)) { if (!HasPublication(method)) return 0; diff --git a/jsonrpc/Makefile.am b/jsonrpc/Makefile.am index fb2f02ff4..92784b9c5 100644 --- a/jsonrpc/Makefile.am +++ b/jsonrpc/Makefile.am @@ -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 \ diff --git a/jsonrpc/i2-jsonrpc.h b/jsonrpc/i2-jsonrpc.h index e5fffaf37..8a47c1d7b 100644 --- a/jsonrpc/i2-jsonrpc.h +++ b/jsonrpc/i2-jsonrpc.h @@ -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" diff --git a/jsonrpc/jsonrpc.vcxproj b/jsonrpc/jsonrpc.vcxproj index a5464f8e5..152a68dc8 100644 --- a/jsonrpc/jsonrpc.vcxproj +++ b/jsonrpc/jsonrpc.vcxproj @@ -16,7 +16,7 @@ - + @@ -24,7 +24,7 @@ - + diff --git a/jsonrpc/jsonrpc.vcxproj.filters b/jsonrpc/jsonrpc.vcxproj.filters index 3b8eec496..233af4920 100644 --- a/jsonrpc/jsonrpc.vcxproj.filters +++ b/jsonrpc/jsonrpc.vcxproj.filters @@ -3,18 +3,18 @@ - + - + \ No newline at end of file diff --git a/jsonrpc/jsonrpcclient.cpp b/jsonrpc/jsonrpcclient.cpp index 0f12adf6e..42dafe9d8 100644 --- a/jsonrpc/jsonrpcclient.cpp +++ b/jsonrpc/jsonrpcclient.cpp @@ -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(); diff --git a/jsonrpc/jsonrpcclient.h b/jsonrpc/jsonrpcclient.h index 21cef5eac..9f7d242ba 100644 --- a/jsonrpc/jsonrpcclient.h +++ b/jsonrpc/jsonrpcclient.h @@ -28,7 +28,7 @@ struct I2_JSONRPC_API NewMessageEventArgs : public EventArgs typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; - icinga::Message Message; + icinga::MessagePart Message; }; class I2_JSONRPC_API JsonRpcClient : public TLSClient @@ -42,7 +42,7 @@ public: JsonRpcClient(TCPClientRole role, shared_ptr sslContext); - void SendMessage(const Message& message); + void SendMessage(const MessagePart& message); virtual void Start(void); diff --git a/jsonrpc/message.cpp b/jsonrpc/messagepart.cpp similarity index 69% rename from jsonrpc/message.cpp rename to jsonrpc/messagepart.cpp index f25ff7ec7..bd1446326 100644 --- a/jsonrpc/message.cpp +++ b/jsonrpc/messagepart.cpp @@ -22,12 +22,12 @@ using namespace icinga; -Message::Message(void) +MessagePart::MessagePart(void) { m_Dictionary = make_shared(); } -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(); @@ -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(); } diff --git a/jsonrpc/message.h b/jsonrpc/messagepart.h similarity index 67% rename from jsonrpc/message.h rename to jsonrpc/messagepart.h index ddca1dcf8..37517f514 100644 --- a/jsonrpc/message.h +++ b/jsonrpc/messagepart.h @@ -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 + 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 + 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 + 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 */ diff --git a/jsonrpc/rpcrequest.h b/jsonrpc/rpcrequest.h index a7d11a898..901353285 100644 --- a/jsonrpc/rpcrequest.h +++ b/jsonrpc/rpcrequest.h @@ -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 */ diff --git a/jsonrpc/rpcresponse.h b/jsonrpc/rpcresponse.h index 199a67445..6d444d3d4 100644 --- a/jsonrpc/rpcresponse.h +++ b/jsonrpc/rpcresponse.h @@ -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 */