From a2168ef165a6a68b24d7fc16019949d7c515ce3a Mon Sep 17 00:00:00 2001 From: Kestred Date: Sat, 8 Nov 2014 21:57:30 -0800 Subject: [PATCH] Use new Bamboo bindings in DistributedObject --- astron/distributed_object.py | 230 ++++++++--------------------------- astron/object_repository.py | 44 +++---- 2 files changed, 71 insertions(+), 203 deletions(-) diff --git a/astron/distributed_object.py b/astron/distributed_object.py index 4b879d2..f7facdc 100644 --- a/astron/distributed_object.py +++ b/astron/distributed_object.py @@ -1,4 +1,4 @@ -# TODO: Split DistributedObject into ClientObject and InternalObject +# TODO: Split DistributedObject into ClientObject and InternalObject from bamboo import module from bamboo.wire import Datagram @@ -9,208 +9,76 @@ from astron.helpers import separate_fields class DistributedObject: - def __init__(self, repository, dclass_id, do_id, parent_id, zone_id): + def __init__(self, repository, class_id, do_id, parent_id, zone_id): self.repo = repository - self.dclass_id = dclass_id - self.dclass = self.repo.mod.get_class(dclass_id) - self.dmethod_name_to_id = dict([(self.dclass.get_field(dmethod_id).name(), dmethod_id) - for dmethod_id in range(0, self.dclass.num_fields())]) - self.dmethod_id_to_name = dict([(dmethod_id, self.dclass.get_field(dmethod_id).name()) - for dmethod_id in range(0, self.dclass.num_fields())]) - self.field_id_to_dmethod_id = dict([(self.dclass.get_field(dmethod_id).id(), dmethod_id) - for dmethod_id in range(0, self.dclass.num_fields())]) + self.class_ = self.repo.mod.get_class(class_id) self.do_id = do_id self.parent = parent_id self.zone = zone_id - self.type_packer = {module.kTypeInt8: self.pack_int8, - module.kTypeInt16: self.pack_int16, - module.kTypeInt32: self.pack_int32, - module.kTypeInt64: self.pack_int64, - module.kTypeUint8: self.pack_uint8, - module.kTypeUint16: self.pack_uint16, - module.kTypeUint32: self.pack_uint32, - module.kTypeUint64: self.pack_uint64, - module.kTypeFloat32: self.pack_float32, - module.kTypeFloat64: self.pack_float64, - module.kTypeChar: self.pack_char, - module.kTypeString: self.pack_string, - module.kTypeVarstring: self.pack_string, - } - self.type_unpacker = {module.kTypeInt8: self.unpack_int8, - module.kTypeInt16: self.unpack_int16, - module.kTypeInt32: self.unpack_int32, - module.kTypeInt64: self.unpack_int64, - module.kTypeUint8: self.unpack_uint8, - module.kTypeUint16: self.unpack_uint16, - module.kTypeUint32: self.unpack_uint32, - module.kTypeUint64: self.unpack_uint64, - module.kTypeFloat32: self.unpack_float32, - module.kTypeFloat64: self.unpack_float64, - module.kTypeChar: self.unpack_char, - module.kTypeString: self.unpack_string, - module.kTypeVarstring: self.unpack_string, - } + + # FIXME: This lumps all non-required field into the other_fields bin + # but it should only lump "ram" fields into that bin. self.required_fields, self.other_fields = separate_fields(self.dclass, ["required"]) + # FIXME: Remove after debug print("Initing %s %d" % (self.dclass.name(), self.do_id)) print(" REQUIRED fields: %s" % (" ".join([str(idx) for idx in self.required_fields]), )) print(" OTHER fields: %s" % (" ".join([str(idx) for idx in self.other_fields]), )) - + # Creating and destroying the view. - + def init(self): """Overwrite this to execute code right after view creation.""" print("DO created without custom init(): %d (%s)" % (self.do_id, str(type(self)))) - + def delete(self): """Overwrite this to execute code just before a views deletion.""" - # FIXME: Redup in object_repository if the docstring is actually accurate. + # FIXME: Redup in object_repository if the docstring is actually accurate. print("DO deleted: %d" % (self.do_id, )) def acquire_ai(self): # FIXME: This should have more intelligence, i.e. using GET_AI (CHANGE_AI?) self.repo.send_STATESERVER_OBJECT_SET_AI(self.do_id) - - def update_field(self, sender, field_id, dgi): - """Handles incoming SET_FIELD updates.""" - decoded_args = [] - field = self.dclass.get_field(self.field_id_to_dmethod_id[field_id]) - num_args = field.type().as_method().num_parameters() - for arg_id in range(0, num_args): - arg = field.type().as_method().get_parameter(arg_id) - arg_type = arg.type().subtype() - if arg_type == module.kTypeStruct: - decoded_args.append(self.unpack_struct(dgi, arg.type().as_struct())) - else: - decoded_args.append(self.type_unpacker[arg_type](dgi)) - # print("Updating field %s in %d with args %s" % (field.name(), self.do_id, str(decoded_args))) - getattr(self, field.name())(sender, *decoded_args) - - def send_update(self, field_name, *values): - """Handles outgoing SET_FIELD updates.""" - dmethod_id = self.dmethod_name_to_id[field_name] - # print(" send_update to field %s, dmethod_id %d" % (field_name, dmethod_id)) - field = self.dclass.get_field(dmethod_id) - num_args = field.type().as_method().num_parameters() - if num_args != len(values): - print("Distributed method %s requires %d args, %d were provided" % (field_name, num_args, len(values))) - else: - if self.repo.msg_type == MSG_TYPE_CLIENT: - dg = self.repo.create_message_stub() - dg.add_int16(clientmsg.CLIENT_OBJECT_SET_FIELD) - else: - # FIXME: If this an AIR object, recipients and sender have to be provided here. - dg = self.repo.create_message_stub(self.do_id, self.do_id) - dg.add_int16(servermsg.STATESERVER_OBJECT_SET_FIELD) - dg.add_int32(self.do_id) - dg.add_int16(field.id()) - for arg_id in range(0, num_args): - arg = field.type().as_method().get_parameter(arg_id) - arg_type = arg.type().subtype() - if arg_type == module.kTypeStruct: - self.pack_struct(dg, arg. type().as_struct(), *values[arg_id]) - else: - self.type_packer[arg_type](dg, values[arg_id]) - self.repo.send_datagram(dg) - - # Packing and unpacking field data. - - def pack_struct(self, dg, struct, *values): - num_args = struct.num_fields() - if num_args != len(values): - print("Struct %s requires %d args, %d were provided" % (struct.name(), num_args, len(values))) - else: - for arg_id in range(0, num_args): - arg = struct.get_field(arg_id) - arg_type = arg.type().subtype() - if arg_type == module.kTypeStruct: - self.pack_struct(dg, arg.type().as_struct(), *values[arg_id]) - else: - self.type_packer[arg_type](dg, values[arg_id]) - - def unpack_struct(self, dgi, struct): - partial_args = [] - num_args = struct.num_fields() - for arg_id in range(0, num_args): - arg = struct.get_field(arg_id) - arg_type = arg.type().subtype() - if arg_type == module.kTypeStruct: - partial_args.append(self.unpack_struct(dgi, arg.type().as_struct())) - else: - partial_args.append(self.type_unpacker[arg_type](dgi)) - return partial_args - - def pack_int8(self, dg, value): - dg.add_int8(value) - - def pack_int16(self, dg, value): - dg.add_int16(value) - - def pack_int32(self, dg, value): - dg.add_int32(value) - - def pack_int64(self, dg, value): - dg.add_int64(value) - - def pack_uint8(self, dg, value): - dg.add_uint8(value) - - def pack_uint16(self, dg, value): - dg.add_uint16(value) - - def pack_uint32(self, dg, value): - dg.add_uint32(value) - - def pack_uint64(self, dg, value): - dg.add_uint64(value) - - def pack_float32(self, dg, value): - dg.add_float32(value) - - def pack_float64(self, dg, value): - dg.add_float64(value) - - def pack_char(self, dg, value): - dg.add_char(value) - - def pack_string(self, dg, value): - dg.add_string(value) - - def unpack_int8(self, dgi): - return dgi.read_int8() - - def unpack_int16(self, dgi): - return dgi.read_int16() - - def unpack_int32(self, dgi): - return dgi.read_int32() - - def unpack_int64(self, dgi): - return dgi.read_int64() - - def unpack_uint8(self, dgi): - return dgi.read_uint8() - - def unpack_uint16(self, dgi): - return dgi.read_uint16() - - def unpack_uint32(self, dgi): - return dgi.read_uint32() - - def unpack_uint64(self, dgi): - return dgi.read_uint64() - def unpack_float32(self, dgi): - return dgi.read_float32() + def recv_update(self, field_id, dgi): + """Handles incoming SET_FIELD updates for Client Repository.""" + field = self.dclass.get_field_by_id(field_id) + print("Receiving update for field %s in %d with args %s" % (field.name(), self.do_id, str(args))) + + args = dgi.read_value(field.type()) + getattr(self, field.name())(args) - def unpack_float64(self, dgi): - return dgi.read_float64() + def recv_update_internal(self, sender, field_id, dgi): + """Handles incoming SET_FIELD updates for Internal Repository.""" + field = self.dclass.get_field_by_id(field_id) + print("Receiving update for field %s in %d with args %s" % (field.name(), self.do_id, str(args))) - def unpack_char(self, dgi): - return dgi.read_char() + args = dgi.read_value(field.type()) + getattr(self, field.name())(sender, args) - def unpack_string(self, dgi): - return dgi.read_string() + def send_update(self, field_name, *args): + """Handles outgoing SET_FIELD updates for Client Repository.""" + field = self.dclass.get_field_by_name(field_name) + print("Sending update for field %s, dmethod_id %d" % (field_name, dmethod_id)) + + dg = self.repo.create_message_stub() + dg.add_uint16(clientmsg.CLIENT_OBJECT_SET_FIELD) + dg.add_uint32(self.do_id) + dg.add_uint16(field.id()) + dg.add_value(arg.type(), args) + self.repo.send_datagram(dg) + + def send_update_internal(self, recipients, field_name, *args): + """Handles outgoing SET_FIELD updates for Internal Repository.""" + field = self.dclass.get_field_by_name(field_name) + print("Sending update for field %s, dmethod_id %d" % (field_name, dmethod_id)) + + dg = self.repo.create_message_stub(self.do_id, recipients) + dg.add_uint16(servermsg.STATESERVER_OBJECT_SET_FIELD) + dg.add_uint32(self.do_id) + dg.add_uint16(field.id()) + dg.add_value(arg.type(), args) + self.repo.send_datagram(dg) # Sending @@ -220,13 +88,13 @@ def send_CLIENTAGENT_EJECT(self, client_id, disconnect_code, reason): dg.add_uint16(disconnect_code) dg.add_string(reason) self.repo.send_datagram(dg) - + def send_CLIENTAGENT_DROP(self, client_id): dg = self.repo.create_message_stub(self.do_id, client_id) dg.add_uint16(servermsg.CLIENTAGENT_DROP) self.repo.send_datagram(dg) - # Handle + # Handle def handle_STATESERVER_OBJECT_CHANGING_LOCATION(self, sender, do_id, new_parent, new_zone, old_parent, old_zone): """Override this to react to STATESERVER_OBJECT_CHANGING_LOCATION messages.""" pass diff --git a/astron/object_repository.py b/astron/object_repository.py index 381e7af..3cf4b0b 100644 --- a/astron/object_repository.py +++ b/astron/object_repository.py @@ -41,7 +41,7 @@ def __init__(self, dcfilename=default_dcfilename): self.owner_views = {} self.handlers = {} - + # FIXME: Maybe move this into ClientRepository, if Internals won't get Interests in the future. # FIXME: Actually, fold this into the general callback mechanism. self.interest_counters = {} @@ -213,14 +213,14 @@ def __init__(self, dcfilename=default_dcfilename, stateserver = 400000, dbss = 4 # FIXME: Add handlers for incoming messages }) self.global_views = {} - + def connect(self, connection_success, connection_failure, host=default_host, port=default_client_port): # FIXME: Handle connection failures ObjectRepository.connect(self, host, port) self.send_CONTROL_ADD_CHANNEL(self.ai_channel) connection_success() - + def handle_datagram(self, dg): dgi = DatagramIterator(dg) num_recipients = dgi.read_uint8() @@ -272,7 +272,7 @@ def create_distobj_db_callback(self, do_id, parent_id, zone_id, set_ai, creation creation_callback(do_id, parent_id, zone_id, set_ai, *additional_args) # Sending messages - + def send_CONTROL_ADD_CHANNEL(self, channel): # CONTROL messages don't have sender fields dg = Datagram() @@ -287,7 +287,7 @@ def send_STATESERVER_OBJECT_SET_AI(self, do_id): dg.add_uint16(servermsg.STATESERVER_OBJECT_SET_AI) dg.add_uint64(self.ai_channel) self.send_datagram(dg) - + def send_STATESERVER_DELETE_AI_OBJECTS(self): dg = self.create_message_stub(self.ai_channel, self.stateserver) dg.add_uint16(servermsg.STATESERVER_DELETE_AI_OBJECTS) @@ -324,7 +324,7 @@ def send_DBSERVER_CREATE_OBJECT(self, dclass_id, context): # and should be accessible via create_distobj_db dg.add_uint16(0) self.send_datagram(dg) - + def send_DBSS_OBJECT_ACTIVATE_WITH_DEFAULTS(self, do_id, parent_id, zone_id): dg = self.create_message_stub(self.ai_channel, do_id) dg.add_uint16(servermsg.DBSS_OBJECT_ACTIVATE_WITH_DEFAULTS) @@ -340,7 +340,7 @@ def send_CLIENTAGENT_SET_STATE(self, clientagent, ca_state, sender = 0): dg.add_uint16(servermsg.CLIENTAGENT_SET_STATE) dg.add_uint16(ca_state) self.send_datagram(dg) - + def send_CLIENTAGENT_ADD_SESSION_OBJECT(self, do_id, client_channel): dg = self.create_message_stub(self.ai_channel, client_channel) dg.add_uint16(servermsg.CLIENTAGENT_ADD_SESSION_OBJECT) @@ -366,7 +366,7 @@ def send_STATESERVER_OBJECT_SET_OWNER(self, do_id, owner): def handle_STATESERVER_OBJECT_SET_FIELD(self, dgi, sender, recipients): do_id = dgi.read_uint32() field_id = dgi.read_uint16() - self.distributed_objects[do_id].update_field(sender, field_id, dgi) + self.distributed_objects[do_id].recv_update_internal(sender, field_id, dgi) def handle_STATESERVER_OBJECT_CHANGING_LOCATION(self, dgi, sender, recipients): print("handle_STATESERVER_OBJECT_CHANGING_LOCATION", sender, recipients) @@ -379,16 +379,16 @@ def handle_STATESERVER_OBJECT_CHANGING_LOCATION(self, dgi, sender, recipients): if recipient in self.distributed_objects.keys(): self.distributed_objects[recipient].handle_STATESERVER_OBJECT_CHANGING_LOCATION(sender, do_id, new_parent, new_zone, old_parent, old_zone) # FIXME: If the repo has "interest" in either location, that should be handled somehow. - + def handle_STATESERVER_OBJECT_GET_AI(self, dgi, sender, recipients): print("handle_STATESERVER_OBJECT_GET_AI", sender, recipients) context = dgi.read_uint32() print(" Context: %d" % (context, )) # FIXME: Implement - + def handle_STATESERVER_OBJECT_ENTER_LOCATION_WITH_REQUIRED(self, dgi, sender, recipients): self.create_view_from_datagram(dgi, cls_postfix = 'AE') - + def handle_STATESERVER_OBJECT_ENTER_AI_WITH_REQUIRED(self, dgi, sender, recipients): print("handle_STATESERVER_OBJECT_ENTER_AI_WITH_REQUIRED", sender, recipients) self.create_view_from_datagram(dgi, cls_postfix = 'AI') @@ -429,7 +429,7 @@ def connect(self, connection_success, connection_failure, connection_eject, ObjectRepository.connect(self, host, port) self.send_CLIENT_HELLO() - + def create_message_stub(self): dg = Datagram() return dg @@ -507,7 +507,7 @@ def send_CLIENT_ADD_INTEREST_MULTIPLE(self, if callback != False: self.interest_callback_map[(interest_id, context_id)] = (callback, callback_args, callback_kwargs) return context_id - + def send_CLIENT_REMOVE_INTEREST(self, interest_id, context_id): # FIXME: Should something be done about callbacks here? dg = Datagram() @@ -550,40 +550,40 @@ def handle_CLIENT_OBJECT_SET_FIELD(self, dgi): field_id = dgi.read_uint16() # FIXME: There's still the value in the dgi # FIXME: Implement - + def handle_CLIENT_OBJECT_SET_FIELDS(self, dgi): do_id = dgi.read_uint32() field_count = dgi.read_uint16() # FIXME: There's still values in the dgi # FIXME: Implement - + def handle_CLIENT_OBJECT_LOCATION(self, dgi): do_id = dgi.read_uint32() parent_id = dgi.read_uint32() zone_id = dgi.read_uint32() # FIXME: Make DO set changed location - + def handle_CLIENT_OBJECT_LEAVING(self, dgi): do_id = dgi.read_uint32() dist_obj = self.distributed_objects[do_id] dist_obj.delete() del self.distributed_objects[do_id] - + def handle_CLIENT_OBJECT_LEAVING_OWNER(self, dgi): do_id = dgi.read_uint32() dist_obj = self.owner_views[do_id] dist_obj.delete() del self.distributed_objects[do_id] - + def handle_CLIENT_ENTER_OBJECT_REQUIRED(self, dgi): self.create_view_from_datagram(dgi) - + def handle_CLIENT_ENTER_OBJECT_REQUIRED_OTHER(self, dgi): self.create_view_from_datagram(dgi) - + def handle_CLIENT_ENTER_OBJECT_REQUIRED_OWNER(self, dgi): self.create_view_from_datagram(dgi, cls_postfix = 'OV') - + def handle_CLIENT_ENTER_OBJECT_REQUIRED_OTHER_OWNER(self, dgi): self.create_view_from_datagram(dgi, cls_postfix = 'OV') @@ -598,7 +598,7 @@ class InterestInternalRepository(InternalRepository): def __init__(self, *args, **kwargs): InternalRepository.__init__(self, *args, **kwargs) self.repo_interests = set() - + def add_ai_interest(self, distobj_id, zone_id): self.repo_interests.add(parent_zone_to_location(distobj_id, zone_id)) # FIXME: As do_ids and zones may be of different sizes,