From b1256d9fe9201742fa88d0c6e8fd36fb1485afee Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 20 Apr 2012 13:49:04 +0200 Subject: [PATCH] Cleaned up message classes. --- base/configcollection.cpp | 10 +-- base/configcollection.h | 8 +-- base/confighive.cpp | 2 +- base/confighive.h | 8 +-- base/configobject.cpp | 63 +++---------------- base/configobject.h | 21 +------ base/dictionary.cpp | 57 ++++++++++------- base/dictionary.h | 30 ++++++--- base/variant.cpp | 30 ++++----- base/variant.h | 16 +++-- components/configfile/configfilecomponent.cpp | 6 +- components/configrpc/configrpccomponent.cpp | 41 ++++++------ components/configrpc/configrpccomponent.h | 6 +- icinga/endpointmanager.cpp | 12 ++-- icinga/icingaapplication.cpp | 52 +++++++++------ icinga/icingaapplication.h | 12 ++-- icinga/identitymessage.h | 4 +- icinga/jsonrpcendpoint.cpp | 2 +- icinga/subscriptioncomponent.cpp | 6 +- icinga/subscriptionmessage.h | 4 +- jsonrpc/jsonrpcrequest.h | 23 +++---- jsonrpc/jsonrpcresponse.h | 16 ++--- jsonrpc/message.cpp | 35 +++++++++++ jsonrpc/message.h | 9 +++ jsonrpc/netstring.cpp | 6 +- 25 files changed, 248 insertions(+), 231 deletions(-) diff --git a/base/configcollection.cpp b/base/configcollection.cpp index a1d24fe06..aecca6de1 100644 --- a/base/configcollection.cpp +++ b/base/configcollection.cpp @@ -14,9 +14,11 @@ ConfigHive::WeakPtr ConfigCollection::GetHive(void) const void ConfigCollection::AddObject(const ConfigObject::Ptr& object) { + RemoveObject(object); + Objects[object->GetName()] = object; - ConfigObjectEventArgs ea; + EventArgs ea; ea.Source = object; OnObjectCreated(ea); @@ -32,7 +34,7 @@ void ConfigCollection::RemoveObject(const ConfigObject::Ptr& object) if (oi != Objects.end()) { Objects.erase(oi); - ConfigObjectEventArgs ea; + EventArgs ea; ea.Source = object; OnObjectRemoved(ea); @@ -52,9 +54,9 @@ ConfigObject::Ptr ConfigCollection::GetObject(const string& name) return oi->second; } -void ConfigCollection::ForEachObject(function callback) +void ConfigCollection::ForEachObject(function callback) { - ConfigObjectEventArgs ea; + EventArgs ea; for (ObjectIterator oi = Objects.begin(); oi != Objects.end(); oi++) { ea.Source = oi->second; diff --git a/base/configcollection.h b/base/configcollection.h index be252c375..dfcb3c84b 100644 --- a/base/configcollection.h +++ b/base/configcollection.h @@ -25,11 +25,11 @@ public: void RemoveObject(const ConfigObject::Ptr& object); ConfigObject::Ptr GetObject(const string& name = string()); - void ForEachObject(function callback); + void ForEachObject(function callback); - Event OnObjectCreated; - Event OnObjectRemoved; - Event OnPropertyChanged; + Event OnObjectCreated; + Event OnObjectRemoved; + Event OnPropertyChanged; }; } diff --git a/base/confighive.cpp b/base/confighive.cpp index 81623b77c..fb43ce3af 100644 --- a/base/confighive.cpp +++ b/base/confighive.cpp @@ -30,7 +30,7 @@ ConfigCollection::Ptr ConfigHive::GetCollection(const string& collection) return ci->second; } -void ConfigHive::ForEachObject(const string& type, function callback) +void ConfigHive::ForEachObject(const string& type, function callback) { CollectionIterator ci = Collections.find(type); diff --git a/base/confighive.h b/base/confighive.h index f2d78e3cc..a8654cbd0 100644 --- a/base/confighive.h +++ b/base/confighive.h @@ -18,11 +18,11 @@ public: ConfigObject::Ptr GetObject(const string& collection, const string& name = string()); ConfigCollection::Ptr GetCollection(const string& collection); - void ForEachObject(const string& type, function callback); + void ForEachObject(const string& type, function callback); - Event OnObjectCreated; - Event OnObjectRemoved; - Event OnPropertyChanged; + Event OnObjectCreated; + Event OnObjectRemoved; + Event OnPropertyChanged; }; } diff --git a/base/configobject.cpp b/base/configobject.cpp index 9f779a7cd..e0800dd1b 100644 --- a/base/configobject.cpp +++ b/base/configobject.cpp @@ -10,7 +10,11 @@ ConfigObject::ConfigObject(const string& type, const string& name) void ConfigObject::SetHive(const ConfigHive::WeakPtr& hive) { + if (m_Hive.lock()) + throw InvalidArgumentException(); + m_Hive = hive; + OnPropertyChanged += bind_weak(&ConfigObject::PropertyChangedHandler, shared_from_this()); } ConfigHive::WeakPtr ConfigObject::GetHive(void) const @@ -38,64 +42,13 @@ string ConfigObject::GetType(void) const return m_Type; } -void ConfigObject::SetProperty(const string& name, const string& value) +int ConfigObject::PropertyChangedHandler(const DictionaryPropertyChangedEventArgs dpcea) { - Properties[name] = value; - ConfigHive::Ptr hive = m_Hive.lock(); if (hive) { - ConfigObjectEventArgs ea; - ea.Source = shared_from_this(); - ea.Property = name; - - string oldValue; - if (GetProperty(name, &oldValue)) - ea.OldValue = oldValue; - - hive->GetCollection(m_Type)->OnPropertyChanged(ea); - hive->OnPropertyChanged(ea); + hive->GetCollection(m_Type)->OnPropertyChanged(dpcea); + hive->OnPropertyChanged(dpcea); } -} - -void ConfigObject::SetPropertyInteger(const string& name, int value) -{ - char valueString[20]; - sprintf(valueString, "%d", value); - - SetProperty(name, string(valueString)); -} - -void ConfigObject::SetPropertyDouble(const string& name, double value) -{ - char valueString[20]; - sprintf(valueString, "%f", value); - - SetProperty(name, string(valueString)); -} - -bool ConfigObject::GetProperty(const string& name, string *value) const -{ - map::const_iterator vi = Properties.find(name); - if (vi == Properties.end()) - return false; - *value = vi->second; - return true; -} -bool ConfigObject::GetPropertyInteger(const string& name, int *value) const -{ - string stringValue; - if (!GetProperty(name, &stringValue)) - return false; - *value = strtol(stringValue.c_str(), NULL, 10); - return true; -} - -bool ConfigObject::GetPropertyDouble(const string& name, double *value) const -{ - string stringValue; - if (!GetProperty(name, &stringValue)) - return false; - *value = strtod(stringValue.c_str(), NULL); - return true; + return 0; } diff --git a/base/configobject.h b/base/configobject.h index a084b5b47..84ac0e40b 100644 --- a/base/configobject.h +++ b/base/configobject.h @@ -8,16 +8,7 @@ namespace icinga class ConfigHive; -struct I2_BASE_API ConfigObjectEventArgs : public EventArgs -{ - typedef shared_ptr Ptr; - typedef weak_ptr WeakPtr; - - string Property; - string OldValue; -}; - -class I2_BASE_API ConfigObject : public Object +class I2_BASE_API ConfigObject : public Dictionary { private: weak_ptr m_Hive; @@ -25,6 +16,8 @@ private: string m_Name; string m_Type; + int PropertyChangedHandler(const DictionaryPropertyChangedEventArgs dpcea); + public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; @@ -42,14 +35,6 @@ public: void SetType(const string& type); string GetType(void) const; - - void SetProperty(const string& name, const string& value); - void SetPropertyInteger(const string& name, int value); - void SetPropertyDouble(const string& name, double value); - - bool GetProperty(const string& name, string *value) const; - bool GetPropertyInteger(const string& name, int *value) const; - bool GetPropertyDouble(const string& name, double *value) const; }; } diff --git a/base/dictionary.cpp b/base/dictionary.cpp index 1f69feb27..965278519 100644 --- a/base/dictionary.cpp +++ b/base/dictionary.cpp @@ -2,62 +2,75 @@ using namespace icinga; -bool Dictionary::GetValueVariant(string key, Variant *value) +bool Dictionary::GetProperty(string key, Variant *value) const { - DictionaryIterator i = m_Data.find(key); + ConstDictionaryIterator i = m_Data.find(key); if (i == m_Data.end()) return false; *value = i->second; - return true; } -void Dictionary::SetValueVariant(string key, const Variant& value) +void Dictionary::SetProperty(string key, const Variant& value) { - m_Data.erase(key); + DictionaryIterator i = m_Data.find(key); + + Variant oldValue; + if (i != m_Data.end()) { + oldValue = i->second; + m_Data.erase(i); + } + m_Data[key] = value; + + DictionaryPropertyChangedEventArgs dpce; + dpce.Source = shared_from_this(); + dpce.Property = key; + dpce.OldValue = oldValue; + dpce.NewValue = value; + OnPropertyChanged(dpce); } -bool Dictionary::GetValueString(string key, string *value) +bool Dictionary::GetPropertyString(string key, string *value) { Variant data; - if (!GetValueVariant(key, &data)) + if (!GetProperty(key, &data)) return false; *value = static_cast(data); return true; } -void Dictionary::SetValueString(string key, const string& value) +void Dictionary::SetPropertyString(string key, const string& value) { - SetValueVariant(key, Variant(value)); + SetProperty(key, Variant(value)); } -bool Dictionary::GetValueInteger(string key, long *value) +bool Dictionary::GetPropertyInteger(string key, long *value) { Variant data; - if (!GetValueVariant(key, &data)) + if (!GetProperty(key, &data)) return false; - *value = data; + *value = static_cast(data); return true; } -void Dictionary::SetValueInteger(string key, long value) +void Dictionary::SetPropertyInteger(string key, long value) { - SetValueVariant(key, Variant(value)); + SetProperty(key, Variant(value)); } -bool Dictionary::GetValueDictionary(string key, Dictionary::Ptr *value) +bool Dictionary::GetPropertyDictionary(string key, Dictionary::Ptr *value) { Dictionary::Ptr dictionary; Variant data; - if (!GetValueVariant(key, &data)) + if (!GetProperty(key, &data)) return false; dictionary = dynamic_pointer_cast(data.GetObject()); @@ -70,25 +83,25 @@ bool Dictionary::GetValueDictionary(string key, Dictionary::Ptr *value) return true; } -void Dictionary::SetValueDictionary(string key, const Dictionary::Ptr& value) +void Dictionary::SetPropertyDictionary(string key, const Dictionary::Ptr& value) { - SetValueVariant(key, Variant(value)); + SetProperty(key, Variant(value)); } -bool Dictionary::GetValueObject(string key, Object::Ptr *value) +bool Dictionary::GetPropertyObject(string key, Object::Ptr *value) { Variant data; - if (!GetValueVariant(key, &data)) + if (!GetProperty(key, &data)) return false; *value = data; return true; } -void Dictionary::SetValueObject(string key, const Object::Ptr& value) +void Dictionary::SetPropertyObject(string key, const Object::Ptr& value) { - SetValueVariant(key, Variant(value)); + SetProperty(key, Variant(value)); } DictionaryIterator Dictionary::Begin(void) diff --git a/base/dictionary.h b/base/dictionary.h index 6f062ee08..127f9d6a6 100644 --- a/base/dictionary.h +++ b/base/dictionary.h @@ -4,8 +4,16 @@ namespace icinga { +typedef map::const_iterator ConstDictionaryIterator; typedef map::iterator DictionaryIterator; +struct I2_BASE_API DictionaryPropertyChangedEventArgs : public EventArgs +{ + string Property; + Variant OldValue; + Variant NewValue; +}; + class I2_BASE_API Dictionary : public Object { private: @@ -15,23 +23,25 @@ public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; - bool GetValueVariant(string key, Variant *value); - void SetValueVariant(string key, const Variant& value); + bool GetProperty(string key, Variant *value) const; + void SetProperty(string key, const Variant& value); - bool GetValueString(string key, string *value); - void SetValueString(string key, const string& value); + bool GetPropertyString(string key, string *value); + void SetPropertyString(string key, const string& value); - bool GetValueInteger(string key, long *value); - void SetValueInteger(string key, long value); + bool GetPropertyInteger(string key, long *value); + void SetPropertyInteger(string key, long value); - bool GetValueDictionary(string key, Dictionary::Ptr *value); - void SetValueDictionary(string key, const Dictionary::Ptr& value); + bool GetPropertyDictionary(string key, Dictionary::Ptr *value); + void SetPropertyDictionary(string key, const Dictionary::Ptr& value); - bool GetValueObject(string key, Object::Ptr *value); - void SetValueObject(string key, const Object::Ptr& value); + bool GetPropertyObject(string key, Object::Ptr *value); + void SetPropertyObject(string key, const Object::Ptr& value); DictionaryIterator Begin(void); DictionaryIterator End(void); + + Event OnPropertyChanged; }; } diff --git a/base/variant.cpp b/base/variant.cpp index d1ab9bd18..95a030e1b 100644 --- a/base/variant.cpp +++ b/base/variant.cpp @@ -2,29 +2,20 @@ using namespace icinga; -Variant::Variant(void) : m_Type(VariantEmpty) -{ -} - -Variant::Variant(long value) : m_Type(VariantInteger), m_IntegerValue(value) -{ -} - -Variant::Variant(string value) : m_Type(VariantString), m_StringValue(value) -{ -} - -Variant::Variant(Object::Ptr value) : m_Type(VariantObject), m_ObjectValue(value) -{ -} - void Variant::Convert(VariantType newType) const { if (newType == m_Type) return; + if (m_Type == VariantString && newType == VariantInteger) { + m_IntegerValue = strtol(m_StringValue.c_str(), NULL, 10); + m_Type = VariantInteger; + + return; + } + // TODO: convert variant data - throw NotImplementedException(); + throw InvalidArgumentException("Invalid variant conversion."); } VariantType Variant::GetType(void) const @@ -53,6 +44,11 @@ Object::Ptr Variant::GetObject(void) const return m_ObjectValue; } +bool Variant::IsEmpty(void) const +{ + return (m_Type == VariantEmpty); +} + Variant::operator long(void) const { return GetInteger(); diff --git a/base/variant.h b/base/variant.h index ef44260e3..060825866 100644 --- a/base/variant.h +++ b/base/variant.h @@ -24,10 +24,16 @@ private: void Convert(VariantType newType) const; public: - Variant(void); - Variant(long value); - Variant(string value); - Variant(Object::Ptr value); + inline Variant::Variant(void) : m_Type(VariantEmpty) { } + + inline Variant::Variant(long value) : m_Type(VariantInteger), m_IntegerValue(value) { } + + inline Variant::Variant(const char *value) : m_Type(VariantString), m_StringValue(string(value)) { } + + inline Variant::Variant(string value) : m_Type(VariantString), m_StringValue(value) { } + + template + Variant(const shared_ptr& value) : m_Type(VariantObject), m_ObjectValue(value) { } VariantType GetType(void) const; @@ -35,6 +41,8 @@ public: string GetString(void) const; Object::Ptr GetObject(void) const; + bool IsEmpty(void) const; + operator long(void) const; operator string(void) const; operator Object::Ptr(void) const; diff --git a/components/configfile/configfilecomponent.cpp b/components/configfile/configfilecomponent.cpp index 3c1b7b4d4..bbd407583 100644 --- a/components/configfile/configfilecomponent.cpp +++ b/components/configfile/configfilecomponent.cpp @@ -16,8 +16,8 @@ void ConfigFileComponent::Start(void) FIFO::Ptr fifo = make_shared(); string filename; - if (!GetConfig()->GetProperty("configFilename", &filename)) - throw ConfigParserException("Missing configFilename property"); + if (!GetConfig()->GetPropertyString("configFilename", &filename)) + throw InvalidArgumentException("Missing 'configFilename' property"); fp.open(filename.c_str(), ifstream::in); if (fp.fail()) @@ -61,7 +61,7 @@ void ConfigFileComponent::Start(void) string value = property->valuestring; - cfgobj->SetProperty(key, value); + cfgobj->SetPropertyString(key, value); } GetApplication()->GetConfigHive()->AddObject(cfgobj); diff --git a/components/configrpc/configrpccomponent.cpp b/components/configrpc/configrpccomponent.cpp index 29a6cf984..4ed2a43f9 100644 --- a/components/configrpc/configrpccomponent.cpp +++ b/components/configrpc/configrpccomponent.cpp @@ -21,7 +21,7 @@ void ConfigRpcComponent::Start(void) m_ConfigRpcEndpoint = make_shared(); - int configSource; + long configSource; if (GetConfig()->GetPropertyInteger("configSource", &configSource) && configSource != 0) { m_ConfigRpcEndpoint->RegisterMethodHandler("config::FetchObjects", bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this())); @@ -82,15 +82,15 @@ JsonRpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& ob Message params; msg.SetParams(params); - params.GetDictionary()->SetValueString("name", object->GetName()); - params.GetDictionary()->SetValueString("type", object->GetType()); + params.GetDictionary()->SetPropertyString("name", object->GetName()); + params.GetDictionary()->SetPropertyString("type", object->GetType()); if (includeProperties) { Message properties; - params.GetDictionary()->SetValueDictionary("properties", properties.GetDictionary()); + params.GetDictionary()->SetPropertyDictionary("properties", properties.GetDictionary()); for (ConfigObject::ParameterIterator pi = object->Properties.begin(); pi != object->Properties.end(); pi++) { - properties.GetDictionary()->SetValueString(pi->first, pi->second); + properties.GetDictionary()->SetPropertyString(pi->first, pi->second); } } @@ -113,11 +113,11 @@ int ConfigRpcComponent::FetchObjectsHandler(const NewRequestEventArgs& ea) return 0; } -int ConfigRpcComponent::LocalObjectCreatedHandler(const ConfigObjectEventArgs& ea) +int ConfigRpcComponent::LocalObjectCreatedHandler(const EventArgs& ea) { ConfigObject::Ptr object = static_pointer_cast(ea.Source); - int replicate = 0; + long replicate = 0; object->GetPropertyInteger("replicate", &replicate); if (replicate) { @@ -128,11 +128,11 @@ int ConfigRpcComponent::LocalObjectCreatedHandler(const ConfigObjectEventArgs& e return 0; } -int ConfigRpcComponent::LocalObjectRemovedHandler(const ConfigObjectEventArgs& ea) +int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea) { ConfigObject::Ptr object = static_pointer_cast(ea.Source); - int replicate = 0; + long replicate = 0; object->GetPropertyInteger("replicate", &replicate); if (replicate) { @@ -143,11 +143,11 @@ int ConfigRpcComponent::LocalObjectRemovedHandler(const ConfigObjectEventArgs& e return 0; } -int ConfigRpcComponent::LocalPropertyChangedHandler(const ConfigObjectEventArgs& ea) +int ConfigRpcComponent::LocalPropertyChangedHandler(const DictionaryPropertyChangedEventArgs& ea) { ConfigObject::Ptr object = static_pointer_cast(ea.Source); - int replicate = 0; + long replicate = 0; object->GetPropertyInteger("replicate", &replicate); if (replicate) { @@ -156,12 +156,13 @@ int ConfigRpcComponent::LocalPropertyChangedHandler(const ConfigObjectEventArgs& msg.SetParams(params); Message properties; - params.GetDictionary()->SetValueDictionary("properties", properties.GetDictionary()); + params.GetDictionary()->SetPropertyDictionary("properties", properties.GetDictionary()); string value; - object->GetProperty(ea.Property, &value); + if (!object->GetPropertyString(ea.Property, &value)) + return 0; - properties.GetDictionary()->SetValueString(ea.Property, value); + properties.GetDictionary()->SetPropertyString(ea.Property, value); EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); mgr->SendMulticastRequest(m_ConfigRpcEndpoint, msg); @@ -180,11 +181,11 @@ int ConfigRpcComponent::RemoteObjectUpdatedHandler(const NewRequestEventArgs& ea return 0; string name; - if (!params.GetDictionary()->GetValueString("name", &name)) + if (!params.GetDictionary()->GetPropertyString("name", &name)) return 0; string type; - if (!params.GetDictionary()->GetValueString("type", &type)) + if (!params.GetDictionary()->GetPropertyString("type", &type)) return 0; ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive(); @@ -196,11 +197,11 @@ int ConfigRpcComponent::RemoteObjectUpdatedHandler(const NewRequestEventArgs& ea } Dictionary::Ptr properties; - if (!params.GetDictionary()->GetValueDictionary("properties", &properties)) + if (!params.GetDictionary()->GetPropertyDictionary("properties", &properties)) return 0; for (DictionaryIterator i = properties->Begin(); i != properties->End(); i++) { - object->SetProperty(i->first, i->second); + object->SetPropertyString(i->first, i->second); } if (was_null) @@ -218,11 +219,11 @@ int ConfigRpcComponent::RemoteObjectRemovedHandler(const NewRequestEventArgs& ea return 0; string name; - if (!params.GetDictionary()->GetValueString("name", &name)) + if (!params.GetDictionary()->GetPropertyString("name", &name)) return 0; string type; - if (!params.GetDictionary()->GetValueString("type", &type)) + if (!params.GetDictionary()->GetPropertyString("type", &type)) return 0; ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive(); diff --git a/components/configrpc/configrpccomponent.h b/components/configrpc/configrpccomponent.h index 92b00b243..05274a529 100644 --- a/components/configrpc/configrpccomponent.h +++ b/components/configrpc/configrpccomponent.h @@ -14,9 +14,9 @@ private: int NewEndpointHandler(const NewEndpointEventArgs& ea); int WelcomeMessageHandler(const NewRequestEventArgs& ea); - int LocalObjectCreatedHandler(const ConfigObjectEventArgs& ea); - int LocalObjectRemovedHandler(const ConfigObjectEventArgs& ea); - int LocalPropertyChangedHandler(const ConfigObjectEventArgs& ea); + int LocalObjectCreatedHandler(const EventArgs& ea); + int LocalObjectRemovedHandler(const EventArgs& ea); + int LocalPropertyChangedHandler(const DictionaryPropertyChangedEventArgs& ea); int FetchObjectsHandler(const NewRequestEventArgs& ea); int RemoteObjectUpdatedHandler(const NewRequestEventArgs& ea); diff --git a/icinga/endpointmanager.cpp b/icinga/endpointmanager.cpp index 8d543a020..a3d8d798b 100644 --- a/icinga/endpointmanager.cpp +++ b/icinga/endpointmanager.cpp @@ -117,9 +117,9 @@ int EndpointManager::NewMethodSinkHandler(const NewMethodEventArgs& ea) request.SetVersion("2.0"); request.SetMethod("message::Subscribe"); - Message params; - params.GetDictionary()->SetValueString("method", ea.Method); - request.SetParams(params); + SubscriptionMessage subscriptionMessage; + subscriptionMessage.SetMethod(ea.Method); + request.SetParams(subscriptionMessage); SendMulticastRequest(sender, request); @@ -137,9 +137,9 @@ int EndpointManager::NewMethodSourceHandler(const NewMethodEventArgs& ea) request.SetVersion("2.0"); request.SetMethod("message::Provide"); - Message params; - params.GetDictionary()->SetValueString("method", ea.Method); - request.SetParams(params); + SubscriptionMessage subscriptionMessage; + subscriptionMessage.SetMethod(ea.Method); + request.SetParams(subscriptionMessage); SendMulticastRequest(sender, request); diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index e2fdd9fc2..c44f389ce 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -32,7 +32,7 @@ int IcingaApplication::Main(const vector& args) ConfigCollection::Ptr componentCollection = GetConfigHive()->GetCollection("component"); - function NewComponentHandler = bind_weak(&IcingaApplication::NewComponentHandler, shared_from_this()); + function NewComponentHandler = bind_weak(&IcingaApplication::NewComponentHandler, shared_from_this()); componentCollection->OnObjectCreated += NewComponentHandler; componentCollection->ForEachObject(NewComponentHandler); @@ -40,7 +40,7 @@ int IcingaApplication::Main(const vector& args) ConfigCollection::Ptr listenerCollection = GetConfigHive()->GetCollection("rpclistener"); - function NewRpcListenerHandler = bind_weak(&IcingaApplication::NewRpcListenerHandler, shared_from_this()); + function NewRpcListenerHandler = bind_weak(&IcingaApplication::NewRpcListenerHandler, shared_from_this()); listenerCollection->OnObjectCreated += NewRpcListenerHandler; listenerCollection->ForEachObject(NewRpcListenerHandler); @@ -48,7 +48,7 @@ int IcingaApplication::Main(const vector& args) ConfigCollection::Ptr connectionCollection = GetConfigHive()->GetCollection("rpcconnection"); - function NewRpcConnectionHandler = bind_weak(&IcingaApplication::NewRpcConnectionHandler, shared_from_this()); + function NewRpcConnectionHandler = bind_weak(&IcingaApplication::NewRpcConnectionHandler, shared_from_this()); connectionCollection->OnObjectCreated += NewRpcConnectionHandler; connectionCollection->ForEachObject(NewRpcConnectionHandler); @@ -58,7 +58,7 @@ int IcingaApplication::Main(const vector& args) RegisterComponent(subscriptionsComponent); ConfigObject::Ptr fileComponentConfig = make_shared("component", "configfile"); - fileComponentConfig->SetProperty("configFilename", args[1]); + fileComponentConfig->SetPropertyString("configFilename", args[1]); fileComponentConfig->SetPropertyInteger("replicate", 0); GetConfigHive()->AddObject(fileComponentConfig); @@ -103,12 +103,12 @@ EndpointManager::Ptr IcingaApplication::GetEndpointManager(void) return m_EndpointManager; } -int IcingaApplication::NewComponentHandler(const ConfigObjectEventArgs& ea) +int IcingaApplication::NewComponentHandler(const EventArgs& ea) { string path; ConfigObject::Ptr object = static_pointer_cast(ea.Source); - - if (!object->GetProperty("path", &path)) { + + if (!object->GetPropertyString("path", &path)) { #ifdef _WIN32 path = object->GetName() + ".dll"; #else /* _WIN32 */ @@ -123,7 +123,7 @@ int IcingaApplication::NewComponentHandler(const ConfigObjectEventArgs& ea) return 0; } -int IcingaApplication::DeletedComponentHandler(const ConfigObjectEventArgs& ea) +int IcingaApplication::DeletedComponentHandler(const EventArgs& ea) { ConfigObject::Ptr object = static_pointer_cast(ea.Source); Component::Ptr component = GetComponent(object->GetName()); @@ -132,13 +132,19 @@ int IcingaApplication::DeletedComponentHandler(const ConfigObjectEventArgs& ea) return 0; } -int IcingaApplication::NewRpcListenerHandler(const ConfigObjectEventArgs& ea) +int IcingaApplication::NewRpcListenerHandler(const EventArgs& ea) { ConfigObject::Ptr object = static_pointer_cast(ea.Source); - int port; + long portValue; + unsigned short port; - if (!object->GetPropertyInteger("port", &port)) - throw Exception("Parameter 'port' is required for 'rpclistener' objects."); + if (!object->GetPropertyInteger("port", &portValue)) + throw InvalidArgumentException("Parameter 'port' is required for 'rpclistener' objects."); + + if (portValue < 0 || portValue > USHRT_MAX) + throw InvalidArgumentException("Parameter 'port' contains an invalid value."); + + port = (unsigned short)portValue; Log("Creating JSON-RPC listener on port %d", port); @@ -147,24 +153,30 @@ int IcingaApplication::NewRpcListenerHandler(const ConfigObjectEventArgs& ea) return 0; } -int IcingaApplication::DeletedRpcListenerHandler(const ConfigObjectEventArgs& ea) +int IcingaApplication::DeletedRpcListenerHandler(const EventArgs& ea) { throw Exception("Unsupported operation."); return 0; } -int IcingaApplication::NewRpcConnectionHandler(const ConfigObjectEventArgs& ea) +int IcingaApplication::NewRpcConnectionHandler(const EventArgs& ea) { ConfigObject::Ptr object = static_pointer_cast(ea.Source); string hostname; - int port; + long portValue; + unsigned short port; + + if (!object->GetPropertyString("hostname", &hostname)) + throw InvalidArgumentException("Parameter 'hostname' is required for 'rpcconnection' objects."); + + if (!object->GetPropertyInteger("port", &portValue)) + throw InvalidArgumentException("Parameter 'port' is required for 'rpcconnection' objects."); - if (!object->GetProperty("hostname", &hostname)) - throw Exception("Parameter 'hostname' is required for 'rpcconnection' objects."); + if (portValue < 0 || portValue > USHRT_MAX) + throw InvalidArgumentException("Parameter 'port' contains an invalid value."); - if (!object->GetPropertyInteger("port", &port)) - throw Exception("Parameter 'port' is required for 'rpcconnection' objects."); + port = (unsigned short)portValue; Log("Creating JSON-RPC connection to %s:%d", hostname.c_str(), port); @@ -173,7 +185,7 @@ int IcingaApplication::NewRpcConnectionHandler(const ConfigObjectEventArgs& ea) return 0; } -int IcingaApplication::DeletedRpcConnectionHandler(const ConfigObjectEventArgs& ea) +int IcingaApplication::DeletedRpcConnectionHandler(const EventArgs& ea) { throw Exception("Unsupported operation."); diff --git a/icinga/icingaapplication.h b/icinga/icingaapplication.h index 7c46296c2..6d86e55d4 100644 --- a/icinga/icingaapplication.h +++ b/icinga/icingaapplication.h @@ -11,14 +11,14 @@ private: Timer::Ptr m_TestTimer; VirtualEndpoint::Ptr m_TestEndpoint; - int NewComponentHandler(const ConfigObjectEventArgs& ea); - int DeletedComponentHandler(const ConfigObjectEventArgs& ea); + int NewComponentHandler(const EventArgs& ea); + int DeletedComponentHandler(const EventArgs& ea); - int NewRpcListenerHandler(const ConfigObjectEventArgs& ea); - int DeletedRpcListenerHandler(const ConfigObjectEventArgs& ea); + int NewRpcListenerHandler(const EventArgs& ea); + int DeletedRpcListenerHandler(const EventArgs& ea); - int NewRpcConnectionHandler(const ConfigObjectEventArgs& ea); - int DeletedRpcConnectionHandler(const ConfigObjectEventArgs& ea); + int NewRpcConnectionHandler(const EventArgs& ea); + int DeletedRpcConnectionHandler(const EventArgs& ea); int TestTimerHandler(const TimerEventArgs& tea); public: diff --git a/icinga/identitymessage.h b/icinga/identitymessage.h index 714e26f3a..2d2567d41 100644 --- a/icinga/identitymessage.h +++ b/icinga/identitymessage.h @@ -13,12 +13,12 @@ public: inline bool GetIdentity(string *value) const { - return GetDictionary()->GetValueString("identity", value); + return GetPropertyString("identity", value); } inline void SetIdentity(const string& value) { - GetDictionary()->SetValueString("identity", value); + SetPropertyString("identity", value); } }; diff --git a/icinga/jsonrpcendpoint.cpp b/icinga/jsonrpcendpoint.cpp index 6e58c64c1..057da887b 100644 --- a/icinga/jsonrpcendpoint.cpp +++ b/icinga/jsonrpcendpoint.cpp @@ -71,7 +71,7 @@ int JsonRpcEndpoint::NewMessageHandler(const NewMessageEventArgs& nmea) Endpoint::Ptr sender = static_pointer_cast(shared_from_this()); string method; - if (message.GetDictionary()->GetValueString("method", &method)) { + if (message.GetPropertyString("method", &method)) { JsonRpcRequest request = message; string id; diff --git a/icinga/subscriptioncomponent.cpp b/icinga/subscriptioncomponent.cpp index f614afc6b..f87ef71e9 100644 --- a/icinga/subscriptioncomponent.cpp +++ b/icinga/subscriptioncomponent.cpp @@ -40,9 +40,9 @@ int SubscriptionComponent::SyncSubscription(Endpoint::Ptr target, string type, c request.SetVersion("2.0"); request.SetMethod(type); - Message params; - params.GetDictionary()->SetValueString("method", nmea.Method); - request.SetParams(params); + SubscriptionMessage subscriptionMessage; + subscriptionMessage.SetMethod(nmea.Method); + request.SetParams(subscriptionMessage); target->ProcessRequest(m_SubscriptionEndpoint, request); diff --git a/icinga/subscriptionmessage.h b/icinga/subscriptionmessage.h index 5e56e1a66..98464f04e 100644 --- a/icinga/subscriptionmessage.h +++ b/icinga/subscriptionmessage.h @@ -13,12 +13,12 @@ public: inline bool GetMethod(string *value) const { - return GetDictionary()->GetValueString("method", value); + return GetPropertyString("method", value); } inline void SetMethod(const string& value) { - GetDictionary()->SetValueString("method", value); + SetPropertyString("method", value); } }; diff --git a/jsonrpc/jsonrpcrequest.h b/jsonrpc/jsonrpcrequest.h index d9d1df769..45362d9e7 100644 --- a/jsonrpc/jsonrpcrequest.h +++ b/jsonrpc/jsonrpcrequest.h @@ -13,49 +13,42 @@ public: inline bool GetVersion(string *value) const { - return GetDictionary()->GetValueString("jsonrpc", value); + return GetPropertyString("jsonrpc", value); } inline void SetVersion(const string& value) { - GetDictionary()->SetValueString("jsonrpc", value); + SetPropertyString("jsonrpc", value); } inline bool GetMethod(string *value) const { - return GetDictionary()->GetValueString("method", value); + return GetPropertyString("method", value); } inline void SetMethod(const string& value) { - GetDictionary()->SetValueString("method", value); + SetPropertyString("method", value); } inline bool GetParams(Message *value) const { - Dictionary::Ptr dictionary; - - if (!GetDictionary()->GetValueDictionary("params", &dictionary)) - return false; - - *value = Message(dictionary); - - return true; + return GetPropertyMessage("params", value); } inline void SetParams(const Message& value) { - GetDictionary()->SetValueDictionary("params", value.GetDictionary()); + SetPropertyMessage("params", value); } inline bool GetID(string *value) const { - return GetDictionary()->GetValueString("id", value); + return GetPropertyString("id", value); } inline void SetID(const string& value) { - GetDictionary()->SetValueString("id", value); + SetPropertyString("id", value); } }; diff --git a/jsonrpc/jsonrpcresponse.h b/jsonrpc/jsonrpcresponse.h index aed6ff38a..ab001b973 100644 --- a/jsonrpc/jsonrpcresponse.h +++ b/jsonrpc/jsonrpcresponse.h @@ -12,42 +12,42 @@ public: inline bool GetVersion(string *value) const { - return GetDictionary()->GetValueString("jsonrpc", value); + return GetPropertyString("jsonrpc", value); } inline void SetJsonRpc(const string& value) { - GetDictionary()->SetValueString("jsonrpc", value); + SetPropertyString("jsonrpc", value); } bool GetResult(string *value) const { - return GetDictionary()->GetValueString("result", value); + return GetPropertyString("result", value); } void SetResult(const string& value) { - GetDictionary()->SetValueString("result", value); + SetPropertyString("result", value); } bool GetError(string *value) const { - return GetDictionary()->GetValueString("error", value); + return GetPropertyString("error", value); } void SetError(const string& value) { - GetDictionary()->SetValueString("error", value); + SetPropertyString("error", value); } bool GetID(string *value) const { - return GetDictionary()->GetValueString("id", value); + return GetPropertyString("id", value); } void SetID(const string& value) { - GetDictionary()->SetValueString("id", value); + SetPropertyString("id", value); } }; diff --git a/jsonrpc/message.cpp b/jsonrpc/message.cpp index 2b23caa93..d7786af21 100644 --- a/jsonrpc/message.cpp +++ b/jsonrpc/message.cpp @@ -21,3 +21,38 @@ Dictionary::Ptr Message::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 +{ + Dictionary::Ptr dictionary; + if (!GetDictionary()->GetPropertyDictionary(key, &dictionary)) + return false; + + *value = Message(dictionary); + return true; +} + +void Message::SetPropertyString(string key, const string& value) +{ + GetDictionary()->SetProperty(key, value); +} + +void Message::SetPropertyInteger(string key, long value) +{ + GetDictionary()->SetProperty(key, value); +} + +void Message::SetPropertyMessage(string key, const Message& value) +{ + GetDictionary()->SetProperty(key, Variant(value.GetDictionary())); +} diff --git a/jsonrpc/message.h b/jsonrpc/message.h index 589de15c0..a48e84fd6 100644 --- a/jsonrpc/message.h +++ b/jsonrpc/message.h @@ -15,6 +15,15 @@ public: Message(const Message& message); Dictionary::Ptr GetDictionary(void) const; + + bool GetPropertyString(string key, string *value) const; + void SetPropertyString(string key, const string& value); + + bool GetPropertyInteger(string key, long *value) const; + void SetPropertyInteger(string key, long value); + + bool GetPropertyMessage(string key, Message *value) const; + void SetPropertyMessage(string key, const Message& value); }; } diff --git a/jsonrpc/netstring.cpp b/jsonrpc/netstring.cpp index 85347cc0e..dd3e0e76f 100644 --- a/jsonrpc/netstring.cpp +++ b/jsonrpc/netstring.cpp @@ -11,13 +11,13 @@ Dictionary::Ptr Netstring::GetDictionaryFromJson(json_t *json) for (cJSON *i = json->child; i != NULL; i = i->next) { switch (i->type) { case cJSON_Number: - dictionary->SetValueInteger(i->string, i->valueint); + dictionary->SetProperty(i->string, i->valueint); break; case cJSON_String: - dictionary->SetValueString(i->string, i->valuestring); + dictionary->SetProperty(i->string, i->valuestring); break; case cJSON_Object: - dictionary->SetValueDictionary(i->string, GetDictionaryFromJson(i)); + dictionary->SetProperty(i->string, GetDictionaryFromJson(i)); break; default: break; -- 2.50.1