<commit_before>/**
 * @file connectionPool.cc
 * @author <NAME>
 * @copyright (C) 2015 ACK CYFRONET AGH
 * @copyright This software is released under the MIT license cited in
 * 'LICENSE.txt'
 */

#include "connectionPool.h"

#include "cert/certificateData.h"
#include "codec/clprotoHandshakeResponseHandler.h"
#include "codec/clprotoMessageHandler.h"
#include "codec/clprotoUpgradeResponseHandler.h"
#include "codec/packetDecoder.h"
#include "codec/packetEncoder.h"
#include "codec/packetLogger.h"
#include "exception.h"

#include <algorithm>
#include <array>
#include <folly/Range.h>
#include <folly/io/IOBuf.h>
#include <iterator>
#include <tuple>

using namespace std::placeholders;
using namespace std::literals::chrono_literals;

namespace one {
namespace communication {

ConnectionPool::ConnectionPool(const std::size_t connectionsNumber,
    const std::size_t workersNumber, std::string host,
    const unsigned short port, const bool verifyServerCertificate,
    const bool clprotoUpgrade)
    : m_connectionsNumber{connectionsNumber}
    , m_workersNumber{workersNumber}
    , m_host{host}
    , m_port{port}
    , m_address{m_host, m_port}
    , m_verifyServerCertificate{verifyServerCertificate}
    , m_clprotoUpgrade{clprotoUpgrade}
    , m_connected{false}
    , m_executor{std::make_shared<folly::IOThreadPoolExecutor>(1)}
    , m_client{std::make_shared<CLProtoClientBootstrap>()}
{
    LOG_FCALL() << LOG_FARG(connectionsNumber) << LOG_FARG(host)
                << LOG_FARG(port) << LOG_FARG(verifyServerCertificate);

    m_pipelineFactory =
        std::make_shared<CLProtoPipelineFactory>(m_clprotoUpgrade);
    m_client->group(m_executor);
    m_client->pipelineFactory(m_pipelineFactory);
}

std::string ConnectionPool::makeHandshake()
{
    auto handshake = m_getHandshake();
    return handshake;
}

std::shared_ptr<folly::SSLContext> ConnectionPool::createSSLContext()
{
    auto context =
        std::make_shared<folly::SSLContext>(folly::SSLContext::TLSv1_2);

    context->authenticate(m_verifyServerCertificate, false);

    folly::ssl::setSignatureAlgorithms<folly::ssl::SSLCommonOptions>(*context);

    context->setVerificationOption(m_verifyServerCertificate
            ? folly::SSLContext::SSLVerifyPeerEnum::VERIFY
            : folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY);

    return context;
}

void ConnectionPool::connect()
{
    LOG_FCALL();

    if (m_connected)
        return;

    m_client->sslContext(createSSLContext());

    std::string host = m_address.getAddressStr();<commit_msg>VFS-4804 Added DNS address resolution<commit_after>/**
 * @file connectionPool.cc
 * @author <NAME>
 * @copyright (C) 2015 ACK CYFRONET AGH
 * @copyright This software is released under the MIT license cited in
 * 'LICENSE.txt'
 */

#include "connectionPool.h"

#include "cert/certificateData.h"
#include "codec/clprotoHandshakeResponseHandler.h"
#include "codec/clprotoMessageHandler.h"
#include "codec/clprotoUpgradeResponseHandler.h"
#include "codec/packetDecoder.h"
#include "codec/packetEncoder.h"
#include "codec/packetLogger.h"
#include "exception.h"

#include <algorithm>
#include <array>
#include <folly/Range.h>
#include <folly/io/IOBuf.h>
#include <iterator>
#include <tuple>

using namespace std::placeholders;
using namespace std::literals::chrono_literals;

namespace one {
namespace communication {

ConnectionPool::ConnectionPool(const std::size_t connectionsNumber,
    const std::size_t workersNumber, std::string host,
    const unsigned short port, const bool verifyServerCertificate,
    const bool clprotoUpgrade)
    : m_connectionsNumber{connectionsNumber}
    , m_workersNumber{workersNumber}
    , m_host{host}
    , m_port{port}
    , m_verifyServerCertificate{verifyServerCertificate}
    , m_clprotoUpgrade{clprotoUpgrade}
    , m_connected{false}
    , m_executor{std::make_shared<folly::IOThreadPoolExecutor>(1)}
    , m_client{std::make_shared<CLProtoClientBootstrap>()}
{
    LOG_FCALL() << LOG_FARG(connectionsNumber) << LOG_FARG(host)
                << LOG_FARG(port) << LOG_FARG(verifyServerCertificate);

    m_pipelineFactory =
        std::make_shared<CLProtoPipelineFactory>(m_clprotoUpgrade);
    m_client->group(m_executor);
    m_client->pipelineFactory(m_pipelineFactory);
}

std::string ConnectionPool::makeHandshake()
{
    auto handshake = m_getHandshake();
    return handshake;
}

std::shared_ptr<folly::SSLContext> ConnectionPool::createSSLContext()
{
    auto context =
        std::make_shared<folly::SSLContext>(folly::SSLContext::TLSv1_2);

    context->authenticate(m_verifyServerCertificate, false);

    folly::ssl::setSignatureAlgorithms<folly::ssl::SSLCommonOptions>(*context);

    context->setVerificationOption(m_verifyServerCertificate
            ? folly::SSLContext::SSLVerifyPeerEnum::VERIFY
            : folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY);

    return context;
}

void ConnectionPool::connect()
{
    LOG_FCALL();

    if (m_connected)
        return;

    m_client->sslContext(createSSLContext());

    // Initialize SocketAddres from host and port provided on the command line.
    // If necessary, DNS lookup will be performed...
    m_address.setFromHostPort(m_host, m_port);
{'fraction_non_alphanumeric': 0.11764705882352941, 'fraction_numerical': 0.0034602076124567475, 'mean_word_length': 5.321992709599028, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '230539', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1607, 'n_words': 280}