Commits

mcc  committed 8ca460c

Server logging

  • Participants
  • Parent commits dc0ba23

Comments (0)

Files changed (1)

File server/server.py

 define("debug_failure", default=0, help="debug: fake request failure", type=int)
 
 history_file = None
+history_dom = None
 
 class MainHandler(tornado.web.RequestHandler):
 	def get(self):
 		r.setAttribute("sid", str(self.sid))
 		self.board.xml(doc,r)
 		
+	def archive_xml(self, doc, r):
+		slo = history_dom.createElement("spawn")
+		slo.setAttribute("sid",str(self.sid))
+		self.board.xml(doc,slo)
+		r.appendChild(slo)
+		
 	def report(self, post_obj):
 		self.checked = True
 		if post_obj.hasAttribute("won") and int(post_obj.getAttribute("won")):
 			self.won = True
 			self.deaths = int(post_obj.getAttribute("deaths"))
 			self.lastf = int(post_obj.getAttribute("lastf"))
+		if history_file:
+			history_file.write(post_obj.toxml())
+			history_file.write("\n")
 	
 class Genome:
 	def __init__(self, _id):
 				sorted_old_specs = sorted(old_specs, cmp=fitness)
 				winner1 = sorted_old_specs[0]
 				winner2 = sorted_old_specs[1]
+				winner1sid = winner1.sid # Only used for logging
+				winner2sid = winner2.sid
 				if options.debug > 1:
 					print("Recombining genome %d. Winning specs: %d, %d" % (self.id, winner1.sid, winner2.sid))
 				for i in range(len(old_specs)):
 					new_specs.append(s)
 				self.specs = new_specs
 				self.generation_count = self.generation_count + 1
+				
+				if history_file:
+					lo = history_dom.createElement("breed")
+					lo.setAttribute("gid", str(self.id))
+					lo.setAttribute("gc", str(self.generation_count))
+					lo.setAttribute("winner1", str(winner1sid))
+					lo.setAttribute("winner2", str(winner2sid))
+					for s in self.specs:
+						s.archive_xml(history_dom, lo)
+					history_file.write(lo.toxml())
+					history_file.write("\n")
+					history_file.flush()
 			finally:
 				self.issue_next = 0
 		return s
 	def __init__(self):
 		self.tanks = []
 		for i in range(options.tanks):
-			print("Generating tank %d" % i)
+			if options.debug > 0:
+				print("Generating tank %d" % i)
 			self.tanks.append(Genome(i))
 	
 	def any(self):
 		print("Debug level %s" % (options.debug))
 	if options.history:
 		global history_file
+		global history_dom
 		print("Logging to %s" % (options.history))
 		history_file = open(options.history, AIR)
+		history_dom = xml.dom.minidom.Document()
 	if options.database:
 		global database
 		print("Will save to %s" % (options.database))
 	global m
 	m = Manager()
 	
+	if history_file: # initial logging
+		lo = history_dom.createElement("init")
+		for g in m.tanks:
+			lot = history_dom.createElement("tank")
+			lot.setAttribute("gid", str(g.id))
+			lot.setAttribute("gc", str(g.generation_count))
+			for s in g.specs:
+				s.archive_xml(history_dom, lot)
+			lo.appendChild(lot)
+			lot = None # How aggressive is Python's gc...?
+		history_file.write(lo.toxml())
+		history_file.write("\n")
+		history_file.flush()
+		lo = None
+	
 	application = tornado.web.Application([
 		(r"/", MainHandler),
 		(r"/welcome", WelcomeHandler),