From e994e7fb2479dd23c5112a9c0fd8ebbb87c98ae1 Mon Sep 17 00:00:00 2001 From: schneider Date: Thu, 15 Dec 2011 19:09:05 +0100 Subject: [PATCH] remote: added game class and callbacks --- .../game/r0ketrem0te/{rem0te.py => bridge.py} | 42 +++++++++++------ tools/game/r0ketrem0te/game.py | 34 ++++++++++++++ tools/game/r0ketrem0te/packets.py | 47 ++++++++++--------- tools/game/simpletest.py | 32 ++++++------- 4 files changed, 102 insertions(+), 53 deletions(-) rename tools/game/r0ketrem0te/{rem0te.py => bridge.py} (86%) create mode 100644 tools/game/r0ketrem0te/game.py diff --git a/tools/game/r0ketrem0te/rem0te.py b/tools/game/r0ketrem0te/bridge.py similarity index 86% rename from tools/game/r0ketrem0te/rem0te.py rename to tools/game/r0ketrem0te/bridge.py index d7abe9f..024edfe 100644 --- a/tools/game/r0ketrem0te/rem0te.py +++ b/tools/game/r0ketrem0te/bridge.py @@ -10,7 +10,7 @@ class QueuePacket: self.mac = mac self.acked = acked self.packet = packet - self.priority = 5 + self.priority = packet.priority self.retriesleft = 5 self.timeout = 0.1 self.timer = None @@ -35,7 +35,7 @@ class QueuePacket: def sent(self, timeoutcallback): with self.lock: self.timedout = False - if self.retrieslef > 0: + if self.retriesleft > 0: self.retriesleft-=1 if self.acked: self.timeoutcallback = timeoutcallback @@ -61,7 +61,7 @@ class QueuePacket: self.callback('timeout') class Bridge: - def __init__(self, path2device): + def __init__(self, path2device, channel, rxmac): self.ser = serialinterface.SerialInterface(path2device, 115200, 0) self.free = threading.Lock() self.queueslock = threading.Lock() @@ -69,6 +69,7 @@ class Bridge: self.outpackets = Queue.Queue() self.crc = crcmod.predefined.mkCrcFun('crc-ccitt-false') self.queues = {} + self.callbacks = [] self.reader = threading.Thread(target = self.readerThread) self.reader.daemon = True @@ -83,11 +84,16 @@ class Bridge: self.txmac = None self.rxmac = None self.channel = None + self.gameChannel = channel self.setPacketLength(0x20) - self.setTxMAC((1,2,3,2,1)) - self.setRxMAC((1,2,3,2,1)) - self.setChannel(81) + self.setRxMAC(rxmac) + self.setChannel(channel) + + self.ctr = 0 + def registerCallback(self, callback): + if callback not in self.callbacks: + self.callbacks.append(callback) def registerQueue(self, queue): if queue not in self.queues: @@ -131,6 +137,9 @@ class Bridge: qp = self.queues[q] if qp == None and not q.empty(): qp = q.get() + if not isinstance(qp.packet,packets.Ack): + self.ctr+=1 + qp.packet.ctr = self.ctr self.queues[q] = qp self.outpackets.put(qp) @@ -140,9 +149,9 @@ class Bridge: #wait until we have packets to take care of qp = self.outpackets.get() #send it and notify the queuepacket - self.sendPacket(qp.packet) - self.setTxMac(qp.mac) + self.setTxMAC(qp.mac) self.setChannel(qp.channel) + self.sendPacket(qp.packet) qp.sent(self.packetTimeout) self.setChannel(self.gameChannel) except Exception as e: @@ -171,13 +180,15 @@ class Bridge: if isinstance(packet,packets.Ack): self.ProcessAck(packet) else: - self.packets.put(packet) + for callback in self.callbacks: + callback(packet) + #self.packets.put(packet) - def gotPacket(self): - return not self.packets.empty() +# def gotPacket(self): +# return not self.packets.empty() - def getPacket(self): - return self.packets.get() +# def getPacket(self): +# return self.packets.get() def sendPacket(self, packet): print 'sending', packet @@ -197,9 +208,10 @@ class Bridge: self.packetLength = length def setTxMAC(self, mac): - if mac == self.txmax: + if mac == self.txmac: return self.free.acquire() + print "setting tx mac", mac self.ser.writeMessage('3', ''.join([chr(x) for x in mac])) self.txmac = mac @@ -207,6 +219,7 @@ class Bridge: if mac == self.rxmac: return self.free.acquire() + print "setting rx mac", mac self.ser.writeMessage('4', ''.join([chr(x) for x in mac])) self.rxmac = mac @@ -214,6 +227,7 @@ class Bridge: if channel == self.channel: return self.free.acquire() + print "setting channel", channel self.ser.writeMessage('5', '%c'%channel) self.channel = channel diff --git a/tools/game/r0ketrem0te/game.py b/tools/game/r0ketrem0te/game.py new file mode 100644 index 0000000..eb54596 --- /dev/null +++ b/tools/game/r0ketrem0te/game.py @@ -0,0 +1,34 @@ +import bridge +import packets +import time +import Queue +import random +import threading + +class Game: + def __init__(self, device, gameName, gameChannel, announcechannel, announcemac): + self.gameName = gameName + self.channel = gameChannel + self.gamemac = [int(random.random()*254) for x in range(1,6)] + self.playermac = list(self.gamemac) + self.playermac[4]+=1 + self.gameid = int(random.random()*(2**31)) + + self.bridge = bridge.Bridge(device, self.channel, self.gamemac) + self.announce = packets.Announce(self.gamemac, self.channel, + self.gameid, 0, "testgame") + + self.announcequeue = Queue.Queue() + self.bridge.registerQueue(self.announcequeue) + self.announcechannel = announcechannel + self.announcemac = announcemac + + self.sendAnnounce() + + def sendAnnounce(self): + aq = bridge.QueuePacket(self.announcechannel, + self.announcemac, False, self.announce) + self.bridge.putInQueue(self.announcequeue, aq) + self.announcetimer = threading.Timer(1, self.sendAnnounce) + self.announcetimer.start() + diff --git a/tools/game/r0ketrem0te/packets.py b/tools/game/r0ketrem0te/packets.py index b5e6e7f..9646603 100644 --- a/tools/game/r0ketrem0te/packets.py +++ b/tools/game/r0ketrem0te/packets.py @@ -5,17 +5,18 @@ def uint32toint(v): return (ord(v[3])<< 24) + (ord(v[2])<<16) + (ord(v[1])<<8) + (ord(v[0])) class Packet: - def __init__(self, command, id=None, ctr=None): - if id == None and ctr == None: + def __init__(self, command, id=None): + self.ctr = 0 + if id == None: message = command command = message[2] id = uint32toint(message[3:7]) - ctr = uint32toint(message[7:11]) + self.ctr = uint32toint(message[7:11]) self.length = 32 self.protocol = 'G' self.command = command self.id = id - self.ctr = ctr + self.priority = 5 def toMessage(self): message = chr(self.length) @@ -35,9 +36,9 @@ class Packet: return s class Button(Packet): - def __init__(self, id, ctr=None, button=None): - if ctr != None and button!= None: - Packet.__init__(self, 'B', id, ctr) + def __init__(self, id, button=None): + if button!= None: + Packet.__init__(self, 'B', id) else: message = id Packet.__init__(self, message) @@ -54,13 +55,15 @@ class Button(Packet): return s class Announce(Packet): - def __init__(self, id, ctr, gameMac, gameChannel, gameId, gameFlags, gameTitle): - Packet.__init__(self, 'A', id, ctr) + def __init__(self, gameMac, gameChannel, gameId, gameFlags, gameTitle): + #always a broadcast + Packet.__init__(self, 'A', 0) self.gameMac = gameMac self.gameChannel = gameChannel self.gameId = gameId self.gameFlags = gameFlags self.gameTitle = gameTitle[0:8] + self.priority = 3 def toMessage(self): message = Packet.toMessage(self) @@ -83,9 +86,9 @@ class Announce(Packet): return s class Join(Packet): - def __init__(self, id, ctr=None, gameId=None): - if ctr != None and gameId != None: - Packet.__init__(self, 'J', id, ctr) + def __init__(self, id, gameId=None): + if gameId != None: + Packet.__init__(self, 'J', id) else: message = id Packet.__init__(self, message) @@ -104,14 +107,16 @@ class Join(Packet): return s class Ack(Packet): - def __init__(self, id, ctr=None, flags=None): + def __init__(self, id, ctr, flags=None): if ctr != None and flags != None: - Packet.__init__(self, 'a', id, ctr) + Packet.__init__(self, 'a', id) + self.ctr = ctr else: message = id Packet.__init__(self, message) flags = ord(message[11]) self.flags = flags + self.priority = 3 def toMessage(self): message = Packet.toMessage(self) @@ -125,17 +130,17 @@ class Ack(Packet): return s class Nickrequest(Packet): - def __init__(self, id, ctr): - Packet.__init__(self, 'N', id, ctr) + def __init__(self, id): + Packet.__init__(self, 'N', id) def __str__(self): s = "Nickrequest packet with " + self.headerString() return s class Nick(Packet): - def __init__(self, id, ctr=None, flags=None, nick=None): - if ctr != None and flags != None and nick != None: - Packet.__init__(self, 'n', id, ctr) + def __init__(self, id, flags=None, nick=None): + if flags != None and nick != None: + Packet.__init__(self, 'n', id) else: message = id Packet.__init__(self, message) @@ -159,8 +164,8 @@ class Nick(Packet): return s class Text(Packet): - def __init__(self, id, ctr, x, y, flags, text): - Packet.__init__(self, 'T', id, ctr) + def __init__(self, id, x, y, flags, text): + Packet.__init__(self, 'T', id) self.x = x self.y = y self. flags = flags diff --git a/tools/game/simpletest.py b/tools/game/simpletest.py index cff1019..c0b1c80 100644 --- a/tools/game/simpletest.py +++ b/tools/game/simpletest.py @@ -1,27 +1,23 @@ -import r0ketrem0te.rem0te +import r0ketrem0te.game +import r0ketrem0te.bridge import r0ketrem0te.packets import time import Queue -announcequeue = Queue.Queue() -r = r0ketrem0te.rem0te.Bridge('/dev/ttyACM0') +def receivedPacket(packet): + if isinstance(packet, r0ketrem0te.packets.Join): + # flags = 1: join ok + # flags = 0: join not ok + ack = r0ketrem0te.packets.Ack(packet.id, packet.ctr, 1) + qp = r0ketrem0te.bridge.QueuePacket(game.channel, game.playermac, False, ack) + game.bridge.putInQueue(queue, qp) -r.registerQueue(announcequeue) +game = r0ketrem0te.game.Game('/dev/ttyACM0', "testgame", 83, 81, (1,2,3,2,1)) -a = r0ketrem0te.packets.Announce(0,2,(1,2,3,2,1), 81, 1, 0, "testgame") -aq = r0ketrem0te.rem0te.QueuePacket(81, (1,2,3,2,1), False, a) -aq.priority = 4 +queue = Queue.Queue() +game.bridge.registerQueue(queue) +game.bridge.registerCallback(receivedPacket) while True: - r.putInQueue(announcequeue, aq) - for i in range(1,1000): - if r.gotPacket(): - packet = r.getPacket() - if isinstance(packet, r0ketrem0te.packets.Join): - r.sendPacket(r0ketrem0te.packets.Ack(packet.id, packet.ctr, 1)) - time.sleep(.001) - - - - + time.sleep(1)