From 77bec95ec590956b0bd6c782f685b936db97f438 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 15 May 2012 16:24:04 +0200 Subject: [PATCH] Cleaned up jsonrpc library. Updated documentation. --- base/application.cpp | 31 +-- base/application.h | 3 - components/configrpc/configrpccomponent.cpp | 32 +-- components/configrpc/configrpccomponent.h | 2 +- components/demo/democomponent.cpp | 8 +- components/discovery/discoverycomponent.cpp | 114 +++++------ components/discovery/discoverycomponent.h | 6 +- components/discovery/discoverymessage.h | 16 +- icinga-app/icinga1.conf | 6 +- icinga-app/icinga2.conf | 6 +- icinga-app/icinga3.conf | 6 +- icinga/endpoint.cpp | 184 ++++++++++++------ icinga/endpoint.h | 66 +++---- icinga/endpointmanager.cpp | 116 +++++++++-- icinga/endpointmanager.h | 14 +- icinga/icingaapplication.cpp | 18 +- icinga/icingaapplication.h | 5 +- icinga/icingacomponent.h | 3 + icinga/jsonrpcendpoint.cpp | 25 +-- icinga/jsonrpcendpoint.h | 7 +- icinga/virtualendpoint.cpp | 20 +- icinga/virtualendpoint.h | 15 +- jsonrpc/Makefile.am | 10 +- jsonrpc/i2-jsonrpc.h | 4 +- jsonrpc/jsonrpc.vcxproj | 8 +- jsonrpc/jsonrpc.vcxproj.filters | 8 +- .../{jsonrpcrequest.cpp => rpcrequest.cpp} | 0 jsonrpc/{jsonrpcrequest.h => rpcrequest.h} | 12 +- .../{jsonrpcresponse.cpp => rpcresponse.cpp} | 0 jsonrpc/{jsonrpcresponse.h => rpcresponse.h} | 12 +- 30 files changed, 439 insertions(+), 318 deletions(-) rename jsonrpc/{jsonrpcrequest.cpp => rpcrequest.cpp} (100%) rename jsonrpc/{jsonrpcrequest.h => rpcrequest.h} (90%) rename jsonrpc/{jsonrpcresponse.cpp => rpcresponse.cpp} (100%) rename jsonrpc/{jsonrpcresponse.h => rpcresponse.h} (90%) diff --git a/base/application.cpp b/base/application.cpp index 5e2e4ab10..d854169f9 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -302,26 +302,6 @@ void Application::Log(string message) cout << "[" << timestamp << "]: " << message << endl; } -/** - * Sets the application's command line arguments. - * - * @param arguments The arguments. - */ -void Application::SetArguments(const vector& arguments) -{ - m_Arguments = arguments; -} - -/** - * Retrieves the application's command line arguments. - * - * @returns The arguments. - */ -const vector& Application::GetArguments(void) const -{ - return m_Arguments; -} - /** * Retrieves the directory the application's binary is contained in. * @@ -469,20 +449,17 @@ int Application::Run(int argc, char **argv) sigaction(SIGPIPE, &sa, NULL); #endif /* _WIN32 */ - vector args; - + m_Arguments.clear(); for (int i = 0; i < argc; i++) - args.push_back(string(argv[i])); - - SetArguments(args); + m_Arguments.push_back(string(argv[i])); if (IsDebugging()) { - result = Main(args); + result = Main(m_Arguments); Application::Instance.reset(); } else { try { - result = Main(args); + result = Main(m_Arguments); } catch (const Exception& ex) { Application::Instance.reset(); diff --git a/base/application.h b/base/application.h index 3b3036d68..f2ddff476 100644 --- a/base/application.h +++ b/base/application.h @@ -54,9 +54,6 @@ public: virtual int Main(const vector& args) = 0; - void SetArguments(const vector& arguments); - const vector& GetArguments(void) const; - void Shutdown(void); static void Log(string message); diff --git a/components/configrpc/configrpccomponent.cpp b/components/configrpc/configrpccomponent.cpp index 86dbd8d1c..1569ef072 100644 --- a/components/configrpc/configrpccomponent.cpp +++ b/components/configrpc/configrpccomponent.cpp @@ -35,22 +35,22 @@ void ConfigRpcComponent::Start(void) long configSource; if (GetConfig()->GetPropertyInteger("configSource", &configSource) && configSource != 0) { - m_ConfigRpcEndpoint->RegisterMethodHandler("config::FetchObjects", + m_ConfigRpcEndpoint->RegisterTopicHandler("config::FetchObjects", bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this())); configHive->OnObjectCommitted += bind_weak(&ConfigRpcComponent::LocalObjectCommittedHandler, shared_from_this()); configHive->OnObjectRemoved += bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this()); - m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectCommitted"); - m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectRemoved"); + m_ConfigRpcEndpoint->RegisterPublication("config::ObjectCommitted"); + m_ConfigRpcEndpoint->RegisterPublication("config::ObjectRemoved"); } endpointManager->OnNewEndpoint += bind_weak(&ConfigRpcComponent::NewEndpointHandler, shared_from_this()); - m_ConfigRpcEndpoint->RegisterMethodSource("config::FetchObjects"); - m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectCommitted", + m_ConfigRpcEndpoint->RegisterPublication("config::FetchObjects"); + m_ConfigRpcEndpoint->RegisterTopicHandler("config::ObjectCommitted", bind_weak(&ConfigRpcComponent::RemoteObjectCommittedHandler, shared_from_this())); - m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectRemoved", + m_ConfigRpcEndpoint->RegisterTopicHandler("config::ObjectRemoved", bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this())); endpointManager->RegisterEndpoint(m_ConfigRpcEndpoint); @@ -73,18 +73,18 @@ int ConfigRpcComponent::NewEndpointHandler(const NewEndpointEventArgs& ea) int ConfigRpcComponent::SessionEstablishedHandler(const EventArgs& ea) { - JsonRpcRequest request; + RpcRequest request; request.SetMethod("config::FetchObjects"); Endpoint::Ptr endpoint = static_pointer_cast(ea.Source); - GetEndpointManager()->SendUnicastRequest(m_ConfigRpcEndpoint, endpoint, request); + GetEndpointManager()->SendUnicastMessage(m_ConfigRpcEndpoint, endpoint, request); return 0; } -JsonRpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties) +RpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties) { - JsonRpcRequest msg; + RpcRequest msg; msg.SetMethod(method); Message params; @@ -121,9 +121,9 @@ int ConfigRpcComponent::FetchObjectsHandler(const NewRequestEventArgs& ea) if (!ShouldReplicateObject(object)) continue; - JsonRpcRequest request = MakeObjectMessage(object, "config::ObjectCreated", true); + RpcRequest request = MakeObjectMessage(object, "config::ObjectCreated", true); - GetEndpointManager()->SendUnicastRequest(m_ConfigRpcEndpoint, client, request); + GetEndpointManager()->SendUnicastMessage(m_ConfigRpcEndpoint, client, request); } } @@ -137,7 +137,7 @@ int ConfigRpcComponent::LocalObjectCommittedHandler(const EventArgs& ea) if (!ShouldReplicateObject(object)) return 0; - GetEndpointManager()->SendMulticastRequest(m_ConfigRpcEndpoint, + GetEndpointManager()->SendMulticastMessage(m_ConfigRpcEndpoint, MakeObjectMessage(object, "config::ObjectCreated", true)); return 0; @@ -150,7 +150,7 @@ int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea) if (!ShouldReplicateObject(object)) return 0; - GetEndpointManager()->SendMulticastRequest(m_ConfigRpcEndpoint, + GetEndpointManager()->SendMulticastMessage(m_ConfigRpcEndpoint, MakeObjectMessage(object, "config::ObjectRemoved", false)); return 0; @@ -158,7 +158,7 @@ int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea) int ConfigRpcComponent::RemoteObjectCommittedHandler(const NewRequestEventArgs& ea) { - JsonRpcRequest message = ea.Request; + RpcRequest message = ea.Request; bool was_null = false; Message params; @@ -199,7 +199,7 @@ int ConfigRpcComponent::RemoteObjectCommittedHandler(const NewRequestEventArgs& int ConfigRpcComponent::RemoteObjectRemovedHandler(const NewRequestEventArgs& ea) { - JsonRpcRequest message = ea.Request; + RpcRequest message = ea.Request; Message params; if (!message.GetParams(¶ms)) diff --git a/components/configrpc/configrpccomponent.h b/components/configrpc/configrpccomponent.h index 9c367f886..b91f64649 100644 --- a/components/configrpc/configrpccomponent.h +++ b/components/configrpc/configrpccomponent.h @@ -38,7 +38,7 @@ private: int RemoteObjectCommittedHandler(const NewRequestEventArgs& ea); int RemoteObjectRemovedHandler(const NewRequestEventArgs& ea); - static JsonRpcRequest MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties); + static RpcRequest MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties); static bool ShouldReplicateObject(const ConfigObject::Ptr& object); public: diff --git a/components/demo/democomponent.cpp b/components/demo/democomponent.cpp index 8fa574876..560f16885 100644 --- a/components/demo/democomponent.cpp +++ b/components/demo/democomponent.cpp @@ -37,9 +37,9 @@ string DemoComponent::GetName(void) const void DemoComponent::Start(void) { m_DemoEndpoint = make_shared(); - m_DemoEndpoint->RegisterMethodHandler("demo::HelloWorld", + m_DemoEndpoint->RegisterTopicHandler("demo::HelloWorld", bind_weak(&DemoComponent::HelloWorldRequestHandler, shared_from_this())); - m_DemoEndpoint->RegisterMethodSource("demo::HelloWorld"); + m_DemoEndpoint->RegisterPublication("demo::HelloWorld"); EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager(); endpointManager->RegisterEndpoint(m_DemoEndpoint); @@ -73,11 +73,11 @@ int DemoComponent::DemoTimerHandler(const TimerEventArgs&) { Application::Log("Sending multicast 'hello world' message."); - JsonRpcRequest request; + RpcRequest request; request.SetMethod("demo::HelloWorld"); EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager(); - endpointManager->SendMulticastRequest(m_DemoEndpoint, request); + endpointManager->SendMulticastMessage(m_DemoEndpoint, request); return 0; } diff --git a/components/discovery/discoverycomponent.cpp b/components/discovery/discoverycomponent.cpp index c6e738e10..ce752a217 100644 --- a/components/discovery/discoverycomponent.cpp +++ b/components/discovery/discoverycomponent.cpp @@ -38,15 +38,15 @@ void DiscoveryComponent::Start(void) { m_DiscoveryEndpoint = make_shared(); - m_DiscoveryEndpoint->RegisterMethodSource("discovery::RegisterComponent"); - m_DiscoveryEndpoint->RegisterMethodHandler("discovery::RegisterComponent", + m_DiscoveryEndpoint->RegisterPublication("discovery::RegisterComponent"); + m_DiscoveryEndpoint->RegisterTopicHandler("discovery::RegisterComponent", bind_weak(&DiscoveryComponent::RegisterComponentMessageHandler, shared_from_this())); - m_DiscoveryEndpoint->RegisterMethodSource("discovery::NewComponent"); - m_DiscoveryEndpoint->RegisterMethodHandler("discovery::NewComponent", + m_DiscoveryEndpoint->RegisterPublication("discovery::NewComponent"); + m_DiscoveryEndpoint->RegisterTopicHandler("discovery::NewComponent", bind_weak(&DiscoveryComponent::NewComponentMessageHandler, shared_from_this())); - m_DiscoveryEndpoint->RegisterMethodHandler("discovery::Welcome", + m_DiscoveryEndpoint->RegisterTopicHandler("discovery::Welcome", bind_weak(&DiscoveryComponent::WelcomeMessageHandler, shared_from_this())); GetEndpointManager()->ForEachEndpoint(bind(&DiscoveryComponent::NewEndpointHandler, this, _1)); @@ -112,42 +112,16 @@ int DiscoveryComponent::NewEndpointHandler(const NewEndpointEventArgs& neea) neea.Endpoint->OnIdentityChanged += bind_weak(&DiscoveryComponent::NewIdentityHandler, shared_from_this()); /* accept discovery::RegisterComponent messages from any endpoint */ - neea.Endpoint->RegisterMethodSource("discovery::RegisterComponent"); + neea.Endpoint->RegisterPublication("discovery::RegisterComponent"); /* accept discovery::Welcome messages from any endpoint */ - neea.Endpoint->RegisterMethodSource("discovery::Welcome"); + neea.Endpoint->RegisterPublication("discovery::Welcome"); return 0; } /** - * Registers a new message sink for a component. - * - * @param nmea Event args for the new message sink. - * @param info The component registration information. - * @returns 0 - */ -int DiscoveryComponent::DiscoverySinkHandler(const NewMethodEventArgs& nmea, ComponentDiscoveryInfo::Ptr info) const -{ - info->SubscribedMethods.insert(nmea.Method); - return 0; -} - -/** - * Registers a new message source for a component. - * - * @param nmea Event args for the new message source. - * @param info The component registration information. - * @returns 0 - */ -int DiscoveryComponent::DiscoverySourceHandler(const NewMethodEventArgs& nmea, ComponentDiscoveryInfo::Ptr info) const -{ - info->PublishedMethods.insert(nmea.Method); - return 0; -} - -/** - * Registers message sinks/sources in the specified component information object. + * Registers message Subscriptions/sources in the specified component information object. * * @param neea Event arguments for the endpoint. * @param info Component information object. @@ -155,8 +129,16 @@ int DiscoveryComponent::DiscoverySourceHandler(const NewMethodEventArgs& nmea, C */ int DiscoveryComponent::DiscoveryEndpointHandler(const NewEndpointEventArgs& neea, ComponentDiscoveryInfo::Ptr info) const { - neea.Endpoint->ForEachMethodSink(bind(&DiscoveryComponent::DiscoverySinkHandler, this, _1, info)); - neea.Endpoint->ForEachMethodSource(bind(&DiscoveryComponent::DiscoverySourceHandler, this, _1, info)); + Endpoint::ConstTopicIterator i; + + for (i = neea.Endpoint->BeginSubscriptions(); i != neea.Endpoint->EndSubscriptions(); i++) { + info->Subscriptions.insert(*i); + } + + for (i = neea.Endpoint->BeginPublications(); i != neea.Endpoint->EndPublications(); i++) { + info->Publications.insert(*i); + } + return 0; } @@ -216,7 +198,7 @@ int DiscoveryComponent::NewIdentityHandler(const EventArgs& ea) // we assume the other component _always_ wants // discovery::RegisterComponent messages from us - endpoint->RegisterMethodSink("discovery::RegisterComponent"); + endpoint->RegisterSubscription("discovery::RegisterComponent"); // send a discovery::RegisterComponent message, if the // other component is a broker this makes sure @@ -227,7 +209,7 @@ int DiscoveryComponent::NewIdentityHandler(const EventArgs& ea) // we assume the other component _always_ wants // discovery::NewComponent messages from us - endpoint->RegisterMethodSink("discovery::NewComponent"); + endpoint->RegisterSubscription("discovery::NewComponent"); // send discovery::NewComponent message for ourselves SendDiscoveryMessage("discovery::NewComponent", GetEndpointManager()->GetIdentity(), endpoint); @@ -248,14 +230,14 @@ int DiscoveryComponent::NewIdentityHandler(const EventArgs& ea) return 0; } - // register published/subscribed methods for this endpoint + // register published/subscribed topics for this endpoint ComponentDiscoveryInfo::Ptr info = ic->second; set::iterator it; - for (it = info->PublishedMethods.begin(); it != info->PublishedMethods.end(); it++) - endpoint->RegisterMethodSource(*it); + for (it = info->Publications.begin(); it != info->Publications.end(); it++) + endpoint->RegisterPublication(*it); - for (it = info->SubscribedMethods.begin(); it != info->SubscribedMethods.end(); it++) - endpoint->RegisterMethodSink(*it); + for (it = info->Subscriptions.begin(); it != info->Subscriptions.end(); it++) + endpoint->RegisterSubscription(*it); FinishDiscoverySetup(endpoint); @@ -288,7 +270,7 @@ int DiscoveryComponent::WelcomeMessageHandler(const NewRequestEventArgs& nrea) /** * Finishes the welcome handshake for a new component - * by registering message sinks/sources for the component + * by registering message Subscriptions/sources for the component * and sending a welcome message if necessary. * * @param endpoint The endpoint to set up. @@ -300,10 +282,10 @@ void DiscoveryComponent::FinishDiscoverySetup(Endpoint::Ptr endpoint) // we assume the other component _always_ wants // discovery::Welcome messages from us - endpoint->RegisterMethodSink("discovery::Welcome"); - JsonRpcRequest request; + endpoint->RegisterSubscription("discovery::Welcome"); + RpcRequest request; request.SetMethod("discovery::Welcome"); - GetEndpointManager()->SendUnicastRequest(m_DiscoveryEndpoint, endpoint, request); + GetEndpointManager()->SendUnicastMessage(m_DiscoveryEndpoint, endpoint, request); endpoint->SetSentWelcome(true); @@ -324,7 +306,7 @@ void DiscoveryComponent::FinishDiscoverySetup(Endpoint::Ptr endpoint) */ void DiscoveryComponent::SendDiscoveryMessage(string method, string identity, Endpoint::Ptr recipient) { - JsonRpcRequest request; + RpcRequest request; request.SetMethod(method); DiscoveryMessage params; @@ -333,10 +315,10 @@ void DiscoveryComponent::SendDiscoveryMessage(string method, string identity, En params.SetIdentity(identity); Message subscriptions; - params.SetSubscribes(subscriptions); + params.SetSubscriptions(subscriptions); Message publications; - params.SetProvides(publications); + params.SetPublications(publications); ComponentDiscoveryInfo::Ptr info; @@ -349,16 +331,16 @@ void DiscoveryComponent::SendDiscoveryMessage(string method, string identity, En } set::iterator i; - for (i = info->PublishedMethods.begin(); i != info->PublishedMethods.end(); i++) + for (i = info->Publications.begin(); i != info->Publications.end(); i++) publications.AddUnnamedPropertyString(*i); - for (i = info->SubscribedMethods.begin(); i != info->SubscribedMethods.end(); i++) + for (i = info->Subscriptions.begin(); i != info->Subscriptions.end(); i++) subscriptions.AddUnnamedPropertyString(*i); if (recipient) - GetEndpointManager()->SendUnicastRequest(m_DiscoveryEndpoint, recipient, request); + GetEndpointManager()->SendUnicastMessage(m_DiscoveryEndpoint, recipient, request); else - GetEndpointManager()->SendMulticastRequest(m_DiscoveryEndpoint, request); + GetEndpointManager()->SendMulticastMessage(m_DiscoveryEndpoint, request); } bool DiscoveryComponent::HasMessagePermission(Dictionary::Ptr roles, string messageType, string message) @@ -419,26 +401,26 @@ void DiscoveryComponent::ProcessDiscoveryMessage(string identity, DiscoveryMessa Endpoint::Ptr endpoint = GetEndpointManager()->GetEndpointByIdentity(identity); - Message provides; - if (message.GetProvides(&provides)) { + Message publications; + if (message.GetPublications(&publications)) { DictionaryIterator i; - for (i = provides.GetDictionary()->Begin(); i != provides.GetDictionary()->End(); i++) { - if (trusted || HasMessagePermission(roles, "publish", i->second)) { - info->PublishedMethods.insert(i->second); + for (i = publications.GetDictionary()->Begin(); i != publications.GetDictionary()->End(); i++) { + if (trusted || HasMessagePermission(roles, "publications", i->second)) { + info->Publications.insert(i->second); if (endpoint) - endpoint->RegisterMethodSource(i->second); + endpoint->RegisterPublication(i->second); } } } - Message subscribes; - if (message.GetSubscribes(&subscribes)) { + Message subscriptions; + if (message.GetSubscriptions(&subscriptions)) { DictionaryIterator i; - for (i = subscribes.GetDictionary()->Begin(); i != subscribes.GetDictionary()->End(); i++) { - if (trusted || HasMessagePermission(roles, "subscribe", i->second)) { - info->SubscribedMethods.insert(i->second); + for (i = subscriptions.GetDictionary()->Begin(); i != subscriptions.GetDictionary()->End(); i++) { + if (trusted || HasMessagePermission(roles, "subscriptions", i->second)) { + info->Subscriptions.insert(i->second); if (endpoint) - endpoint->RegisterMethodSink(i->second); + endpoint->RegisterSubscription(i->second); } } } diff --git a/components/discovery/discoverycomponent.h b/components/discovery/discoverycomponent.h index 049b916c4..ad7f6a0ea 100644 --- a/components/discovery/discoverycomponent.h +++ b/components/discovery/discoverycomponent.h @@ -32,8 +32,8 @@ public: string Node; string Service; - set SubscribedMethods; - set PublishedMethods; + set Subscriptions; + set Publications; time_t LastSeen; }; @@ -60,8 +60,6 @@ private: int CheckExistingEndpoint(Endpoint::Ptr endpoint, const NewEndpointEventArgs& neea); int DiscoveryEndpointHandler(const NewEndpointEventArgs& neea, ComponentDiscoveryInfo::Ptr info) const; - int DiscoverySinkHandler(const NewMethodEventArgs& nmea, ComponentDiscoveryInfo::Ptr info) const; - int DiscoverySourceHandler(const NewMethodEventArgs& nmea, ComponentDiscoveryInfo::Ptr info) const; int DiscoveryTimerHandler(const TimerEventArgs& tea); diff --git a/components/discovery/discoverymessage.h b/components/discovery/discoverymessage.h index ad88e3b93..f57540929 100644 --- a/components/discovery/discoverymessage.h +++ b/components/discovery/discoverymessage.h @@ -41,24 +41,24 @@ public: SetPropertyString("service", value); } - inline bool GetSubscribes(Message *value) const + inline bool GetSubscriptions(Message *value) const { - return GetPropertyMessage("subscribes", value); + return GetPropertyMessage("subscriptions", value); } - inline void SetSubscribes(Message value) + inline void SetSubscriptions(Message value) { - SetPropertyMessage("subscribes", value); + SetPropertyMessage("subscriptions", value); } - inline bool GetProvides(Message *value) const + inline bool GetPublications(Message *value) const { - return GetPropertyMessage("provides", value); + return GetPropertyMessage("publications", value); } - inline void SetProvides(Message value) + inline void SetPublications(Message value) { - SetPropertyMessage("provides", value); + SetPropertyMessage("publications", value); } }; diff --git a/icinga-app/icinga1.conf b/icinga-app/icinga1.conf index 6b6a1bafb..679874d3e 100644 --- a/icinga-app/icinga1.conf +++ b/icinga-app/icinga1.conf @@ -29,11 +29,11 @@ }, "role": { "broker": { - "publish": [ "discovery::NewComponent" ] + "publications": [ "discovery::NewComponent" ] }, "demo": { - "publish": [ "demo::*" ], - "subscribe": [ "demo::*" ] + "publications": [ "demo::*" ], + "subscriptions": [ "demo::*" ] } } } \ No newline at end of file diff --git a/icinga-app/icinga2.conf b/icinga-app/icinga2.conf index c457b86da..b6e79c99a 100644 --- a/icinga-app/icinga2.conf +++ b/icinga-app/icinga2.conf @@ -24,11 +24,11 @@ }, "role": { "broker": { - "publish": [ "discovery::NewComponent" ] + "publications": [ "discovery::NewComponent" ] }, "demo": { - "publish": [ "demo::*" ], - "subscribe": [ "demo::*" ] + "publications": [ "demo::*" ], + "subscriptions": [ "demo::*" ] } } } \ No newline at end of file diff --git a/icinga-app/icinga3.conf b/icinga-app/icinga3.conf index c6af61273..879a39f22 100644 --- a/icinga-app/icinga3.conf +++ b/icinga-app/icinga3.conf @@ -24,11 +24,11 @@ }, "role": { "broker": { - "publish": [ "discovery::NewComponent" ] + "publications": [ "discovery::NewComponent" ] }, "demo": { - "publish": [ "demo::*" ], - "subscribe": [ "demo::*" ] + "publications": [ "demo::*" ], + "subscriptions": [ "demo::*" ] } } } \ No newline at end of file diff --git a/icinga/endpoint.cpp b/icinga/endpoint.cpp index ada9056de..e00ffff94 100644 --- a/icinga/endpoint.cpp +++ b/icinga/endpoint.cpp @@ -21,17 +21,30 @@ using namespace icinga; +/** + * Constructor for the Endpoint class. + */ Endpoint::Endpoint(void) { m_ReceivedWelcome = false; m_SentWelcome = false; } +/** + * Retrieves the identity of this endpoint. + * + * @returns The identity of the endpoint. + */ string Endpoint::GetIdentity(void) const { return m_Identity; } +/** + * Sets the identity of this endpoint. + * + * @param identity The new identity of the endpoint. + */ void Endpoint::SetIdentity(string identity) { m_Identity = identity; @@ -41,126 +54,179 @@ void Endpoint::SetIdentity(string identity) OnIdentityChanged(ea); } -bool Endpoint::HasIdentity(void) const -{ - return !m_Identity.empty(); -} - +/** + * Retrieves the endpoint manager this endpoint is registered with. + * + * @returns The EndpointManager object. + */ EndpointManager::Ptr Endpoint::GetEndpointManager(void) const { return m_EndpointManager.lock(); } +/** + * Sets the endpoint manager this endpoint is registered with. + * + * @param manager The EndpointManager object. + */ void Endpoint::SetEndpointManager(EndpointManager::WeakPtr manager) { m_EndpointManager = manager; } -void Endpoint::RegisterMethodSink(string method) +/** + * Registers a topic subscription for this endpoint. + * + * @param topic The name of the topic. + */ +void Endpoint::RegisterSubscription(string topic) { - m_MethodSinks.insert(method); + m_Subscriptions.insert(topic); } -void Endpoint::UnregisterMethodSink(string method) +/** + * Removes a topic subscription from this endpoint. + * + * @param topic The name of the topic. + */ +void Endpoint::UnregisterSubscription(string topic) { - m_MethodSinks.erase(method); + m_Subscriptions.erase(topic); } -bool Endpoint::IsMethodSink(string method) const +/** + * Checks whether the endpoint has a subscription for the specified topic. + * + * @param topic The name of the topic. + * @returns true if the endpoint is subscribed to the topic, false otherwise. + */ +bool Endpoint::HasSubscription(string topic) const { - return (m_MethodSinks.find(method) != m_MethodSinks.end()); + return (m_Subscriptions.find(topic) != m_Subscriptions.end()); } -void Endpoint::ForEachMethodSink(function callback) +/** + * Registers a topic publication for this endpoint. + * + * @param topic The name of the topic. + */ +void Endpoint::RegisterPublication(string topic) { - for (set::iterator i = m_MethodSinks.begin(); i != m_MethodSinks.end(); i++) { - NewMethodEventArgs nmea; - nmea.Source = shared_from_this(); - nmea.Method = *i; - callback(nmea); - } + m_Publications.insert(topic); } -void Endpoint::RegisterMethodSource(string method) +/** + * Removes a topic publication from this endpoint. + * + * @param topic The name of the topic. + */ +void Endpoint::UnregisterPublication(string topic) { - m_MethodSources.insert(method); + m_Publications.erase(topic); } -void Endpoint::UnregisterMethodSource(string method) +/** + * Checks whether the endpoint has a publication for the specified topic. + * + * @param topic The name of the topic. + * @returns true if the endpoint is publishing this topic, false otherwise. + */ +bool Endpoint::HasPublication(string topic) const { - m_MethodSources.erase(method); + return (m_Publications.find(topic) != m_Publications.end()); } -bool Endpoint::IsMethodSource(string method) const +/** + * Removes all subscriptions for the endpoint. + */ +void Endpoint::ClearSubscriptions(void) { - return (m_MethodSources.find(method) != m_MethodSources.end()); + m_Subscriptions.clear(); } -void Endpoint::ForEachMethodSource(function callback) +/** + * Removes all publications for the endpoint. + */ +void Endpoint::ClearPublications(void) { - for (set::iterator i = m_MethodSources.begin(); i != m_MethodSources.end(); i++) { - NewMethodEventArgs nmea; - nmea.Source = shared_from_this(); - nmea.Method = *i; - callback(nmea); - } + m_Publications.clear(); } -void Endpoint::ClearMethodSinks(void) +/** + * Returns the beginning of the subscriptions list. + * + * @returns An iterator that points to the first subscription. + */ +Endpoint::ConstTopicIterator Endpoint::BeginSubscriptions(void) const { - m_MethodSinks.clear(); + return m_Subscriptions.begin(); } -void Endpoint::ClearMethodSources(void) +/** + * Returns the end of the subscriptions list. + * + * @returns An iterator that points past the last subscription. + */ +Endpoint::ConstTopicIterator Endpoint::EndSubscriptions(void) const { - m_MethodSources.clear(); + return m_Subscriptions.end(); } -int Endpoint::CountMethodSinks(void) const +/** + * Returns the beginning of the publications list. + * + * @returns An iterator that points to the first publication. + */ +Endpoint::ConstTopicIterator Endpoint::BeginPublications(void) const { - return m_MethodSinks.size(); + return m_Publications.begin(); } -int Endpoint::CountMethodSources(void) const +/** + * Returns the end of the publications list. + * + * @returns An iterator that points past the last publication. + */ +Endpoint::ConstTopicIterator Endpoint::EndPublications(void) const { - return m_MethodSources.size(); -} - -set::const_iterator Endpoint::BeginSinks(void) const -{ - return m_MethodSinks.begin(); -} - -set::const_iterator Endpoint::EndSinks(void) const -{ - return m_MethodSinks.end(); -} - -set::const_iterator Endpoint::BeginSources(void) const -{ - return m_MethodSources.begin(); -} - -set::const_iterator Endpoint::EndSources(void) const -{ - return m_MethodSources.end(); + return m_Publications.end(); } +/** + * Sets whether a welcome message has been received from this endpoint. + * + * @param value Whether we've received a welcome message. + */ void Endpoint::SetReceivedWelcome(bool value) { m_ReceivedWelcome = value; } +/** + * Retrieves whether a welcome message has been received from this endpoint. + * + * @returns Whether we've received a welcome message. + */ bool Endpoint::GetReceivedWelcome(void) const { return m_ReceivedWelcome; } +/** + * Sets whether a welcome message has been sent to this endpoint. + * + * @param value Whether we've sent a welcome message. + */ void Endpoint::SetSentWelcome(bool value) { m_SentWelcome = value; } +/** + * Retrieves whether a welcome message has been sent to this endpoint. + * + * @returns Whether we've sent a welcome message. + */ bool Endpoint::GetSentWelcome(void) const { return m_SentWelcome; diff --git a/icinga/endpoint.h b/icinga/endpoint.h index 9aec18e5b..71939675a 100644 --- a/icinga/endpoint.h +++ b/icinga/endpoint.h @@ -25,33 +25,38 @@ namespace icinga class EndpointManager; -struct I2_ICINGA_API NewMethodEventArgs : public EventArgs -{ - string Method; -}; - +/** + * An endpoint that can be used to send and receive messages. + */ class I2_ICINGA_API Endpoint : public Object { private: - string m_Identity; - set m_MethodSinks; - set m_MethodSources; - bool m_ReceivedWelcome; - bool m_SentWelcome; + string m_Identity; /**< The identity of this endpoint. */ + set m_Subscriptions; /**< The topics this endpoint is + subscribed to. */ + set m_Publications; /**< The topics this endpoint is + publishing. */ + bool m_ReceivedWelcome; /**< Have we received a welcome message + from this endpoint? */ + bool m_SentWelcome; /**< Have we sent a welcome message to this + endpoint? */ - weak_ptr m_EndpointManager; + weak_ptr m_EndpointManager; /**< The endpoint manager + this endpoint is + registered with. */ public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; + typedef set::const_iterator ConstTopicIterator; + Endpoint(void); virtual string GetAddress(void) const = 0; string GetIdentity(void) const; void SetIdentity(string identity); - bool HasIdentity(void) const; void SetReceivedWelcome(bool value); bool GetReceivedWelcome(void) const; @@ -62,39 +67,30 @@ public: shared_ptr GetEndpointManager(void) const; void SetEndpointManager(weak_ptr manager); - void RegisterMethodSink(string method); - void UnregisterMethodSink(string method); - bool IsMethodSink(string method) const; + void RegisterSubscription(string topic); + void UnregisterSubscription(string topic); + bool HasSubscription(string topic) const; - void RegisterMethodSource(string method); - void UnregisterMethodSource(string method); - bool IsMethodSource(string method) const; + void RegisterPublication(string topic); + void UnregisterPublication(string topic); + bool HasPublication(string topic) const; virtual bool IsLocal(void) const = 0; virtual bool IsConnected(void) const = 0; - virtual void ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& message) = 0; - virtual void ProcessResponse(Endpoint::Ptr sender, const JsonRpcResponse& message) = 0; + virtual void ProcessRequest(Endpoint::Ptr sender, const RpcRequest& message) = 0; + virtual void ProcessResponse(Endpoint::Ptr sender, const RpcResponse& message) = 0; virtual void Stop(void) = 0; - Event OnNewMethodSink; - Event OnNewMethodSource; + void ClearSubscriptions(void); + void ClearPublications(void); - void ForEachMethodSink(function callback); - void ForEachMethodSource(function callback); + ConstTopicIterator BeginSubscriptions(void) const; + ConstTopicIterator EndSubscriptions(void) const; - void ClearMethodSinks(void); - void ClearMethodSources(void); - - int CountMethodSinks(void) const; - int CountMethodSources(void) const; - - set::const_iterator BeginSinks(void) const; - set::const_iterator EndSinks(void) const; - - set::const_iterator BeginSources(void) const; - set::const_iterator EndSources(void) const; + ConstTopicIterator BeginPublications(void) const; + ConstTopicIterator EndPublications(void) const; Event OnIdentityChanged; Event OnSessionEstablished; diff --git a/icinga/endpointmanager.cpp b/icinga/endpointmanager.cpp index 3a2012095..11f88e1c2 100644 --- a/icinga/endpointmanager.cpp +++ b/icinga/endpointmanager.cpp @@ -21,26 +21,52 @@ using namespace icinga; +/** + * Sets the identity of the endpoint manager. This identity is used when + * connecting to remote peers. + * + * @param identity The new identity. + */ void EndpointManager::SetIdentity(string identity) { m_Identity = identity; } +/** + * Retrieves the identity for the endpoint manager. + * + * @returns The identity. + */ string EndpointManager::GetIdentity(void) const { return m_Identity; } +/** + * Sets the SSL context that is used for remote connections. + * + * @param sslContext The new SSL context. + */ void EndpointManager::SetSSLContext(shared_ptr sslContext) { m_SSLContext = sslContext; } +/** + * Retrieves the SSL context that is used for remote connections. + * + * @returns The SSL context. + */ shared_ptr EndpointManager::GetSSLContext(void) const { return m_SSLContext; } +/** + * Creates a new JSON-RPC listener on the specified port. + * + * @param service The name of the service to listen on (@see getaddrinfo). + */ void EndpointManager::AddListener(string service) { if (!GetSSLContext()) @@ -58,6 +84,12 @@ void EndpointManager::AddListener(string service) server->Start(); } +/** + * Creates a new JSON-RPC client and connects to the specified host and port. + * + * @param node The remote host (@see getaddrinfo). + * @param service The remote port (@see getaddrinfo). + */ void EndpointManager::AddConnection(string node, string service) { stringstream s; @@ -69,12 +101,22 @@ void EndpointManager::AddConnection(string node, string service) endpoint->Connect(node, service, m_SSLContext); } +/** + * Registers a new JSON-RPC server with this endpoint manager. + * + * @param server The JSON-RPC server. + */ void EndpointManager::RegisterServer(JsonRpcServer::Ptr server) { m_Servers.push_back(server); server->OnNewClient += bind_weak(&EndpointManager::NewClientHandler, shared_from_this()); } +/** + * Processes a new client connection. + * + * @param ncea Event arguments. + */ int EndpointManager::NewClientHandler(const NewClientEventArgs& ncea) { string address = ncea.Client->GetPeerAddress(); @@ -87,6 +129,11 @@ int EndpointManager::NewClientHandler(const NewClientEventArgs& ncea) return 0; } +/** + * Unregisters a JSON-RPC server. + * + * @param server The JSON-RPC server. + */ void EndpointManager::UnregisterServer(JsonRpcServer::Ptr server) { m_Servers.erase( @@ -95,6 +142,11 @@ void EndpointManager::UnregisterServer(JsonRpcServer::Ptr server) // TODO: unbind event } +/** + * Registers a new endpoint with this endpoint manager. + * + * @param endpoint The new endpoint. + */ void EndpointManager::RegisterEndpoint(Endpoint::Ptr endpoint) { if (!endpoint->IsLocal() && endpoint->GetIdentity() != "") @@ -109,6 +161,11 @@ void EndpointManager::RegisterEndpoint(Endpoint::Ptr endpoint) OnNewEndpoint(neea); } +/** + * Unregisters an endpoint. + * + * @param endpoint The endpoint. + */ void EndpointManager::UnregisterEndpoint(Endpoint::Ptr endpoint) { m_Endpoints.erase( @@ -116,48 +173,68 @@ void EndpointManager::UnregisterEndpoint(Endpoint::Ptr endpoint) m_Endpoints.end()); } -void EndpointManager::SendUnicastRequest(Endpoint::Ptr sender, Endpoint::Ptr recipient, const JsonRpcRequest& request, bool fromLocal) +/** + * Sends a unicast message to the specified recipient. + * + * @param sender The sender of the message. + * @param recipient The recipient of the message. + * @param message The request. + */ +void EndpointManager::SendUnicastMessage(Endpoint::Ptr sender, Endpoint::Ptr recipient, const Message& message) { + /* don't forward messages back to the sender */ if (sender == recipient) return; /* don't forward messages between non-local endpoints */ - if (!fromLocal && !recipient->IsLocal()) + if (!sender->IsLocal() && !recipient->IsLocal()) return; - string method; - if (!request.GetMethod(&method)) - throw InvalidArgumentException("Missing 'method' parameter."); - - if (recipient->IsMethodSink(method)) { - //Application::Log(sender->GetAddress() + " -> " + recipient->GetAddress() + ": " + method); - recipient->ProcessRequest(sender, request); - } + recipient->ProcessRequest(sender, message); } -void EndpointManager::SendAnycastRequest(Endpoint::Ptr sender, const JsonRpcRequest& request, bool fromLocal) +/** + * Sends a message to exactly one recipient out of all recipients who have a + * subscription for the message's topic. + * + * @param sender The sender of the message. + * @param message The message. + */ +void EndpointManager::SendAnycastMessage(Endpoint::Ptr sender, const RpcRequest& message) { throw NotImplementedException(); } -void EndpointManager::SendMulticastRequest(Endpoint::Ptr sender, const JsonRpcRequest& request, bool fromLocal) +/** + * Sends a message to all recipients who have a subscription for the + * message's topic. + * + * @param sender The sender of the message. + * @param message The message. + */ +void EndpointManager::SendMulticastMessage(Endpoint::Ptr sender, const RpcRequest& message) { -#ifdef _DEBUG string id; - if (request.GetID(&id)) + if (message.GetID(&id)) throw InvalidArgumentException("Multicast requests must not have an ID."); -#endif /* _DEBUG */ string method; - if (!request.GetMethod(&method)) + if (!message.GetMethod(&method)) throw InvalidArgumentException("Message is missing the 'method' property."); for (vector::iterator i = m_Endpoints.begin(); i != m_Endpoints.end(); i++) { - SendUnicastRequest(sender, *i, request, fromLocal); + Endpoint::Ptr recipient = *i; + if (recipient->HasSubscription(method)) + SendUnicastMessage(sender, recipient, message); } } +/** + * Calls the specified callback function for each registered endpoint. + * + * @param callback The callback function. + */ void EndpointManager::ForEachEndpoint(function callback) { NewEndpointEventArgs neea; @@ -173,6 +250,11 @@ void EndpointManager::ForEachEndpoint(function::const_iterator i; diff --git a/icinga/endpointmanager.h b/icinga/endpointmanager.h index b866a83f8..816c1f9e0 100644 --- a/icinga/endpointmanager.h +++ b/icinga/endpointmanager.h @@ -23,11 +23,17 @@ namespace icinga { +/** + * Event arguments for the "new endpoint registered" event. + */ struct I2_ICINGA_API NewEndpointEventArgs : public EventArgs { - icinga::Endpoint::Ptr Endpoint; + icinga::Endpoint::Ptr Endpoint; /**< The new endpoint. */ }; +/** + * Forwards messages between endpoints. + */ class I2_ICINGA_API EndpointManager : public Object { string m_Identity; @@ -57,9 +63,9 @@ public: void RegisterEndpoint(Endpoint::Ptr endpoint); void UnregisterEndpoint(Endpoint::Ptr endpoint); - void SendUnicastRequest(Endpoint::Ptr sender, Endpoint::Ptr recipient, const JsonRpcRequest& request, bool fromLocal = true); - void SendAnycastRequest(Endpoint::Ptr sender, const JsonRpcRequest& request, bool fromLocal = true); - void SendMulticastRequest(Endpoint::Ptr sender, const JsonRpcRequest& request, bool fromLocal = true); + void SendUnicastMessage(Endpoint::Ptr sender, Endpoint::Ptr recipient, const Message& message); + void SendAnycastMessage(Endpoint::Ptr sender, const RpcRequest& message); + void SendMulticastMessage(Endpoint::Ptr sender, const RpcRequest& message); void ForEachEndpoint(function callback); diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index c515cade3..b029c9aa8 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -28,6 +28,12 @@ using namespace icinga; +/** + * The entry point for the Icinga application. + * + * @param args Command-line arguments. + * @returns An exit status. + */ int IcingaApplication::Main(const vector& args) { #ifdef _WIN32 @@ -37,7 +43,7 @@ int IcingaApplication::Main(const vector& args) #endif /* _WIN32 */ if (args.size() < 2) { - PrintUsage(args[0]); + cout << "Syntax: " << args[0] << " " << endl; return EXIT_FAILURE; } @@ -87,11 +93,11 @@ int IcingaApplication::Main(const vector& args) return EXIT_SUCCESS; } -void IcingaApplication::PrintUsage(const string& programPath) -{ - cout << "Syntax: " << programPath << " " << endl; -} - +/** + * Retrieves Icinga's endpoint manager. + * + * @returns The endpoint manager. + */ EndpointManager::Ptr IcingaApplication::GetEndpointManager(void) { return m_EndpointManager; diff --git a/icinga/icingaapplication.h b/icinga/icingaapplication.h index 9901b20b8..15f903e83 100644 --- a/icinga/icingaapplication.h +++ b/icinga/icingaapplication.h @@ -23,6 +23,9 @@ namespace icinga { +/** + * The Icinga application. + */ class I2_ICINGA_API IcingaApplication : public Application { private: @@ -54,8 +57,6 @@ public: int Main(const vector& args); - void PrintUsage(const string& programPath); - EndpointManager::Ptr GetEndpointManager(void); void SetPrivateKeyFile(string privkey); diff --git a/icinga/icingacomponent.h b/icinga/icingacomponent.h index 8275c65d8..06745ac92 100644 --- a/icinga/icingacomponent.h +++ b/icinga/icingacomponent.h @@ -23,6 +23,9 @@ namespace icinga { +/** + * A component that can be loaded into the Icinga application at run-time. + */ class I2_ICINGA_API IcingaComponent : public Component { protected: diff --git a/icinga/jsonrpcendpoint.cpp b/icinga/jsonrpcendpoint.cpp index 2c4421c16..9491c3497 100644 --- a/icinga/jsonrpcendpoint.cpp +++ b/icinga/jsonrpcendpoint.cpp @@ -61,7 +61,7 @@ bool JsonRpcEndpoint::IsConnected(void) const return (bool)m_Client; } -void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& message) +void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const RpcRequest& message) { if (IsConnected()) { string id; @@ -75,7 +75,7 @@ void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& } } -void JsonRpcEndpoint::ProcessResponse(Endpoint::Ptr sender, const JsonRpcResponse& message) +void JsonRpcEndpoint::ProcessResponse(Endpoint::Ptr sender, const RpcResponse& message) { if (IsConnected()) m_Client->SendMessage(message); @@ -88,18 +88,18 @@ int JsonRpcEndpoint::NewMessageHandler(const NewMessageEventArgs& nmea) string method; if (message.GetPropertyString("method", &method)) { - if (!IsMethodSource(method)) + if (!HasPublication(method)) return 0; - JsonRpcRequest request = message; + RpcRequest request = message; string id; if (request.GetID(&id)) - GetEndpointManager()->SendAnycastRequest(sender, request, false); + GetEndpointManager()->SendAnycastMessage(sender, request); else - GetEndpointManager()->SendMulticastRequest(sender, request, false); + GetEndpointManager()->SendMulticastMessage(sender, request); } else { - JsonRpcResponse response = message; + RpcResponse response = message; // TODO: deal with response messages throw NotImplementedException(); @@ -114,12 +114,13 @@ int JsonRpcEndpoint::ClientClosedHandler(const EventArgs&) m_PendingCalls.clear(); - // TODO: _only_ clear non-persistent method sources/sinks - // unregister ourselves if no persistent sources/sinks are left (use a timer for that, once we have a TTL property for the methods) - ClearMethodSinks(); - ClearMethodSources(); + // TODO: _only_ clear non-persistent publications/subscriptions + // unregister ourselves if no persistent publications/subscriptions are left (use a timer for that, once we have a TTL property for the topics) + ClearSubscriptions(); + ClearPublications(); - if (CountMethodSinks() == 0) + // remove the endpoint if there are no more subscriptions */ + if (BeginSubscriptions() == EndSubscriptions()) GetEndpointManager()->UnregisterEndpoint(static_pointer_cast(shared_from_this())); m_Client.reset(); diff --git a/icinga/jsonrpcendpoint.h b/icinga/jsonrpcendpoint.h index 849ac1de8..5bf74de71 100644 --- a/icinga/jsonrpcendpoint.h +++ b/icinga/jsonrpcendpoint.h @@ -23,6 +23,9 @@ namespace icinga { +/** + * A JSON-RPC endpoint that can be used to communicate with a remote + * Icinga instance. */ class I2_ICINGA_API JsonRpcEndpoint : public Endpoint { private: @@ -52,8 +55,8 @@ public: virtual bool IsLocal(void) const; virtual bool IsConnected(void) const; - virtual void ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& message); - virtual void ProcessResponse(Endpoint::Ptr sender, const JsonRpcResponse& message); + virtual void ProcessRequest(Endpoint::Ptr sender, const RpcRequest& message); + virtual void ProcessResponse(Endpoint::Ptr sender, const RpcResponse& message); virtual void Stop(void); }; diff --git a/icinga/virtualendpoint.cpp b/icinga/virtualendpoint.cpp index 4bca79d42..d36426b09 100644 --- a/icinga/virtualendpoint.cpp +++ b/icinga/virtualendpoint.cpp @@ -38,31 +38,31 @@ bool VirtualEndpoint::IsConnected(void) const return true; } -void VirtualEndpoint::RegisterMethodHandler(string method, function callback) +void VirtualEndpoint::RegisterTopicHandler(string topic, function callback) { - m_MethodHandlers[method] += callback; + m_TopicHandlers[topic] += callback; - RegisterMethodSink(method); + RegisterSubscription(topic); } -void VirtualEndpoint::UnregisterMethodHandler(string method, function callback) +void VirtualEndpoint::UnregisterTopicHandler(string topic, function callback) { // TODO: implement - //m_MethodHandlers[method] -= callback; - //UnregisterMethodSink(method); + //m_TopicHandlers[method] -= callback; + //UnregisterMethodSubscription(method); throw NotImplementedException(); } -void VirtualEndpoint::ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& request) +void VirtualEndpoint::ProcessRequest(Endpoint::Ptr sender, const RpcRequest& request) { string method; if (!request.GetMethod(&method)) return; - map >::iterator i = m_MethodHandlers.find(method); + map >::iterator i = m_TopicHandlers.find(method); - if (i == m_MethodHandlers.end()) + if (i == m_TopicHandlers.end()) return; NewRequestEventArgs nrea; @@ -72,7 +72,7 @@ void VirtualEndpoint::ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& i->second(nrea); } -void VirtualEndpoint::ProcessResponse(Endpoint::Ptr sender, const JsonRpcResponse& response) +void VirtualEndpoint::ProcessResponse(Endpoint::Ptr sender, const RpcResponse& response) { // TODO: figure out which request this response belongs to and notify the caller throw NotImplementedException(); diff --git a/icinga/virtualendpoint.h b/icinga/virtualendpoint.h index 173eef6be..c7774d556 100644 --- a/icinga/virtualendpoint.h +++ b/icinga/virtualendpoint.h @@ -29,28 +29,31 @@ struct I2_ICINGA_API NewRequestEventArgs : public EventArgs typedef weak_ptr WeakPtr; Endpoint::Ptr Sender; - JsonRpcRequest Request; + RpcRequest Request; }; +/** + * A local endpoint. + */ class I2_ICINGA_API VirtualEndpoint : public Endpoint { private: - map< string, Event > m_MethodHandlers; + map< string, Event > m_TopicHandlers; public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; - void RegisterMethodHandler(string method, function callback); - void UnregisterMethodHandler(string method, function callback); + void RegisterTopicHandler(string topic, function callback); + void UnregisterTopicHandler(string topic, function callback); virtual string GetAddress(void) const; virtual bool IsLocal(void) const; virtual bool IsConnected(void) const; - virtual void ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& message); - virtual void ProcessResponse(Endpoint::Ptr sender, const JsonRpcResponse& message); + virtual void ProcessRequest(Endpoint::Ptr sender, const RpcRequest& message); + virtual void ProcessResponse(Endpoint::Ptr sender, const RpcResponse& message); virtual void Stop(void); }; diff --git a/jsonrpc/Makefile.am b/jsonrpc/Makefile.am index 3f1210a5a..fb2f02ff4 100644 --- a/jsonrpc/Makefile.am +++ b/jsonrpc/Makefile.am @@ -8,16 +8,16 @@ libjsonrpc_la_SOURCES = \ i2-jsonrpc.h \ jsonrpcclient.cpp \ jsonrpcclient.h \ - jsonrpcrequest.cpp \ - jsonrpcrequest.h \ - jsonrpcresponse.cpp \ - jsonrpcresponse.h \ jsonrpcserver.cpp \ jsonrpcserver.h \ message.cpp \ message.h \ netstring.cpp \ - netstring.h + netstring.h \ + rpcrequest.cpp \ + rpcrequest.h \ + rpcresponse.cpp \ + rpcresponse.h libjsonrpc_la_CXXFLAGS = \ -DI2_JSONRPC_BUILD \ diff --git a/jsonrpc/i2-jsonrpc.h b/jsonrpc/i2-jsonrpc.h index 5ddc0e602..e5fffaf37 100644 --- a/jsonrpc/i2-jsonrpc.h +++ b/jsonrpc/i2-jsonrpc.h @@ -32,9 +32,9 @@ #include "variant.h" #include "dictionary.h" #include "message.h" +#include "rpcrequest.h" +#include "rpcresponse.h" #include "netstring.h" -#include "jsonrpcrequest.h" -#include "jsonrpcresponse.h" #include "jsonrpcclient.h" #include "jsonrpcserver.h" diff --git a/jsonrpc/jsonrpc.vcxproj b/jsonrpc/jsonrpc.vcxproj index 1eaa212b9..a5464f8e5 100644 --- a/jsonrpc/jsonrpc.vcxproj +++ b/jsonrpc/jsonrpc.vcxproj @@ -13,16 +13,16 @@ - - + + - - + + diff --git a/jsonrpc/jsonrpc.vcxproj.filters b/jsonrpc/jsonrpc.vcxproj.filters index 6e33d2942..3b8eec496 100644 --- a/jsonrpc/jsonrpc.vcxproj.filters +++ b/jsonrpc/jsonrpc.vcxproj.filters @@ -5,8 +5,8 @@ - - + + @@ -14,7 +14,7 @@ - - + + \ No newline at end of file diff --git a/jsonrpc/jsonrpcrequest.cpp b/jsonrpc/rpcrequest.cpp similarity index 100% rename from jsonrpc/jsonrpcrequest.cpp rename to jsonrpc/rpcrequest.cpp diff --git a/jsonrpc/jsonrpcrequest.h b/jsonrpc/rpcrequest.h similarity index 90% rename from jsonrpc/jsonrpcrequest.h rename to jsonrpc/rpcrequest.h index 90d1b9a15..a7d11a898 100644 --- a/jsonrpc/jsonrpcrequest.h +++ b/jsonrpc/rpcrequest.h @@ -17,21 +17,21 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#ifndef JSONRPCREQUEST_H -#define JSONRPCREQUEST_H +#ifndef RpcRequest_H +#define RpcRequest_H namespace icinga { -class I2_JSONRPC_API JsonRpcRequest : public Message +class I2_JSONRPC_API RpcRequest : public Message { public: - JsonRpcRequest(void) : Message() { + RpcRequest(void) : Message() { SetVersion("2.0"); } - JsonRpcRequest(const Message& message) : Message(message) { } + RpcRequest(const Message& message) : Message(message) { } inline bool GetVersion(string *value) const { @@ -76,4 +76,4 @@ public: } -#endif /* JSONRPCREQUEST_H */ +#endif /* RpcRequest_H */ diff --git a/jsonrpc/jsonrpcresponse.cpp b/jsonrpc/rpcresponse.cpp similarity index 100% rename from jsonrpc/jsonrpcresponse.cpp rename to jsonrpc/rpcresponse.cpp diff --git a/jsonrpc/jsonrpcresponse.h b/jsonrpc/rpcresponse.h similarity index 90% rename from jsonrpc/jsonrpcresponse.h rename to jsonrpc/rpcresponse.h index db2556858..199a67445 100644 --- a/jsonrpc/jsonrpcresponse.h +++ b/jsonrpc/rpcresponse.h @@ -17,20 +17,20 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#ifndef JSONRPCRESPONSE_H -#define JSONRPCRESPONSE_H +#ifndef RpcResponse_H +#define RpcResponse_H namespace icinga { -class I2_JSONRPC_API JsonRpcResponse : public Message +class I2_JSONRPC_API RpcResponse : public Message { public: - JsonRpcResponse(void) : Message() { + RpcResponse(void) : Message() { SetVersion("2.0"); } - JsonRpcResponse(const Message& message) : Message(message) { } + RpcResponse(const Message& message) : Message(message) { } inline bool GetVersion(string *value) const { @@ -75,4 +75,4 @@ public: } -#endif /* JSONRPCRESPONSE_H */ +#endif /* RpcResponse_H */