3
0
mirror of https://github.com/jlu5/PyLink.git synced 2025-01-25 19:54:25 +01:00

servermaps: implement network maps over relay!

Local-only map is available too using the 'localmap' command.
This commit is contained in:
James Lu 2016-09-24 11:54:53 -07:00
parent 68c247f764
commit 595bceda2e

View File

@ -9,7 +9,7 @@ import collections
def _percent(num, total): def _percent(num, total):
return '%.1f' % (num/total*100) return '%.1f' % (num/total*100)
def _map(irc, source, args): def _map(irc, source, args, show_relay=True):
"""[<network>] """[<network>]
Shows the network map for the given network, or the current network if not specified.""" Shows the network map for the given network, or the current network if not specified."""
@ -31,23 +31,28 @@ def _map(irc, source, args):
hostsid = ircobj.sid hostsid = ircobj.sid
usercount = len(ircobj.users) usercount = len(ircobj.users)
# Iterate over every connected server. # Iterate over every connected server on every network.
serverlist = ircobj.servers.copy() for remotenet, remoteirc in world.networkobjects.items():
for sid, serverobj in serverlist.items(): for sid, serverobj in remoteirc.servers.copy().items():
# Save the server as UNDER its uplink. if sid == remoteirc.sid: # Don't re-add our own SID to the index
if sid == hostsid: continue
continue
servers[(netname, serverobj.uplink or ircobj.sid)].add(sid) # Save the server as UNDER its uplink.
servers[(remotenet, serverobj.uplink or remoteirc.sid)].add(sid)
log.debug('(%s) servermaps.map servers fetched for %s: %s', irc.name, netname, servers) log.debug('(%s) servermaps.map servers fetched for %s: %s', irc.name, netname, servers)
reply = lambda text: irc.reply(text, private=True) reply = lambda text: irc.reply(text, private=True)
def showall(sid, hops=0): def showall(ircobj, sid, hops=0, is_relay_server=False):
log.debug('servermaps: got showall() for SID %s on network %s', sid, ircobj.name)
serverlist = ircobj.servers.copy()
netname = ircobj.name
if hops == 0: if hops == 0:
# Show our root server once. # Show our root server once.
rootusers = len(serverlist[sid].users) rootusers = len(serverlist[sid].users)
reply('%s[%s]: %s user(s) (%s%%)' % (serverlist[sid].name, sid, reply('\x02%s\x02[%s]: %s user(s) (%s%%)' % (serverlist[sid].name, sid,
rootusers, _percent(rootusers, usercount))) rootusers, _percent(rootusers, usercount)))
log.debug('(%s) servermaps: servers under sid %s: %s', irc.name, sid, servers) log.debug('(%s) servermaps: servers under sid %s: %s', irc.name, sid, servers)
@ -56,22 +61,39 @@ def _map(irc, source, args):
hops += 1 hops += 1
leaves = servers[(netname, sid)] leaves = servers[(netname, sid)]
for leafcount, leaf in enumerate(leaves): for leafcount, leaf in enumerate(leaves):
if is_relay_server and hasattr(serverlist[leaf], 'remote'):
# Don't show relay subservers more than once.
continue
# If we reach the end of a server's map, display `- instead of |- for prettier output. # If we reach the end of a server's map, display `- instead of |- for prettier output.
linechar = '`-' if leafcount == len(leaves)-1 else '|-' linechar = '`-' if leafcount == len(leaves)-1 else '|-'
serverusers = len(serverlist[leaf].users) serverusers = len(serverlist[leaf].users)
reply("%s%s%s[%s]: %s user(s) (%s%%)" % (' '*hops, linechar, serverlist[leaf].name, leaf, if is_relay_server:
serverusers, _percent(serverusers, usercount))) # Skip showing user data for relay servers.
showall(leaf, hops) reply("%s%s\x02%s\x02[%s] (via PyLink Relay)" % (' '*hops, linechar, serverlist[leaf].name, leaf))
else:
reply("%s%s\x02%s\x02[%s]: %s user(s) (%s%%)" % (' '*hops, linechar, serverlist[leaf].name, leaf,
serverusers, _percent(serverusers, usercount)))
showall(ircobj, leaf, hops, is_relay_server=is_relay_server)
if (not is_relay_server) and hasattr(serverlist[leaf], 'remote') and show_relay:
# This is a relay server - display the remote map of the network it represents
relay_server = serverlist[leaf].remote
remoteirc = world.networkobjects[relay_server]
# Only ever show relay subservers once - this prevents infinite loops.
showall(remoteirc, remoteirc.sid, hops=hops, is_relay_server=True)
else: else:
# Afterwards, decrement the hopcount. # Afterwards, decrement the hopcount.
hops -= 1 hops -= 1
# Start the map at our PyLink server # Start the map at our PyLink server
firstserver = hostsid firstserver = hostsid
showall(firstserver) showall(ircobj, firstserver)
reply('Total %s users on %s servers - average of %1.f per server' % (usercount, len(serverlist), serverlist = irc.servers
reply('Total %s users on %s local servers - average of %1.f per server' % (usercount, len(serverlist),
usercount/len(serverlist))) usercount/len(serverlist)))
utils.add_cmd(_map, 'map') utils.add_cmd(_map, 'map')
utils.add_cmd(lambda irc, source, args: _map(irc, source, args, show_relay=False), 'localmap')