# Path: bts_tools/monitoring/backbone.py
# def reconnect_backbone(node, ctx):
#     # try to connect to backbone nodes to which we are not currently connected
#     period = WAIT_RECONNECT // core.config['monitoring']['monitor_time_interval']
#     old_node_connect_age = ctx.get('node_connect_age', {})
#     ctx.node_connect_age = {}
# 
#     for n in non_connected_node_list(node):
#         ctx.node_connect_age[n] = old_node_connect_age.get(n, -1) + 1
#         # implement rate limiting to avoid hammering the server in case it's down and could
#         # have a hard time coming back up if all delegates try connecting like crazy
#         if ctx.node_connect_age[n] % period == 0:
#             log.debug('Trying to reconnect to backbone node: %s' % n)
#             node.network_add_node(n, 'add')
# 
# Path: bts_tools/backbone.py
# def node_list(node):
#     backbone_nodes = {(n.split(':')[0], int(n.split(':')[1]))
#                       for n in core.config.get('backbone', [])}
#     if not backbone_nodes:
#         log.warning('No backbone nodes configured. Cannot reconnect to backbone...')
#         return set()
#     # resolve dns names
#     try:
#         backbone_nodes = {(resolve_dns(host), port) for host, port in backbone_nodes}
#     except Exception:
#         # if we can't resolve names, we're probably not connected to the internet
#         # FIXME: if DNS is down but we're still connected to the internet, we still
#         #        want to connect to those nodes for which we already have the ip addr
#         log.warning('Cannot resolve IP addresses for backbone nodes...')
#         return set()
#     backbone_nodes = {'%s:%d' % (host, port) for host, port in backbone_nodes}
#     # need to exclude the calling node from the list
#     with suppress(Exception):
#         backbone_nodes -= {'%s:%d' % (get_ip(), get_p2p_port(node))}
#     return backbone_nodes


from .backbone import reconnect_backbone
from ..backbone import node_list
import logging
#
# bts_tools - Tools to easily manage the bitshares client
# Copyright (c) 2015 Nicolas Wack <wackou@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


log = logging.getLogger(__name__)


def is_valid_node(node):
    return node.is_witness()


def monitor(node, ctx, cfg):
    # TODO: set list of allowed peers to be only backbone nodes

    # try to connect to backbone nodes to which we are not currently connected

# Path: bts_tools/monitoring/backbone.py
# def reconnect_backbone(node, ctx):
#     # try to connect to backbone nodes to which we are not currently connected
#     period = WAIT_RECONNECT // core.config['monitoring']['monitor_time_interval']
#     old_node_connect_age = ctx.get('node_connect_age', {})
#     ctx.node_connect_age = {}
# 
#     for n in non_connected_node_list(node):
#         ctx.node_connect_age[n] = old_node_connect_age.get(n, -1) + 1
#         # implement rate limiting to avoid hammering the server in case it's down and could
#         # have a hard time coming back up if all delegates try connecting like crazy
#         if ctx.node_connect_age[n] % period == 0:
#             log.debug('Trying to reconnect to backbone node: %s' % n)
#             node.network_add_node(n, 'add')
# 
# Path: bts_tools/backbone.py
# def node_list(node):
#     backbone_nodes = {(n.split(':')[0], int(n.split(':')[1]))
#                       for n in core.config.get('backbone', [])}
#     if not backbone_nodes:
#         log.warning('No backbone nodes configured. Cannot reconnect to backbone...')
#         return set()
#     # resolve dns names
#     try:
#         backbone_nodes = {(resolve_dns(host), port) for host, port in backbone_nodes}
#     except Exception:
#         # if we can't resolve names, we're probably not connected to the internet
#         # FIXME: if DNS is down but we're still connected to the internet, we still
#         #        want to connect to those nodes for which we already have the ip addr
#         log.warning('Cannot resolve IP addresses for backbone nodes...')
#         return set()
#     backbone_nodes = {'%s:%d' % (host, port) for host, port in backbone_nodes}
#     # need to exclude the calling node from the list
#     with suppress(Exception):
#         backbone_nodes -= {'%s:%d' % (get_ip(), get_p2p_port(node))}
#     return backbone_nodes


# Path: bts_tools/monitoring/prefer_backbone_exclusively.py
from .backbone import reconnect_backbone
from ..backbone import node_list
import logging

#
# bts_tools - Tools to easily manage the bitshares client
# Copyright (c) 2015 Nicolas Wack <wackou@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


log = logging.getLogger(__name__)


def is_valid_node(node):
    return node.is_witness()


def monitor(node, ctx, cfg):
    # TODO: set list of allowed peers to be only backbone nodes

    # try to connect to backbone nodes to which we are not currently connected

    reconnect_backbone(node, ctx)