X-Git-Url: https://git.ucc.asn.au/?p=progcomp2013.git;a=blobdiff_plain;f=qchess%2Fsrc%2Flog.py;h=12b43b84be24077fa996e75ded53c771c86311ad;hp=0712910e76ae616df0edd7f77171080984b0514c;hb=a238aa7acac990bae67644d1dc7f518ce3e2e8c6;hpb=5e4b175a7410b8878705b69ed8f97caf10b45a64 diff --git a/qchess/src/log.py b/qchess/src/log.py index 0712910..12b43b8 100644 --- a/qchess/src/log.py +++ b/qchess/src/log.py @@ -3,15 +3,19 @@ import datetime import urllib2 class LogFile(): - def __init__(self, file_name): + def __init__(self, log): - self.log = open(file_name, "w", 0) + self.log = log + self.logged = [] + self.log.write("# Log starts " + str(datetime.datetime.now()) + "\n") def write(self, s): - self.log.write(str(datetime.datetime.now()) + " : " + s + "\n") + now = datetime.datetime.now() + self.log.write(str(now) + " : " + s + "\n") + self.logged.append((now, s)) def setup(self, board, players): - self.log.write("# Log starts " + str(datetime.datetime.now()) + "\n") + for p in players: self.log.write("# " + p.colour + " : " + p.name + "\n") @@ -23,52 +27,100 @@ class LogFile(): self.log.write("# Start game\n") + def close(self): + self.log.write("# EOF\n") + self.log.close() + class HttpLog(LogFile): def __init__(self, file_name): - LogFile.__init__(self, file_name) + LogFile.__init__(self, open(file_name, "w", 0)) self.file_name = file_name + self.phase = 0 def write(self, s): - self.log.close() - self.log = open(self.file_name, "w", 0) + now = datetime.datetime.now() + self.logged.append((now, s)) + + if self.phase == 0: + self.log.close() + self.log = open(self.file_name, "w", 0) + self.log.write("# Short log updated " + str(datetime.datetime.now()) + "\n") + LogFile.setup(self, game.board, game.players) - LogFile.setup(self, game.board, game.players) + elif self.phase == 1: + for message in self.logged[len(self.logged)-2:]: + self.log.write(str(message[0]) + " : " + message[1] + "\n") - LogFile.write(self, s) + self.phase = (self.phase + 1) % 2 + + def close(self): + self.log.write("# EOF\n") + self.log.close() class HeadRequest(urllib2.Request): def get_method(self): return "HEAD" - -class HttpReplay(): + +class HttpGetter(StoppableThread): def __init__(self, address): - self.read_setup = False - self.log = urllib2.urlopen(address) + StoppableThread.__init__(self) self.address = address + self.log = urllib2.urlopen(address) + self.lines = [] + self.lock = threading.RLock() #lock for access of self.state + self.cond = threading.Condition() # conditional - def readline(self): - - line = self.log.readline() - sys.stderr.write(sys.argv[0] + " : " + str(self) + " read \""+str(line) + "\" from address " + str(self.address) + "\n") - if line == "": - sys.stderr.write(sys.argv[0] + " : " + str(self) + " retrieving from address " + str(self.address) + "\n") - date_mod = datetime.datetime.strptime(self.log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") - self.log.close() - - next_log = urllib2.urlopen(HeadRequest(self.address)) - date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") - while date_new <= date_mod: + def run(self): + while not self.stopped(): + line = self.log.readline() + if line == "": + date_mod = datetime.datetime.strptime(self.log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") + self.log.close() + next_log = urllib2.urlopen(HeadRequest(self.address)) date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") + while date_new <= date_mod and not self.stopped(): + next_log = urllib2.urlopen(HeadRequest(self.address)) + date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") + if self.stopped(): + break - self.log = urllib2.urlopen(address) - game.setup() - line = self.log.readline() + self.log = urllib2.urlopen(self.address) + line = self.log.readline() + + self.cond.acquire() + self.lines.append(line) + self.cond.notifyAll() + self.cond.release() + #sys.stderr.write(" HttpGetter got \'" + str(line) + "\'\n") + + self.log.close() + + + + + +class HttpReplay(): + def __init__(self, address): + self.getter = HttpGetter(address) + self.getter.start() + + def readline(self): + self.getter.cond.acquire() + while len(self.getter.lines) == 0: + self.getter.cond.wait() + + result = self.getter.lines[0] + self.getter.lines = self.getter.lines[1:] + self.getter.cond.release() - return line + return result + + def close(self): + self.getter.stop() def log(s): if log_file != None: