LCOV - code coverage report
Current view: top level - src - ThriftClient.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 44 72.7 %
Date: 2025-11-04 00:23:26 Functions: 49 49 100.0 %

          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         469 : ThriftClient<TThriftClient>::ThriftClient(
      74         469 :     const std::string &addr, int port, int keepalive_ms, const json &config_json) {
      75         469 :   _addr = addr;
      76         469 :   _port = port;
      77         469 :   bool ssl_enabled = config_json["ssl"]["enabled"];
      78             : 
      79         469 :   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         469 :     _socket = std::shared_ptr<TSocket>(new TSocket(addr, port));
      99             :   }
     100         469 :   _socket->setKeepAlive(true);
     101         469 :   _transport = std::shared_ptr<TTransport>(new TFramedTransport(_socket));
     102         469 :   _protocol = std::shared_ptr<TProtocol>(new TBinaryProtocol(_transport));
     103         469 :   _client = new TThriftClient(_protocol);
     104        1407 :   _connect_timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
     105         938 :                            std::chrono::system_clock::now().time_since_epoch())
     106         938 :                            .count();
     107         469 :   _keepalive_ms = keepalive_ms;
     108         469 : }
     109             : 
     110             : template<class TThriftClient>
     111         924 : ThriftClient<TThriftClient>::~ThriftClient() {
     112         462 :   Disconnect();
     113         462 :   delete _client;
     114        1386 : }
     115             : 
     116             : template<class TThriftClient>
     117        1400 : TThriftClient *ThriftClient<TThriftClient>::GetClient() const {
     118        1400 :   return _client;
     119             : }
     120             : 
     121             : template<class TThriftClient>
     122        1862 : bool ThriftClient<TThriftClient>::IsConnected() {
     123        1862 :   return _transport->isOpen();
     124             : }
     125             : 
     126             : template<class TThriftClient>
     127        1400 : void ThriftClient<TThriftClient>::Connect() {
     128        1400 :   if (!IsConnected()) {
     129             :     try {
     130         469 :       _transport->open();
     131           0 :     } catch (TException &tx) {
     132           0 :       throw tx;
     133             :     }
     134             :   }
     135        1400 : }
     136             : 
     137             : template<class TThriftClient>
     138         462 : void ThriftClient<TThriftClient>::Disconnect() {
     139         462 :   if (IsConnected()) {
     140             :     try {
     141         462 :       _transport->close();
     142           0 :     } catch (TException &tx) {
     143           0 :       throw tx;
     144             :     }
     145             :   }
     146         462 : }
     147             : 
     148             : } // namespace social_network
     149             : 
     150             : 
     151             : #endif //SOCIAL_NETWORK_MICROSERVICES_THRIFTCLIENT_H

Generated by: LCOV version 1.12