2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
#ifndef JSONRPCCONNECTION_H
|
|
|
|
#define JSONRPCCONNECTION_H
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
#include "remote/i2-remote.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/endpoint.hpp"
|
2019-02-22 16:13:28 +01:00
|
|
|
#include "base/io-engine.hpp"
|
2014-10-16 12:27:09 +02:00
|
|
|
#include "base/tlsstream.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/timer.hpp"
|
2014-07-01 09:38:22 +02:00
|
|
|
#include "base/workqueue.hpp"
|
2019-02-19 13:57:36 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2019-06-07 16:30:34 +02:00
|
|
|
#include <boost/asio/io_service.hpp>
|
2019-02-19 13:57:36 +01:00
|
|
|
#include <boost/asio/io_service_strand.hpp>
|
|
|
|
#include <boost/asio/spawn.hpp>
|
2014-04-12 04:21:09 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
enum ClientRole
|
|
|
|
{
|
|
|
|
ClientInbound,
|
|
|
|
ClientOutbound
|
|
|
|
};
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
enum ClientType
|
|
|
|
{
|
|
|
|
ClientJsonRpc,
|
|
|
|
ClientHttp
|
|
|
|
};
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
class MessageOrigin;
|
2014-12-09 13:17:27 +01:00
|
|
|
|
2014-04-12 04:21:09 +02:00
|
|
|
/**
|
2014-05-03 20:02:22 +02:00
|
|
|
* An API client connection.
|
2014-04-12 04:21:09 +02:00
|
|
|
*
|
2014-05-03 20:02:22 +02:00
|
|
|
* @ingroup remote
|
2014-04-12 04:21:09 +02:00
|
|
|
*/
|
2018-01-04 06:11:04 +01:00
|
|
|
class JsonRpcConnection final : public Object
|
2014-04-12 04:21:09 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-06-22 11:11:21 +02:00
|
|
|
DECLARE_PTR_TYPEDEFS(JsonRpcConnection);
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2019-02-19 13:57:36 +01:00
|
|
|
JsonRpcConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream, ConnectionRole role);
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Start();
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
double GetTimestamp() const;
|
|
|
|
String GetIdentity() const;
|
|
|
|
bool IsAuthenticated() const;
|
|
|
|
Endpoint::Ptr GetEndpoint() const;
|
2019-02-19 13:57:36 +01:00
|
|
|
std::shared_ptr<AsioTlsStream> GetStream() const;
|
2018-01-04 04:25:35 +01:00
|
|
|
ConnectionRole GetRole() const;
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2019-02-20 12:00:11 +01:00
|
|
|
void Disconnect();
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
void SendMessage(const Dictionary::Ptr& request);
|
2019-02-26 11:13:34 +01:00
|
|
|
void SendRawMessage(const String& request);
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
static Value HeartbeatAPIHandler(const intrusive_ptr<MessageOrigin>& origin, const Dictionary::Ptr& params);
|
2014-12-09 13:17:27 +01:00
|
|
|
|
2019-02-20 14:24:09 +01:00
|
|
|
static double GetWorkQueueRate();
|
|
|
|
|
2017-09-04 16:19:46 +02:00
|
|
|
static void SendCertificateRequest(const JsonRpcConnection::Ptr& aclient, const intrusive_ptr<MessageOrigin>& origin, const String& path);
|
2017-09-04 13:18:06 +02:00
|
|
|
|
2014-04-12 04:21:09 +02:00
|
|
|
private:
|
2014-05-08 15:00:09 +02:00
|
|
|
String m_Identity;
|
2014-10-16 09:01:18 +02:00
|
|
|
bool m_Authenticated;
|
2014-05-03 20:02:22 +02:00
|
|
|
Endpoint::Ptr m_Endpoint;
|
2019-02-19 13:57:36 +01:00
|
|
|
std::shared_ptr<AsioTlsStream> m_Stream;
|
2014-05-03 20:02:22 +02:00
|
|
|
ConnectionRole m_Role;
|
2016-01-25 10:57:06 +01:00
|
|
|
double m_Timestamp;
|
2019-02-20 13:49:50 +01:00
|
|
|
double m_Seen;
|
2019-02-20 12:28:49 +01:00
|
|
|
double m_NextHeartbeat;
|
2019-02-19 13:57:36 +01:00
|
|
|
boost::asio::io_service::strand m_IoStrand;
|
2019-02-26 11:13:34 +01:00
|
|
|
std::vector<String> m_OutgoingMessagesQueue;
|
2019-02-22 16:13:28 +01:00
|
|
|
AsioConditionVariable m_OutgoingMessagesQueued;
|
|
|
|
AsioConditionVariable m_WriterDone;
|
2019-02-20 12:00:11 +01:00
|
|
|
bool m_ShuttingDown;
|
2019-06-05 10:32:20 +02:00
|
|
|
boost::asio::deadline_timer m_CheckLivenessTimer, m_HeartbeatTimer;
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2019-06-07 16:30:34 +02:00
|
|
|
JsonRpcConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream, ConnectionRole role, boost::asio::io_service& io);
|
|
|
|
|
2019-02-19 13:57:36 +01:00
|
|
|
void HandleIncomingMessages(boost::asio::yield_context yc);
|
|
|
|
void WriteOutgoingMessages(boost::asio::yield_context yc);
|
2019-02-20 12:28:49 +01:00
|
|
|
void HandleAndWriteHeartbeats(boost::asio::yield_context yc);
|
2019-02-20 13:49:50 +01:00
|
|
|
void CheckLiveness(boost::asio::yield_context yc);
|
2015-02-14 16:34:36 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool ProcessMessage();
|
2016-01-27 16:43:23 +01:00
|
|
|
void MessageHandler(const String& jsonString);
|
2017-08-29 14:37:13 +02:00
|
|
|
|
|
|
|
void CertificateRequestResponseHandler(const Dictionary::Ptr& message);
|
2019-02-26 10:17:10 +01:00
|
|
|
|
|
|
|
void SendMessageInternal(const Dictionary::Ptr& request);
|
2014-04-12 04:21:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
#endif /* JSONRPCCONNECTION_H */
|