]> granicus.if.org Git - icinga2/blob - lib/remote/jsonrpcconnection.hpp
JsonRpcConnection: re-add automatic disconnect
[icinga2] / lib / remote / jsonrpcconnection.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef JSONRPCCONNECTION_H
4 #define JSONRPCCONNECTION_H
5
6 #include "remote/i2-remote.hpp"
7 #include "remote/endpoint.hpp"
8 #include "base/tlsstream.hpp"
9 #include "base/timer.hpp"
10 #include "base/workqueue.hpp"
11 #include <memory>
12 #include <vector>
13 #include <boost/asio/io_service_strand.hpp>
14 #include <boost/asio/spawn.hpp>
15 #include <boost/asio/deadline_timer.hpp>
16
17 namespace icinga
18 {
19
20 enum ClientRole
21 {
22         ClientInbound,
23         ClientOutbound
24 };
25
26 enum ClientType
27 {
28         ClientJsonRpc,
29         ClientHttp
30 };
31
32 class MessageOrigin;
33
34 /**
35  * An API client connection.
36  *
37  * @ingroup remote
38  */
39 class JsonRpcConnection final : public Object
40 {
41 public:
42         DECLARE_PTR_TYPEDEFS(JsonRpcConnection);
43
44         JsonRpcConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream, ConnectionRole role);
45
46         void Start();
47
48         double GetTimestamp() const;
49         String GetIdentity() const;
50         bool IsAuthenticated() const;
51         Endpoint::Ptr GetEndpoint() const;
52         std::shared_ptr<AsioTlsStream> GetStream() const;
53         ConnectionRole GetRole() const;
54
55         void Disconnect();
56
57         void SendMessage(const Dictionary::Ptr& request);
58
59         static Value HeartbeatAPIHandler(const intrusive_ptr<MessageOrigin>& origin, const Dictionary::Ptr& params);
60
61         static void SendCertificateRequest(const JsonRpcConnection::Ptr& aclient, const intrusive_ptr<MessageOrigin>& origin, const String& path);
62
63 private:
64         String m_Identity;
65         bool m_Authenticated;
66         Endpoint::Ptr m_Endpoint;
67         std::shared_ptr<AsioTlsStream> m_Stream;
68         ConnectionRole m_Role;
69         double m_Timestamp;
70         double m_Seen;
71         double m_NextHeartbeat;
72         boost::asio::io_service::strand m_IoStrand;
73         std::vector<Dictionary::Ptr> m_OutgoingMessagesQueue;
74         boost::asio::deadline_timer m_OutgoingMessagesQueued;
75         boost::asio::deadline_timer m_WriterDone;
76         bool m_ShuttingDown;
77
78         void HandleIncomingMessages(boost::asio::yield_context yc);
79         void WriteOutgoingMessages(boost::asio::yield_context yc);
80         void HandleAndWriteHeartbeats(boost::asio::yield_context yc);
81         void CheckLiveness(boost::asio::yield_context yc);
82
83         bool ProcessMessage();
84         void MessageHandler(const String& jsonString);
85
86         void CertificateRequestResponseHandler(const Dictionary::Ptr& message);
87 };
88
89 }
90
91 #endif /* JSONRPCCONNECTION_H */