def testEdefStringReload(self): """ Tries to convert an EndpointDescription to its XML format (EDEF) and to reload this string """ original = beans.EndpointDescription( self.svc_ref, { pelix.remote.PROP_ENDPOINT_ID: "toto", pelix.remote.PROP_IMPORTED_CONFIGS: ['titi'], pelix.constants.OBJECTCLASS: "spec" }) # Write the endpoint to an XML string writer = EDEFWriter() xml_string = writer.to_string([original]) # Parse the XML reader = EDEFReader() endpoints = reader.parse(xml_string) # Ensure we have a valid result self.assertEqual(len(endpoints), 1, "Parsed more than one endpoint") endpoint = endpoints[0] # Ensure equality self.assertIsNot(original, endpoint, "Same exact endpoint object returned") self.assertEqual(original, endpoint, "Parsed endpoint is different") self.assertEqual(endpoint, original, "Parsed endpoint is different") # Ensure properties equality self.assertDictEqual(original.get_properties(), endpoint.get_properties(), "Endpoint properties changed")
def _register_service(self, endpoint): # type: (beans.ExportEndpoint) -> None """ Register a local endpoint :param endpoint: A local endpoint """ # Prepare node content path = self._endpoint_path(self._fw_uid, endpoint.uid) data = to_bytes( EDEFWriter().to_string( [beans.EndpointDescription.from_export(endpoint)] ) ) try: try: # Create an ephemeral node self._zk.create(path, data, True) except NodeExistsError: # Service already exists: update it self._zk.set(path, data) except KazooException as ex: _logger.warning( "Error registering local service: %s", type(ex).__name__ )
def testEdefIOTypes(self): """ Tests the writing and parsing of an EndpointDescription bean with "complex" properties """ properties = { # Strings whitespaces are not well kept in XML "string": "some string just to see...", "int": 12, "float": 12.0, "tuple_str": ("a", "b", "c"), "tuple_int": (1, 2, 3), "tuple_float": (1.0, 2.0, 3.0), "list_str": ["a", "b", "c"], "list_int": [1, 2, 3], "list_float": [1.0, 2.0, 3.0], "set_str": set(["a", "b", "c"]), "set_int": set([1, 2, 3]), "set_float": set([1.0, 2.0, 3.0])} all_props = properties.copy() all_props[pelix.remote.PROP_ENDPOINT_ID] = 'toto' all_props[pelix.remote.PROP_IMPORTED_CONFIGS] = ['titi'] # Prepare an endpoint description with different property values endpoint = beans.EndpointDescription(self.svc_ref, all_props) # Write it & parse it xml_string = EDEFWriter().to_string([endpoint]) parsed = EDEFReader().parse(xml_string)[0] parsed_properties = parsed.get_properties() # Check values self.assertDictContainsSubset(endpoint.get_properties(), parsed_properties)
def endpoints_added(self, endpoints): """ Multiple endpoints have been added :param endpoints: A list of ExportEndpoint beans """ # Convert the beans to XML (EDEF format) xml_string = EDEFWriter().to_string( beans.EndpointDescription.from_export(endpoint) for endpoint in endpoints) # Send the message self.__send_message(EVENT_ADD, xml_string)
def endpoint_removed(self, endpoint): """ An end point is removed :param endpoint: Endpoint being removed """ # Convert the endpoint into an EndpointDescription bean endpoint_desc = beans.EndpointDescription.from_export(endpoint) # Convert the bean to XML (EDEF format) xml_string = EDEFWriter().to_string([endpoint_desc]) # Send the message self.__send_message(EVENT_REMOVE, xml_string)
def _register_service(self, endpoint): """ Registers/Updates an exported service to Redis. :param endpoint: A :class:`~pelix.remote.ExportEndpoint` object """ # Convert to EDEF key = PATTERN_ENDPOINT_KEY.format(fw_uid=self._fw_uid, endpoint_uid=endpoint.uid) xml_string = EDEFWriter().to_string( [beans.EndpointDescription.from_export(endpoint)]) # Send to Redis (without expiration) self._redis.set(key, xml_string)
def endpoint_updated(self, endpoint, old_properties): """ An end point is updated :param endpoint: The updated endpoint :param old_properties: Previous properties of the endpoint """ # Convert the endpoint into an EndpointDescription bean endpoint_desc = beans.EndpointDescription.from_export(endpoint) # Convert the bean to XML (EDEF format) xml_string = EDEFWriter().to_string([endpoint_desc]) # Send the message self.__send_message(EVENT_UPDATE, xml_string)
def testEdefStringReload(self): """ Tries to convert an EndpointDescription to its XML format (EDEF) and to reload this string """ original = beans.EndpointDescription( self.svc_ref, {pelix.remote.PROP_ENDPOINT_ID: "toto", pelix.remote.PROP_IMPORTED_CONFIGS: ['titi'], pelix.constants.OBJECTCLASS: "spec"}) # Write the endpoint to an XML string writer = EDEFWriter() xml_string = writer.to_string([original]) # Parse the XML reader = EDEFReader() endpoints = reader.parse(xml_string) # Ensure we have a valid result self.assertEqual(len(endpoints), 1, "Parsed more than one endpoint") endpoint = endpoints[0] # Ensure equality self.assertIsNot(original, endpoint, "Same exact endpoint object returned") self.assertEqual(original, endpoint, "Parsed endpoint is different") self.assertEqual(endpoint, original, "Parsed endpoint is different") # Ensure properties equality self.assertDictEqual(original.get_properties(), endpoint.get_properties(), "Endpoint properties changed")
def _handle_discover(self, payload): """ A framework wants to discover all services :param payload: The UID of the sender """ if payload == self._framework_uid: # We are the sender, ignore this message return # Get the list of our exported services endpoints = self._dispatcher.get_endpoints() if not endpoints: # Nothing to say return # Convert the beans to XML (EDEF format) xml_string = EDEFWriter().to_string( beans.EndpointDescription.from_export(endpoint) for endpoint in endpoints) # Send the message self.__send_message(EVENT_ADD, xml_string)