indexing description: "Represenets an access point advertisement" license: "MIT license (see ../../../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" class P2P_ACCESSPOINT_ADVERTISEMENT inherit P2P_ADVERTISEMENT redefine initialize, document end create make, make_from_element, parse_from_string feature {NONE} -- Initialization make is -- Create empty access point advertisement do initialize is_valid := True end feature -- Access peer_id: P2P_PEER_ID endpoint_addresses: DS_LIST [P2P_ENDPOINT_ADDRESS] index_elements: DS_LIST [TUPLE [STRING, STRING]] is -- Indexable elements do if peer_id /= Void then create {DS_ARRAYED_LIST [TUPLE [STRING, STRING]]} Result.make (1) Result.put_last ([element_peer_id, peer_id.out]) else create {DS_ARRAYED_LIST [TUPLE [STRING, STRING]]} Result.make (0) end end unique_id: STRING is -- Unique id do if peer_id /= Void then Result := root_element_name + "-" + peer_id.out_short end end match (an_element_name, an_element_value: STRING): BOOLEAN is -- Is given element's value equal with `an_element_value'? do if element_peer_id.is_equal (an_element_name) then Result := does_element_match (peer_id, an_element_value) end end feature -- Element change set_peer_id (a_peer_id: like peer_id) is -- Set new peer id require Peer_id_valid: a_peer_id /= Void and a_peer_id.is_valid do peer_id := a_peer_id renew_document ensure Peer_id_set: peer_id = a_peer_id end add_address (an_address: P2P_ENDPOINT_ADDRESS) is -- Add endpoint address require Address_valid: an_address /= Void and an_address.is_valid do if not endpoint_addresses.has (an_address) then endpoint_addresses.put_last (an_address) renew_document end ensure Address_added: endpoint_addresses.has (an_address) end feature -- Removal remove_address (an_address: P2P_ENDPOINT_ADDRESS) is -- Remove address require Address_valid: an_address /= Void do endpoint_addresses.delete (an_address) renew_document ensure Address_removed: not endpoint_addresses.has (an_address) end remove_addresses_with_protocol (a_protocol_name: STRING) is -- Remove addresses with given protocol name require Name_valid: a_protocol_name /= Void local cursor: DS_LIST_CURSOR [P2P_ENDPOINT_ADDRESS] do from cursor := endpoint_addresses.new_cursor cursor.start until cursor.after loop if a_protocol_name.is_equal (cursor.item.protocol_name) then cursor.remove else cursor.forth end end renew_document end feature -- Output document: P2P_XML_DOCUMENT is -- Create XML document local cached: BOOLEAN do cached := cached_document /= Void -- create document Result := Precursor if not cached then -- element PID if peer_id /= Void then Result.create_root_child_element (element_peer_id, Result.namespace_empty) Result.create_content (Result.last_element, peer_id.out) end -- elements EA from endpoint_addresses.start until endpoint_addresses.after loop Result.create_root_child_element (element_endpoint_address, Result.namespace_empty) Result.create_content (Result.last_element, endpoint_addresses.item_for_iteration.out) endpoint_addresses.forth end end end feature {NONE} -- Implementation Element_peer_id: STRING is "PID" Element_endpoint_address: STRING is "EA" root_element_name: STRING is "APA" -- Root element name element_handler (element: XM_ELEMENT) is -- Handle a root child element of a parsed advertisement document local ea: P2P_ENDPOINT_ADDRESS do if element_peer_id.is_equal (element.name) then create peer_id.make_from_urn (element_text (element)) if not peer_id.is_valid then peer_id := Void end elseif element_endpoint_address.is_equal (element.name) then create ea.make_from_uri (element_text (element)) if ea.is_valid then endpoint_addresses.put_last (ea) end end end initialize is -- Initialize object's data structure local equality_tester: KL_EQUALITY_TESTER [P2P_ENDPOINT_ADDRESS] do Precursor create {DS_LINKED_LIST [P2P_ENDPOINT_ADDRESS]} endpoint_addresses.make create equality_tester endpoint_addresses.set_equality_tester (equality_tester) end invariant Addresses_existent: endpoint_addresses /= Void end