Cleaned up message classes.

This commit is contained in:
Gunnar Beutner 2012-04-20 13:49:04 +02:00
parent a4b0da505f
commit b1256d9fe9
25 changed files with 249 additions and 232 deletions

View File

@ -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<int (const ConfigObjectEventArgs&)> callback)
void ConfigCollection::ForEachObject(function<int (const EventArgs&)> callback)
{
ConfigObjectEventArgs ea;
EventArgs ea;
for (ObjectIterator oi = Objects.begin(); oi != Objects.end(); oi++) {
ea.Source = oi->second;

View File

@ -25,11 +25,11 @@ public:
void RemoveObject(const ConfigObject::Ptr& object);
ConfigObject::Ptr GetObject(const string& name = string());
void ForEachObject(function<int (const ConfigObjectEventArgs&)> callback);
void ForEachObject(function<int (const EventArgs&)> callback);
Event<ConfigObjectEventArgs> OnObjectCreated;
Event<ConfigObjectEventArgs> OnObjectRemoved;
Event<ConfigObjectEventArgs> OnPropertyChanged;
Event<EventArgs> OnObjectCreated;
Event<EventArgs> OnObjectRemoved;
Event<DictionaryPropertyChangedEventArgs> OnPropertyChanged;
};
}

View File

@ -30,7 +30,7 @@ ConfigCollection::Ptr ConfigHive::GetCollection(const string& collection)
return ci->second;
}
void ConfigHive::ForEachObject(const string& type, function<int (const ConfigObjectEventArgs&)> callback)
void ConfigHive::ForEachObject(const string& type, function<int (const EventArgs&)> callback)
{
CollectionIterator ci = Collections.find(type);

View File

@ -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<int (const ConfigObjectEventArgs&)> callback);
void ForEachObject(const string& type, function<int (const EventArgs&)> callback);
Event<ConfigObjectEventArgs> OnObjectCreated;
Event<ConfigObjectEventArgs> OnObjectRemoved;
Event<ConfigObjectEventArgs> OnPropertyChanged;
Event<EventArgs> OnObjectCreated;
Event<EventArgs> OnObjectRemoved;
Event<DictionaryPropertyChangedEventArgs> OnPropertyChanged;
};
}

View File

@ -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<string, string>::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;
}

View File

@ -8,16 +8,7 @@ namespace icinga
class ConfigHive;
struct I2_BASE_API ConfigObjectEventArgs : public EventArgs
{
typedef shared_ptr<ConfigObjectEventArgs> Ptr;
typedef weak_ptr<ConfigObjectEventArgs> WeakPtr;
string Property;
string OldValue;
};
class I2_BASE_API ConfigObject : public Object
class I2_BASE_API ConfigObject : public Dictionary
{
private:
weak_ptr<ConfigHive> m_Hive;
@ -25,6 +16,8 @@ private:
string m_Name;
string m_Type;
int PropertyChangedHandler(const DictionaryPropertyChangedEventArgs dpcea);
public:
typedef shared_ptr<ConfigObject> Ptr;
typedef weak_ptr<ConfigObject> 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;
};
}

View File

