1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "remote/endpoint.hpp"
21 #include "remote/apilistener.hpp"
22 #include "remote/apiclient.hpp"
23 #include "remote/zone.hpp"
24 #include "base/dynamictype.hpp"
25 #include "base/utility.hpp"
26 #include "base/exception.hpp"
27 #include "base/convert.hpp"
28 #include <boost/foreach.hpp>
30 using namespace icinga;
32 REGISTER_TYPE(Endpoint);
34 boost::signals2::signal<void(const Endpoint::Ptr&, const ApiClient::Ptr&)> Endpoint::OnConnected;
35 boost::signals2::signal<void(const Endpoint::Ptr&, const ApiClient::Ptr&)> Endpoint::OnDisconnected;
37 void Endpoint::OnAllConfigLoaded(void)
39 DynamicObject::OnConfigLoaded();
41 BOOST_FOREACH(const Zone::Ptr& zone, DynamicType::GetObjectsByType<Zone>()) {
42 const std::set<Endpoint::Ptr> members = zone->GetEndpoints();
47 if (members.find(this) != members.end()) {
49 BOOST_THROW_EXCEPTION(std::runtime_error("Endpoint '" + GetName() + "' is in more than one zone."));
56 BOOST_THROW_EXCEPTION(std::runtime_error("Endpoint '" + GetName() + "' does not belong to a zone."));
59 void Endpoint::AddClient(const ApiClient::Ptr& client)
61 bool was_master = ApiListener::GetInstance()->IsMaster();
64 boost::mutex::scoped_lock lock(m_ClientsLock);
65 m_Clients.insert(client);
68 bool is_master = ApiListener::GetInstance()->IsMaster();
70 if (was_master != is_master)
71 ApiListener::OnMasterChanged(is_master);
73 OnConnected(this, client);
76 void Endpoint::RemoveClient(const ApiClient::Ptr& client)
78 bool was_master = ApiListener::GetInstance()->IsMaster();
81 boost::mutex::scoped_lock lock(m_ClientsLock);
82 m_Clients.erase(client);
84 Log(LogWarning, "ApiListener")
85 << "Removing API client for endpoint '" << GetName() << "'. " << m_Clients.size() << " API clients left.";
88 bool is_master = ApiListener::GetInstance()->IsMaster();
90 if (was_master != is_master)
91 ApiListener::OnMasterChanged(is_master);
93 OnDisconnected(this, client);
96 std::set<ApiClient::Ptr> Endpoint::GetClients(void) const
98 boost::mutex::scoped_lock lock(m_ClientsLock);
102 Zone::Ptr Endpoint::GetZone(void) const
107 bool Endpoint::IsConnected(void) const
109 boost::mutex::scoped_lock lock(m_ClientsLock);
110 return !m_Clients.empty();
113 Endpoint::Ptr Endpoint::GetLocalEndpoint(void)
115 ApiListener::Ptr listener = ApiListener::GetInstance();
118 return Endpoint::Ptr();
120 return Endpoint::GetByName(listener->GetIdentity());