using namespace icinga;
+/**
+ * GetName
+ *
+ * Returns the name of this component.
+ *
+ * @returns The name.
+ */
string DiscoveryComponent::GetName(void) const
{
return "discoverycomponent";
}
+/**
+ * Start
+ *
+ * Starts the discovery component.
+ */
void DiscoveryComponent::Start(void)
{
m_DiscoveryEndpoint = make_shared<VirtualEndpoint>();
m_DiscoveryTimer->Start();
}
+/**
+ * Stop
+ *
+ * Stops the discovery component.
+ */
void DiscoveryComponent::Stop(void)
{
EndpointManager::Ptr mgr = GetEndpointManager();
mgr->UnregisterEndpoint(m_DiscoveryEndpoint);
}
+/**
+ * CheckExistingEndpoint
+ *
+ * Checks whether the specified endpoint is already connected
+ * and disconnects older endpoints.
+ *
+ * @param endpoint The endpoint that is to be checked.
+ * @param neea Event arguments for another endpoint.
+ * @returns 0
+ */
int DiscoveryComponent::CheckExistingEndpoint(Endpoint::Ptr endpoint, const NewEndpointEventArgs& neea)
{
if (endpoint == neea.Endpoint)
return 0;
}
+/**
+ * NewEndpointHandler
+ *
+ * Registers handlers for new endpoints.
+ *
+ * @param neea Event arguments for the new endpoint.
+ * @returns 0
+ */
int DiscoveryComponent::NewEndpointHandler(const NewEndpointEventArgs& neea)
{
neea.Endpoint->OnIdentityChanged += bind_weak(&DiscoveryComponent::NewIdentityHandler, shared_from_this());
return 0;
}
+/**
+ * DiscoverySinkHandler
+ *
+ * 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;
}
+/**
+ * DiscoverySourceHandler
+ *
+ * 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;
}
+/**
+ * DiscoveryEndpointHandler
+ *
+ * Registers message sinks/sources in the specified component information object.
+ *
+ * @param neea Event arguments for the endpoint.
+ * @param info Component information object.
+ * @return 0
+ */
int DiscoveryComponent::DiscoveryEndpointHandler(const NewEndpointEventArgs& neea, ComponentDiscoveryInfo::Ptr info) const
{
neea.Endpoint->ForEachMethodSink(bind(&DiscoveryComponent::DiscoverySinkHandler, this, _1, info));
return 0;
}
+/**
+ * GetComponentDiscoveryInfo
+ *
+ * Retrieves the component information object for the specified component.
+ *
+ * @param component The identity of the component.
+ * @param info Pointer to the information object.
+ * @returns true if the info object was successfully retrieved, false otherwise.
+ */
bool DiscoveryComponent::GetComponentDiscoveryInfo(string component, ComponentDiscoveryInfo::Ptr *info) const
{
if (component == GetEndpointManager()->GetIdentity()) {
return true;
}
+/**
+ * IsBroker
+ *
+ * Returns whether this component is a broker.
+ *
+ * @returns true if the component is a broker, false otherwise.
+ */
bool DiscoveryComponent::IsBroker(void) const
{
return m_Broker;
}
+/**
+ * NewIdentityHandler
+ *
+ * Deals with a new endpoint whose identity has just become known.
+ *
+ * @param ea Event arguments for the component.
+ * @returns 0
+ */
int DiscoveryComponent::NewIdentityHandler(const EventArgs& ea)
{
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(ea.Source);
string identity = endpoint->GetIdentity();
- if (!GetIcingaApplication()->IsDebugging()) {
- if (identity == GetEndpointManager()->GetIdentity()) {
- Application::Log("Detected loop-back connection - Disconnecting endpoint.");
+ if (identity == GetEndpointManager()->GetIdentity()) {
+ Application::Log("Detected loop-back connection - Disconnecting endpoint.");
- endpoint->Stop();
- GetEndpointManager()->UnregisterEndpoint(endpoint);
+ endpoint->Stop();
+ GetEndpointManager()->UnregisterEndpoint(endpoint);
- return 0;
- }
-
- GetEndpointManager()->ForEachEndpoint(bind(&DiscoveryComponent::CheckExistingEndpoint, this, endpoint, _1));
+ return 0;
}
+ GetEndpointManager()->ForEachEndpoint(bind(&DiscoveryComponent::CheckExistingEndpoint, this, endpoint, _1));
+
ConfigCollection::Ptr brokerCollection = GetApplication()->GetConfigHive()->GetCollection("broker");
if (brokerCollection->GetObject(identity)) {
/* accept discovery::NewComponent messages from brokers */
endpoint->RegisterMethodSource("discovery::NewComponent");
}
-
// we assume the other component _always_ wants
// discovery::RegisterComponent messages from us
endpoint->RegisterMethodSink("discovery::RegisterComponent");
return 0;
}
+/**
+ * WelcomeMessageHandler
+ *
+ * Processes discovery::Welcome messages.
+ *
+ * @param nrea Event arguments for the request.
+ * @returns 0
+ */
int DiscoveryComponent::WelcomeMessageHandler(const NewRequestEventArgs& nrea)
{
Endpoint::Ptr endpoint = nrea.Sender;
return 0;
}
+/**
+ * FinishDiscoverySetup
+ *
+ * Finishes the welcome handshake for a new component
+ * by registering message sinks/sources for the component
+ * and sending a welcome message if necessary.
+ *
+ * @param endpoint The endpoint to set up.
+ */
void DiscoveryComponent::FinishDiscoverySetup(Endpoint::Ptr endpoint)
{
if (endpoint->GetSentWelcome())
}
}
+/**
+ * SendDiscoveryMessage
+ *
+ * Sends a discovery message for the specified identity using the
+ * specified message type.
+ *
+ * @param method The method to use for the message ("discovery::NewComponent" or "discovery::RegisterComponent").
+ * @param identity The identity of the component for which a message should be sent.
+ * @param recipient The recipient of the message. A multicast message is sent if this parameter is empty.
+ */
void DiscoveryComponent::SendDiscoveryMessage(string method, string identity, Endpoint::Ptr recipient)
{
JsonRpcRequest request;
GetEndpointManager()->SendMulticastRequest(m_DiscoveryEndpoint, request);
}
+/**
+ * ProcessDiscoveryMessage
+ *
+ * Processes a discovery message by registering the component in the
+ * discovery component registry.
+ *
+ * @param identity The authorative identity of the component.
+ * @param message The discovery message.
+ */
void DiscoveryComponent::ProcessDiscoveryMessage(string identity, DiscoveryMessage message)
{
/* ignore discovery messages that are about ourselves */
FinishDiscoverySetup(endpoint);
}
+/**
+ * NewComponentMessageHandler
+ *
+ * Processes "discovery::NewComponent" messages.
+ *
+ * @param nrea Event arguments for the request.
+ * @returns 0
+ */
int DiscoveryComponent::NewComponentMessageHandler(const NewRequestEventArgs& nrea)
{
DiscoveryMessage message;
return 0;
}
+/**
+ * RegisterComponentMessageHandler
+ *
+ * Processes "discovery::RegisterComponent" messages.
+ *
+ * @param nrea Event arguments for the request.
+ * @returns 0
+ */
int DiscoveryComponent::RegisterComponentMessageHandler(const NewRequestEventArgs& nrea)
{
/* ignore discovery::RegisterComponent messages when we're not a broker */
return 0;
}
+/**
+ * BrokerConfigHandler
+ *
+ * Processes "broker" config objects.
+ *
+ * @param ea Event arguments for the new config object.
+ * @returns 0
+ */
int DiscoveryComponent::BrokerConfigHandler(const EventArgs& ea)
{
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
return 0;
}
+/**
+ * DiscoveryTimerHandler
+ *
+ * Checks whether we have to reconnect to other components and removes stale
+ * components from the registry.
+ *
+ * @param tea Event arguments for the timer.
+ * @returns 0
+ */
int DiscoveryComponent::DiscoveryTimerHandler(const TimerEventArgs& tea)
{
EndpointManager::Ptr endpointManager = GetEndpointManager();
time_t now;
time(&now);
+ /* check whether we have to reconnect to one of our upstream brokers */
ConfigCollection::Ptr brokerCollection = GetApplication()->GetConfigHive()->GetCollection("broker");
brokerCollection->ForEachObject(bind(&DiscoveryComponent::BrokerConfigHandler, this, _1));