def answer_to_gamecontroller(self, peer): """ Sends a life sign to the game controller """ return_message = 0 if self.man_penalize else 2 data = Container(header="RGrt", version=GAME_CONTROLLER_RESPONSE_VERSION, team=self.team, player=self.player, message=return_message) try: destination = peer[0], GAME_CONTROLLER_ANSWER_PORT self.socket.sendto(ReturnData.build(data), destination) except Exception as e: logger.log("Network Error: %s" % str(e))
def answer_to_gamecontroller(self, peer): """ Sends a life sign to the game controller """ return_message = 0 if self.man_penalize else 2 data = Container(header=b"RGrt", version=GAME_CONTROLLER_RESPONSE_VERSION, team=self.team, player=self.player, message=return_message) try: destination = peer[0], self.answer_port rospy.logdebug('Sending answer to {} port {}'.format( destination[0], destination[1])) self.socket.sendto(ReturnData.build(data), destination) except Exception as e: rospy.logerr("Network Error: %s" % str(e))
def answer_to_gamecontroller(self, peer): """ 向GameController发送生命信号(life sign) """ return_message = 0 if self.man_penalize else 2 data = Container( header=b"RGrt", # 设置ReturnData消息头 version=GAME_CONTROLLER_RESPONSE_VERSION, # 设置该比赛消息结构版本号 team_number=self.team_number, # 设置该球队号码 player_number=self.player_number, # 设置该队员号码 message=return_message # 设置要返回的消息 ) try: destination = peer[0], GAME_CONTROLLER_ANSWER_PORT self.socket.sendto(ReturnData.build(data), destination) except Exception as e: logger.log("Network Error: %s" % str(e))
def answer_to_gamecontroller(self, peer): if self.is_goal_keeper: return_message = 3 else: return_message = 2 if self.execute_game_interruption: return_message = 4 data = Container(header=b"RGrt", version=GAME_CONTROLLER_RESPONSE_VERSION, team=self.team_id, player=self.robot_id, message=return_message) destination = peer[0], self.GAME_CONTROLLER_ANSWER_PORT self.send_socket.sendto(ReturnData.build(data), destination)
def main(): udpSocket = socket(AF_INET, SOCK_DGRAM) try: #tenta bindar no endereço de broadcast da rede, muda de acordo com as configurações dela. Se o programa não for matado corretamente (ex. dar ctrl+c em vez de ctrl+\), não vai dar pra bindar nessa porta, pois ela já vai estar ocupada udpSocket.bind(('169.254.255.255', GAME_CONTROLLER_LISTEN_PORT)) except Exception as identifier: logging.exception("Bind error") if (identifier.errno == 98): #addr already in use return try: while (1): #tenta catar pacotes com o tamanho setado no game controller data, peer = udpSocket.recvfrom(GameState.sizeof()) logging.debug(len(data)) #transforma dos dados puros que foram recebidos na porta para a "struct" de python. parsed_state = GameState.parse(data) #Checagem para a troca de lado ao trocar o tempo if (int(parsed_state.teams[0].team_number) == 31): unboard.teamNumber = 0 else: unboard.teamNumber = 1 # logging.debug("packet from broadcast is :" + peer[0]) # for structKey, structValue in thisState.iteritems(): # logging.info("key: {} in struct recieved is {}".format( # structKey, structValue)) #loggando alguns dados para saber se está recebendo o pacote corretamente logging.debug("gamephase : {}".format(parsed_state.gamePhase)) logging.debug("game_state : {}".format(parsed_state.game_state)) logging.debug("first_half : {}".format(parsed_state.first_half)) logging.debug("secsRemaining : {}".format( parsed_state.secsRemaining)) logging.debug("penalty : {}".format(parsed_state.teams[ unboard.teamNumber].players[unboard.playerNumber].penalty)) #update unboard unboard.gameState = parsed_state.game_state # print("1", unboard.gameState) unboard.penalty = parsed_state.teams[unboard.teamNumber].players[ unboard.playerNumber].penalty #dados de retorn para o GC data = Container( header=b"RGrt", version=GAME_CONTROLLER_RESPONSE_VERSION, team=31, #UnBeatables number player=1, message=0) try: destination = peer[0], GAME_CONTROLLER_ANSWER_PORT udpSocket.sendto(ReturnData.build(data), destination) except Exception as e: logger.exception("Network Error: %s" % str(e)) finally: #fecha a conexão se o codigo sair do while para poder ser reaberta dps try: udpSocket.shutdown(SHUT_RDWR) except Exception as ex: logging.exception("could not shutdown") udpSocket.close()
def main(): udpSocket = socket(AF_INET, SOCK_DGRAM) try: #tenta bindar no endereco de broadcast da rede, muda de acordo com as #configuracoes dela. Se o programa nao for matado corretamente ( #ex. dar ctrl+c em vez de ctrl+\), nao vai dar pra bindar nessa porta, #pois ela ja vai estar ocupada #leds.off('LeftFaceLeds') leds.off('LeftFaceLeds') leds.on('LeftFaceLedsGreen') #leds.on('LefFootLedsGreen') udpSocket.bind((confNAO.ip_gm, GAME_CONTROLLER_LISTEN_PORT)) print("Conectou!!!") except Exception as identifier: print("Bind error") #leds.off('LeftFaceLeds') leds.on('LeftFaceLedsRed') #leds.on('LeftFootLedsRed') #if (identifier.erro == 98): #addr already in use # return try: while (1): #tenta catar pacotes com o tamanho setado no game controller data, peer = udpSocket.recvfrom(GameState.sizeof()) #logging.debug(len(data)) #transforma dos dados puros que foram recebidos na porta para a "struct" de python. parsed_state = GameState.parse(data) #Checagem para a troca de lado ao trocar o tempo if (int(parsed_state.teams[0].team_number) == confNAO.team_number): teamNumber = 0 else: teamNumber = 1 # logging.debug("packet from broadcast is :" + peer[0]) # for structKey, structValue in thisState.iteritems(): # logging.info("key: {} in struct recieved is {}".format( # structKey, structValue)) #loggando alguns dados para saber se esta recebendo o pacote corretamente #print("gamephase : {}".format(parsed_state.gamePhase)) #print("game_state : {}".format(parsed_state.game_state)) memoria.insertData("gamestate", parsed_state.game_state) #print("first_half : {}".format(parsed_state.first_half)) #print("secsRemaining : {}".format(parsed_state.secsRemaining)) memoria.insertData("secsRemaining", parsed_state.secsRemaining) #print("penalty : {}".format(parsed_state.teams[teamNumber].players[confNAO.player_number].penalty)) memoria.insertData( "penalty", parsed_state.teams[teamNumber].players[ confNAO.player_number].penalty) #print("pickup : {}".format(parsed_state.teams[teamNumber].players[confNAO.player_number].pick_up)) if parsed_state.game_state == "STATE_FINISHED": leds.off('LeftFaceLeds') leds.on('LeftFaceLedsRed') break #update unboard #print(parsed_state.game_state) # print("1", unboard.gameState) #print( parsed_state.teams[teamNumber].players[confNAO.player_number].penalty) #dados de retorn para o GC data = Container( header=b"RGrt", version=GAME_CONTROLLER_RESPONSE_VERSION, team=confNAO.team_number, #50, #UnBeatables number player=confNAO.player_number, #1, message=0) try: destination = peer[0], GAME_CONTROLLER_ANSWER_PORT udpSocket.sendto(ReturnData.build(data), destination) except Exception as e: print("Network Error: %s" % str(e)) finally: #fecha a conexao se o codigo sair do while para poder ser reaberta dps try: udpSocket.shutdown(SHUT_RDWR) except Exception as ex: print("could not shutdown") udpSocket.close()