mirror of
https://github.com/chocolate-doom/master-server.git
synced 2024-11-21 20:11:22 +00:00
Track age of servers. Display age on master server web page and sort by
decreasing age. Subversion-branch: /master Subversion-revision: 2219
This commit is contained in:
parent
5432fd9b0f
commit
2687e6ddb5
4 changed files with 33 additions and 6 deletions
|
@ -83,12 +83,16 @@ class Server:
|
|||
|
||||
def __init__(self, addr):
|
||||
self.addr = addr
|
||||
self.add_time = time()
|
||||
self.verified = False
|
||||
self.metadata = {}
|
||||
self.refresh()
|
||||
|
||||
def refresh(self):
|
||||
self.add_time = time()
|
||||
self.refresh_time = time()
|
||||
|
||||
def age(self):
|
||||
return time() - self.add_time
|
||||
|
||||
def set_metadata(self, metadata):
|
||||
self.metadata_time = time()
|
||||
|
@ -98,7 +102,7 @@ class Server:
|
|||
return time() - self.metadata_time
|
||||
|
||||
def timed_out(self):
|
||||
return time() - self.add_time > SERVER_TIMEOUT
|
||||
return time() - self.refresh_time > SERVER_TIMEOUT
|
||||
|
||||
def __str__(self):
|
||||
return "%s:%i" % self.addr
|
||||
|
@ -272,11 +276,16 @@ class MasterServer:
|
|||
|
||||
self.log_output(addr, "Metadata request")
|
||||
|
||||
def metadata_string(server):
|
||||
metadata = server.metadata.copy()
|
||||
metadata["age"] = server.age()
|
||||
return simplejson.dumps(metadata)
|
||||
|
||||
# Generate a list of strings containing JSON-encoded metadata
|
||||
# about servers. Only include verified servers.
|
||||
|
||||
verified_servers = filter(lambda s: s.verified, self.servers.values())
|
||||
strings = [ simplejson.dumps(s.metadata) for s in verified_servers]
|
||||
strings = [ metadata_string(server) for server in verified_servers]
|
||||
|
||||
# Send response packets.
|
||||
|
||||
|
@ -324,13 +333,13 @@ class MasterServer:
|
|||
if server.timed_out():
|
||||
self.log_output(server.addr,
|
||||
"Timed out: no heartbeat in %i secs" %
|
||||
(time() - server.add_time))
|
||||
(time() - server.refresh_time))
|
||||
del self.servers[server.addr]
|
||||
|
||||
# Expect a response to queries quickly, otherwise add
|
||||
# requests are rejected.
|
||||
|
||||
if not server.verified and time() - server.add_time > 5:
|
||||
if not server.verified and time() - server.refresh_time > 5:
|
||||
self.log_output(server.addr,
|
||||
"No response to query, add rejected")
|
||||
self.send_add_response(server, 0)
|
||||
|
|
|
@ -156,6 +156,7 @@ def get_metadata(addr_str):
|
|||
for json in servers:
|
||||
metadata = simplejson.loads(json)
|
||||
print "\tServer: %s:%i" % (metadata["address"], metadata["port"])
|
||||
print "\t\tAge: %i seconds" % metadata["age"]
|
||||
print "\t\tName: %s" % metadata["name"]
|
||||
print "\t\tVersion: %s" % metadata["version"]
|
||||
print "\t\tMax. players: %i" % metadata["max_players"]
|
||||
|
|
18
web/index.py
18
web/index.py
|
@ -116,6 +116,19 @@ def get_server_data():
|
|||
|
||||
return get_metadata(MASTER_SERVER)
|
||||
|
||||
def age_string(seconds):
|
||||
seconds = int(seconds)
|
||||
minutes, seconds = seconds / 60, seconds % 60
|
||||
hours, minutes = minutes / 60, minutes % 60
|
||||
days, hours = hours / 24, hours % 24
|
||||
|
||||
result = "%02i:%02i:%02i" % (hours, minutes, seconds)
|
||||
|
||||
if days > 0:
|
||||
result = "%i days, %s" % (days, result)
|
||||
|
||||
return result
|
||||
|
||||
def generate_table_row(server):
|
||||
""" Generate a row of the HTML table, containing data for a
|
||||
particular server. """
|
||||
|
@ -124,7 +137,8 @@ def generate_table_row(server):
|
|||
"%s:%i" % (server["address"], server["port"]),
|
||||
escape(server["name"]),
|
||||
escape(server["version"]),
|
||||
server["max_players"]
|
||||
server["max_players"],
|
||||
age_string(server["age"])
|
||||
]
|
||||
|
||||
result = []
|
||||
|
@ -137,6 +151,8 @@ def generate_table_row(server):
|
|||
def generate_table(server_data):
|
||||
""" Generate an HTML table from list of server metadata. """
|
||||
|
||||
server_data = sorted(server_data, key=lambda server: -server["age"])
|
||||
|
||||
result = []
|
||||
|
||||
for server in server_data:
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
<th> Server name
|
||||
<th> Version
|
||||
<th> Max. players
|
||||
<th> Age
|
||||
|
||||
___TABLE_DATA___
|
||||
|
||||
|
|
Loading…
Reference in a new issue