icinga2/lib/remote/endpoint.cpp

127 lines
3.9 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 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 "remote/endpoint.h"
#include "remote/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"
#include "base/exception.h"
2013-03-17 20:19:29 +01:00
#include "config/configitembuilder.h"
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;
boost::signals2::signal<void (const Endpoint::Ptr&)> Endpoint::OnDisconnected;
2013-08-26 16:53:17 +02:00
boost::signals2::signal<void (const Endpoint::Ptr&, const Dictionary::Ptr&)> Endpoint::OnMessageReceived;
/**
* 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
{
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;
}
2013-04-04 16:08:02 +02:00
void Endpoint::SetClient(const Stream::Ptr& client)
{
2013-10-17 10:56:42 +02:00
if (m_Client)
m_Client->Close();
m_Client = client;
2013-03-02 09:07:47 +01: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
OnConnected(GetSelf());
Log(LogWarning, "remote", "Endpoint connected: " + GetName());
} else {
OnDisconnected(GetSelf());
Log(LogWarning, "remote", "Endpoint disconnected: " + GetName());
}
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() << "': " << DiagnosticInformation(ex);
Log(LogWarning, "remote", msgbuf.str());
2013-08-26 16:53:17 +02:00
m_Client.reset();
OnDisconnected(GetSelf());
Log(LogWarning, "remote", "Endpoint disconnected: " + GetName());
}
}
2013-04-04 16:08:02 +02:00
void Endpoint::MessageThreadProc(const Stream::Ptr& stream)
{
2013-10-18 10:41:09 +02:00
Utility::SetThreadName("EndpointMsg");
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, "remote", "Error while reading JSON-RPC message for endpoint '" + GetName() + "': " + DiagnosticInformation(ex));
2013-04-04 16:08:02 +02:00
m_Client.reset();
OnDisconnected(GetSelf());
Log(LogWarning, "remote", "Endpoint disconnected: " + GetName());
return;
2013-04-04 16:08:02 +02:00
}
OnMessageReceived(GetSelf(), message);
2013-03-02 09:07:47 +01: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);
}