Esempio n. 1
0
File: Gen.py Progetto: broly/HaloNet
    async def start(self):
        self.db, _ = await self.async_wakeup_service_locally_by_name("DBApp")
        if is_valid(self.db):
            if CommandLine.get_arguments().clearall:
                await self.db.ClearDatabase()
                return

            for cls_name, cls_info_by_context in code_gen.classes_info.items():
                if 'base' in cls_info_by_context:
                    cls_info = cls_info_by_context['base']
                    await self.update_class_properties(cls_name,
                                                       cls_info['Properties'])

            for storage_name, storage_info in code_gen.storage_info.items():
                await self.update_storage(storage_name, storage_info['Type'])

            await self.db.Synchronize()

            enums = dict()
            for name, info in code_gen.types_info.items():
                if info['Kind'] == "Enum":
                    enums[name] = info['Members'].keys()
            # if enums:
            #     await self.update_enums(enums)
            self.db.Terminate()
        else:
            ERROR_MSG("Failed to connect to DB")
Esempio n. 2
0
    async def load_table(self):
        if not is_valid(self.db):
            self.failed = True
            return

        class_name = self.type.__name__
        T = self.find_type(class_name)
        serialized_data = await self.db.GetStorageData(self.name, class_name)
        # self.md5hash = hashlib.md5(serialized_data).hexdigest()
        self.data_as_list = TArray[T].deserialize(serialized_data)

        data = json.loads(json.dumps(self.data_as_list,
                                     cls=JSONHaloNetEncoder))
        import pickle
        self.md5hash = hashlib.md5(pickle.dumps(data)).hexdigest()

        self.data = dict()
        for entry in self.data_as_list:
            if self.primary_key in entry:
                self.data[entry[self.primary_key]] = entry
                self.indices.add(entry[self.primary_key])
            else:
                self.primary_key = list(entry.keys())[0]
                self.data[entry[self.primary_key]] = entry
                self.indices.add(entry[self.primary_key])
Esempio n. 3
0
 async def create_client_connection(self, endpoint, on_lost=None):
     INFO_MSG(f"{endpoint}")
     if endpoint not in self.tcp_server.clients:
         _, connection = await create_connection(
             endpoint, on_lost=on_lost
         )  #  await TCPClient(endpoint, ClientConnectionHandler, do_open_connection=True)
         if is_valid(connection):
             self.tcp_server.clients[endpoint] = connection
             connection.add_lost_callback(
                 partial(self.on_lost_client, endpoint))
         return connection
     else:
         self.tcp_server.clients[endpoint].add_lost_callback(on_lost)
     connection = self.tcp_server.clients[endpoint]
     connection.add_lost_callback(partial(self.on_lost_client, endpoint))
     if not is_valid(connection):
         del self.tcp_server.clients[endpoint]
     return connection
Esempio n. 4
0
 async def send(self, msg):
     INFO_MSG("Sending to", self.writer.transport._extra['peername'], msg)
     if is_valid(self):
         self.writer.write(struct.pack('Q', len(msg)) + msg)
         await self.writer.drain()
         INFO_MSG("SENT")
         return True
     ERROR_MSG("Unable to send info. Not connected")
     return False
Esempio n. 5
0
    async def load_persistents(self):
        if not is_valid(self.db):
            self.failed = True
            return

        class_name = self.__class__.__name__
        serialized_data = await self.db.GetEntityData(self.dbid, class_name)
        srp = BinarySerialization(serialized_data).proxy()
        for prop_name, prop_info in self.properties.items():
            if Persistent in prop_info.specifiers:
                if not issubclass(prop_info.prop_type, BaseEntity):
                    prop_value = srp >> bytes
                    if "FDateTime" in prop_info.prop_type.__name__:
                        print(",,,")
                    deserialized = prop_info.prop_type.deserialize(prop_value)
                    T = prop_info.prop_type
                    value = T.instantiate(deserialized)

                    value.set_db_interface(
                        DatabaseVariableProxy(self.db, self.dbid, class_name,
                                              prop_name, prop_info))

                    value.initialize_property(self, prop_name)
                    self.properties_locks[prop_name] = asyncio.Lock()
                else:
                    prop_value = srp >> int32
                    value = EntitiesDispatcher().get_by_dbid(prop_value)
                    if value is None:
                        is_new_entity = False
                        if prop_value == 0:
                            prop_value = await self.db.CreateDefaultEntity(
                                prop_info.prop_type.__name__)
                            is_new_entity = True

                        value = await prop_info.prop_type(prop_value,
                                                          outer=self)
                        value.initialize_property(self, prop_name)
                        value.set_db_interface(
                            DatabaseVariableProxy(self.db, self.dbid,
                                                  class_name, prop_name,
                                                  prop_info))

                        if is_new_entity:
                            await value.on_entity_created()

                        if is_new_entity:
                            await value.async_save()

                # ERROR_MSG("Lolz %s %s" % (prop_name, value))
                # if issubclass(prop_info.prop_type, BaseEntity):
                #     if value
                self.set_property_value(prop_name, value)
Esempio n. 6
0
 def __isvalid__(self):
     return super().__isvalid__() and \
            is_valid(self.client_connection)
Esempio n. 7
0
 async def client():
     client = await TCPClient(('127.0.0.1', 8888), handler)
     if is_valid(client):
         await client.send(b"Yo!")