<commit_before>/**
 * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS)
 * Copyright (c) 2016-2018 metaverse core developers (see MVS-AUTHORS)
 *
 * This file is part of metaverse.
 *
 * metaverse 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 <metaverse/network/protocols/protocol_address.hpp>

#include <functional>
#include <metaverse/bitcoin.hpp>
#include <metaverse/network/channel.hpp>
#include <metaverse/network/p2p.hpp>
#include <metaverse/network/protocols/protocol.hpp>
#include <metaverse/network/protocols/protocol_events.hpp>

namespace libbitcoin {
namespace network {

#define NAME "address"
#define CLASS protocol_address

using namespace bc::message;
using namespace std::placeholders;

protocol_address::protocol_address(p2p& network, channel::ptr channel)
  : protocol_events(network, channel, NAME),
    network_(network),
    CONSTRUCT_TRACK(protocol_address)
{
}

protocol_address::ptr protocol_address::do_subscribe()
{
    SUBSCRIBE2(address, handle_receive_address, _1, _2);
    SUBSCRIBE2(get_address, handle_receive_get_address, _1, _2);
    // Must have a handler to capture a shared self pointer in stop subscriber.
	protocol_events::start(BIND1(handle_stop, _1));
    return std::dynamic_pointer_cast<protocol_address>(protocol::shared_from_this());
}

// Start sequence.
// ----------------------------------------------------------------------------

void protocol_address::start()
{
    const auto& settings = network_.network_settings();

    if (settings.self.port() != 0)
    {
		network_address nt_address=settings.self.to_network_address();

		//for testnet don't filter local ip
		if (settings.hosts_file == "hosts-test.cache") {
			self_ = address({ { nt_address } });
			SEND2(self_, handle_send, _1, self_.command);
		}
		//only outer address can be broadcast
		else if (nt_address.is_routable()) {
			self_ = address({ { nt_address } });
			SEND2(self_, handle_send, _1, self_.command);
		}
        
    }

#ifdef USE_UPNP
	if (settings.self != network_.get_out_address()) {
		network_address nt_address = network_.get_out_address().to_network_address();
		if (settings.hosts_file == "hosts.cache") {
			address self = address({ { nt_address } });
			log::info("UPnP") << "send by host judge addresss " << network_.get_out_address().to_string();
			SEND2(self, handle_send, _1, self.command);
		}
		else if (nt_address.is_routable()) {
			address self = address({ { nt_address } });
			log::info("UPnP") << "send by routable addresss " << network_.get_out_address().to_string();
			SEND2(self, handle_send, _1, self.command);
		}
	}
#endif
	

    // If we can't store addresses we don't ask for or handle them.<commit_msg>'测试代码'<commit_after>/**
 * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS)
 * Copyright (c) 2016-2018 metaverse core developers (see MVS-AUTHORS)
 *
 * This file is part of metaverse.
 *
 * metaverse 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 <metaverse/network/protocols/protocol_address.hpp>

#include <functional>
#include <metaverse/bitcoin.hpp>
#include <metaverse/network/channel.hpp>
#include <metaverse/network/p2p.hpp>
#include <metaverse/network/protocols/protocol.hpp>
#include <metaverse/network/protocols/protocol_events.hpp>

namespace libbitcoin {
namespace network {

#define NAME "address"
#define CLASS protocol_address

using namespace bc::message;
using namespace std::placeholders;

protocol_address::protocol_address(p2p& network, channel::ptr channel)
  : protocol_events(network, channel, NAME),
    network_(network),
    CONSTRUCT_TRACK(protocol_address)
{
}

protocol_address::ptr protocol_address::do_subscribe()
{
    SUBSCRIBE2(address, handle_receive_address, _1, _2);
    SUBSCRIBE2(get_address, handle_receive_get_address, _1, _2);
    // Must have a handler to capture a shared self pointer in stop subscriber.
	protocol_events::start(BIND1(handle_stop, _1));
    return std::dynamic_pointer_cast<protocol_address>(protocol::shared_from_this());
}

// Start sequence.
// ----------------------------------------------------------------------------

void protocol_address::start()
{
    const auto& settings = network_.network_settings();

    if (settings.self.port() != 0)
    {
		network_address nt_address=settings.self.to_network_address();

		//for testnet don't filter local ip
		if (settings.hosts_file == "hosts-test.cache") {
			self_ = address({ { nt_address } });
			SEND2(self_, handle_send, _1, self_.command);
		}
		//only outer address can be broadcast
		else if (nt_address.is_private_network()) {
			self_ = address({ { nt_address } });
			SEND2(self_, handle_send, _1, self_.command);
		}
        
    }

#ifdef USE_UPNP
	if (settings.self != network_.get_out_address()) {
		network_address nt_address = network_.get_out_address().to_network_address();
		if (settings.hosts_file == "hosts.cache") {
			address self = address({ { nt_address } });
			log::info("UPnP") << "send by host judge addresss " << network_.get_out_address().to_string();
			SEND2(self, handle_send, _1, self.command);
		}
		else if (nt_address.is_private_network()) {
			address self = address({ { nt_address } });
			log::info("UPnP") << "send by judge not private addresss " << network_.get_out_address().to_string();
			SEND2(self, handle_send, _1, self.command);
		}
	}
#endif
	

    // If we can't store addresses we don't ask for or handle them.
{'fraction_non_alphanumeric': 0.13341239252890602, 'fraction_numerical': 0.010672991402312482, 'mean_word_length': 4.01635687732342, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 21, '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': '7241138', 'n_tokens_mistral': 2135, 'n_tokens_neox': 1990, 'n_words': 601}