1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #ifndef JSONRPCCONNECTION_H
4 #define JSONRPCCONNECTION_H
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"
13 #include <boost/asio/io_service_strand.hpp>
14 #include <boost/asio/spawn.hpp>
15 #include <boost/asio/deadline_timer.hpp>
35 * An API client connection.
39 class JsonRpcConnection final : public Object
42 DECLARE_PTR_TYPEDEFS(JsonRpcConnection);
44 JsonRpcConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream, ConnectionRole role);
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;
57 void SendMessage(const Dictionary::Ptr& request);
59 static Value HeartbeatAPIHandler(const intrusive_ptr<MessageOrigin>& origin, const Dictionary::Ptr& params);
61 static void SendCertificateRequest(const JsonRpcConnection::Ptr& aclient, const intrusive_ptr<MessageOrigin>& origin, const String& path);
66 Endpoint::Ptr m_Endpoint;
67 std::shared_ptr<AsioTlsStream> m_Stream;
68 ConnectionRole m_Role;
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;
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);
83 bool ProcessMessage();
84 void MessageHandler(const String& jsonString);
86 void CertificateRequestResponseHandler(const Dictionary::Ptr& message);
91 #endif /* JSONRPCCONNECTION_H */