Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

Commit

Permalink
merge upstream develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsha256 committed Aug 23, 2014
2 parents 5227808 + 879476d commit 987d551
Show file tree
Hide file tree
Showing 16 changed files with 871 additions and 86 deletions.
1 change: 1 addition & 0 deletions libraries/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ file(GLOB HEADERS "include/bts/client/*.hpp")
add_library( bts_client
client.cpp
messages.cpp
notifier.cpp
${HEADERS} )

target_link_libraries( bts_client
Expand Down
102 changes: 93 additions & 9 deletions libraries/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <bts/client/client.hpp>
#include <bts/client/messages.hpp>
#include <bts/client/notifier.hpp>
#include <bts/cli/cli.hpp>
#include <bts/net/node.hpp>
#include <bts/net/exceptions.hpp>
Expand Down Expand Up @@ -123,6 +124,7 @@ program_options::variables_map parse_option_variables(int argc, char** argv)
"fresh copy of the entire blockchain from the network")

("p2p-port", program_options::value<uint16_t>(), "Set network port to listen on")
("accept-incoming-connections", program_options::value<bool>()->default_value(true), "Set to false to reject incoming p2p connections and only establish outbound connections")
("upnp", program_options::value<bool>()->default_value(true), "Enable UPNP")

("max-connections", program_options::value<uint16_t>(),
Expand All @@ -149,6 +151,8 @@ program_options::variables_map parse_option_variables(int argc, char** argv)

("input-log", program_options::value< vector<string> >(), "Set log file with CLI commands to execute at startup")
("log-commands", "Log all command input and output")
("growl", program_options::value<std::string>()->implicit_value("127.0.0.1"), "Send notifications about potential problems to Growl")
("growl-password", program_options::value<std::string>(), "Password for authenticating to a Growl server")
;

program_options::variables_map option_variables;
Expand Down Expand Up @@ -555,14 +559,24 @@ config load_config( const fc::path& datadir )

virtual ~client_impl() override
{
cancel_blocks_too_old_monitor_task();
cancel_rebroadcast_pending_loop();
_p2p_node.reset();
delete _cli;
}

void start()
{
try
{
_cli->start();
}
catch (...)
{
if (_notifier)
_notifier->notify_client_exiting_unexpectedly();
throw;
}
}

void reschedule_delegate_loop();
Expand All @@ -586,6 +600,8 @@ config load_config( const fc::path& datadir )
bool sync_mode);

bool on_new_transaction(const signed_transaction& trx);
void blocks_too_old_monitor_task();
void cancel_blocks_too_old_monitor_task();

/* Implement node_delegate */
// @{
Expand Down Expand Up @@ -654,6 +670,9 @@ config load_config( const fc::path& datadir )

rpc_server_config _tmp_rpc_config;
bts::net::node_ptr _p2p_node = nullptr;
bts_gntp_notifier_ptr _notifier;
fc::future<void> _blocks_too_old_monitor_done;


//-------------------------------------------------- JSON-RPC Method Implementations
// include all of the method overrides generated by the bts_api_generator
Expand Down Expand Up @@ -1429,11 +1448,13 @@ config load_config( const fc::path& datadir )
}
}

void client_impl::connection_count_changed(uint32_t c)
void client_impl::connection_count_changed(uint32_t new_connection_count)
{
std::ostringstream message;
message << "--- there are now " << c << " active connections to the p2p network";
message << "--- there are now " << new_connection_count << " active connections to the p2p network";
ulog( message.str() );
if (_notifier)
_notifier->notify_connection_count_changed(new_connection_count);
}

uint32_t client_impl::get_block_number(const bts::net::item_hash_t& block_id)
Expand Down Expand Up @@ -1478,6 +1499,35 @@ config load_config( const fc::path& datadir )
ulog( message );
}

void client_impl::blocks_too_old_monitor_task()
{
// if we have no connections, don't warn about the head block too old --
// we should already be warning about no connections
// if we're syncing, don't warn, we wouldn't be syncing if the head block weren't old
if (_chain_db->get_head_block().timestamp < bts::blockchain::now() - fc::seconds(BTS_BLOCKCHAIN_BLOCK_INTERVAL_SEC * 2) &&
!_sync_mode &&
_p2p_node->get_connection_count() > 0 &&
_notifier)
_notifier->notify_head_block_too_old(_chain_db->get_head_block().timestamp);

if (!_blocks_too_old_monitor_done.canceled())
_blocks_too_old_monitor_done = fc::schedule([=]() { blocks_too_old_monitor_task(); },
fc::time_point::now() + fc::seconds(BTS_BLOCKCHAIN_BLOCK_INTERVAL_SEC),
"block_monitor_task");
}

void client_impl::cancel_blocks_too_old_monitor_task()
{
try
{
_blocks_too_old_monitor_done.cancel_and_wait();
}
catch( const fc::exception& e )
{
wlog( "Unexpected exception thrown while canceling blocks_too_old_monitor(): ${e}", ("e",e.to_detail_string() ) );
}
}

} // end namespace detail

