diff --git a/chocolate-master b/chocolate-master index efcdb7c..af09108 100755 --- a/chocolate-master +++ b/chocolate-master @@ -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) diff --git a/chocolate-master-test.py b/chocolate-master-test.py index ba625fb..5098ef0 100755 --- a/chocolate-master-test.py +++ b/chocolate-master-test.py @@ -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"] diff --git a/web/index.py b/web/index.py index bc3d43f..b60d85d 100644 --- a/web/index.py +++ b/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: diff --git a/web/index.template b/web/index.template index 4672120..dd7bd19 100644 --- a/web/index.template +++ b/web/index.template @@ -17,6 +17,7 @@