def move(self, ctx): sendersqueue = ctx.fgpsession.getSendersQueue() lastsender = ctx.fgpsession.getLastSender() droppdu = DropPDU() droppdu.setSendersays("Opponent quit!") if lastsender == sendersqueue[0]: ctx.fgpsession.send(droppdu, 0) else: ctx.fgpsession.send(droppdu, 1) ctx.stopListening()
def drop(self, ctx): sendersqueue = ctx.fgpsession.getSendersQueue() lastsender = ctx.fgpsession.getLastSender() if lastsender == sendersqueue[0]: # who sent the drop? reply to the other one. droppdu = DropPDU() droppdu.setSendersays("Opponent quit!") ctx.fgpsession.send(droppdu, 1) ctx.stopListening() else: ctx.setState(DROP_PENDING())
class FGPSession(): def __init__(self, gamesession, sendersqueue): logging.debug("FGPSession") self.gamesession = gamesession self.local_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.local_socket.bind( (host, port) ) self.sendersqueue = sendersqueue self.lastsender = None def getSendersQueue(self): return self.sendersqueue def getLastSender(self): return self.lastsender ''' baseDecode is used to read the type of the received message. It creates a new MessagePDU of that type and calls decode on that message passing in the buf (each PDU knows how to decode itself). ''' def baseDecode(self,buf): logging.debug(buf_debug(buf)) offset = 0 self.msgtype = struct.unpack("H", buf[offset:offset + 2])[0] if self.msgtype == 2: logging.debug("FGPSession received MovePDU") self.movepdu = MovePDU() self.movepdu.decode(buf) self.gamesession.move(self.movepdu) elif self.msgtype == 5: logging.debug("FGPSession received DropPDU") self.droppdu = DropPDU() self.droppdu.decode(buf) self.gamesession.drop(self.droppdu) else: logging.debug("Dude, I did not expect that msg!") #self.queue.pop() # TODO: Needs testing. def send(self, msg, player): buf = msg.encode() destination = self.sendersqueue[player] host = destination[0] port = destination[1] logging.debug("FGPSession transmitting " + msg.name() + " to " + str(host) + ":" + str(port)) self.local_socket.sendto(buf, (host, port)) def listen(self): logging.debug("FGPSession listening...") buf, sender = self.local_socket.recvfrom(4096) self.lastsender = sender self.baseDecode(buf)
class FGPClient(object): def __init__(self, gameclient): logging.debug("FGPClient") self.local_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.local_socket.bind( (host, port) ) self.gameclient = gameclient def setServHost(self,serv_host): self.serv_host = serv_host def setServPort(self,serv_port): self.serv_port = serv_port def baseDecode(self,buf): logging.debug(buf_debug(buf)) offset = 0 self.msgtype = struct.unpack("H", buf[offset:offset + 2])[0] if self.msgtype == 3: logging.debug("FGPClient received UpdatePDU") self.updatepdu = UpdatePDU() self.updatepdu.decode(buf) self.gameclient.update(self.updatepdu) elif self.msgtype == 4: logging.debug("FGPClient received GameOverPDU") self.gameoverpdu = GameOverPDU() self.gameoverpdu.decode(buf) self.gameclient.gameover(self.gameoverpdu) elif self.msgtype == 5: logging.debug("FGPClient received DropPDU") self.droppdu = DropPDU() self.droppdu.decode(buf) self.gameclient.drop(self.droppdu) else: logging.debug("Dude, I did not expect that!") def send(self,msg): buf = msg.encode() logging.debug("FGPClient transmitting " + msg.name()) self.local_socket.sendto(buf, (self.serv_host, self.serv_port)) def listen(self): logging.debug("FGPClient listening...") buf, sender = self.local_socket.recvfrom(4096) self.setServHost(sender[0]) self.setServPort(sender[1]) self.baseDecode(buf)
def baseDecode(self,buf): logging.debug(buf_debug(buf)) offset = 0 self.msgtype = struct.unpack("H", buf[offset:offset + 2])[0] if self.msgtype == 2: logging.debug("FGPSession received MovePDU") self.movepdu = MovePDU() self.movepdu.decode(buf) self.gamesession.move(self.movepdu) elif self.msgtype == 5: logging.debug("FGPSession received DropPDU") self.droppdu = DropPDU() self.droppdu.decode(buf) self.gamesession.drop(self.droppdu) else: logging.debug("Dude, I did not expect that msg!")
def baseDecode(self,buf): logging.debug(buf_debug(buf)) offset = 0 self.msgtype = struct.unpack("H", buf[offset:offset + 2])[0] if self.msgtype == 3: logging.debug("FGPClient received UpdatePDU") self.updatepdu = UpdatePDU() self.updatepdu.decode(buf) self.gameclient.update(self.updatepdu) elif self.msgtype == 4: logging.debug("FGPClient received GameOverPDU") self.gameoverpdu = GameOverPDU() self.gameoverpdu.decode(buf) self.gameclient.gameover(self.gameoverpdu) elif self.msgtype == 5: logging.debug("FGPClient received DropPDU") self.droppdu = DropPDU() self.droppdu.decode(buf) self.gameclient.drop(self.droppdu) else: logging.debug("Dude, I did not expect that!")