@ -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<string>(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<long>(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<Dictionary>(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)

View File

@ -4,8 +4,16 @@
namespace icinga
{
typedef map<string, Variant>::const_iterator ConstDictionaryIterator;
typedef map<string, Variant>::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<Dictionary> Ptr;
typedef weak_ptr<Dictionary> 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<DictionaryPropertyChangedEventArgs> OnPropertyChanged;
};
}

View File

@ -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();

View File

@ -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<typename T>
Variant(const shared_ptr<T>& 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;

View File

@ -16,8 +16,8 @@ void ConfigFileComponent::Start(void)
FIFO::Ptr fifo = make_shared<FIFO>();
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);

View File

@ -21,7 +21,7 @@ void ConfigRpcComponent::Start(void)
m_ConfigRpcEndpoint = make_shared<VirtualEndpoint>();
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<ConfigObject>(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<ConfigObject>(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<ConfigObject>(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();

View File

@ -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);

View File

@ -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);

View File

@ -32,7 +32,7 @@ int IcingaApplication::Main(const vector<string>& args)
ConfigCollection::Ptr componentCollection = GetConfigHive()->GetCollection("component");
function<int (const ConfigObjectEventArgs&)> NewComponentHandler = bind_weak(&IcingaApplication::NewComponentHandler, shared_from_this());
function<int (const EventArgs&)> NewComponentHandler = bind_weak(&IcingaApplication::NewComponentHandler, shared_from_this());
componentCollection->OnObjectCreated += NewComponentHandler;
componentCollection->ForEachObject(NewComponentHandler);
@ -40,7 +40,7 @@ int IcingaApplication::Main(const vector<string>& args)
ConfigCollection::Ptr listenerCollection = GetConfigHive()->GetCollection("rpclistener");
function<int (const ConfigObjectEventArgs&)> NewRpcListenerHandler = bind_weak(&IcingaApplication::NewRpcListenerHandler, shared_from_this());
function<int (const EventArgs&)> NewRpcListenerHandler = bind_weak(&IcingaApplication::NewRpcListenerHandler, shared_from_this());
listenerCollection->OnObjectCreated += NewRpcListenerHandler;
listenerCollection->ForEachObject(NewRpcListenerHandler);
@ -48,7 +48,7 @@ int IcingaApplication::Main(const vector<string>& args)
ConfigCollection::Ptr connectionCollection = GetConfigHive()->GetCollection("rpcconnection");
function<int (const ConfigObjectEventArgs&)> NewRpcConnectionHandler = bind_weak(&IcingaApplication::NewRpcConnectionHandler, shared_from_this());
function<int (const EventArgs&)> NewRpcConnectionHandler = bind_weak(&IcingaApplication::NewRpcConnectionHandler, shared_from_this());
connectionCollection->OnObjectCreated += NewRpcConnectionHandler;
connectionCollection->ForEachObject(NewRpcConnectionHandler);
@ -58,7 +58,7 @@ int IcingaApplication::Main(const vector<string>& args)
RegisterComponent(subscriptionsComponent);
ConfigObject::Ptr fileComponentConfig = make_shared<ConfigObject>("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<ConfigObject>(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<ConfigObject>(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<ConfigObject>(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<ConfigObject>(ea.Source);
string hostname;
int port;
long portValue;
unsigned short port;
if (!object->GetProperty("hostname", &hostname))
throw Exception("Parameter 'hostname' is required for 'rpcconnection' objects.");
if (!object->GetPropertyString("hostname", &hostname))
throw InvalidArgumentException("Parameter 'hostname' is required for 'rpcconnection' objects.");
if (!object->GetPropertyInteger("port", &port))
throw Exception("Parameter 'port' is required for 'rpcconnection' objects.");
if (!object->GetPropertyInteger("port", &portValue))
throw InvalidArgumentException("Parameter 'port' is required for 'rpcconnection' objects.");
if (portValue < 0 || portValue > USHRT_MAX)
throw InvalidArgumentException("Parameter 'port' contains an invalid value.");
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.");

View File

@ -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:

View File

@ -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);
}
};

View File

@ -71,7 +71,7 @@ int JsonRpcEndpoint::NewMessageHandler(const NewMessageEventArgs& nmea)
Endpoint::Ptr sender = static_pointer_cast<Endpoint>(shared_from_this());
string method;
if (message.GetDictionary()->GetValueString("method", &method)) {
if (message.GetPropertyString("method", &method)) {
JsonRpcRequest request = message;
string id;

View File

@ -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);

View File

@ -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);
}
};

View File

@ -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);
}
};

View File

@ -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);
}
};

View File

@ -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()));
}

View File

@ -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);
};
}

View File

@ -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;