]> granicus.if.org Git - icinga2/blob - lib/remote/endpoint.cpp
Make sure we don't include zones.d directories for zones which were removed
[icinga2] / lib / remote / endpoint.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
29
30 using namespace icinga;
31
32 REGISTER_TYPE(Endpoint);
33
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;
36
37 void Endpoint::OnAllConfigLoaded(void)
38 {
39         DynamicObject::OnConfigLoaded();
40
41         BOOST_FOREACH(const Zone::Ptr& zone, DynamicType::GetObjectsByType<Zone>()) {
42                 const std::set<Endpoint::Ptr> members = zone->GetEndpoints();
43
44                 if (members.empty())
45                         continue;
46
47                 if (members.find(this) != members.end()) {
48                         if (m_Zone)
49                                 BOOST_THROW_EXCEPTION(std::runtime_error("Endpoint '" + GetName() + "' is in more than one zone."));
50
51                         m_Zone = zone;
52                 }
53         }
54
55         if (!m_Zone)
56                 BOOST_THROW_EXCEPTION(std::runtime_error("Endpoint '" + GetName() + "' does not belong to a zone."));
57 }
58
59 void Endpoint::AddClient(const ApiClient::Ptr& client)
60 {
61         bool was_master = ApiListener::GetInstance()->IsMaster();
62
63         {
64                 boost::mutex::scoped_lock lock(m_ClientsLock);
65                 m_Clients.insert(client);
66         }
67
68         bool is_master = ApiListener::GetInstance()->IsMaster();
69
70         if (was_master != is_master)
71                 ApiListener::OnMasterChanged(is_master);
72
73         OnConnected(this, client);
74 }
75
76 void Endpoint::RemoveClient(const ApiClient::Ptr& client)
77 {
78         bool was_master = ApiListener::GetInstance()->IsMaster();
79
80         {
81                 boost::mutex::scoped_lock lock(m_ClientsLock);
82                 m_Clients.erase(client);
83
84                 Log(LogWarning, "ApiListener")
85                     << "Removing API client for endpoint '" << GetName() << "'. " << m_Clients.size() << " API clients left.";
86         }
87
88         bool is_master = ApiListener::GetInstance()->IsMaster();
89
90         if (was_master != is_master)
91                 ApiListener::OnMasterChanged(is_master);
92
93         OnDisconnected(this, client);
94 }
95
96 std::set<ApiClient::Ptr> Endpoint::GetClients(void) const
97 {
98         boost::mutex::scoped_lock lock(m_ClientsLock);
99         return m_Clients;
100 }
101
102 Zone::Ptr Endpoint::GetZone(void) const
103 {
104         return m_Zone;
105 }
106
107 bool Endpoint::IsConnected(void) const
108 {
109         boost::mutex::scoped_lock lock(m_ClientsLock);
110         return !m_Clients.empty();
111 }
112
113 Endpoint::Ptr Endpoint::GetLocalEndpoint(void)
114 {
115         ApiListener::Ptr listener = ApiListener::GetInstance();
116
117         if (!listener)
118                 return Endpoint::Ptr();
119
120         return Endpoint::GetByName(listener->GetIdentity());
121 }