remote: added game class and callbacks

This commit is contained in:
schneider 2011-12-15 19:09:05 +01:00
parent 6609d3253c
commit e994e7fb24
4 changed files with 102 additions and 53 deletions

View File

@ -10,7 +10,7 @@ class QueuePacket:
self.mac = mac self.mac = mac
self.acked = acked self.acked = acked
self.packet = packet self.packet = packet
self.priority = 5 self.priority = packet.priority
self.retriesleft = 5 self.retriesleft = 5
self.timeout = 0.1 self.timeout = 0.1
self.timer = None self.timer = None
@ -35,7 +35,7 @@ class QueuePacket:
def sent(self, timeoutcallback): def sent(self, timeoutcallback):
with self.lock: with self.lock:
self.timedout = False self.timedout = False
if self.retrieslef > 0: if self.retriesleft > 0:
self.retriesleft-=1 self.retriesleft-=1
if self.acked: if self.acked:
self.timeoutcallback = timeoutcallback self.timeoutcallback = timeoutcallback
@ -61,7 +61,7 @@ class QueuePacket:
self.callback('timeout') self.callback('timeout')
class Bridge: class Bridge:
def __init__(self, path2device): def __init__(self, path2device, channel, rxmac):
self.ser = serialinterface.SerialInterface(path2device, 115200, 0) self.ser = serialinterface.SerialInterface(path2device, 115200, 0)
self.free = threading.Lock() self.free = threading.Lock()
self.queueslock = threading.Lock() self.queueslock = threading.Lock()
@ -69,6 +69,7 @@ class Bridge:
self.outpackets = Queue.Queue() self.outpackets = Queue.Queue()
self.crc = crcmod.predefined.mkCrcFun('crc-ccitt-false') self.crc = crcmod.predefined.mkCrcFun('crc-ccitt-false')
self.queues = {} self.queues = {}
self.callbacks = []
self.reader = threading.Thread(target = self.readerThread) self.reader = threading.Thread(target = self.readerThread)
self.reader.daemon = True self.reader.daemon = True
@ -83,11 +84,16 @@ class Bridge:
self.txmac = None self.txmac = None
self.rxmac = None self.rxmac = None
self.channel = None self.channel = None
self.gameChannel = channel
self.setPacketLength(0x20) self.setPacketLength(0x20)
self.setTxMAC((1,2,3,2,1)) self.setRxMAC(rxmac)
self.setRxMAC((1,2,3,2,1)) self.setChannel(channel)
self.setChannel(81)
self.ctr = 0
def registerCallback(self, callback):
if callback not in self.callbacks:
self.callbacks.append(callback)
def registerQueue(self, queue): def registerQueue(self, queue):
if queue not in self.queues: if queue not in self.queues:
@ -131,6 +137,9 @@ class Bridge:
qp = self.queues[q] qp = self.queues[q]
if qp == None and not q.empty(): if qp == None and not q.empty():
qp = q.get() qp = q.get()
if not isinstance(qp.packet,packets.Ack):
self.ctr+=1
qp.packet.ctr = self.ctr
self.queues[q] = qp self.queues[q] = qp
self.outpackets.put(qp) self.outpackets.put(qp)
@ -140,9 +149,9 @@ class Bridge:
#wait until we have packets to take care of #wait until we have packets to take care of
qp = self.outpackets.get() qp = self.outpackets.get()
#send it and notify the queuepacket #send it and notify the queuepacket
self.sendPacket(qp.packet) self.setTxMAC(qp.mac)
self.setTxMac(qp.mac)
self.setChannel(qp.channel) self.setChannel(qp.channel)
self.sendPacket(qp.packet)
qp.sent(self.packetTimeout) qp.sent(self.packetTimeout)
self.setChannel(self.gameChannel) self.setChannel(self.gameChannel)
except Exception as e: except Exception as e:
@ -171,13 +180,15 @@ class Bridge:
if isinstance(packet,packets.Ack): if isinstance(packet,packets.Ack):
self.ProcessAck(packet) self.ProcessAck(packet)
else: else:
self.packets.put(packet) for callback in self.callbacks:
callback(packet)
#self.packets.put(packet)
def gotPacket(self): # def gotPacket(self):
return not self.packets.empty() # return not self.packets.empty()
def getPacket(self): # def getPacket(self):
return self.packets.get() # return self.packets.get()
def sendPacket(self, packet): def sendPacket(self, packet):
print 'sending', packet print 'sending', packet
@ -197,9 +208,10 @@ class Bridge:
self.packetLength = length self.packetLength = length
def setTxMAC(self, mac): def setTxMAC(self, mac):
if mac == self.txmax: if mac == self.txmac:
return return
self.free.acquire() self.free.acquire()
print "setting tx mac", mac
self.ser.writeMessage('3', ''.join([chr(x) for x in mac])) self.ser.writeMessage('3', ''.join([chr(x) for x in mac]))
self.txmac = mac self.txmac = mac
@ -207,6 +219,7 @@ class Bridge:
if mac == self.rxmac: if mac == self.rxmac:
return return
self.free.acquire() self.free.acquire()
print "setting rx mac", mac
self.ser.writeMessage('4', ''.join([chr(x) for x in mac])) self.ser.writeMessage('4', ''.join([chr(x) for x in mac]))
self.rxmac = mac self.rxmac = mac
@ -214,6 +227,7 @@ class Bridge:
if channel == self.channel: if channel == self.channel:
return return
self.free.acquire() self.free.acquire()
print "setting channel", channel
self.ser.writeMessage('5', '%c'%channel) self.ser.writeMessage('5', '%c'%channel)
self.channel = channel self.channel = channel

