indexing description: "Represents a route advertisement" license: "MIT license (see ../../../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" class P2P_ROUTE_ADVERTISEMENT inherit P2P_ADVERTISEMENT redefine initialize, document, validate end create make, make_from_element, parse_from_string feature {NONE} -- Initialization make (a_destination: P2P_ACCESSPOINT_ADVERTISEMENT) is -- Create a route advertisement require Destination_set: a_destination /= Void do initialize set_destination (a_destination) is_valid := True ensure Destination_set: destination = a_destination end feature -- Access destination_peer_id: P2P_PEER_ID destination: P2P_ACCESSPOINT_ADVERTISEMENT hops: DS_LIST [P2P_ACCESSPOINT_ADVERTISEMENT] index_elements: DS_LIST [TUPLE [STRING, STRING]] is -- Indexable elements do if destination_peer_id /= Void then create {DS_ARRAYED_LIST [TUPLE [STRING, STRING]]} Result.make (1) Result.put_last ([element_destination_peer_id, destination_peer_id.out]) else create {DS_ARRAYED_LIST [TUPLE [STRING, STRING]]} Result.make (0) end end unique_id: STRING is -- Unique id do if destination_peer_id /= Void then Result := root_element_name + "-" + destination_peer_id.out_short elseif destination.peer_id /= Void then Result := root_element_name + "-" + destination.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_destination_peer_id.is_equal (an_element_name) then Result := does_element_match (destination_peer_id, an_element_value) end end feature -- Element change set_destination_peer_id (a_pid: like destination_peer_id) is -- Set new destination peer id require Pid_valid: a_pid /= Void and a_pid.is_valid do destination_peer_id := a_pid renew_document ensure Pid_set: destination_peer_id = a_pid end set_destination (a_destination: like destination) is -- Set new destination access point advertisement require Destination_valid: a_destination /= Void and a_destination.is_valid do destination := a_destination renew_document ensure Destination_set: destination = a_destination end add_hop (a_hop: P2P_ACCESSPOINT_ADVERTISEMENT) is -- Add hop require Hop_valid: a_hop /= Void and a_hop.is_valid do hops.put_last (a_hop) renew_document ensure Hop_added: hops.has (a_hop) end feature -- Removal remove_hop (a_hop: P2P_ACCESSPOINT_ADVERTISEMENT) is -- Add hop require Hop_valid: a_hop /= Void do hops.delete (a_hop) renew_document ensure Hop_removed: not hops.has (a_hop) end feature -- Output document: P2P_XML_DOCUMENT is -- Create XML document local cached: BOOLEAN element: XM_ELEMENT do cached := cached_document /= Void -- create document Result := Precursor if not cached then if destination_peer_id /= Void then -- element DstPID Result.create_root_child_element (element_destination_peer_id, Result.namespace_empty) Result.create_content (Result.last_element, destination_peer_id.out) end -- element Dst (use copy, because elements are double linked) Result.create_root_child_element (element_destination, Result.namespace_empty) Result.add_child_element (Result.last_element, destination.document.document.root_element.deep_twin) if hops.count > 0 then -- element Hops Result.create_root_child_element (element_hops, Result.namespace_empty) element := Result.last_element from hops.start until hops.after loop -- use copy, because elements are double linked Result.add_child_element (element, hops.item_for_iteration.document.document.root_element.deep_twin) hops.forth end end end end feature {NONE} -- Implementation Element_destination_peer_id: STRING is "DstPID" Element_destination: STRING is "Dst" Element_hops: STRING is "Hops" root_element_name: STRING is "RA" -- Root element name element_handler (element: XM_ELEMENT) is -- Handle a root child element of a parsed advertisement document local children: DS_LIST [XM_ELEMENT] el_apa: XM_ELEMENT apa: P2P_ACCESSPOINT_ADVERTISEMENT do if element_destination_peer_id.is_equal (element.name) then create destination_peer_id.make_from_urn (element_text (element)) if not destination_peer_id.is_valid then destination_peer_id := Void end elseif element_destination.is_equal (element.name) then el_apa ?= element.elements.first if el_apa /= Void then create destination.make_from_element (el_apa.deep_twin) if not destination.is_valid then destination := Void end end elseif element_hops.is_equal (element.name) then from children := element.elements children.start until children.after loop el_apa := children.item_for_iteration if el_apa.name.is_equal ("APA") then create apa.make_from_element (el_apa.deep_twin) if apa.is_valid then hops.put_last (apa) end end children.forth end end end validate is -- Validate advertisement and set `is_valid' do is_valid := is_valid and destination /= Void end initialize is -- Initialize object's data structure do Precursor create {DS_LINKED_LIST [P2P_ACCESSPOINT_ADVERTISEMENT]} hops.make end invariant Hops_existent: hops /= Void end