def test_compress_decompress(self): bytes = range(128) * 10000 string = str(bytearray(bytes)) encoded = pngcompression.encode(string) self.assertNotEqual(string, encoded) decoded = pngcompression.decode(encoded) self.assertEqual(string, decoded)
def test_compress_decompress(self): bytes = list(range(128)) * 10000 string = str(bytearray(bytes)) encoded = pngcompression.encode(string) self.assertNotEqual(string, encoded) decoded = pngcompression.decode(encoded) self.assertEqual(string, decoded)
def publish(self, topic, message, fragment_size=None, compression="none"): """ Publish a message to the client Keyword arguments: topic -- the topic to publish the message on message -- a dict of key-value pairs. Will be wrapped in a message with opcode publish fragment_size -- (optional) fragment the serialized message into msgs with payloads not greater than this value compression -- (optional) compress the message. valid values are 'png' and 'none' """ # TODO: fragmentation, proper ids if Subscribe.topics_glob and Subscribe.topics_glob: self.protocol.log("debug", "Topic security glob enabled, checking topic: " + topic) match = False for glob in Subscribe.topics_glob: if (fnmatch.fnmatch(topic, glob)): self.protocol.log("debug", "Found match with glob " + glob + ", continuing topic publish...") match = True break if not match: self.protocol.log("warn", "No match found for topic, cancelling topic publish to: " + topic) return else: self.protocol.log("debug", "No topic security glob, not checking topic publish.") outgoing_msg = {"op": "publish", "topic": topic, "msg": message} if compression == "png": outgoing_msg_dumped = dumps(outgoing_msg) outgoing_msg = {"op": "png", "topic": topic, "data": encode(outgoing_msg_dumped)} self.protocol.send(outgoing_msg)
def publish(self, topic, message, fragment_size=None, compression="none"): """ Publish a message to the client Keyword arguments: topic -- the topic to publish the message on message -- a dict of key-value pairs. Will be wrapped in a message with opcode publish fragment_size -- (optional) fragment the serialized message into msgs with payloads not greater than this value compression -- (optional) compress the message. valid values are 'png' and 'none' """ # TODO: fragmentation, proper ids if Subscribe.topics_glob and Subscribe.topics_glob: self.protocol.log("debug", "Topic security glob enabled, checking topic: " + topic) match = False for glob in Subscribe.topics_glob: if (fnmatch.fnmatch(topic, glob)): self.protocol.log("debug", "Found match with glob " + glob + ", continuing topic publish...") match = True break if not match: self.protocol.log("warn", "No match found for topic, cancelling topic publish to: " + topic) return else: self.protocol.log("debug", "No topic security glob, not checking topic publish.") outgoing_msg = {"op": "publish", "topic": topic, "msg": message} if compression == "png": outgoing_msg_dumped = dumps(outgoing_msg) outgoing_msg = {"op": "png", "data": encode(outgoing_msg_dumped)} self.protocol.send(outgoing_msg)
def test_compress(self): bytes = list(range(128)) * 10000 string = str(bytearray(bytes)) print "string = " print string encoded = pngcompression.encode(string) print "encoded = " print encoded self.assertNotEqual(string, encoded)
def test_encode_png(self): with open('testDepth.txt', 'r') as outfile: data = outfile.read() #data="Salam1234!--- 'ASS':'12243.384764'" #data = pngcompression.encode(data) encoded = pngcompression.encode(data) with open('encoded_png.txt', 'w') as outfile: outfile.write(encoded) print("done")
def publish(self, topic, message, fragment_size=None, compression="none"): """ Publish a message to the client Keyword arguments: topic -- the topic to publish the message on message -- a dict of key-value pairs. Will be wrapped in a message with opcode publish fragment_size -- (optional) fragment the serialized message into msgs with payloads not greater than this value compression -- (optional) compress the message. valid values are 'png' and 'none' """ # TODO: fragmentation, proper ids outgoing_msg = {"op": "publish", "topic": topic, "msg": message} if compression=="png": outgoing_msg_dumped = dumps(outgoing_msg) outgoing_msg = {"op": "png", "data": encode(outgoing_msg_dumped)} self.protocol.send(outgoing_msg)
def publish(self, topic, message, fragment_size=None, compression="none"): """ Publish a message to the client Keyword arguments: topic -- the topic to publish the message on message -- a dict of key-value pairs. Will be wrapped in a message with opcode publish fragment_size -- (optional) fragment the serialized message into msgs with payloads not greater than this value compression -- (optional) compress the message. valid values are 'png' and 'none' """ # TODO: fragmentation, proper ids outgoing_msg = {"op": "publish", "topic": topic, "msg": message} if compression == "png": outgoing_msg_dumped = dumps(outgoing_msg) outgoing_msg = {"op": "png", "data": encode(outgoing_msg_dumped)} self.protocol.send(outgoing_msg)
def compress(self, topic, data): if self.outgoing_topics_compression_state.get(topic) == "png": data = json.dumps(data) return pngcompression.encode(data) else: return data
def test_compress(self): bytes = range(128) * 10000 string = str(bytearray(bytes)) encoded = pngcompression.encode(string) self.assertNotEqual(string, encoded)