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"
|
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
|
|
|
|
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-11-01 00:13:30 +01:00
|
|
|
return GetClient() != NULL;
|
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();
|
|
|
|
|
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-18 12:02:35 +02:00
|
|
|
boost::thread thread(boost::bind(&Endpoint::MessageThreadProc, this, client));
|
|
|
|
thread.detach();
|
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-10-18 10:41:09 +02:00
|
|
|
Utility::SetThreadName("EndpointMsg");
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-12 10:03:48 +02:00
|
|
|
bool Endpoint::HasFeature(const String& type) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr features = GetFeatures();
|
|
|
|
|
|
|
|
if (!features)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return features->Get(type);
|
|
|
|
}
|
|
|
|
|