Write out server messages on the stats page

This commit is contained in:
simon.kagstrom 2010-02-28 08:52:34 +00:00
parent ae23c0c4c2
commit 839972cabc
2 changed files with 33 additions and 2 deletions

View File

@ -641,14 +641,23 @@ class Broker(SocketServer.UDPServer):
def enqueue_message(self, sending_peer, message): def enqueue_message(self, sending_peer, message):
# Store last 10 messages # Store last 10 messages
self.messages = self.messages[-9:] + [message] self.messages = self.messages[-9:] + [message]
all_msgs = []
for addr, peer in self.peers.iteritems(): for addr, peer in self.peers.iteritems():
all_msgs.append(message.message)
if peer != sending_peer: if peer != sending_peer:
peer.send_packet(message.marshal()) peer.send_packet(message.marshal())
stats.set_messages(all_msgs)
try:
stats.generate_html(self.stat_html)
except Exception, e:
pass
def dequeue_old_messages(self): def dequeue_old_messages(self):
now = time.mktime(time.localtime()) now = time.mktime(time.localtime())
to_delete = 0 to_delete = 0
all_msgs = []
for msg in self.messages: for msg in self.messages:
diff = now - msg.get_timestamp() diff = now - msg.get_timestamp()
@ -658,6 +667,14 @@ class Broker(SocketServer.UDPServer):
log_info("Deleting old message") log_info("Deleting old message")
self.messages = self.messages[ to_delete : ] self.messages = self.messages[ to_delete : ]
for msg in self.messages:
all_msgs.append(msg.message)
stats.set_messages(all_msgs)
try:
stats.generate_html(self.stat_html)
except Exception, e:
pass
def ping_all_peers(self): def ping_all_peers(self):
"""Ping all peers (to see that they are alive)""" """Ping all peers (to see that they are alive)"""
for k,v in self.peers.iteritems(): for k,v in self.peers.iteritems():

View File

@ -6,6 +6,7 @@ class Container:
self.total_connections = 0 self.total_connections = 0
self.country_count = {} self.country_count = {}
self.last_10 = [] self.last_10 = []
self.messages = []
self.nr_active = 0 self.nr_active = 0
self.nr_waiting = 0 self.nr_waiting = 0
@ -17,11 +18,12 @@ class Container:
def copy_from_other(self, other): def copy_from_other(self, other):
try: try:
self.nr_active = 0 # Always reset
self.nr_waiting = 0
self.total_connections = other.total_connections self.total_connections = other.total_connections
self.country_count = other.country_count self.country_count = other.country_count
self.last_10 = other.last_10 self.last_10 = other.last_10
self.nr_active = 0 # Always reset self.messages = other.messages
self.nr_waiting = 0
except: except:
pass pass
@ -33,6 +35,9 @@ class Container:
self.country_count[country] = cur + 1 self.country_count[country] = cur + 1
def set_messages(self, messages):
self.messages = messages
def add_connection(self, who, country): def add_connection(self, who, country):
time_now = time.strftime("%Y-%m-%d %H:%M", time.gmtime()) time_now = time.strftime("%Y-%m-%d %H:%M", time.gmtime())
s = "%s - %s (%s)" % (time_now, who, country) s = "%s - %s (%s)" % (time_now, who, country)
@ -71,6 +76,11 @@ class HtmlGenerator:
outf.write("<TR><TD colspan=3><H3>List of countries</H3></TD></TR>\n") outf.write("<TR><TD colspan=3><H3>List of countries</H3></TD></TR>\n")
count = 1 count = 1
outf.write("<H3>Last server messages</H3>\n")
for msg in self.container.messages:
outf.write("<br>%s\n" % (msg))
outf.write("<br><br>")
n_countries = len(sorted_countries) n_countries = len(sorted_countries)
for i in range(0, n_countries / 3): for i in range(0, n_countries / 3):
c1, n1 = sorted_countries[i] c1, n1 = sorted_countries[i]
@ -124,6 +134,9 @@ def update_peer_nr(waiting, active):
def add_connection(who, country): def add_connection(who, country):
g_stat.add_connection(who, country) g_stat.add_connection(who, country)
def set_messages(messages):
g_stat.set_messages(messages)
if __name__ == "__main__": if __name__ == "__main__":
load("/tmp/vobb") load("/tmp/vobb")
for i in range(0, 10): for i in range(0, 10):
@ -133,6 +146,7 @@ if __name__ == "__main__":
add_connection("SIMONK", "Sweden") add_connection("SIMONK", "Sweden")
add_connection("Linda", "Germany") add_connection("Linda", "Germany")
add_connection("Linda", "Germany") add_connection("Linda", "Germany")
set_messages(["Hej", "Who framed Roger Rabbit?", "IK+ at 19.00 CET?"])
save("/tmp/vobb") save("/tmp/vobb")
hg = HtmlGenerator(g_stat) hg = HtmlGenerator(g_stat)