2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-04-30 15:30:45 +02:00
|
|
|
#include "i2-discovery.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* GetName
|
|
|
|
*
|
|
|
|
* Returns the name of this component.
|
|
|
|
*
|
|
|
|
* @returns The name.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
string DiscoveryComponent::GetName(void) const
|
|
|
|
{
|
|
|
|
return "discoverycomponent";
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* Start
|
|
|
|
*
|
|
|
|
* Starts the discovery component.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
void DiscoveryComponent::Start(void)
|
|
|
|
{
|
|
|
|
m_DiscoveryEndpoint = make_shared<VirtualEndpoint>();
|
|
|
|
|
|
|
|
long isBroker = 0;
|
|
|
|
GetConfig()->GetPropertyInteger("broker", &isBroker);
|
|
|
|
m_Broker = (isBroker != 0);
|
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
m_DiscoveryEndpoint->RegisterMethodSource("discovery::RegisterComponent");
|
|
|
|
m_DiscoveryEndpoint->RegisterMethodHandler("discovery::RegisterComponent",
|
|
|
|
bind_weak(&DiscoveryComponent::RegisterComponentMessageHandler, shared_from_this()));
|
|
|
|
|
|
|
|
if (IsBroker())
|
2012-04-30 15:30:45 +02:00
|
|
|
m_DiscoveryEndpoint->RegisterMethodSource("discovery::NewComponent");
|
|
|
|
|
|
|
|
m_DiscoveryEndpoint->RegisterMethodHandler("discovery::NewComponent",
|
|
|
|
bind_weak(&DiscoveryComponent::NewComponentMessageHandler, shared_from_this()));
|
2012-05-09 10:15:51 +02:00
|
|
|
|
2012-05-07 14:52:49 +02:00
|
|
|
m_DiscoveryEndpoint->RegisterMethodHandler("discovery::Welcome",
|
|
|
|
bind_weak(&DiscoveryComponent::WelcomeMessageHandler, shared_from_this()));
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-08 09:20:42 +02:00
|
|
|
GetEndpointManager()->ForEachEndpoint(bind(&DiscoveryComponent::NewEndpointHandler, this, _1));
|
2012-04-30 15:30:45 +02:00
|
|
|
GetEndpointManager()->OnNewEndpoint += bind_weak(&DiscoveryComponent::NewEndpointHandler, shared_from_this());
|
|
|
|
|
|
|
|
GetEndpointManager()->RegisterEndpoint(m_DiscoveryEndpoint);
|
2012-05-07 13:48:17 +02:00
|
|
|
|
2012-05-08 12:03:24 +02:00
|
|
|
/* create the reconnect timer */
|
2012-05-08 09:20:42 +02:00
|
|
|
m_DiscoveryTimer = make_shared<Timer>();
|
|
|
|
m_DiscoveryTimer->SetInterval(30);
|
|
|
|
m_DiscoveryTimer->OnTimerExpired += bind_weak(&DiscoveryComponent::DiscoveryTimerHandler, shared_from_this());
|
|
|
|
m_DiscoveryTimer->Start();
|
2012-05-09 10:15:51 +02:00
|
|
|
|
|
|
|
/* call the timer as soon as possible */
|
|
|
|
m_DiscoveryTimer->Reschedule(0);
|
2012-04-30 15:30:45 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* Stop
|
|
|
|
*
|
|
|
|
* Stops the discovery component.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
void DiscoveryComponent::Stop(void)
|
|
|
|
{
|
|
|
|
EndpointManager::Ptr mgr = GetEndpointManager();
|
|
|
|
|
|
|
|
if (mgr)
|
|
|
|
mgr->UnregisterEndpoint(m_DiscoveryEndpoint);
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
int DiscoveryComponent::CheckExistingEndpoint(Endpoint::Ptr endpoint, const NewEndpointEventArgs& neea)
|
|
|
|
{
|
|
|
|
if (endpoint == neea.Endpoint)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!neea.Endpoint->IsConnected())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (endpoint->GetIdentity() == neea.Endpoint->GetIdentity()) {
|
2012-05-08 15:14:20 +02:00
|
|
|
Application::Log("Detected duplicate identity:" + endpoint->GetIdentity() + " - Disconnecting old endpoint.");
|
2012-04-30 15:30:45 +02:00
|
|
|
|
|
|
|
neea.Endpoint->Stop();
|
|
|
|
GetEndpointManager()->UnregisterEndpoint(neea.Endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* NewEndpointHandler
|
|
|
|
*
|
|
|
|
* Registers handlers for new endpoints.
|
|
|
|
*
|
|
|
|
* @param neea Event arguments for the new endpoint.
|
|
|
|
* @returns 0
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
int DiscoveryComponent::NewEndpointHandler(const NewEndpointEventArgs& neea)
|
|
|
|
{
|
|
|
|
neea.Endpoint->OnIdentityChanged += bind_weak(&DiscoveryComponent::NewIdentityHandler, shared_from_this());
|
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
/* accept discovery::RegisterComponent messages from any endpoint */
|
|
|
|
neea.Endpoint->RegisterMethodSource("discovery::RegisterComponent");
|
2012-05-07 13:48:17 +02:00
|
|
|
|
2012-05-08 09:20:42 +02:00
|
|
|
/* accept discovery::Welcome messages from any endpoint */
|
2012-05-07 14:52:49 +02:00
|
|
|
neea.Endpoint->RegisterMethodSource("discovery::Welcome");
|
|
|
|
|
2012-04-30 15:30:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
int DiscoveryComponent::DiscoverySinkHandler(const NewMethodEventArgs& nmea, ComponentDiscoveryInfo::Ptr info) const
|
|
|
|
{
|
|
|
|
info->SubscribedMethods.insert(nmea.Method);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
int DiscoveryComponent::DiscoverySourceHandler(const NewMethodEventArgs& nmea, ComponentDiscoveryInfo::Ptr info) const
|
|
|
|
{
|
|
|
|
info->PublishedMethods.insert(nmea.Method);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
int DiscoveryComponent::DiscoveryEndpointHandler(const NewEndpointEventArgs& neea, ComponentDiscoveryInfo::Ptr info) const
|
|
|
|
{
|
2012-05-08 09:20:42 +02:00
|
|
|
neea.Endpoint->ForEachMethodSink(bind(&DiscoveryComponent::DiscoverySinkHandler, this, _1, info));
|
|
|
|
neea.Endpoint->ForEachMethodSource(bind(&DiscoveryComponent::DiscoverySourceHandler, this, _1, info));
|
2012-04-30 15:30:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
bool DiscoveryComponent::GetComponentDiscoveryInfo(string component, ComponentDiscoveryInfo::Ptr *info) const
|
|
|
|
{
|
|
|
|
if (component == GetEndpointManager()->GetIdentity()) {
|
|
|
|
/* Build fake discovery info for ourselves */
|
|
|
|
*info = make_shared<ComponentDiscoveryInfo>();
|
2012-05-08 09:20:42 +02:00
|
|
|
GetEndpointManager()->ForEachEndpoint(bind(&DiscoveryComponent::DiscoveryEndpointHandler, this, _1, *info));
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-08 09:20:42 +02:00
|
|
|
(*info)->LastSeen = 0;
|
2012-04-30 15:30:45 +02:00
|
|
|
(*info)->Node = GetIcingaApplication()->GetNode();
|
|
|
|
(*info)->Service = GetIcingaApplication()->GetService();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
map<string, ComponentDiscoveryInfo::Ptr>::const_iterator i;
|
|
|
|
|
|
|
|
i = m_Components.find(component);
|
|
|
|
|
|
|
|
if (i == m_Components.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*info = i->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* IsBroker
|
|
|
|
*
|
|
|
|
* Returns whether this component is a broker.
|
|
|
|
*
|
|
|
|
* @returns true if the component is a broker, false otherwise.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
bool DiscoveryComponent::IsBroker(void) const
|
|
|
|
{
|
|
|
|
return m_Broker;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* NewIdentityHandler
|
|
|
|
*
|
|
|
|
* Deals with a new endpoint whose identity has just become known.
|
|
|
|
*
|
|
|
|
* @param ea Event arguments for the component.
|
|
|
|
* @returns 0
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
int DiscoveryComponent::NewIdentityHandler(const EventArgs& ea)
|
|
|
|
{
|
|
|
|
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(ea.Source);
|
|
|
|
string identity = endpoint->GetIdentity();
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
if (identity == GetEndpointManager()->GetIdentity()) {
|
|
|
|
Application::Log("Detected loop-back connection - Disconnecting endpoint.");
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
endpoint->Stop();
|
|
|
|
GetEndpointManager()->UnregisterEndpoint(endpoint);
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
return 0;
|
2012-05-07 13:48:17 +02:00
|
|
|
}
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
GetEndpointManager()->ForEachEndpoint(bind(&DiscoveryComponent::CheckExistingEndpoint, this, endpoint, _1));
|
|
|
|
|
2012-04-30 15:30:45 +02:00
|
|
|
// we assume the other component _always_ wants
|
|
|
|
// discovery::RegisterComponent messages from us
|
|
|
|
endpoint->RegisterMethodSink("discovery::RegisterComponent");
|
|
|
|
|
|
|
|
// send a discovery::RegisterComponent message, if the
|
|
|
|
// other component is a broker this makes sure
|
|
|
|
// the broker knows about our message types
|
|
|
|
SendDiscoveryMessage("discovery::RegisterComponent", GetEndpointManager()->GetIdentity(), endpoint);
|
|
|
|
|
2012-05-09 12:34:11 +02:00
|
|
|
map<string, ComponentDiscoveryInfo::Ptr>::iterator ic;
|
2012-04-30 15:30:45 +02:00
|
|
|
|
|
|
|
if (IsBroker()) {
|
|
|
|
// we assume the other component _always_ wants
|
|
|
|
// discovery::NewComponent messages from us
|
|
|
|
endpoint->RegisterMethodSink("discovery::NewComponent");
|
|
|
|
|
|
|
|
// send discovery::NewComponent message for ourselves
|
|
|
|
SendDiscoveryMessage("discovery::NewComponent", GetEndpointManager()->GetIdentity(), endpoint);
|
|
|
|
|
|
|
|
// send discovery::NewComponent messages for all components
|
|
|
|
// we know about
|
2012-05-09 12:34:11 +02:00
|
|
|
for (ic = m_Components.begin(); ic != m_Components.end(); ic++) {
|
|
|
|
SendDiscoveryMessage("discovery::NewComponent", ic->first, endpoint);
|
2012-04-30 15:30:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we already know the other component
|
2012-05-09 12:34:11 +02:00
|
|
|
ic = m_Components.find(endpoint->GetIdentity());
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-09 12:34:11 +02:00
|
|
|
if (ic == m_Components.end()) {
|
2012-04-30 15:30:45 +02:00
|
|
|
// we don't know the other component yet, so
|
|
|
|
// wait until we get a discovery::NewComponent message
|
|
|
|
// from a broker
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-09 12:34:11 +02:00
|
|
|
// register published/subscribed methods for this endpoint
|
|
|
|
ComponentDiscoveryInfo::Ptr info = ic->second;
|
|
|
|
set<string>::iterator it;
|
|
|
|
for (it = info->PublishedMethods.begin(); it != info->PublishedMethods.end(); it++)
|
|
|
|
endpoint->RegisterMethodSource(*it);
|
|
|
|
|
|
|
|
for (it = info->SubscribedMethods.begin(); it != info->SubscribedMethods.end(); it++)
|
|
|
|
endpoint->RegisterMethodSink(*it);
|
|
|
|
|
2012-05-07 14:52:49 +02:00
|
|
|
FinishDiscoverySetup(endpoint);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* WelcomeMessageHandler
|
|
|
|
*
|
|
|
|
* Processes discovery::Welcome messages.
|
|
|
|
*
|
|
|
|
* @param nrea Event arguments for the request.
|
|
|
|
* @returns 0
|
|
|
|
*/
|
2012-05-07 14:52:49 +02:00
|
|
|
int DiscoveryComponent::WelcomeMessageHandler(const NewRequestEventArgs& nrea)
|
|
|
|
{
|
|
|
|
Endpoint::Ptr endpoint = nrea.Sender;
|
|
|
|
|
2012-05-08 10:13:15 +02:00
|
|
|
if (endpoint->GetReceivedWelcome())
|
2012-05-07 14:52:49 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-05-08 10:13:15 +02:00
|
|
|
endpoint->SetReceivedWelcome(true);
|
2012-05-07 14:52:49 +02:00
|
|
|
|
2012-05-08 10:13:15 +02:00
|
|
|
if (endpoint->GetSentWelcome()) {
|
2012-05-07 14:52:49 +02:00
|
|
|
EventArgs ea;
|
2012-05-08 13:40:22 +02:00
|
|
|
ea.Source = endpoint;
|
2012-05-07 14:52:49 +02:00
|
|
|
endpoint->OnSessionEstablished(ea);
|
|
|
|
}
|
|
|
|
|
2012-04-30 15:30:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-05-07 14:52:49 +02:00
|
|
|
void DiscoveryComponent::FinishDiscoverySetup(Endpoint::Ptr endpoint)
|
|
|
|
{
|
2012-05-08 10:13:15 +02:00
|
|
|
if (endpoint->GetSentWelcome())
|
2012-05-07 14:52:49 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// we assume the other component _always_ wants
|
|
|
|
// discovery::Welcome messages from us
|
|
|
|
endpoint->RegisterMethodSink("discovery::Welcome");
|
|
|
|
JsonRpcRequest request;
|
|
|
|
request.SetMethod("discovery::Welcome");
|
|
|
|
GetEndpointManager()->SendUnicastRequest(m_DiscoveryEndpoint, endpoint, request);
|
|
|
|
|
2012-05-08 10:13:15 +02:00
|
|
|
endpoint->SetSentWelcome(true);
|
|
|
|
|
|
|
|
if (endpoint->GetReceivedWelcome()) {
|
2012-05-07 14:52:49 +02:00
|
|
|
EventArgs ea;
|
2012-05-08 13:40:22 +02:00
|
|
|
ea.Source = endpoint;
|
2012-05-07 14:52:49 +02:00
|
|
|
endpoint->OnSessionEstablished(ea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
void DiscoveryComponent::SendDiscoveryMessage(string method, string identity, Endpoint::Ptr recipient)
|
|
|
|
{
|
|
|
|
JsonRpcRequest request;
|
|
|
|
request.SetMethod(method);
|
|
|
|
|
|
|
|
DiscoveryMessage params;
|
|
|
|
request.SetParams(params);
|
|
|
|
|
|
|
|
params.SetIdentity(identity);
|
|
|
|
|
|
|
|
Message subscriptions;
|
|
|
|
params.SetSubscribes(subscriptions);
|
|
|
|
|
|
|
|
Message publications;
|
|
|
|
params.SetProvides(publications);
|
|
|
|
|
|
|
|
ComponentDiscoveryInfo::Ptr info;
|
|
|
|
|
|
|
|
if (!GetComponentDiscoveryInfo(identity, &info))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!info->Node.empty() && !info->Service.empty()) {
|
2012-05-07 13:48:17 +02:00
|
|
|
params.SetNode(info->Node);
|
|
|
|
params.SetService(info->Service);
|
2012-04-30 15:30:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set<string>::iterator i;
|
|
|
|
for (i = info->PublishedMethods.begin(); i != info->PublishedMethods.end(); i++)
|
|
|
|
publications.AddUnnamedPropertyString(*i);
|
|
|
|
|
|
|
|
for (i = info->SubscribedMethods.begin(); i != info->SubscribedMethods.end(); i++)
|
|
|
|
subscriptions.AddUnnamedPropertyString(*i);
|
|
|
|
|
|
|
|
if (recipient)
|
|
|
|
GetEndpointManager()->SendUnicastRequest(m_DiscoveryEndpoint, recipient, request);
|
|
|
|
else
|
|
|
|
GetEndpointManager()->SendMulticastRequest(m_DiscoveryEndpoint, request);
|
|
|
|
}
|
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
bool DiscoveryComponent::HasMessagePermission(Dictionary::Ptr roles, string messageType, string message)
|
|
|
|
{
|
2012-05-09 12:21:56 +02:00
|
|
|
if (!roles)
|
|
|
|
return false;
|
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
ConfigHive::Ptr configHive = GetApplication()->GetConfigHive();
|
|
|
|
ConfigCollection::Ptr roleCollection = configHive->GetCollection("role");
|
|
|
|
|
|
|
|
for (DictionaryIterator ip = roles->Begin(); ip != roles->End(); ip++) {
|
|
|
|
ConfigObject::Ptr role = roleCollection->GetObject(ip->second);
|
|
|
|
|
|
|
|
if (!role)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Dictionary::Ptr permissions;
|
|
|
|
if (!role->GetPropertyDictionary(messageType, &permissions))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (DictionaryIterator is = permissions->Begin(); is != permissions->End(); is++) {
|
|
|
|
if (Utility::Match(is->second.GetString(), message))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2012-05-09 10:15:51 +02:00
|
|
|
* @param trusted Whether the message comes from a trusted source (i.e. a broker).
|
2012-05-08 11:58:46 +02:00
|
|
|
*/
|
2012-05-09 10:15:51 +02:00
|
|
|
void DiscoveryComponent::ProcessDiscoveryMessage(string identity, DiscoveryMessage message, bool trusted)
|
2012-04-30 15:30:45 +02:00
|
|
|
{
|
2012-05-08 09:20:42 +02:00
|
|
|
/* ignore discovery messages that are about ourselves */
|
|
|
|
if (identity == GetEndpointManager()->GetIdentity())
|
|
|
|
return;
|
|
|
|
|
2012-04-30 15:30:45 +02:00
|
|
|
ComponentDiscoveryInfo::Ptr info = make_shared<ComponentDiscoveryInfo>();
|
|
|
|
|
2012-05-08 09:20:42 +02:00
|
|
|
time(&(info->LastSeen));
|
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
message.GetNode(&info->Node);
|
|
|
|
message.GetService(&info->Service);
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
ConfigHive::Ptr configHive = GetApplication()->GetConfigHive();
|
|
|
|
ConfigCollection::Ptr endpointCollection = configHive->GetCollection("endpoint");
|
|
|
|
|
|
|
|
ConfigObject::Ptr endpointConfig = endpointCollection->GetObject(identity);
|
|
|
|
Dictionary::Ptr roles;
|
|
|
|
if (endpointConfig)
|
|
|
|
endpointConfig->GetPropertyDictionary("roles", &roles);
|
|
|
|
|
2012-05-09 12:34:11 +02:00
|
|
|
Endpoint::Ptr endpoint = GetEndpointManager()->GetEndpointByIdentity(identity);
|
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
Message provides;
|
|
|
|
if (message.GetProvides(&provides)) {
|
|
|
|
DictionaryIterator i;
|
|
|
|
for (i = provides.GetDictionary()->Begin(); i != provides.GetDictionary()->End(); i++) {
|
2012-05-09 12:34:11 +02:00
|
|
|
if (trusted || HasMessagePermission(roles, "publish", i->second)) {
|
2012-05-09 10:15:51 +02:00
|
|
|
info->PublishedMethods.insert(i->second);
|
2012-05-09 12:34:11 +02:00
|
|
|
if (endpoint)
|
|
|
|
endpoint->RegisterMethodSource(i->second);
|
|
|
|
}
|
2012-05-07 13:48:17 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
Message subscribes;
|
|
|
|
if (message.GetSubscribes(&subscribes)) {
|
|
|
|
DictionaryIterator i;
|
|
|
|
for (i = subscribes.GetDictionary()->Begin(); i != subscribes.GetDictionary()->End(); i++) {
|
2012-05-09 12:34:11 +02:00
|
|
|
if (trusted || HasMessagePermission(roles, "subscribe", i->second)) {
|
2012-05-09 10:15:51 +02:00
|
|
|
info->SubscribedMethods.insert(i->second);
|
2012-05-09 12:34:11 +02:00
|
|
|
if (endpoint)
|
|
|
|
endpoint->RegisterMethodSink(i->second);
|
|
|
|
}
|
2012-05-07 13:48:17 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
map<string, ComponentDiscoveryInfo::Ptr>::iterator i;
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
i = m_Components.find(identity);
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
if (i != m_Components.end())
|
|
|
|
m_Components.erase(i);
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
m_Components[identity] = info;
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-07 14:52:49 +02:00
|
|
|
if (IsBroker())
|
|
|
|
SendDiscoveryMessage("discovery::NewComponent", identity, Endpoint::Ptr());
|
|
|
|
|
2012-05-09 15:09:14 +02:00
|
|
|
/* don't send a welcome message for discovery::RegisterComponent
|
|
|
|
messages unless we're a broker */
|
|
|
|
if (endpoint && (trusted || IsBroker()))
|
2012-05-07 14:52:49 +02:00
|
|
|
FinishDiscoverySetup(endpoint);
|
2012-04-30 15:30:45 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* NewComponentMessageHandler
|
|
|
|
*
|
|
|
|
* Processes "discovery::NewComponent" messages.
|
|
|
|
*
|
|
|
|
* @param nrea Event arguments for the request.
|
|
|
|
* @returns 0
|
|
|
|
*/
|
2012-05-07 13:48:17 +02:00
|
|
|
int DiscoveryComponent::NewComponentMessageHandler(const NewRequestEventArgs& nrea)
|
2012-04-30 15:30:45 +02:00
|
|
|
{
|
2012-05-07 13:48:17 +02:00
|
|
|
DiscoveryMessage message;
|
|
|
|
nrea.Request.GetParams(&message);
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
string identity;
|
|
|
|
if (!message.GetIdentity(&identity))
|
|
|
|
return 0;
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
ProcessDiscoveryMessage(identity, message, true);
|
2012-05-07 13:48:17 +02:00
|
|
|
return 0;
|
2012-04-30 15:30:45 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* RegisterComponentMessageHandler
|
|
|
|
*
|
|
|
|
* Processes "discovery::RegisterComponent" messages.
|
|
|
|
*
|
|
|
|
* @param nrea Event arguments for the request.
|
|
|
|
* @returns 0
|
|
|
|
*/
|
2012-05-07 13:48:17 +02:00
|
|
|
int DiscoveryComponent::RegisterComponentMessageHandler(const NewRequestEventArgs& nrea)
|
2012-04-30 15:30:45 +02:00
|
|
|
{
|
2012-05-07 13:48:17 +02:00
|
|
|
DiscoveryMessage message;
|
|
|
|
nrea.Request.GetParams(&message);
|
2012-05-09 10:15:51 +02:00
|
|
|
ProcessDiscoveryMessage(nrea.Sender->GetIdentity(), message, false);
|
2012-05-08 09:20:42 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
2012-05-09 10:15:51 +02:00
|
|
|
* EndpointConfigHandler
|
2012-05-08 11:58:46 +02:00
|
|
|
*
|
2012-05-09 10:15:51 +02:00
|
|
|
* Processes "endpoint" config objects.
|
2012-05-08 11:58:46 +02:00
|
|
|
*
|
|
|
|
* @param ea Event arguments for the new config object.
|
|
|
|
* @returns 0
|
|
|
|
*/
|
2012-05-09 10:15:51 +02:00
|
|
|
int DiscoveryComponent::EndpointConfigHandler(const EventArgs& ea)
|
2012-05-07 13:48:17 +02:00
|
|
|
{
|
2012-05-08 09:20:42 +02:00
|
|
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
|
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
EndpointManager::Ptr endpointManager = GetEndpointManager();
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
/* Check if we're already connected to this endpoint. */
|
2012-05-08 09:20:42 +02:00
|
|
|
if (endpointManager->GetEndpointByIdentity(object->GetName()))
|
|
|
|
return 0;
|
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
string node, service;
|
|
|
|
if (object->GetPropertyString("node", &node) && object->GetPropertyString("service", &service)) {
|
|
|
|
/* reconnect to this endpoint */
|
|
|
|
endpointManager->AddConnection(node, service);
|
|
|
|
}
|
2012-05-08 09:20:42 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:46 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-05-08 09:20:42 +02:00
|
|
|
int DiscoveryComponent::DiscoveryTimerHandler(const TimerEventArgs& tea)
|
|
|
|
{
|
|
|
|
EndpointManager::Ptr endpointManager = GetEndpointManager();
|
|
|
|
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
/* check whether we have to reconnect to one of our upstream endpoints */
|
|
|
|
ConfigCollection::Ptr endpointCollection = GetApplication()->GetConfigHive()->GetCollection("endpoint");
|
|
|
|
endpointCollection->ForEachObject(bind(&DiscoveryComponent::EndpointConfigHandler, this, _1));
|
2012-05-08 09:20:42 +02:00
|
|
|
|
2012-05-09 13:49:26 +02:00
|
|
|
map<string, ComponentDiscoveryInfo::Ptr>::iterator curr, i;
|
2012-05-08 09:20:42 +02:00
|
|
|
for (i = m_Components.begin(); i != m_Components.end(); ) {
|
|
|
|
string identity = i->first;
|
|
|
|
ComponentDiscoveryInfo::Ptr info = i->second;
|
|
|
|
|
2012-05-09 13:49:26 +02:00
|
|
|
curr = i;
|
|
|
|
i++;
|
|
|
|
|
2012-05-08 09:20:42 +02:00
|
|
|
if (info->LastSeen < now - DiscoveryComponent::RegistrationTTL) {
|
|
|
|
/* unregister this component if its registration has expired */
|
2012-05-09 13:49:26 +02:00
|
|
|
m_Components.erase(curr);
|
2012-05-07 13:48:17 +02:00
|
|
|
continue;
|
2012-05-08 09:20:42 +02:00
|
|
|
}
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-08 09:20:42 +02:00
|
|
|
if (IsBroker()) {
|
|
|
|
/* send discovery message to all connected components to
|
|
|
|
refresh their TTL for this component */
|
2012-05-09 14:10:14 +02:00
|
|
|
SendDiscoveryMessage("discovery::NewComponent", identity, Endpoint::Ptr());
|
2012-05-08 09:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Endpoint::Ptr endpoint = endpointManager->GetEndpointByIdentity(identity);
|
2012-05-08 10:22:47 +02:00
|
|
|
if (endpoint && endpoint->IsConnected()) {
|
2012-05-08 09:20:42 +02:00
|
|
|
/* update LastSeen if we're still connected to this endpoint */
|
|
|
|
info->LastSeen = now;
|
|
|
|
} else {
|
2012-05-08 10:13:15 +02:00
|
|
|
/* TODO: figure out whether we actually want to connect to this component (_always_ if IsBroker() == true) */
|
2012-05-08 09:20:42 +02:00
|
|
|
/* try and reconnect to this component */
|
|
|
|
endpointManager->AddConnection(info->Node, info->Service);
|
|
|
|
}
|
2012-05-07 13:48:17 +02:00
|
|
|
}
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
return 0;
|
2012-04-30 15:30:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_COMPONENT(DiscoveryComponent);
|