icinga2/components/cluster/endpoint.cpp

232 lines
5.7 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2013-09-25 07:43:57 +02:00
* Copyright (C) 2012-2013 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. *
******************************************************************************/
#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>
using namespace icinga;
2013-03-01 12:07:52 +01:00
REGISTER_TYPE(Endpoint);
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;
Endpoint::Endpoint(void)
: m_Syncing(false)
{ }
/**
* Checks whether this endpoint is connected.
*
* @returns true if the endpoint is connected, false otherwise.
*/
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;
}
2013-04-04 16:08:02 +02:00
void Endpoint::SetClient(const Stream::Ptr& client)
{
m_Client = client;
2013-03-02 09:07:47 +01:00
if (client) {
boost::thread thread(boost::bind(&Endpoint::MessageThreadProc, this, client));
thread.detach();
2013-04-04 16:08:02 +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
{
Stream::Ptr client = GetClient();
if (!client)
return;
2013-08-26 16:53:17 +02:00
try {
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);
Log(LogWarning, "cluster", msgbuf.str());
2013-08-26 16:53:17 +02:00
m_Client.reset();
}
}
2013-04-04 16:08:02 +02:00
void Endpoint::MessageThreadProc(const Stream::Ptr& stream)
{
for (;;) {
2013-08-26 16:53:17 +02:00
Dictionary::Ptr message;
2013-04-04 16:08:02 +02:00
try {
message = JsonRpc::ReadMessage(stream);
} catch (const std::exception& ex) {
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();
return;
2013-04-04 16:08:02 +02:00
}
OnMessageReceived(GetSelf(), message);
2013-03-02 09:07:47 +01: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
{
2013-08-26 16:53:17 +02:00
return m_Host;
}
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
{
2013-08-26 16:53:17 +02:00
return m_Port;
}
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;
}
double Endpoint::GetLocalLogPosition(void) const
{
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;
}
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);
}
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-30 09:34:58 +02:00
if (attributeTypes & Attribute_State) {
2013-08-30 09:34:58 +02:00
bag->Set("seen", m_Seen);
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);
}
}
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-30 09:34:58 +02:00
if (attributeTypes & Attribute_State) {
2013-08-30 09:34:58 +02:00
m_Seen = bag->Get("seen");
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-08-26 16:53:17 +02:00
}