Exemplo n.º 1
0
    def handle_query_object_resp(self, message_type, di):
        ctx = di.get_uint32()
        success = di.get_uint8()

        if ctx not in self._callbacks:
            self.notify.warning('Received unexpected %s (ctx %d)' %
                                (MsgId2Names[message_type], ctx))

            return

        try:
            if not success:
                if self._callbacks[ctx]:
                    self._callbacks[ctx](None, None)

                return

            if message_type == types.DBSERVER_OBJECT_GET_ALL_RESP:
                dclass_id = di.get_uint16()
                dclass = self._network.dc_loader.dclasses_by_number.get(
                    dclass_id)
            else:
                dclass = self._dclasses[ctx]

            if not dclass:
                self.notify.error(
                    'Received bad dclass %d in DBSERVER_OBJECT_GET_ALL_RESP' %
                    (dclass_id))

            if message_type == types.DBSERVER_OBJECT_GET_FIELD_RESP:
                field_count = 1
            else:
                field_count = di.get_uint16()

            field_packer = DCPacker()
            field_packer.set_unpack_data(di.get_remaining_bytes())
            fields = {}
            for x in xrange(field_count):
                field_id = field_packer.raw_unpack_uint16()
                field = dclass.get_field_by_index(field_id)

                if not field:
                    self.notify.error(
                        'Received bad field %d in query for %s object' %
                        (field_id, dclass.get_name()))

                field_packer.begin_unpack(field)
                fields[field.get_name()] = field.unpack_args(field_packer)
                field_packer.end_unpack()

            if self._callbacks[ctx]:
                self._callbacks[ctx](dclass, fields)

        finally:
            del self._callbacks[ctx]
            del self._dclasses[ctx]
Exemplo n.º 2
0
    def handle_object_set_field(self, sender, di):
        do_id = di.get_uint32()
        file_object = self._backend.open_file(
            self._backend.get_filename(do_id))

        if not file_object:
            self.notify.warning(
                'Failed to set fields for object: %d, unknown object!' %
                (do_id))

            return

        dc_name = file_object.get_value('dclass')
        dc_class = self.dc_loader.dclasses_by_name.get(dc_name)

        if not dc_class:
            self.notify.warning(
                'Failed to set fields for object: %d, unknown dclass: %s!' %
                (do_id, dc_name))

            return

        fields = file_object.get_value('fields')

        if not fields:
            self.notify.warning(
                'Failed to set fields for object: %d, invalid fields!' %
                (do_id))

            return

        field_packer = DCPacker()
        field_packer.set_unpack_data(di.get_remaining_bytes())
        field_id = field_packer.raw_unpack_uint16()
        field = dc_class.get_field_by_index(field_id)

        if not field:
            self.notify.error(
                'Failed to unpack field: %d dclass: %s, invalid field!' %
                (field_id, dc_class.get_name()))

        field_packer.begin_unpack(field)
        field_args = field.unpack_args(field_packer)
        field_packer.end_unpack()

        if not field_args:
            self.notify.error(
                'Failed to unpack field args for field: %d dclass: %s, invalid result!'
                % (field.get_name(), dc_class.get_name()))

        fields[field.get_name()] = field_args
        file_object.set_value('fields', fields)

        self._backend.close_file(file_object)
Exemplo n.º 3
0
    def handle_create_object(self, sender, di):
        context = di.get_uint32()
        dc_id = di.get_uint16()
        field_count = di.get_uint16()
        dc_class = self.dc_loader.dclasses_by_number.get(dc_id)

        if not dc_class:
            self.notify.error(
                'Failed to create object: %d context: %d, unknown dclass!' %
                (dc_id, context))

        do_id = self._backend.allocator.allocate()
        file_object = self._backend.open_file(
            self._backend.get_filename(do_id))

        file_object.set_value('dclass', dc_class.get_name())
        file_object.set_value('do_id', do_id)

        fields = {}
        field_packer = DCPacker()
        field_packer.set_unpack_data(di.get_remaining_bytes())

        for _ in xrange(field_count):
            field_id = field_packer.raw_unpack_uint16()
            field = dc_class.get_field_by_index(field_id)

            if not field:
                self.notify.error(
                    'Failed to unpack field: %d dclass: %s, invalid field!' %
                    (field_id, dc_class.get_name()))

            field_packer.begin_unpack(field)
            field_args = field.unpack_args(field_packer)
            field_packer.end_unpack()

            if not field_args:
                self.notify.error(
                    'Failed to unpack field args for field: %d dclass: %s, invalid result!'
                    % (field.get_name(), dc_class.get_name()))

            fields[field.get_name()] = field_args

        for index in xrange(dc_class.get_num_inherited_fields()):
            field_packer = DCPacker()
            field = dc_class.get_inherited_field(index)

            if not field:
                continue

            if field.get_name() in fields:
                continue

            if not field.is_db() or not field.has_default_value():
                continue

            field_packer.set_unpack_data(field.get_default_value())
            field_packer.begin_unpack(field)
            field_args = field.unpack_args(field_packer)
            field_packer.end_unpack()

            if not field_args:
                self.notify.error(
                    'Failed to unpack field args for field: %d dclass: %s, invalid result!'
                    % (field.get_name(), dc_class.get_name()))

            fields[field.get_name()] = field_args

        file_object.set_value('fields', fields)

        self._backend.close_file(file_object)
        self._backend.tracker.set_value('next', do_id + 1)

        datagram = io.NetworkDatagram()
        datagram.add_header(sender, self.channel,
                            types.DBSERVER_CREATE_OBJECT_RESP)

        datagram.add_uint32(context)
        datagram.add_uint32(do_id)
        self.handle_send_connection_datagram(datagram)