icinga2/lib/remote/endpoint.cpp

115 lines
3.6 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/apilistener.h"
#include "remote/apiclient.h"
#include "remote/zone.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
2013-03-18 11:02:18 +01:00
#include "base/utility.h"
#include "base/exception.h"
#include <boost/foreach.hpp>
using namespace icinga;
2013-03-01 12:07:52 +01:00
REGISTER_TYPE(Endpoint);
boost::signals2::signal<void(const Endpoint::Ptr&, const ApiClient::Ptr&)> Endpoint::OnConnected;
boost::signals2::signal<void(const Endpoint::Ptr&, const ApiClient::Ptr&)> Endpoint::OnDisconnected;
void Endpoint::OnConfigLoaded(void)
2012-04-18 15:22:25 +02:00
{
DynamicObject::OnConfigLoaded();
2012-04-18 15:22:25 +02:00
BOOST_FOREACH(const Zone::Ptr& zone, DynamicType::GetObjects<Zone>()) {
const std::set<Endpoint::Ptr> members = zone->GetEndpoints();
if (members.find(GetSelf()) != members.end()) {
if (m_Zone)
BOOST_THROW_EXCEPTION(std::runtime_error("Endpoint '" + GetName() + "' is in more than one zone."));
m_Zone = zone;
}
}
if (!m_Zone)
BOOST_THROW_EXCEPTION(std::runtime_error("Endpoint '" + GetName() + "' does not belong to a zone."));
}
void Endpoint::AddClient(const ApiClient::Ptr& client)
{
bool was_master = ApiListener::GetInstance()->IsMaster();
{
boost::mutex::scoped_lock lock(m_ClientsLock);
m_Clients.insert(client);
}
2013-10-17 10:56:42 +02:00
bool is_master = ApiListener::GetInstance()->IsMaster();
2013-03-02 09:07:47 +01:00
if (was_master != is_master)
ApiListener::OnMasterChanged(is_master);
2013-04-04 16:08:02 +02:00
OnConnected(GetSelf(), client);
2012-04-18 15:22:25 +02:00
}
void Endpoint::RemoveClient(const ApiClient::Ptr& client)
2012-05-08 10:13:15 +02:00
{
bool was_master = ApiListener::GetInstance()->IsMaster();
{
boost::mutex::scoped_lock lock(m_ClientsLock);
m_Clients.erase(client);
}
bool is_master = ApiListener::GetInstance()->IsMaster();
if (was_master != is_master)
ApiListener::OnMasterChanged(is_master);
OnDisconnected(GetSelf(), client);
}
std::set<ApiClient::Ptr> Endpoint::GetClients(void) const
{
boost::mutex::scoped_lock lock(m_ClientsLock);
return m_Clients;
}
Zone::Ptr Endpoint::GetZone(void) const
{
return m_Zone;
}
2013-04-04 16:08:02 +02:00
bool Endpoint::IsConnected(void) const
{
boost::mutex::scoped_lock lock(m_ClientsLock);
return !m_Clients.empty();
}
Endpoint::Ptr Endpoint::GetLocalEndpoint(void)
2013-09-12 10:03:48 +02:00
{
ApiListener::Ptr listener = ApiListener::GetInstance();
2013-09-12 10:03:48 +02:00
if (!listener)
return Endpoint::Ptr();
2013-09-12 10:03:48 +02:00
return Endpoint::GetByName(listener->GetIdentity());
2013-09-12 10:03:48 +02:00
}