View File

@ -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()

View File

@ -5,17 +5,18 @@ def uint32toint(v):
return (ord(v[3])<< 24) + (ord(v[2])<<16) + (ord(v[1])<<8) + (ord(v[0])) return (ord(v[3])<< 24) + (ord(v[2])<<16) + (ord(v[1])<<8) + (ord(v[0]))
class Packet: class Packet:
def __init__(self, command, id=None, ctr=None): def __init__(self, command, id=None):
if id == None and ctr == None: self.ctr = 0
if id == None:
message = command message = command
command = message[2] command = message[2]
id = uint32toint(message[3:7]) id = uint32toint(message[3:7])
ctr = uint32toint(message[7:11]) self.ctr = uint32toint(message[7:11])
self.length = 32 self.length = 32
self.protocol = 'G' self.protocol = 'G'
self.command = command self.command = command
self.id = id self.id = id
self.ctr = ctr self.priority = 5
def toMessage(self): def toMessage(self):
message = chr(self.length) message = chr(self.length)
@ -35,9 +36,9 @@ class Packet:
return s return s
class Button(Packet): class Button(Packet):
def __init__(self, id, ctr=None, button=None): def __init__(self, id, button=None):
if ctr != None and button!= None: if button!= None:
Packet.__init__(self, 'B', id, ctr) Packet.__init__(self, 'B', id)
else: else:
message = id message = id
Packet.__init__(self, message) Packet.__init__(self, message)
@ -54,13 +55,15 @@ class Button(Packet):
return s return s
class Announce(Packet): class Announce(Packet):
def __init__(self, id, ctr, gameMac, gameChannel, gameId, gameFlags, gameTitle): def __init__(self, gameMac, gameChannel, gameId, gameFlags, gameTitle):
Packet.__init__(self, 'A', id, ctr) #always a broadcast
Packet.__init__(self, 'A', 0)
self.gameMac = gameMac self.gameMac = gameMac
self.gameChannel = gameChannel self.gameChannel = gameChannel
self.gameId = gameId self.gameId = gameId
self.gameFlags = gameFlags self.gameFlags = gameFlags
self.gameTitle = gameTitle[0:8] self.gameTitle = gameTitle[0:8]
self.priority = 3
def toMessage(self): def toMessage(self):
message = Packet.toMessage(self) message = Packet.toMessage(self)
@ -83,9 +86,9 @@ class Announce(Packet):
return s return s
class Join(Packet): class Join(Packet):
def __init__(self, id, ctr=None, gameId=None): def __init__(self, id, gameId=None):
if ctr != None and gameId != None: if gameId != None:
Packet.__init__(self, 'J', id, ctr) Packet.__init__(self, 'J', id)
else: else:
message = id message = id
Packet.__init__(self, message) Packet.__init__(self, message)
@ -104,14 +107,16 @@ class Join(Packet):
return s return s
class Ack(Packet): class Ack(Packet):
def __init__(self, id, ctr=None, flags=None): def __init__(self, id, ctr, flags=None):
if ctr != None and flags != None: if ctr != None and flags != None:
Packet.__init__(self, 'a', id, ctr) Packet.__init__(self, 'a', id)
self.ctr = ctr
else: else:
message = id message = id
Packet.__init__(self, message) Packet.__init__(self, message)
flags = ord(message[11]) flags = ord(message[11])
self.flags = flags self.flags = flags
self.priority = 3
def toMessage(self): def toMessage(self):
message = Packet.toMessage(self) message = Packet.toMessage(self)
@ -125,17 +130,17 @@ class Ack(Packet):
return s return s
class Nickrequest(Packet): class Nickrequest(Packet):
def __init__(self, id, ctr): def __init__(self, id):
Packet.__init__(self, 'N', id, ctr) Packet.__init__(self, 'N', id)
def __str__(self): def __str__(self):
s = "Nickrequest packet with " + self.headerString() s = "Nickrequest packet with " + self.headerString()
return s return s
class Nick(Packet): class Nick(Packet):
def __init__(self, id, ctr=None, flags=None, nick=None): def __init__(self, id, flags=None, nick=None):
if ctr != None and flags != None and nick != None: if flags != None and nick != None:
Packet.__init__(self, 'n', id, ctr) Packet.__init__(self, 'n', id)
else: else:
message = id message = id
Packet.__init__(self, message) Packet.__init__(self, message)
@ -159,8 +164,8 @@ class Nick(Packet):
return s return s
class Text(Packet): class Text(Packet):
def __init__(self, id, ctr, x, y, flags, text): def __init__(self, id, x, y, flags, text):
Packet.__init__(self, 'T', id, ctr) Packet.__init__(self, 'T', id)
self.x = x self.x = x
self.y = y self.y = y
self. flags = flags self. flags = flags

View File

@ -1,27 +1,23 @@
import r0ketrem0te.rem0te import r0ketrem0te.game
import r0ketrem0te.bridge
import r0ketrem0te.packets import r0ketrem0te.packets
import time import time
import Queue import Queue
announcequeue = Queue.Queue() def receivedPacket(packet):
r = r0ketrem0te.rem0te.Bridge('/dev/ttyACM0') 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") queue = Queue.Queue()
aq = r0ketrem0te.rem0te.QueuePacket(81, (1,2,3,2,1), False, a) game.bridge.registerQueue(queue)
aq.priority = 4 game.bridge.registerCallback(receivedPacket)
while True: while True:
r.putInQueue(announcequeue, aq) time.sleep(1)
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)