2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
2012-05-10 12:06:41 +02:00
|
|
|
* *
|
|
|
|
* 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-08-27 12:21:41 +02:00
|
|
|
#include "cluster/endpoint.h"
|
|
|
|
#include "cluster/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;
|
2013-08-26 16:53:17 +02:00
|
|
|
boost::signals2::signal<void (const Endpoint::Ptr&, const Dictionary::Ptr&)> Endpoint::OnMessageReceived;
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2013-09-16 09:30:31 +02:00
|
|
|
Endpoint::Endpoint(void)
|
|
|
|
: m_Syncing(false)
|
|
|
|
{ }
|
|
|
|
|
2013-10-17 10:56:42 +02:00
|
|
|
Endpoint::~Endpoint(void)
|
|
|
|
{
|
|
|
|
SetClient(Stream::Ptr());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-08-26 16:53:17 +02:00
|
|
|
return GetClient();
|
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-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-10-17 10:56:42 +02:00
|
|
|
if (m_Client)
|
|
|
|
m_Client->Close();
|
|
|
|
|
|
|
|
m_Thread.join();
|
|
|
|
|
2013-09-16 09:30:31 +02:00
|
|
|
m_Client = client;
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-09-11 09:44:23 +02:00
|
|
|
if (client) {
|
2013-10-17 10:56:42 +02:00
|
|
|
m_Thread = boost::thread(boost::bind(&Endpoint::MessageThreadProc, this, client));
|
2013-04-04 16:08:02 +02:00
|
|
|
|
2013-09-11 09:44:23 +02:00
|
|
|
OnConnected(GetSelf());
|
|
|
|
}
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 16:53:17 +02:00
|
|
|
void Endpoint::SendMessage(const Dictionary::Ptr& message)
|
2012-05-08 10:13:15 +02:00
|
|
|
{
|
2013-09-03 10:08:02 +02:00
|
|
|
Stream::Ptr client = GetClient();
|
2013-09-02 15:12:20 +02:00
|
|
|
|
2013-09-03 10:08:02 +02:00
|
|
|
if (!client)
|
|
|
|
return;
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2013-08-26 16:53:17 +02:00
|
|
|
try {
|
2013-09-03 10:08:02 +02:00
|
|
|
JsonRpc::SendMessage(client, message);
|
2013-08-26 16:53:17 +02:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Error while sending JSON-RPC message for endpoint '" << GetName() << "': " << boost::diagnostic_information(ex);
|
2013-08-27 12:21:41 +02:00
|
|
|
Log(LogWarning, "cluster", msgbuf.str());
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2013-08-26 16:53:17 +02:00
|
|
|
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 (;;) {
|
2013-08-26 16:53:17 +02:00
|
|
|
Dictionary::Ptr 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) {
|
2013-08-27 12:21:41 +02:00
|
|
|
Log(LogWarning, "cluster", "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-08-27 12:21:41 +02:00
|
|
|
|
|
|
|
return;
|
2013-04-04 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 10:42:19 +02:00
|
|
|
OnMessageReceived(GetSelf(), message);
|
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).
|
|
|
|
*/
|
2013-08-26 16:53:17 +02:00
|
|
|
String Endpoint::GetHost(void) const
|
2012-09-03 10:28:14 +02:00
|
|
|
{
|
2013-08-26 16:53:17 +02:00
|
|
|
return m_Host;
|
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).
|
|
|
|
*/
|
2013-08-26 16:53:17 +02:00
|
|
|
String Endpoint::GetPort(void) const
|
2012-09-03 10:28:14 +02:00
|
|
|
{
|
2013-08-26 16:53:17 +02:00
|
|
|
return m_Port;
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-09-04 15:47:15 +02:00
|
|
|
Array::Ptr Endpoint::GetConfigFiles(void) const
|
|
|
|
{
|
|
|
|
return m_ConfigFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array::Ptr Endpoint::GetAcceptConfig(void) const
|
|
|
|
{
|
|
|
|
return m_AcceptConfig;
|
|
|
|
}
|
|
|
|
|
2013-08-30 09:34:58 +02:00
|
|
|
double Endpoint::GetSeen(void) const
|
|
|
|
{
|
|
|
|
return m_Seen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Endpoint::SetSeen(double ts)
|
|
|
|
{
|
|
|
|
m_Seen = ts;
|
|
|
|
}
|
|
|
|
|
2013-09-03 10:08:02 +02:00
|
|
|
double Endpoint::GetLocalLogPosition(void) const
|
2013-09-02 15:12:20 +02:00
|
|
|
{
|
2013-09-03 10:08:02 +02:00
|
|
|
return m_LocalLogPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Endpoint::SetLocalLogPosition(double ts)
|
|
|
|
{
|
|
|
|
m_LocalLogPosition = ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Endpoint::GetRemoteLogPosition(void) const
|
|
|
|
{
|
|
|
|
return m_RemoteLogPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Endpoint::SetRemoteLogPosition(double ts)
|
|
|
|
{
|
|
|
|
m_RemoteLogPosition = ts;
|
2013-09-02 15:12:20 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 09:30:31 +02:00
|
|
|
bool Endpoint::IsSyncing(void) const
|
|
|
|
{
|
|
|
|
return m_Syncing;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Endpoint::SetSyncing(bool syncing)
|
|
|
|
{
|
|
|
|
m_Syncing = syncing;
|
|
|
|
}
|
|
|
|
|
2013-09-12 10:03:48 +02:00
|
|
|
Dictionary::Ptr Endpoint::GetFeatures(void) const
|
|
|
|
{
|
|
|
|
return m_Features;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Endpoint::SetFeatures(const Dictionary::Ptr& features)
|
|
|
|
{
|
|
|
|
m_Features = features;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Endpoint::HasFeature(const String& type) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr features = GetFeatures();
|
|
|
|
|
|
|
|
if (!features)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return features->Get(type);
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Endpoint::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
|
|
|
|
{
|
|
|
|
DynamicObject::InternalSerialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
2013-08-26 16:53:17 +02:00
|
|
|
bag->Set("host", m_Host);
|
|
|
|
bag->Set("port", m_Port);
|
2013-09-04 15:47:15 +02:00
|
|
|
bag->Set("config_files", m_ConfigFiles);
|
|
|
|
bag->Set("accept_config", m_AcceptConfig);
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
2013-08-30 09:34:58 +02:00
|
|
|
|
2013-09-03 10:08:02 +02:00
|
|
|
if (attributeTypes & Attribute_State) {
|
2013-08-30 09:34:58 +02:00
|
|
|
bag->Set("seen", m_Seen);
|
2013-09-03 10:08:02 +02:00
|
|
|
bag->Set("local_log_position", m_LocalLogPosition);
|
|
|
|
bag->Set("remote_log_position", m_RemoteLogPosition);
|
2013-09-12 10:03:48 +02:00
|
|
|
bag->Set("features", m_Features);
|
2013-09-03 10:08:02 +02:00
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Endpoint::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
|
|
|
|
{
|
|
|
|
DynamicObject::InternalDeserialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
2013-08-26 16:53:17 +02:00
|
|
|
m_Host = bag->Get("host");
|
|
|
|
m_Port = bag->Get("port");
|
2013-09-04 15:47:15 +02:00
|
|
|
m_ConfigFiles = bag->Get("config_files");
|
|
|
|
m_AcceptConfig = bag->Get("accept_config");
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
2013-08-30 09:34:58 +02:00
|
|
|
|
2013-09-03 10:08:02 +02:00
|
|
|
if (attributeTypes & Attribute_State) {
|
2013-08-30 09:34:58 +02:00
|
|
|
m_Seen = bag->Get("seen");
|
2013-09-03 10:08:02 +02:00
|
|
|
m_LocalLogPosition = bag->Get("local_log_position");
|
|
|
|
m_RemoteLogPosition = bag->Get("remote_log_position");
|
2013-09-12 10:03:48 +02:00
|
|
|
m_Features = bag->Get("features");
|
2013-09-03 10:08:02 +02:00
|
|
|
}
|
2013-08-26 16:53:17 +02:00
|
|
|
}
|