client::client()
Expand Down Expand Up @@ -1580,6 +1630,8 @@ config load_config( const fc::path& datadir )

client::~client()
{
if (my->_notifier)
my->_notifier->client_is_shutting_down();
my->cancel_delegate_loop();
}

Expand Down Expand Up @@ -2305,6 +2357,8 @@ config load_config( const fc::path& datadir )
uint16_t p2pport = option_variables["p2p-port"].as<uint16_t>();
listen_on_port(p2pport, option_variables.count("p2p-port") != 0);
}
accept_incoming_p2p_connections(option_variables["accept-incoming-connections"].as<bool>());

// else we use the default set in bts::net::node

//initialize cli
Expand Down Expand Up @@ -2376,6 +2430,25 @@ config load_config( const fc::path& datadir )
get_node()->clear_peer_database();
}

if (option_variables.count("growl"))
{
std::string host_to_notify = option_variables["growl"].as<std::string>();
uint16_t port_to_notify = 23053;
std::string::size_type colon_pos = host_to_notify.find(':');
if (colon_pos != std::string::npos)
{
port_to_notify = boost::lexical_cast<uint16_t>(host_to_notify.substr(colon_pos + 1));
host_to_notify = host_to_notify.substr(0, colon_pos);
}
fc::optional<std::string> growl_password;
if (option_variables.count("growl-password"))
growl_password = option_variables["growl-password"].as<std::string>();
my->_notifier = std::make_shared<bts_gntp_notifier>(host_to_notify, port_to_notify, growl_password);
my->_blocks_too_old_monitor_done = fc::schedule([=]() { my->blocks_too_old_monitor_task(); },
fc::time_point::now() + fc::seconds(BTS_BLOCKCHAIN_BLOCK_INTERVAL_SEC),
"block_monitor_task");
}

start_networking([=]{
fc::ip::endpoint actual_p2p_endpoint = this->get_p2p_listening_endpoint();
std::ostringstream port_stream;
Expand All @@ -2385,13 +2458,19 @@ config load_config( const fc::path& datadir )
port_stream << (string)actual_p2p_endpoint;

if( option_variables.count("log-commands") <= 0) /* Was breaking regression tests */
ulog("Listening for P2P connections on ${port}",("port",port_stream.str()));

if (option_variables.count("p2p-port"))
{
uint16_t p2p_port = option_variables["p2p-port"].as<uint16_t>();
if (p2p_port != 0 && p2p_port != actual_p2p_endpoint.port())
ulog(" (unable to bind to the desired port ${p2p_port} )", ("p2p_port",p2p_port));
if (option_variables["accept-incoming-connections"].as<bool>())
{
ulog("Listening for P2P connections on ${port}",("port",port_stream.str()));
if (option_variables.count("p2p-port"))
{
uint16_t p2p_port = option_variables["p2p-port"].as<uint16_t>();
if (p2p_port != 0 && p2p_port != actual_p2p_endpoint.port())
ulog(" (unable to bind to the desired port ${p2p_port} )", ("p2p_port",p2p_port));
}
}
else
ulog("Not accepting incoming P2P connections");
}

if (option_variables.count("connect-to"))
Expand Down Expand Up @@ -2433,7 +2512,12 @@ config load_config( const fc::path& datadir )

void client::listen_on_port(uint16_t port_to_listen, bool wait_if_not_available)
{
my->_p2p_node->listen_on_port(port_to_listen, wait_if_not_available);
my->_p2p_node->listen_on_port(port_to_listen, wait_if_not_available);
}

void client::accept_incoming_p2p_connections(bool accept)
{
my->_p2p_node->accept_incoming_connections(accept);
}

const config& client::configure( const fc::path& configuration_directory )
Expand Down
1 change: 1 addition & 0 deletions libraries/client/include/bts/client/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace bts { namespace client {

// functions for taking command-line parameters and passing them on to the p2p node
void listen_on_port( uint16_t port_to_listen, bool wait_if_not_available);
void accept_incoming_p2p_connections(bool accept);
void listen_to_p2p_network();
void connect_to_peer( const string& remote_endpoint );
void connect_to_p2p_network();
Expand Down
28 changes: 28 additions & 0 deletions libraries/client/include/bts/client/notifier.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <memory>
#include <string>
#include <stdint.h>

#include <fc/time.hpp>

namespace bts { namespace client {
namespace detail {
class bts_gntp_notifier_impl;
}

class bts_gntp_notifier {
public:
bts_gntp_notifier(const std::string& host_to_notify = "127.0.0.1", uint16_t port = 23053,
const fc::optional<std::string>& password = fc::optional<std::string>());
~bts_gntp_notifier();

void client_is_shutting_down();
void notify_connection_count_changed(uint32_t new_connection_count);
void notify_client_exiting_unexpectedly();
void notify_head_block_too_old(const fc::time_point_sec& head_block_age);
private:
std::unique_ptr<detail::bts_gntp_notifier_impl> my;
};
typedef std::shared_ptr<bts_gntp_notifier> bts_gntp_notifier_ptr;

} } // end namespace bts::client
Loading

0 comments on commit 987d551

Please sign in to comment.