]> granicus.if.org Git - icinga2/blob - icinga/endpointmanager.h
699efd5905bfc79f5d8d46b9fa88a9bb31744819
[icinga2] / icinga / endpointmanager.h
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 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 #ifndef ENDPOINTMANAGER_H
21 #define ENDPOINTMANAGER_H
22
23 namespace icinga
24 {
25
26 /**
27  * Event arguments for the "new endpoint registered" event.
28  *
29  * @ingroup icinga
30  */
31 struct I2_ICINGA_API NewEndpointEventArgs : public EventArgs
32 {
33         icinga::Endpoint::Ptr Endpoint; /**< The new endpoint. */
34 };
35
36 struct NewResponseEventArgs;
37
38 /**
39  * Information about a pending API request.
40  *
41  * @ingroup icinga
42  */
43 struct I2_ICINGA_API PendingRequest
44 {
45         time_t Timeout;
46         RequestMessage Request;
47         function<int(const NewResponseEventArgs&)> Callback;
48
49         bool HasTimedOut(void) const
50         {
51                 return time(NULL) > Timeout;
52         }
53 };
54
55 /**
56  * Event arguments for the "new response" event.
57  *
58  * @ingroup icinga
59  */
60 struct I2_ICINGA_API NewResponseEventArgs : public EventArgs
61 {
62         Endpoint::Ptr Sender;
63         RequestMessage Request;
64         ResponseMessage Response;
65         bool TimedOut;
66 };
67
68 /**
69  * Forwards messages between endpoints.
70  *
71  * @ingroup icinga
72  */
73 class I2_ICINGA_API EndpointManager : public Object
74 {
75 public:
76         typedef shared_ptr<EndpointManager> Ptr;
77         typedef weak_ptr<EndpointManager> WeakPtr;
78
79         EndpointManager(void)
80                 : m_NextMessageID(0)
81         { }
82
83         void SetIdentity(string identity);
84         string GetIdentity(void) const;
85
86         void SetSSLContext(shared_ptr<SSL_CTX> sslContext);
87         shared_ptr<SSL_CTX> GetSSLContext(void) const;
88
89         void AddListener(string service);
90         void AddConnection(string node, string service);
91
92         void RegisterEndpoint(Endpoint::Ptr endpoint);
93         void UnregisterEndpoint(Endpoint::Ptr endpoint);
94
95         void SendUnicastMessage(Endpoint::Ptr sender, Endpoint::Ptr recipient, const MessagePart& message);
96         void SendAnycastMessage(Endpoint::Ptr sender, const RequestMessage& message);
97         void SendMulticastMessage(Endpoint::Ptr sender, const RequestMessage& message);
98
99         void SendAPIMessage(Endpoint::Ptr sender, RequestMessage& message,
100             function<int(const NewResponseEventArgs&)> callback, time_t timeout = 10);
101
102         void ProcessResponseMessage(const Endpoint::Ptr& sender, const ResponseMessage& message);
103
104         void ForEachEndpoint(function<int (const NewEndpointEventArgs&)> callback);
105
106         Endpoint::Ptr GetEndpointByIdentity(string identity) const;
107
108         Observable<NewEndpointEventArgs> OnNewEndpoint;
109
110 private:
111         string m_Identity;
112         shared_ptr<SSL_CTX> m_SSLContext;
113
114         vector<JsonRpcServer::Ptr> m_Servers;
115         vector<Endpoint::Ptr> m_Endpoints;
116
117         long m_NextMessageID;
118         map<string, PendingRequest> m_Requests;
119         Timer::Ptr m_RequestTimer;
120
121         void RegisterServer(JsonRpcServer::Ptr server);
122         void UnregisterServer(JsonRpcServer::Ptr server);
123
124         static bool RequestTimeoutLessComparer(const pair<string, PendingRequest>& a, const pair<string, PendingRequest>& b);
125         void RescheduleRequestTimer(void);
126         int RequestTimerHandler(const TimerEventArgs& ea);
127
128         int NewClientHandler(const NewClientEventArgs& ncea);
129 };
130
131 }
132
133 #endif /* ENDPOINTMANAGER_H */