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:
Simon Howard 2010-12-11 01:27:13 +00:00
parent 5432fd9b0f
commit 2687e6ddb5
4 changed files with 33 additions and 6 deletions

View file

@ -83,12 +83,16 @@ class Server:
def __init__(self, addr): def __init__(self, addr):
self.addr = addr self.addr = addr
self.add_time = time()
self.verified = False self.verified = False
self.metadata = {} self.metadata = {}
self.refresh() self.refresh()
def refresh(self): def refresh(self):
self.add_time = time() self.refresh_time = time()
def age(self):
return time() - self.add_time
def set_metadata(self, metadata): def set_metadata(self, metadata):
self.metadata_time = time() self.metadata_time = time()
@ -98,7 +102,7 @@ class Server:
return time() - self.metadata_time return time() - self.metadata_time
def timed_out(self): def timed_out(self):
return time() - self.add_time > SERVER_TIMEOUT return time() - self.refresh_time > SERVER_TIMEOUT
def __str__(self): def __str__(self):
return "%s:%i" % self.addr return "%s:%i" % self.addr
@ -272,11 +276,16 @@ class MasterServer:
self.log_output(addr, "Metadata request") 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 # Generate a list of strings containing JSON-encoded metadata
# about servers. Only include verified servers. # about servers. Only include verified servers.
verified_servers = filter(lambda s: s.verified, self.servers.values()) 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. # Send response packets.
@ -324,13 +333,13 @@ class MasterServer:
if server.timed_out(): if server.timed_out():
self.log_output(server.addr, self.log_output(server.addr,
"Timed out: no heartbeat in %i secs" % "Timed out: no heartbeat in %i secs" %
(time() - server.add_time)) (time() - server.refresh_time))
del self.servers[server.addr] del self.servers[server.addr]
# Expect a response to queries quickly, otherwise add # Expect a response to queries quickly, otherwise add
# requests are rejected. # 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, self.log_output(server.addr,
"No response to query, add rejected") "No response to query, add rejected")
self.send_add_response(server, 0) self.send_add_response(server, 0)

View file

@ -156,6 +156,7 @@ def get_metadata(addr_str):
for json in servers: for json in servers:
metadata = simplejson.loads(json) metadata = simplejson.loads(json)
print "\tServer: %s:%i" % (metadata["address"], metadata["port"]) print "\tServer: %s:%i" % (metadata["address"], metadata["port"])
print "\t\tAge: %i seconds" % metadata["age"]
print "\t\tName: %s" % metadata["name"] print "\t\tName: %s" % metadata["name"]
print "\t\tVersion: %s" % metadata["version"] print "\t\tVersion: %s" % metadata["version"]
print "\t\tMax. players: %i" % metadata["max_players"] print "\t\tMax. players: %i" % metadata["max_players"]

View file

@ -116,6 +116,19 @@ def get_server_data():
return get_metadata(MASTER_SERVER) 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): def generate_table_row(server):
""" Generate a row of the HTML table, containing data for a """ Generate a row of the HTML table, containing data for a
particular server. """ particular server. """
@ -124,7 +137,8 @@ def generate_table_row(server):
"%s:%i" % (server["address"], server["port"]), "%s:%i" % (server["address"], server["port"]),
escape(server["name"]), escape(server["name"]),
escape(server["version"]), escape(server["version"]),
server["max_players"] server["max_players"],
age_string(server["age"])
] ]
result = [] result = []
@ -137,6 +151,8 @@ def generate_table_row(server):
def generate_table(server_data): def generate_table(server_data):
""" Generate an HTML table from list of server metadata. """ """ Generate an HTML table from list of server metadata. """
server_data = sorted(server_data, key=lambda server: -server["age"])
result = [] result = []
for server in server_data: for server in server_data:

View file

@ -17,6 +17,7 @@
<th> Server name <th> Server name
<th> Version <th> Version
<th> Max. players <th> Max. players
<th> Age
___TABLE_DATA___ ___TABLE_DATA___