diff --git a/__init__.py b/__init__.py index 8b7f844..8433918 100644 --- a/__init__.py +++ b/__init__.py @@ -180,8 +180,9 @@ class struct_json_protocol(object): AUTH_STATE_KEY_TRANSFERRED: 'Key has been sent', AUTH_STATE_TRUSTED_CLIENT: 'Trusted Client'} - def __init__(self, comm_instance, secret=None): + def __init__(self, comm_instance, secret=None, auto_auth=False): self.__secret__ = secret + self.__auto_auth__ = auto_auth self.__clean_receive_buffer__() self.__callbacks__ = callback_storage() self.__callbacks__.add(self.SID_AUTH_SEED_REQUEST, 0, self.__authentificate_create_seed__) @@ -192,7 +193,7 @@ class struct_json_protocol(object): self.__seed__ = None self.__comm_inst__ = comm_instance self.__comm_inst__.register_callback(self.__data_available_callback__) - self.__comm_inst__.register_connect_callback(self.__clean_receive_buffer__) + self.__comm_inst__.register_connect_callback(self.__connection_established__) self.__comm_inst__.register_disconnect_callback(self.__authentification_state_reset__) def is_connected(self): @@ -201,6 +202,11 @@ class struct_json_protocol(object): def reconnect(self): return self.__comm_inst__.reconnect() + def __connection_established__(self): + self.__clean_receive_buffer__() + if self.__auto_auth__ and self.__comm_inst__.IS_CLIENT and self.__secret__ is not None: + self.authentificate() + def __authentification_state_reset__(self): logger.info("%s Resetting authentification state to AUTH_STATE_UNKNOWN_CLIENT", self.LOG_PREFIX) self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT @@ -468,8 +474,8 @@ class pure_json_protocol(struct_json_protocol): .. literalinclude:: ../../socket_protocol/_examples_/socket_protocol__pure_json_protocol_client.log """ - def __init__(self, comm_instance, secret=None): - struct_json_protocol.__init__(self, comm_instance, secret) + def __init__(self, *args, **kwargs): + struct_json_protocol.__init__(self, *args, **kwargs) def __build_frame__(self, service_id, data_id, data, status=struct_json_protocol.STATUS_OKAY): data_frame = json.dumps(self.__mk_msg__(status, service_id, data_id, data))