def test_serialize_playerinfo(self): #print("") ia = remuco.ItemAction("ia_l", multiple=True) pi = data.PlayerInfo("dings", 123, 4, [ia], ["sm1", "sm2"]) #self.__serialize(pi) serial.pack(pi)
def build_message(id, serializable): """Create a message ready to send on a socket. @param id: message id @param serializable: message content (object of type Serializable) @return: the message as a binary string or None if serialization failed """ # This is not included in ClientConnection.send() because if there are # multiple clients, each client would serialize the data to send again. # Using this method, a message can be serialized once and send to many # clients. if serializable is not None: ba = serial.pack(serializable) if ba is None: log.warning("failed to serialize (msg-id %d)" % id) return None else: ba = "" header = struct.pack("!hi", id, len(ba)) return "%s%s" % (header, ba)
def test_serialize_itemlist(self): #print("") path = [ "path", "to" ] nested = [ "n1", "n2" ] ids = ["id1", "id2", "id3" ] names = [ "na1", "na2", "na3" ] ia1 = remuco.ItemAction("ia1_l", multiple=True) ia2 = remuco.ItemAction("ia2_l", multiple=False) ias = [ ia1, ia2 ] la1 = remuco.ListAction("la1_l") las = [ la1 ] il = data.ItemList(1, path, nested, ids, names, 0, 1, 2, ias, las) #self.__serialize(il) serial.pack(il) # --------------------------------------------------------------------- path = [ ] nested = [ ] ids = None names = [ "na1", "na2", "na3" ] ia1 = remuco.ItemAction("ia1_l", multiple=True) ia2 = remuco.ItemAction("ia2_l", multiple=False) ias = [ ia1, ia2 ] #la1 = remuco.ListAction("la1_l") #las = [ la1 ] las = None il = data.ItemList(2, path, nested, ids, names, 0, 1, 2, ias, las) #self.__serialize(il) serial.pack(il)
def test_serialize_itemlist(self): #print("") path = ["path", "to"] nested = ["n1", "n2"] ids = ["id1", "id2", "id3"] names = ["na1", "na2", "na3"] ia1 = remuco.ItemAction("ia1_l", multiple=True) ia2 = remuco.ItemAction("ia2_l", multiple=False) ias = [ia1, ia2] la1 = remuco.ListAction("la1_l") las = [la1] il = data.ItemList(1, path, nested, ids, names, 0, 1, 2, ias, las) #self.__serialize(il) serial.pack(il) # --------------------------------------------------------------------- path = [] nested = [] ids = None names = ["na1", "na2", "na3"] ia1 = remuco.ItemAction("ia1_l", multiple=True) ia2 = remuco.ItemAction("ia2_l", multiple=False) ias = [ia1, ia2] #la1 = remuco.ListAction("la1_l") #las = [ la1 ] las = None il = data.ItemList(2, path, nested, ids, names, 0, 1, 2, ias, las) #self.__serialize(il) serial.pack(il)
def __serialize_and_dump(self, ser): print("--> serializable:\n%s" % ser) bindata = serial.pack(ser) print("--> binary data:") out = "" counter = 0 for byte in bindata: counter += 1 out = "%s %02X" % (out, ord(byte)) if counter % 32 == 0: out = "%s\n" % out print(out) return bindata
def test_serialize_deserialize(self): sc1 = _SerialzableClass() sc1.init() #bindata = self.__serialize(sc1) bindata = serial.pack(sc1) self.assertFalse(bindata is None) sc2 = _SerialzableClass() serial.unpack(sc2, bindata) self.assertFalse(sc2.sa1 is None) self.assertTrue(len(sc2.sa1) > 2) self.assertEquals(sc2.sa1[2], "") # None becomes empty string sc2.sa1[2] = None self.assertEquals(sc2.ia2, []) # None becomes empty list sc2.ia2 = None self.assertEquals(sc1, sc2) sc3 = serial.unpack(_SerialzableClass, "%strash" % bindata) self.assertTrue(sc3 is None) sc3 = serial.unpack(_SerialzableClass, "df") self.assertTrue(sc3 is None) sc3 = serial.unpack(_SerialzableClass(), "dfäsadfasd") self.assertTrue(sc3 is None) sc3 = serial.unpack(_SerialzableClass, "") self.assertTrue(sc3 is None) sc3 = serial.unpack(_SerialzableClass(), None) self.assertTrue(sc3 is None)