def __init__(self, parentThread): """ generated source for method __init__ """ super(ClientManager, self).__init__() self.parentThread = parentThread command = GamerConfiguration.getCommandForJava() processArgs = ArrayList() processArgs.add(command) processArgs.add("-mx" + GamerConfiguration.getMemoryForGamer() + "m") processArgs.add("-server") processArgs.add("-XX:-DontCompileHugeMethods") processArgs.add("-XX:MinHeapFreeRatio=10") processArgs.add("-XX:MaxHeapFreeRatio=10") processArgs.add("-classpath") processArgs.add(System.getProperty("java.class.path")) processArgs.add("org.ggp.base.player.proxy.ProxyGamePlayerClient") processArgs.add(self.gamerName) processArgs.add("" + self.clientListener.getLocalPort()) if GamerConfiguration.runningOnLinux(): processArgs.add(0, "nice") pb = ProcessBuilder(processArgs) try: GamerLogger.log("Proxy", "[PROXY] Starting a new proxy client, using gamer " + self.gamerName + ".") self.theClientProcess = pb.start() self.outConnector = StreamConnector(self.theClientProcess.getErrorStream(), System.err) self.errConnector = StreamConnector(self.theClientProcess.getInputStream(), System.out) self.outConnector.start() self.errConnector.start() self.theClientConnection = self.clientListener.accept() self.theOutput = PrintStream(self.theClientConnection.getOutputStream()) self.theInput = BufferedReader(InputStreamReader(self.theClientConnection.getInputStream())) GamerLogger.log("Proxy", "[PROXY] Proxy client started.") except IOException as e: GamerLogger.logStackTrace("Proxy", e)
def run(self): """ generated source for method run """ GamerConfiguration.showConfiguration() GamerLogger.setSpilloverLogfile("spilloverLog") # Start up the client manager self.theClientManager = self.ClientManager(Thread.currentThread()) self.theClientManager.start() # Start up the input queue listener inputQueue = ArrayBlockingQueue(100) inputConnectionQueue = ArrayBlockingQueue(100) theListener = QueueListenerThread() theListener.start() while True: try: # First, read a message from the server. self.notifyObservers(PlayerReceivedMessageEvent(in_)) GamerLogger.log("Proxy", "[PROXY] Got incoming message:" + in_) # Formulate a request, and see how the legal gamer responds. try: legalProxiedResponse = request.process(receptionTime) except OutOfMemoryError as e: # Something went horribly wrong -- our baseline prover failed. System.gc() GamerLogger.logStackTrace("Proxy", e) legalProxiedResponse = "SORRY" latestProxiedResponse = legalProxiedResponse GamerLogger.log("Proxy", "[PROXY] Selected fallback move:" + latestProxiedResponse) if not (isinstance(request, (InfoRequest, ))): # Update the move codes and prepare to send the request on to the client. self.receivedClientMove = False self.currentMoveCode = 1 + self.theRandomGenerator.nextLong() if isinstance(request, (StopRequest, )) or isinstance(request, (AbortRequest, )): self.theClientManager.expectStop = True # Send the request on to the client, along with the move code. self.theClientManager.sendMessage(theMessage) if not (isinstance(request, (PlayRequest, ))): self.currentMoveCode = 0L # the default gamer handle it by switching move code. # Wait the appropriate amount of time for the request. proxyProcessRequest(request, receptionTime) else: self.receivedClientMove = True # Get the latest response, and complain if it's the default response, or isn't a valid response. if not self.receivedClientMove and (isinstance(request, (PlayRequest, ))): GamerLogger.logError("Proxy", "[PROXY] Did not receive any move information from client for this turn; falling back to first legal move.") GamerLogger.logError("ExecutiveSummary", "Proxy did not receive any move information from client this turn: used first legal move.") # Cycle the move codes again so that we will ignore any more responses # that the client sends along to us. self.currentMoveCode = 0L # And finally write the latest response out to the server. GamerLogger.log("Proxy", "[PROXY] Wrote outgoing message:" + out) HttpWriter.writeAsServer(connection, out) connection.close() self.notifyObservers(PlayerSentMessageEvent(out)) # Once everything is said and done, restart the client if we're # due for a restart (having finished playing a game). if self.needRestart: self.theClientManager.closeClient() self.theClientManager.pleaseStop = True if GamerConfiguration.runningOnLinux(): # Clean up the working directory and terminate any orphan processes. Thread.sleep(500) GamerLogger.log("Proxy", "[PROXY] Calling cleanup scripts.") try: Runtime.getRuntime().exec_("./cleanup.sh").waitFor() except IOException as e: GamerLogger.logStackTrace("Proxy", e) Thread.sleep(500) self.theClientManager = self.ClientManager(Thread.currentThread()) self.theClientManager.start() self.theDefaultGamer = RandomGamer() GdlPool.drainPool() SymbolPool.drainPool() GamerLogger.log("Proxy", "[PROXY] Before collection, using " + usedMemoryInMegs + "mb of memory as proxy.") while i < 10: System.gc() Thread.sleep(100) i += 1 usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() usedMemoryInMegs = usedMemory / 1024.0 / 1024.0 GamerLogger.log("Proxy", "[PROXY] After collection, using a non-transient " + usedMemoryInMegs + "mb of memory as proxy.") print "Cleaned up completed match, with a residual " + usedMemoryInMegs + "mb of memory as proxy." self.needRestart = False except Exception as e: GamerLogger.logStackTrace("Proxy", e) self.notifyObservers(PlayerDroppedPacketEvent()) except Error as e: GamerLogger.logStackTrace("Proxy", e) self.notifyObservers(PlayerDroppedPacketEvent())