Line data Source code
1 : #ifndef SOCIAL_NETWORK_MICROSERVICES_THRIFTCLIENT_H
2 : #define SOCIAL_NETWORK_MICROSERVICES_THRIFTCLIENT_H
3 :
4 : #include <string>
5 : #include <thread>
6 : #include <iostream>
7 : #include <chrono>
8 : #include <boost/log/trivial.hpp>
9 :
10 : #include <thrift/protocol/TBinaryProtocol.h>
11 : #include <thrift/transport/TSocket.h>
12 : #include <thrift/transport/TSSLSocket.h>
13 : #include <thrift/transport/TTransportUtils.h>
14 : #include <thrift/stdcxx.h>
15 : #include <nlohmann/json.hpp>
16 : #include "logger.h"
17 : #include "GenericClient.h"
18 :
19 :
20 : namespace social_network {
21 :
22 : using apache::thrift::protocol::TProtocol;
23 : using apache::thrift::protocol::TBinaryProtocol;
24 : using apache::thrift::transport::TFramedTransport;
25 : using apache::thrift::transport::TSocket;
26 : using apache::thrift::transport::TSSLSocketFactory;
27 : using apache::thrift::transport::TTransport;
28 : using apache::thrift::TException;
29 : using json = nlohmann::json;
30 :
31 : template<class TThriftClient>
32 : class ThriftClient : public GenericClient {
33 : public:
34 : ThriftClient(const std::string &addr, int port);
35 : ThriftClient(const std::string &addr, int port, int keepalive_ms, const json &config_json);
36 :
37 : ThriftClient(const ThriftClient &) = delete;
38 : ThriftClient &operator=(const ThriftClient &) = delete;
39 : ThriftClient(ThriftClient<TThriftClient> &&) = default;
40 : ThriftClient &operator=(ThriftClient &&) = default;
41 :
42 : ~ThriftClient() override;
43 :
44 : TThriftClient *GetClient() const;
45 :
46 : void Connect() override;
47 : void Disconnect() override;
48 : bool IsConnected() override;
49 :
50 : private:
51 : TThriftClient *_client;
52 :
53 : std::shared_ptr<TSocket> _socket;
54 : std::shared_ptr<TTransport> _transport;
55 : std::shared_ptr<TProtocol> _protocol;
56 : };
57 :
58 : template<class TThriftClient>
59 : ThriftClient<TThriftClient>::ThriftClient(
60 : const std::string &addr, int port) {
61 : _addr = addr;
62 : _port = port;
63 : _socket = std::shared_ptr<TSocket>(new TSocket(addr, port));
64 : _socket->setKeepAlive(true);
65 : _transport = std::shared_ptr<TTransport>(new TFramedTransport(_socket));
66 : _protocol = std::shared_ptr<TProtocol>(new TBinaryProtocol(_transport));
67 : _client = new TThriftClient(_protocol);
68 : _connect_timestamp = 0;
69 : _keepalive_ms = 0;
70 : }
71 :
72 : template <class TThriftClient>
73 224 : ThriftClient<TThriftClient>::ThriftClient(
74 224 : const std::string &addr, int port, int keepalive_ms, const json &config_json) {
75 224 : _addr = addr;
76 224 : _port = port;
77 224 : bool ssl_enabled = config_json["ssl"]["enabled"];
78 :
79 224 : if (ssl_enabled) {
80 0 : std::string ca_path = config_json["ssl"]["caPath"];
81 0 : std::string ciphers = config_json["ssl"]["ciphers"];
82 :
83 0 : std::shared_ptr<TSSLSocketFactory> factory;
84 0 : factory = std::make_shared<TSSLSocketFactory>();
85 0 : factory->ciphers(ciphers);
86 0 : factory->loadTrustedCertificates(ca_path.c_str());
87 :
88 : // if (config_json["ssl"]["verifyClient"]) {
89 : // std::string cert_path = config_json["ssl"]["clientCertPath"];
90 : // std::string key_path = config_json["ssl"]["clientKeyPath"];
91 : // factory->loadCertificate(cert_path.c_str());
92 : // factory->loadPrivateKey(key_path.c_str());
93 : // }
94 : // Need verify server
95 0 : factory->authenticate(true);
96 0 : _socket = factory->createSocket(addr, port);
97 : } else {
98 224 : _socket = std::shared_ptr<TSocket>(new TSocket(addr, port));
99 : }
100 224 : _socket->setKeepAlive(true);
101 224 : _transport = std::shared_ptr<TTransport>(new TFramedTransport(_socket));
102 224 : _protocol = std::shared_ptr<TProtocol>(new TBinaryProtocol(_transport));
103 224 : _client = new TThriftClient(_protocol);
104 672 : _connect_timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
105 448 : std::chrono::system_clock::now().time_since_epoch())
106 448 : .count();
107 224 : _keepalive_ms = keepalive_ms;
108 224 : }
109 :
110 : template<class TThriftClient>
111 372 : ThriftClient<TThriftClient>::~ThriftClient() {
112 186 : Disconnect();
113 186 : delete _client;
114 558 : }
115 :
116 : template<class TThriftClient>
117 75239 : TThriftClient *ThriftClient<TThriftClient>::GetClient() const {
118 75239 : return _client;
119 : }
120 :
121 : template<class TThriftClient>
122 75412 : bool ThriftClient<TThriftClient>::IsConnected() {
123 75412 : return _transport->isOpen();
124 : }
125 :
126 : template<class TThriftClient>
127 75235 : void ThriftClient<TThriftClient>::Connect() {
128 75235 : if (!IsConnected()) {
129 : try {
130 224 : _transport->open();
131 0 : } catch (TException &tx) {
132 0 : throw tx;
133 : }
134 : }
135 75243 : }
136 :
137 : template<class TThriftClient>
138 186 : void ThriftClient<TThriftClient>::Disconnect() {
139 186 : if (IsConnected()) {
140 : try {
141 186 : _transport->close();
142 0 : } catch (TException &tx) {
143 0 : throw tx;
144 : }
145 : }
146 186 : }
147 :
148 : } // namespace social_network
149 :
150 :
151 : #endif //SOCIAL_NETWORK_MICROSERVICES_THRIFTCLIENT_H
|