2019-12-18 10:08:33 +00:00
|
|
|
"""
|
2019-07-10 22:38:20 +00:00
|
|
|
Onionr - Private P2P Communication
|
|
|
|
|
|
|
|
get online peers in a communicator instance
|
2019-12-18 10:08:33 +00:00
|
|
|
"""
|
|
|
|
import time
|
|
|
|
from etc import humanreadabletime
|
|
|
|
import logger
|
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-07-10 22:38:20 +00:00
|
|
|
def get_online_peers(comm_inst):
|
2019-12-18 10:08:33 +00:00
|
|
|
"""
|
|
|
|
Manages the comm_inst.onlinePeers attribute list,
|
|
|
|
]connects to more peers if we have none connected
|
|
|
|
"""
|
2019-07-18 17:40:48 +00:00
|
|
|
config = comm_inst.config
|
2019-12-18 10:09:37 +00:00
|
|
|
if config.get('general.offline_mode', False):
|
|
|
|
comm_inst.decrementThreadCount('get_online_peers')
|
|
|
|
return
|
2019-07-10 22:38:20 +00:00
|
|
|
logger.debug('Refreshing peer pool...')
|
|
|
|
maxPeers = int(config.get('peers.max_connect', 10))
|
|
|
|
needed = maxPeers - len(comm_inst.onlinePeers)
|
|
|
|
|
2019-12-18 10:10:00 +00:00
|
|
|
last_seen = 'never'
|
2019-12-18 10:13:38 +00:00
|
|
|
if not isinstance(comm_inst.lastNodeSeen, type(None)):
|
2019-12-18 10:10:00 +00:00
|
|
|
last_seen = humanreadabletime.human_readable_time(
|
|
|
|
comm_inst.lastNodeSeen)
|
|
|
|
|
2019-07-10 22:38:20 +00:00
|
|
|
for i in range(needed):
|
|
|
|
if len(comm_inst.onlinePeers) == 0:
|
|
|
|
comm_inst.connectNewPeer(useBootstrap=True)
|
|
|
|
else:
|
|
|
|
comm_inst.connectNewPeer()
|
|
|
|
|
|
|
|
if comm_inst.shutdown:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if len(comm_inst.onlinePeers) == 0:
|
2019-12-18 10:10:00 +00:00
|
|
|
logger.debug
|
|
|
|
('Couldn\'t connect to any peers.' +
|
|
|
|
f' Last node seen {last_seen} ago.')
|
2019-07-10 22:38:20 +00:00
|
|
|
else:
|
|
|
|
comm_inst.lastNodeSeen = time.time()
|
2019-12-18 10:10:00 +00:00
|
|
|
comm_inst.decrementThreadCount('get_online_peers')
|