1 /******************************************************************************
3 * Copyright (C) 2012 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 #ifndef ENDPOINTMANAGER_H
21 #define ENDPOINTMANAGER_H
27 * Event arguments for the "new endpoint registered" event.
31 struct I2_ICINGA_API NewEndpointEventArgs : public EventArgs
33 icinga::Endpoint::Ptr Endpoint; /**< The new endpoint. */
36 struct NewResponseEventArgs;
39 * Information about a pending API request.
43 struct I2_ICINGA_API PendingRequest
46 RequestMessage Request;
47 function<int(const NewResponseEventArgs&)> Callback;
49 bool HasTimedOut(void) const
51 return time(NULL) > Timeout;
56 * Event arguments for the "new response" event.
60 struct I2_ICINGA_API NewResponseEventArgs : public EventArgs
63 RequestMessage Request;
64 ResponseMessage Response;
69 * Forwards messages between endpoints.
73 class I2_ICINGA_API EndpointManager : public Object
76 typedef shared_ptr<EndpointManager> Ptr;
77 typedef weak_ptr<EndpointManager> WeakPtr;
83 void SetIdentity(string identity);
84 string GetIdentity(void) const;
86 void SetSSLContext(shared_ptr<SSL_CTX> sslContext);
87 shared_ptr<SSL_CTX> GetSSLContext(void) const;
89 void AddListener(string service);
90 void AddConnection(string node, string service);
92 void RegisterEndpoint(Endpoint::Ptr endpoint);
93 void UnregisterEndpoint(Endpoint::Ptr endpoint);
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);
99 void SendAPIMessage(Endpoint::Ptr sender, RequestMessage& message,
100 function<int(const NewResponseEventArgs&)> callback, time_t timeout = 10);
102 void ProcessResponseMessage(const Endpoint::Ptr& sender, const ResponseMessage& message);
104 void ForEachEndpoint(function<int (const NewEndpointEventArgs&)> callback);
106 Endpoint::Ptr GetEndpointByIdentity(string identity) const;
108 Observable<NewEndpointEventArgs> OnNewEndpoint;
112 shared_ptr<SSL_CTX> m_SSLContext;
114 vector<JsonRpcServer::Ptr> m_Servers;
115 vector<Endpoint::Ptr> m_Endpoints;
117 long m_NextMessageID;
118 map<string, PendingRequest> m_Requests;
119 Timer::Ptr m_RequestTimer;
121 void RegisterServer(JsonRpcServer::Ptr server);
122 void UnregisterServer(JsonRpcServer::Ptr server);
124 static bool RequestTimeoutLessComparer(const pair<string, PendingRequest>& a, const pair<string, PendingRequest>& b);
125 void RescheduleRequestTimer(void);
126 int RequestTimerHandler(const TimerEventArgs& ea);
128 int NewClientHandler(const NewClientEventArgs& ncea);
133 #endif /* ENDPOINTMANAGER_H */