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 *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "remoting/endpoint.h"
|
|
|
|
#include "remoting/endpointmanager.h"
|
2013-04-04 16:08:02 +02:00
|
|
|
#include "remoting/jsonrpc.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/application.h"
|
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "base/utility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/logger_fwd.h"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "config/configitembuilder.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(Endpoint);
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
boost::signals2::signal<void (const Endpoint::Ptr&)> Endpoint::OnConnected;
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2012-09-17 14:47:43 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the Endpoint class.
|
|
|
|
*
|
|
|
|
* @param properties A serialized dictionary containing attributes.
|
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
Endpoint::Endpoint(const Dictionary::Ptr& serializedUpdate)
|
|
|
|
: DynamicObject(serializedUpdate)
|
2013-02-26 10:13:54 +01:00
|
|
|
{
|
|
|
|
RegisterAttribute("local", Attribute_Config, &m_Local);
|
|
|
|
|
|
|
|
RegisterAttribute("node", Attribute_Replicated, &m_Node);
|
|
|
|
RegisterAttribute("service", Attribute_Replicated, &m_Service);
|
|
|
|
RegisterAttribute("subscriptions", Attribute_Replicated, &m_Subscriptions);
|
|
|
|
}
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
Endpoint::~Endpoint(void)
|
|
|
|
{ }
|
|
|
|
|
2012-09-17 14:47:43 +02:00
|
|
|
/**
|
|
|
|
* Retrieves an endpoint by name.
|
|
|
|
*
|
|
|
|
* @param name The name of the endpoint.
|
|
|
|
* @returns The endpoint.
|
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
Endpoint::Ptr Endpoint::GetByName(const String& name)
|
|
|
|
{
|
|
|
|
DynamicObject::Ptr configObject = DynamicObject::GetObject("Endpoint", name);
|
|
|
|
|
|
|
|
return dynamic_pointer_cast<Endpoint>(configObject);
|
|
|
|
}
|
|
|
|
|
2012-09-17 14:47:43 +02:00
|
|
|
/**
|
|
|
|
* Helper function for creating new endpoint objects.
|
|
|
|
*
|
|
|
|
* @param name The name of the new endpoint.
|
2013-01-18 09:36:28 +01:00
|
|
|
* @param replicated Whether replication is enabled for the endpoint object.
|
2012-09-17 14:47:43 +02:00
|
|
|
* @param local Whether the new endpoint should be local.
|
|
|
|
* @returns The new endpoint.
|
|
|
|
*/
|
2013-01-18 09:36:28 +01:00
|
|
|
Endpoint::Ptr Endpoint::MakeEndpoint(const String& name, bool replicated, bool local)
|
2012-09-03 10:28:14 +02:00
|
|
|
{
|
|
|
|
ConfigItemBuilder::Ptr endpointConfig = boost::make_shared<ConfigItemBuilder>();
|
|
|
|
endpointConfig->SetType("Endpoint");
|
2013-01-18 10:37:22 +01:00
|
|
|
endpointConfig->SetName((!replicated && local) ? "local:" + name : name);
|
2013-01-18 09:36:28 +01:00
|
|
|
endpointConfig->SetLocal(!replicated);
|
2012-09-03 10:28:14 +02:00
|
|
|
endpointConfig->AddExpression("local", OperatorSet, local);
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
ConfigItem::Ptr item = endpointConfig->Compile();
|
|
|
|
DynamicObject::Ptr object = item->Commit();
|
2012-09-03 10:28:14 +02:00
|
|
|
return dynamic_pointer_cast<Endpoint>(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether this is a local endpoint.
|
|
|
|
*
|
|
|
|
* @returns true if this is a local endpoint, false otherwise.
|
|
|
|
*/
|
|
|
|
bool Endpoint::IsLocalEndpoint(void) const
|
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Local;
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
2012-09-03 10:28:14 +02:00
|
|
|
* Checks whether this endpoint is connected.
|
2012-05-15 16:24:04 +02:00
|
|
|
*
|
2012-09-03 10:28:14 +02:00
|
|
|
* @returns true if the endpoint is connected, false otherwise.
|
2012-05-15 16:24:04 +02:00
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
bool Endpoint::IsConnected(void) const
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-09-03 10:28:14 +02:00
|
|
|
if (IsLocalEndpoint()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2013-04-04 16:08:02 +02:00
|
|
|
return GetClient();
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
Stream::Ptr Endpoint::GetClient(void) const
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Client;
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
void Endpoint::SetClient(const Stream::Ptr& client)
|
2012-09-03 10:28:14 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Client = client;
|
|
|
|
}
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
boost::thread thread(boost::bind(&Endpoint::MessageThreadProc, this, client));
|
|
|
|
thread.detach();
|
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
OnConnected(GetSelf());
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Registers a topic subscription for this endpoint.
|
|
|
|
*
|
|
|
|
* @param topic The name of the topic.
|
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
void Endpoint::RegisterSubscription(const String& topic)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
2012-09-03 10:28:14 +02:00
|
|
|
Dictionary::Ptr subscriptions = GetSubscriptions();
|
|
|
|
|
|
|
|
if (!subscriptions)
|
|
|
|
subscriptions = boost::make_shared<Dictionary>();
|
|
|
|
|
|
|
|
if (!subscriptions->Contains(topic)) {
|
|
|
|
Dictionary::Ptr newSubscriptions = subscriptions->ShallowClone();
|
|
|
|
newSubscriptions->Set(topic, topic);
|
2013-03-04 15:52:42 +01:00
|
|
|
|
|
|
|
ObjectLock olock(this);
|
2012-09-03 10:28:14 +02:00
|
|
|
SetSubscriptions(newSubscriptions);
|
|
|
|
}
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Removes a topic subscription from this endpoint.
|
|
|
|
*
|
|
|
|
* @param topic The name of the topic.
|
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
void Endpoint::UnregisterSubscription(const String& topic)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
2012-09-03 10:28:14 +02:00
|
|
|
Dictionary::Ptr subscriptions = GetSubscriptions();
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
if (!subscriptions)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (subscriptions->Contains(topic)) {
|
2012-09-03 10:28:14 +02:00
|
|
|
Dictionary::Ptr newSubscriptions = subscriptions->ShallowClone();
|
|
|
|
newSubscriptions->Remove(topic);
|
|
|
|
SetSubscriptions(newSubscriptions);
|
|
|
|
}
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
bool Endpoint::HasSubscription(const String& topic) const
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
2012-09-03 10:28:14 +02:00
|
|
|
Dictionary::Ptr subscriptions = GetSubscriptions();
|
|
|
|
|
|
|
|
return (subscriptions && subscriptions->Contains(topic));
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Removes all subscriptions for the endpoint.
|
|
|
|
*/
|
|
|
|
void Endpoint::ClearSubscriptions(void)
|
2012-04-19 12:16:52 +02:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
m_Subscriptions = Empty;
|
|
|
|
Touch("subscriptions");
|
2012-04-19 12:16:52 +02:00
|
|
|
}
|
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
Dictionary::Ptr Endpoint::GetSubscriptions(void) const
|
2012-04-30 15:30:45 +02:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Subscriptions;
|
2012-04-30 15:30:45 +02:00
|
|
|
}
|
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
void Endpoint::SetSubscriptions(const Dictionary::Ptr& subscriptions)
|
2012-04-30 15:30:45 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
subscriptions->Seal();
|
2013-02-26 10:13:54 +01:00
|
|
|
m_Subscriptions = subscriptions;
|
|
|
|
Touch("subscriptions");
|
2012-04-30 15:30:45 +02:00
|
|
|
}
|
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
void Endpoint::RegisterTopicHandler(const String& topic, const boost::function<Endpoint::Callback>& callback)
|
2012-05-07 14:52:49 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::map<String, shared_ptr<boost::signals2::signal<Endpoint::Callback> > >::iterator it;
|
2012-09-03 10:28:14 +02:00
|
|
|
it = m_TopicHandlers.find(topic);
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
shared_ptr<boost::signals2::signal<Endpoint::Callback> > sig;
|
2012-09-03 10:28:14 +02:00
|
|
|
|
|
|
|
if (it == m_TopicHandlers.end()) {
|
2013-03-16 21:18:53 +01:00
|
|
|
sig = boost::make_shared<boost::signals2::signal<Endpoint::Callback> >();
|
2012-09-03 10:28:14 +02:00
|
|
|
m_TopicHandlers.insert(make_pair(topic, sig));
|
|
|
|
} else {
|
|
|
|
sig = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
sig->connect(callback);
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
olock.Unlock();
|
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
RegisterSubscription(topic);
|
2012-05-07 14:52:49 +02:00
|
|
|
}
|
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
void Endpoint::ProcessRequest(const Endpoint::Ptr& sender, const RequestMessage& request)
|
2012-05-08 10:13:15 +02:00
|
|
|
{
|
2012-09-03 10:28:14 +02:00
|
|
|
if (!IsConnected()) {
|
|
|
|
// TODO: persist the message
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsLocalEndpoint()) {
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
String method;
|
|
|
|
if (!request.GetMethod(&method))
|
|
|
|
return;
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::map<String, shared_ptr<boost::signals2::signal<Endpoint::Callback> > >::iterator it;
|
2012-09-03 10:28:14 +02:00
|
|
|
it = m_TopicHandlers.find(method);
|
|
|
|
|
|
|
|
if (it == m_TopicHandlers.end())
|
|
|
|
return;
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
Utility::QueueAsyncCallback(boost::bind(boost::ref(*it->second), GetSelf(), sender, request));
|
2012-09-03 10:28:14 +02:00
|
|
|
} else {
|
2013-04-05 12:09:26 +02:00
|
|
|
try {
|
|
|
|
JsonRpc::SendMessage(GetClient(), request);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Error while sending JSON-RPC message for endpoint '" << GetName() << "': " << boost::diagnostic_information(ex);
|
|
|
|
Log(LogWarning, "remoting", msgbuf.str());
|
|
|
|
|
|
|
|
m_Client.reset();
|
|
|
|
}
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
2012-05-09 13:49:26 +02:00
|
|
|
}
|
2012-09-03 10:28:14 +02:00
|
|
|
|
|
|
|
void Endpoint::ProcessResponse(const Endpoint::Ptr& sender, const ResponseMessage& response)
|
|
|
|
{
|
|
|
|
if (!IsConnected())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (IsLocalEndpoint())
|
|
|
|
EndpointManager::GetInstance()->ProcessResponseMessage(sender, response);
|
2013-04-04 16:08:02 +02:00
|
|
|
else {
|
2013-04-05 12:09:26 +02:00
|
|
|
try {
|
|
|
|
JsonRpc::SendMessage(GetClient(), response);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Error while sending JSON-RPC message for endpoint '" << GetName() << "': " << boost::diagnostic_information(ex);
|
|
|
|
Log(LogWarning, "remoting", msgbuf.str());
|
|
|
|
|
|
|
|
m_Client.reset();
|
|
|
|
}
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
void Endpoint::MessageThreadProc(const Stream::Ptr& stream)
|
2012-09-03 10:28:14 +02:00
|
|
|
{
|
2013-04-05 12:09:26 +02:00
|
|
|
for (;;) {
|
|
|
|
MessagePart message;
|
2013-04-04 16:08:02 +02:00
|
|
|
|
2013-04-05 12:09:26 +02:00
|
|
|
try {
|
|
|
|
message = JsonRpc::ReadMessage(stream);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogWarning, "jsonrpc", "Error while reading JSON-RPC message for endpoint '" + GetName() + "': " + boost::diagnostic_information(ex));
|
2013-04-04 16:08:02 +02:00
|
|
|
|
|
|
|
m_Client.reset();
|
|
|
|
}
|
|
|
|
|
2013-04-05 12:09:26 +02:00
|
|
|
Endpoint::Ptr sender = GetSelf();
|
|
|
|
|
|
|
|
if (ResponseMessage::IsResponseMessage(message)) {
|
|
|
|
/* rather than routing the message to the right virtual
|
|
|
|
* endpoint we just process it here right away. */
|
|
|
|
EndpointManager::GetInstance()->ProcessResponseMessage(sender, message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestMessage request = message;
|
|
|
|
|
|
|
|
String method;
|
|
|
|
if (!request.GetMethod(&method))
|
|
|
|
return;
|
|
|
|
|
|
|
|
String id;
|
|
|
|
if (request.GetID(&id))
|
|
|
|
EndpointManager::GetInstance()->SendAnycastMessage(sender, request);
|
|
|
|
else
|
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(sender, request);
|
2013-03-02 09:07:47 +01:00
|
|
|
}
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:47:43 +02:00
|
|
|
/**
|
|
|
|
* Gets the node address for this endpoint.
|
|
|
|
*
|
|
|
|
* @returns The node address (hostname).
|
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
String Endpoint::GetNode(void) const
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Node;
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:47:43 +02:00
|
|
|
/**
|
|
|
|
* Gets the service name for this endpoint.
|
|
|
|
*
|
|
|
|
* @returns The service name (port).
|
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
String Endpoint::GetService(void) const
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Service;
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|