Beispiel #1
0
    def onMessage(self, msg, binary):
        if (binary):
            messages = unpack_mesages(msg)

            if (not hasattr(self, "interface")):
                try:
                    if (messages[0]['action'] == 'init'):
                        self.onWantPlay()
                except (IndexError, KeyError):
                    logging.warning(
                        "Got garbage message {}, discarding".format(msg))
            else:
                for msg in messages:
                    self.interface.trigger('useraction', msg)
        else:
            try:
                msg = simplejson.loads(msg)
                self.interface.trigger('usermsg', msg)
            except Exception:
                #ping pong
                if (msg == "?"):
                    self.sendMessage("!")
                else:
                    logging.warning(
                        "Unable to decode meta msg %s coming from the client" %
                        msg)
	def test_message_unpacking(self):
		x1 = 12
		y1 = 4.44
		dir1 = DirectionMessageMixin.direction_lookup[directions['west']]
		packed = struct.pack("!I", x1) + struct.pack("!f", y1) + struct.pack("!B", dir1)
		x2 = 42
		y2 = 9.987654
		dir2 = DirectionMessageMixin.direction_lookup[directions['north']]
		packed = packed + struct.pack("!I", x2) + struct.pack("!f", y2) + struct.pack("!B", dir2) + "\n"

		packed = struct.pack("!B", 255) + packed

		messages = unpack_mesages(packed, message_types=message_types)
		self.assertEqual(len(messages), 2)
		self.assertEqual(messages[0]['x'], x1)
		self.assertAlmostEqual(messages[0]['y'], y1, places=4)
		self.assertEqual(messages[0]['direction'], directions['west'])
		self.assertEqual(messages[1]['x'], x2)
		self.assertAlmostEqual(messages[1]['y'], y2, places=4)
		self.assertEqual(messages[1]['direction'], directions['north'])
Beispiel #3
0
    def test_message_unpacking(self):
        x1 = 12
        y1 = 4.44
        dir1 = DirectionMessageMixin.direction_lookup[directions['west']]
        packed = struct.pack("!I", x1) + struct.pack("!f", y1) + struct.pack(
            "!B", dir1)
        x2 = 42
        y2 = 9.987654
        dir2 = DirectionMessageMixin.direction_lookup[directions['north']]
        packed = packed + struct.pack("!I", x2) + struct.pack(
            "!f", y2) + struct.pack("!B", dir2) + "\n"

        packed = struct.pack("!B", 255) + packed

        messages = unpack_mesages(packed, message_types=message_types)
        self.assertEqual(len(messages), 2)
        self.assertEqual(messages[0]['x'], x1)
        self.assertAlmostEqual(messages[0]['y'], y1, places=4)
        self.assertEqual(messages[0]['direction'], directions['west'])
        self.assertEqual(messages[1]['x'], x2)
        self.assertAlmostEqual(messages[1]['y'], y2, places=4)
        self.assertEqual(messages[1]['direction'], directions['north'])
Beispiel #4
0
	def onMessage(self, msg, binary):
		if(binary):
			messages = unpack_mesages(msg)

			if(not hasattr(self, "interface")):
				try:
					if(messages[0]['action'] == 'init'):
						self.onWantPlay()
				except (IndexError, KeyError):
					logging.warning("Got garbage message {}, discarding".format(msg))
			else:
				for msg in messages:
					self.interface.trigger('useraction', msg)
		else:
			try:
				msg = simplejson.loads(msg)
				self.interface.trigger('usermsg', msg)
			except Exception:
				#ping pong
				if(msg == "?"):
					self.sendMessage("!")
				else:
					logging.warning("Unable to decode meta msg %s coming from the client" % msg)