<commit_before>/**
 * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS)
 *
 * This file is part of libbitcoin-server.
 *
 * libbitcoin-server is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License with
 * additional permissions to the one published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * any later version. For more information see LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include <bitcoin/server/message/receiver.hpp>

#include <cstdint>
#include <vector>
#include <boost/date_time.hpp>
#include <czmq++/czmqpp.hpp>
#include <bitcoin/node.hpp>
#include <bitcoin/server/configuration.hpp>
#include <bitcoin/server/message/incoming.hpp>
#include <bitcoin/server/message/outgoing.hpp>
#include <bitcoin/server/server_node.hpp>
#include <bitcoin/server/settings.hpp>

namespace libbitcoin {
namespace server {

using std::placeholders::_1;
using namespace bc::config;

// TODO: should we be testing zmq_fail in each bind call?
static constexpr int zmq_true = 1;
static constexpr int zmq_false = 0;
static constexpr int zmq_fail = -1;
static constexpr int zmq_curve_enabled = zmq_true;
static constexpr int zmq_socket_no_linger = zmq_false;

const auto now = []()
{
    return boost::posix_time::second_clock::universal_time();
};

receiver::receiver(server_node::ptr node)
  : counter_(0),
    sender_(context_),
    settings_(node->server_settings()),
    socket_(context_, ZMQ_ROUTER),
    wakeup_socket_(context_, ZMQ_PULL),
    heartbeat_socket_(context_, ZMQ_PUB),
    authenticate_(context_)
{
    BITCOIN_ASSERT(socket_.self() != nullptr);
    BITCOIN_ASSERT(wakeup_socket_.self() != nullptr);
    BITCOIN_ASSERT(heartbeat_socket_.self() != nullptr);
}

bool receiver::start()
{
    // The receiver is only enabled for query and notification.
    if (!settings_.queries_enabled && settings_.subscription_limit == 0)
        return true;

#ifndef _MSC_VER
    // Hack to prevent czmq from writing to stdout/stderr on Windows.
    // This will prevent authentication feedback, but also prevent crashes.
    // It is necessary to prevent stdio when using our utf8-everywhere pattern.
    // TODO: modify czmq to not hardcode stdout/stderr for verbose output.
    if (log_requests_)
        authenticate_.set_verbose(true);
#endif

    if (!settings_.whitelists.empty())
        whitelist();

    if (settings_.certificate_file.empty())
        socket_.set_zap_domain("global");

    if (!enable_crypto())
    {
        log::error(LOG_SERVICE)
            << "Invalid server certificate.";
        return false;
    }

    // This binds the request queue.
    // Returns 0 if OK, -1 if the endpoint was invalid.
    auto rc = wakeup_socket_.bind("inproc://trigger-send");

    if (rc == zmq_fail)
    {
        log::error(LOG_SERVICE)
            << "Failed to connect to request queue.";
        return false;<commit_msg>Fix conditionally compiled verbose log setting.<commit_after>/**
 * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS)
 *
 * This file is part of libbitcoin-server.
 *
 * libbitcoin-server is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License with
 * additional permissions to the one published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * any later version. For more information see LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include <bitcoin/server/message/receiver.hpp>

#include <cstdint>
#include <vector>
#include <boost/date_time.hpp>
#include <czmq++/czmqpp.hpp>
#include <bitcoin/node.hpp>
#include <bitcoin/server/configuration.hpp>
#include <bitcoin/server/message/incoming.hpp>
#include <bitcoin/server/message/outgoing.hpp>
#include <bitcoin/server/server_node.hpp>
#include <bitcoin/server/settings.hpp>

namespace libbitcoin {
namespace server {

using std::placeholders::_1;
using namespace bc::config;

// TODO: should we be testing zmq_fail in each bind call?
static constexpr int zmq_true = 1;
static constexpr int zmq_false = 0;
static constexpr int zmq_fail = -1;
static constexpr int zmq_curve_enabled = zmq_true;
static constexpr int zmq_socket_no_linger = zmq_false;

const auto now = []()
{
    return boost::posix_time::second_clock::universal_time();
};

receiver::receiver(server_node::ptr node)
  : counter_(0),
    sender_(context_),
    settings_(node->server_settings()),
    socket_(context_, ZMQ_ROUTER),
    wakeup_socket_(context_, ZMQ_PULL),
    heartbeat_socket_(context_, ZMQ_PUB),
    authenticate_(context_)
{
    BITCOIN_ASSERT(socket_.self() != nullptr);
    BITCOIN_ASSERT(wakeup_socket_.self() != nullptr);
    BITCOIN_ASSERT(heartbeat_socket_.self() != nullptr);
}

bool receiver::start()
{
    // The receiver is only enabled for query and notification.
    if (!settings_.queries_enabled && settings_.subscription_limit == 0)
        return true;

#ifndef _MSC_VER
    // Hack to prevent czmq from writing to stdout/stderr on Windows.
    // This will prevent authentication feedback, but also prevent crashes.
    // It is necessary to prevent stdio when using our utf8-everywhere pattern.
    // TODO: modify czmq to not hardcode stdout/stderr for verbose output.
    if (settings_.log_requests)
        authenticate_.set_verbose(true);
#endif

    if (!settings_.whitelists.empty())
        whitelist();

    if (settings_.certificate_file.empty())
        socket_.set_zap_domain("global");

    if (!enable_crypto())
    {
        log::error(LOG_SERVICE)
            << "Invalid server certificate.";
        return false;
    }

    // This binds the request queue.
    // Returns 0 if OK, -1 if the endpoint was invalid.
    auto rc = wakeup_socket_.bind("inproc://trigger-send");

    if (rc == zmq_fail)
    {
        log::error(LOG_SERVICE)
            << "Failed to connect to request queue.";
        return false;
{'fraction_non_alphanumeric': 0.10150768771458427, 'fraction_numerical': 0.005373936408419167, 'mean_word_length': 3.799426934097421, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '7626098', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1935, 'n_words': 646}