2012-05-10 06:06:41 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
* Icinga 2 *
|
2014-03-18 20:02:29 -04:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2012-05-10 06:06:41 -04: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 07:33:57 -04:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 06:06:41 -04:00
|
|
|
******************************************************************************/
|
|
|
|
|
|
2014-03-07 11:40:10 -05:00
|
|
|
#include "remote/endpoint.h"
|
|
|
|
|
#include "remote/jsonrpc.h"
|
2013-03-16 16:18:53 -04:00
|
|
|
#include "base/application.h"
|
|
|
|
|
#include "base/dynamictype.h"
|
|
|
|
|
#include "base/objectlock.h"
|
2013-03-18 06:02:18 -04:00
|
|
|
#include "base/utility.h"
|
2013-03-16 16:18:53 -04:00
|
|
|
#include "base/logger_fwd.h"
|
2013-11-20 15:55:14 -05:00
|
|
|
#include "base/exception.h"
|
2013-03-17 15:19:29 -04:00
|
|
|
#include "config/configitembuilder.h"
|
2012-04-06 02:56:52 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2013-03-01 06:07:52 -05:00
|
|
|
REGISTER_TYPE(Endpoint);
|
2012-09-03 04:28:14 -04:00
|
|
|
|
2013-03-16 16:18:53 -04:00
|
|
|
boost::signals2::signal<void (const Endpoint::Ptr&)> Endpoint::OnConnected;
|
2014-02-21 08:12:34 -05:00
|
|
|
boost::signals2::signal<void (const Endpoint::Ptr&)> Endpoint::OnDisconnected;
|
2013-08-26 10:53:17 -04:00
|
|
|
boost::signals2::signal<void (const Endpoint::Ptr&, const Dictionary::Ptr&)> Endpoint::OnMessageReceived;
|
2012-09-03 04:28:14 -04:00
|
|
|
|
2012-05-15 10:24:04 -04:00
|
|
|
/**
|
2012-09-03 04:28:14 -04:00
|
|
|
* Checks whether this endpoint is connected.
|
2012-05-15 10:24:04 -04:00
|
|
|
*
|
2012-09-03 04:28:14 -04:00
|
|
|
* @returns true if the endpoint is connected, false otherwise.
|
2012-05-15 10:24:04 -04:00
|
|
|
*/
|
2012-09-03 04:28:14 -04:00
|
|
|
bool Endpoint::IsConnected(void) const
|
2012-04-18 09:22:25 -04:00
|
|
|
{
|
2013-10-31 19:13:30 -04:00
|
|
|
return GetClient() != NULL;
|
2012-04-18 09:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
2013-04-04 10:08:02 -04:00
|
|
|
Stream::Ptr Endpoint::GetClient(void) const
|
2012-04-18 09:22:25 -04:00
|
|
|
{
|
2013-02-26 04:13:54 -05:00
|
|
|
return m_Client;
|
2012-09-03 04:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
2013-04-04 10:08:02 -04:00
|
|
|
void Endpoint::SetClient(const Stream::Ptr& client)
|
2012-09-03 04:28:14 -04:00
|
|
|
{
|
2013-10-17 04:56:42 -04:00
|
|
|
if (m_Client)
|
|
|
|
|
m_Client->Close();
|
|
|
|
|
|
2013-09-16 03:30:31 -04:00
|
|
|
m_Client = client;
|
2013-03-02 03:07:47 -05:00
|
|
|
|
2013-09-11 03:44:23 -04:00
|
|
|
if (client) {
|
2013-10-18 06:02:35 -04:00
|
|
|
boost::thread thread(boost::bind(&Endpoint::MessageThreadProc, this, client));
|
|
|
|
|
thread.detach();
|
2013-04-04 10:08:02 -04:00
|
|
|
|
2013-09-11 03:44:23 -04:00
|
|
|
OnConnected(GetSelf());
|
2014-03-07 11:40:10 -05:00
|
|
|
Log(LogWarning, "remote", "Endpoint connected: " + GetName());
|
2014-02-21 08:12:34 -05:00
|
|
|
} else {
|
|
|
|
|
OnDisconnected(GetSelf());
|
2014-03-07 11:40:10 -05:00
|
|
|
Log(LogWarning, "remote", "Endpoint disconnected: " + GetName());
|
2013-09-11 03:44:23 -04:00
|
|
|
}
|
2012-04-18 09:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
2013-08-26 10:53:17 -04:00
|
|
|
void Endpoint::SendMessage(const Dictionary::Ptr& message)
|
2012-05-08 04:13:15 -04:00
|
|
|
{
|
2013-09-03 04:08:02 -04:00
|
|
|
Stream::Ptr client = GetClient();
|
2013-09-02 09:12:20 -04:00
|
|
|
|
2013-09-03 04:08:02 -04:00
|
|
|
if (!client)
|
|
|
|
|
return;
|
2012-09-03 04:28:14 -04:00
|
|
|
|
2013-08-26 10:53:17 -04:00
|
|
|
try {
|
2013-09-03 04:08:02 -04:00
|
|
|
JsonRpc::SendMessage(client, message);
|
2013-08-26 10:53:17 -04:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
|
std::ostringstream msgbuf;
|
2013-11-20 15:55:14 -05:00
|
|
|
msgbuf << "Error while sending JSON-RPC message for endpoint '" << GetName() << "': " << DiagnosticInformation(ex);
|
2014-03-07 11:40:10 -05:00
|
|
|
Log(LogWarning, "remote", msgbuf.str());
|
2012-09-03 04:28:14 -04:00
|
|
|
|
2013-08-26 10:53:17 -04:00
|
|
|
m_Client.reset();
|
2014-02-21 08:12:34 -05:00
|
|
|
|
|
|
|
|
OnDisconnected(GetSelf());
|
2014-03-07 11:40:10 -05:00
|
|
|
Log(LogWarning, "remote", "Endpoint disconnected: " + GetName());
|
2012-09-03 04:28:14 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-04 10:08:02 -04:00
|
|
|
void Endpoint::MessageThreadProc(const Stream::Ptr& stream)
|
2012-09-03 04:28:14 -04:00
|
|
|
{
|
2013-10-18 04:41:09 -04:00
|
|
|
Utility::SetThreadName("EndpointMsg");
|
|
|
|
|
|
2013-04-05 06:09:26 -04:00
|
|
|
for (;;) {
|
2013-08-26 10:53:17 -04:00
|
|
|
Dictionary::Ptr message;
|
2013-04-04 10:08:02 -04:00
|
|
|
|
2013-04-05 06:09:26 -04:00
|
|
|
try {
|
|
|
|
|
message = JsonRpc::ReadMessage(stream);
|
|
|
|
|
} catch (const std::exception& ex) {
|
2014-03-07 11:40:10 -05:00
|
|
|
Log(LogWarning, "remote", "Error while reading JSON-RPC message for endpoint '" + GetName() + "': " + DiagnosticInformation(ex));
|
2013-04-04 10:08:02 -04:00
|
|
|
|
|
|
|
|
m_Client.reset();
|
2013-08-27 06:21:41 -04:00
|
|
|
|
2014-02-21 08:12:34 -05:00
|
|
|
OnDisconnected(GetSelf());
|
2014-03-07 11:40:10 -05:00
|
|
|
Log(LogWarning, "remote", "Endpoint disconnected: " + GetName());
|
2014-02-21 08:12:34 -05:00
|
|
|
|
2013-08-27 06:21:41 -04:00
|
|
|
return;
|
2013-04-04 10:08:02 -04:00
|
|
|
}
|
|
|
|
|
|
2013-09-03 04:42:19 -04:00
|
|
|
OnMessageReceived(GetSelf(), message);
|
2013-03-02 03:07:47 -05:00
|
|
|
}
|
2012-09-03 04:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
2013-09-12 04:03:48 -04:00
|
|
|
bool Endpoint::HasFeature(const String& type) const
|
|
|
|
|
{
|
|
|
|
|
Dictionary::Ptr features = GetFeatures();
|
|
|
|
|
|
|
|
|
|
if (!features)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return features->Get(type);
|
|
|
|
|
}
|
|
|
|
|
|