<commit_before>/*
 * Copyright 2014-2020 Real Logic Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef AERON_CLIENT_CONDUCTOR_H
#define AERON_CLIENT_CONDUCTOR_H

#include <unordered_map>
#include <vector>
#include <mutex>
#include <concurrent/logbuffer/TermReader.h>
#include <concurrent/status/UnsafeBufferPosition.h>
#include <util/LangUtil.h>
#include <util/ScopeUtils.h>
#include "Publication.h"
#include "ExclusivePublication.h"
#include "Subscription.h"
#include "Counter.h"
#include "DriverProxy.h"
#include "Context.h"
#include "DriverListenerAdapter.h"
#include "LogBuffers.h"
#include "HeartbeatTimestamp.h"
#include "util/Export.h"

namespace aeron {

using namespace aeron::concurrent::logbuffer;
using namespace aeron::concurrent::status;
using namespace aeron::concurrent;
using namespace aeron::util;

typedef std::function<long long()> epoch_clock_t;
typedef std::function<long long()> nano_clock_t;

static const long KEEPALIVE_TIMEOUT_MS = 500;
static const long RESOURCE_TIMEOUT_MS = 1000;

class CLIENT_EXPORT ClientConductor
{
public:

    ClientConductor(
        epoch_clock_t epochClock,
        DriverProxy& driverProxy,
        CopyBroadcastReceiver& broadcastReceiver,
        AtomicBuffer& counterMetadataBuffer,
        AtomicBuffer& counterValuesBuffer,
        const on_new_publication_t& newPublicationHandler,
        const on_new_publication_t& newExclusivePublicationHandler,
        const on_new_subscription_t& newSubscriptionHandler,
        const exception_handler_t& errorHandler,
        const on_available_counter_t& availableCounterHandler,
        const on_unavailable_counter_t& unavailableCounterHandler,
        const on_close_client_t& onCloseClientHandler,
        long driverTimeoutMs,
        long resourceLingerTimeoutMs,
        long long interServiceTimeoutNs,
        bool preTouchMappedMemory) :
        m_driverProxy(driverProxy),
        m_driverListenerAdapter(broadcastReceiver, *this),
        m_countersReader(counterMetadataBuffer, counterValuesBuffer),
        m_counterValuesBuffer(counterValuesBuffer),
        m_onNewPublicationHandler(newPublicationHandler),
        m_onNewExclusivePublicationHandler(newExclusivePublicationHandler),
        m_onNewSubscriptionHandler(newSubscriptionHandler),
        m_errorHandler(errorHandler),
        m_epochClock(std::move(epochClock)),
        m_driverTimeoutMs(driverTimeoutMs),
        m_resourceLingerTimeoutMs(resourceLingerTimeoutMs),
        m_interServiceTimeoutMs(static_cast<long>(interServiceTimeoutNs / 1000000)),
        m_preTouchMappedMemory(preTouchMappedMemory),
        m_driverActive(true),
        m_isClosed(false),
        m_timeOfLastDoWorkMs(m_epochClock()),
        m_timeOfLastKeepaliveMs(m_epochClock()),
        m_timeOfLastCheckManagedResourcesMs(m_epochClock()),
        m_padding()
    {
        static_cast<void>(m_padding);
<commit_msg>[C++] Reduce the number of clock calls when constructing ClientConductor.<commit_after>/*
 * Copyright 2014-2020 Real Logic Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef AERON_CLIENT_CONDUCTOR_H
#define AERON_CLIENT_CONDUCTOR_H

#include <unordered_map>
#include <vector>
#include <mutex>
#include <concurrent/logbuffer/TermReader.h>
#include <concurrent/status/UnsafeBufferPosition.h>
#include <util/LangUtil.h>
#include <util/ScopeUtils.h>
#include "Publication.h"
#include "ExclusivePublication.h"
#include "Subscription.h"
#include "Counter.h"
#include "DriverProxy.h"
#include "Context.h"
#include "DriverListenerAdapter.h"
#include "LogBuffers.h"
#include "HeartbeatTimestamp.h"
#include "util/Export.h"

namespace aeron {

using namespace aeron::concurrent::logbuffer;
using namespace aeron::concurrent::status;
using namespace aeron::concurrent;
using namespace aeron::util;

typedef std::function<long long()> epoch_clock_t;
typedef std::function<long long()> nano_clock_t;

static const long KEEPALIVE_TIMEOUT_MS = 500;
static const long RESOURCE_TIMEOUT_MS = 1000;

class CLIENT_EXPORT ClientConductor
{
public:

    ClientConductor(
        epoch_clock_t epochClock,
        DriverProxy& driverProxy,
        CopyBroadcastReceiver& broadcastReceiver,
        AtomicBuffer& counterMetadataBuffer,
        AtomicBuffer& counterValuesBuffer,
        const on_new_publication_t& newPublicationHandler,
        const on_new_publication_t& newExclusivePublicationHandler,
        const on_new_subscription_t& newSubscriptionHandler,
        const exception_handler_t& errorHandler,
        const on_available_counter_t& availableCounterHandler,
        const on_unavailable_counter_t& unavailableCounterHandler,
        const on_close_client_t& onCloseClientHandler,
        long driverTimeoutMs,
        long resourceLingerTimeoutMs,
        long long interServiceTimeoutNs,
        bool preTouchMappedMemory) :
        m_driverProxy(driverProxy),
        m_driverListenerAdapter(broadcastReceiver, *this),
        m_countersReader(counterMetadataBuffer, counterValuesBuffer),
        m_counterValuesBuffer(counterValuesBuffer),
        m_onNewPublicationHandler(newPublicationHandler),
        m_onNewExclusivePublicationHandler(newExclusivePublicationHandler),
        m_onNewSubscriptionHandler(newSubscriptionHandler),
        m_errorHandler(errorHandler),
        m_epochClock(std::move(epochClock)),
        m_driverTimeoutMs(driverTimeoutMs),
        m_resourceLingerTimeoutMs(resourceLingerTimeoutMs),
        m_interServiceTimeoutMs(static_cast<long>(interServiceTimeoutNs / 1000000)),
        m_preTouchMappedMemory(preTouchMappedMemory),
        m_driverActive(true),
        m_isClosed(false),
        m_timeOfLastDoWorkMs(m_epochClock()),
        m_timeOfLastKeepaliveMs(m_timeOfLastDoWorkMs),
        m_timeOfLastCheckManagedResourcesMs(m_timeOfLastDoWorkMs),
        m_padding()
    {
        static_cast<void>(m_padding);

{'fraction_non_alphanumeric': 0.07747093023255813, 'fraction_numerical': 0.007558139534883721, 'mean_word_length': 4.691480562448304, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '4321833', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1941, 'n_words': 453}