class ProcessHandler: def __init__(self, processName): self.client = subprocess.Popen(processName, stdin = subprocess.PIPE, stdout = subprocess.PIPE) self.requestCount = 0 self.inputHandler = ClientInputHandler(self) self.inputHandler.start() self.requests = {} self.__condition = threading.Condition() self.__responseLock = threading.Lock() self.__response = None def __del__(self): self.client.terminate() def __generateRequest(self, requestType, args=None): self.requestCount += 1 requestText = str(requestType) self.requests[self.requestCount] = requestType if args != None: for arg in args: requestText += '\n' + arg self.__condition.acquire() self.client.stdin.write( bytes('REQUEST\n' + str(self.requestCount) + '\n' + requestText + '\nEND\n', 'UTF-8')) self.client.stdin.flush() self.__condition.wait() with self.__responseLock: return copy(self.__response) #@return int representing PowerPlant to begin bidding on def requestAuctionStart(self): return self.__generateRequest(ServerRequestTypes.AUCTION_START) #@param player - player that currently has highest bid #@return int representing Price player bid def requestBid(self, powerPlant, minBid, player): return self.__generateRequest(ServerRequestTypes.POWER_PLANT_BID, args=[str(powerPlant), str(minBid), str(player)]) def requestMaterialPurchase(self): return self.__generateRequest(ServerRequestTypes.RESOURCE_PURCHASE) def requestCityPurchase(self): return self.__generateRequest(ServerRequestTypes.CITY_PURCHASE) #return a tuple(number of cities, list of plant ids) def requestSupplyPowerForCities(self): return self.__generateRequest(ServerRequestTypes.SUPPLY_POWER_FOR_CITIES) def getRequestType(self, requestId): return self.requests[int(requestId)] def writeResponse(self, response): with self.__condition: with self.__responseLock: self.__response = response self.__condition.notify()
def __init__(self, processName): self.client = subprocess.Popen(processName, stdin = subprocess.PIPE, stdout = subprocess.PIPE) self.requestCount = 0 self.inputHandler = ClientInputHandler(self) self.inputHandler.start() self.requests = {} self.__condition = threading.Condition() self.__responseLock = threading.Lock() self.__response = None