2019-12-20 07:24:38 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-07-10 22:38:20 +00:00
|
|
|
|
2019-12-20 07:24:38 +00:00
|
|
|
get online peers in a communicator instance
|
2019-12-18 10:08:33 +00:00
|
|
|
"""
|
|
|
|
import time
|
2019-12-20 07:24:38 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2020-07-31 01:15:36 +00:00
|
|
|
import config
|
2020-07-29 08:57:06 +00:00
|
|
|
from etc.humanreadabletime import human_readable_time
|
2020-07-31 01:15:36 +00:00
|
|
|
from communicatorutils.connectnewpeers import connect_new_peer_to_communicator
|
2019-12-18 10:08:33 +00:00
|
|
|
import logger
|
2019-12-20 07:24:38 +00:00
|
|
|
if TYPE_CHECKING:
|
2020-07-26 03:28:32 +00:00
|
|
|
from deadsimplekv import DeadSimpleKV
|
2019-12-18 10:08:33 +00:00
|
|
|
"""
|
2019-07-10 22:38:20 +00:00
|
|
|
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 <https://www.gnu.org/licenses/>.
|
2019-12-18 10:08:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-07-31 01:15:36 +00:00
|
|
|
def get_online_peers(shared_state):
|
2020-07-26 03:28:32 +00:00
|
|
|
"""Manage the kv.get('onlinePeers') attribute list.
|
2019-12-20 07:24:38 +00:00
|
|
|
|
|
|
|
Connect to more peers if we have none connected
|
2019-12-18 10:08:33 +00:00
|
|
|
"""
|
2020-07-31 01:15:36 +00:00
|
|
|
kv: "DeadSimpleKV" = shared_state.get_by_string("DeadSimpleKV")
|
2019-12-18 10:09:37 +00:00
|
|
|
if config.get('general.offline_mode', False):
|
|
|
|
return
|
2020-07-31 01:15:36 +00:00
|
|
|
logger.info('Refreshing peer pool...')
|
2019-12-20 07:24:38 +00:00
|
|
|
max_peers = int(config.get('peers.max_connect', 10))
|
2020-07-26 03:28:32 +00:00
|
|
|
needed = max_peers - len(kv.get('onlinePeers'))
|
2019-07-10 22:38:20 +00:00
|
|
|
|
2019-12-18 10:10:00 +00:00
|
|
|
last_seen = 'never'
|
2020-07-29 08:57:06 +00:00
|
|
|
if not isinstance(kv.get('lastNodeSeen'), type(None)):
|
|
|
|
last_seen = human_readable_time(kv.get('lastNodeSeen'))
|
2019-12-18 10:10:00 +00:00
|
|
|
|
2019-12-20 07:24:38 +00:00
|
|
|
for _ in range(needed):
|
2020-07-26 03:28:32 +00:00
|
|
|
if len(kv.get('onlinePeers')) == 0:
|
2020-07-31 01:15:36 +00:00
|
|
|
connect_new_peer_to_communicator(shared_state, useBootstrap=True)
|
2019-07-10 22:38:20 +00:00
|
|
|
else:
|
2020-07-31 01:15:36 +00:00
|
|
|
connect_new_peer_to_communicator(shared_state)
|
2019-07-10 22:38:20 +00:00
|
|
|
|
2020-07-26 02:36:48 +00:00
|
|
|
if kv.get('shutdown'):
|
2019-07-10 22:38:20 +00:00
|
|
|
break
|
|
|
|
else:
|
2020-07-26 03:28:32 +00:00
|
|
|
if len(kv.get('onlinePeers')) == 0:
|
2019-12-20 07:24:38 +00:00
|
|
|
logger.debug('Couldn\'t connect to any peers.' +
|
2019-12-20 08:34:52 +00:00
|
|
|
f' Last node seen {last_seen} ago.')
|
2020-06-28 09:17:45 +00:00
|
|
|
try:
|
2020-08-04 13:44:24 +00:00
|
|
|
get_online_peers(shared_state)
|
2020-06-28 09:17:45 +00:00
|
|
|
except RecursionError:
|
|
|
|
pass
|
2019-07-10 22:38:20 +00:00
|
|
|
else:
|
2020-07-29 08:57:06 +00:00
|
|
|
kv.put('lastNodeSeen', time.time())
|