indexing description: "A rendezvous configuration document" license: "MIT license (see ../../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" class P2P_RENDEZVOUS_CONFIGURATION inherit P2P_XML_CACHE redefine initialize, document end create make, make_from_element, parse_from_string feature {NONE} -- Initialization make is -- Create new rendezvous configuration do initialize is_valid := True end feature -- Access Type_client: INTEGER is 1 Type_rendezvous: INTEGER is 2 Type_adhoc: INTEGER is 3 seed_peers: DS_LIST [P2P_ENDPOINT_ADDRESS] seed_uris: DS_LIST [STRING] type: INTEGER match (an_element_name, an_element_value: STRING): BOOLEAN is -- Is given element's value equal with `an_element_value'? do if element_seeds.is_equal (an_element_name) then Result := seed_peers.count /= 0 or seed_uris.count /= 0 end end feature -- Element change add_seed_address (a_seed_address: P2P_ENDPOINT_ADDRESS) is -- Add new seed address require Seed_valid: a_seed_address /= Void and a_seed_address.is_valid do if not seed_peers.has (a_seed_address) then seed_peers.put_first (a_seed_address) renew_document end ensure Seed_added: seed_peers.has (a_seed_address) end add_seed_addresses (a_list: DS_LIST [P2P_ENDPOINT_ADDRESS]) is -- Add new seed addresses from `a_list' require Seed_valid: a_list /= Void local cursor: DS_LIST_CURSOR [P2P_ENDPOINT_ADDRESS] do from cursor := a_list.new_cursor cursor.start until cursor.after loop if cursor.item.is_valid and not seed_peers.has (cursor.item) then seed_peers.put_first (cursor.item) renew_document end cursor.forth end end add_seed_uri (a_seed_uri: STRING) is -- Set new seed address require Seed_valid: a_seed_uri /= Void do if not seed_uris.has (a_seed_uri) then seed_uris.put_last (a_seed_uri) renew_document end ensure Seed_added: seed_uris.has (a_seed_uri) end feature -- Output document: P2P_XML_DOCUMENT is -- Create XML document local cached: BOOLEAN el_seeds: XM_ELEMENT peer_cursor: DS_LIST_CURSOR [P2P_ENDPOINT_ADDRESS] uri_cursor: DS_LIST_CURSOR [STRING] do cached := cached_document /= Void -- create document Result := Precursor if not cached then -- Attribute type Result.document.root_element.add_unqualified_attribute ("type", "jxta:" + root_element_name) -- Attribute config if type = type_rendezvous then Result.document.root_element.add_unqualified_attribute (attribute_type, attribute_type_rendezvous) elseif type = type_adhoc then Result.document.root_element.add_unqualified_attribute (attribute_type, attribute_type_adhoc) else Result.document.root_element.add_unqualified_attribute (attribute_type, attribute_type_client) end -- element seeds if seed_peers.count /= 0 or seed_uris.count /= 0 then Result.create_root_child_element (element_seeds, Result.namespace_empty) el_seeds := Result.last_element -- seed addresses from peer_cursor := seed_peers.new_cursor peer_cursor.start until peer_cursor.after loop Result.create_child_element (el_seeds, element_address, Result.namespace_empty) Result.create_content (Result.last_element, peer_cursor.item.out) peer_cursor.forth end -- seed uris from uri_cursor := seed_uris.new_cursor uri_cursor.start until uri_cursor.after loop Result.create_child_element (el_seeds, element_address, Result.namespace_empty) Result.last_element.add_unqualified_attribute (attribute_seeding, attribute_seeding_true) Result.create_content (Result.last_element, uri_cursor.item) uri_cursor.forth end end end end feature {NONE} -- Implementation Element_seeds: STRING is "seeds" Element_address: STRING is "addr" Attribute_seeding: STRING is "seeding" Attribute_seeding_true: STRING is "true" Attribute_type: STRING is "config" Attribute_type_client: STRING is "client" Attribute_type_rendezvous: STRING is "rendezvous" Attribute_type_adhoc: STRING is "adhoc" root_element_name: STRING is "RdvConfig" attribute_handler (root_attribute: XM_ATTRIBUTE) is -- Handle a root element attribute of a parsed document do if root_attribute.name.is_equal (attribute_type) then if equal (trimmed (root_attribute.value), attribute_type_rendezvous) then type := type_rendezvous elseif equal (trimmed (root_attribute.value), attribute_type_adhoc) then type := type_adhoc else type := type_client end end end element_handler (element: XM_ELEMENT) is -- Handle root child element of parsed advertisement local addr: XM_ELEMENT attr: XM_ATTRIBUTE content: STRING ea: P2P_ENDPOINT_ADDRESS do if element_seeds.is_equal (element.name) then from element.start until element.after loop addr ?= element.item_for_iteration if addr /= Void and element_address.is_equal (addr.name) then attr := addr.attribute_by_name (attribute_seeding) if attr /= Void and equal (trimmed (attr.value), attribute_seeding_true) then -- seeding uri content := trimmed (addr.text) if content /= Void and not seed_uris.has (content) then seed_uris.put_last (content) end else -- seed address create ea.make_from_uri (element_text (addr)) if ea.is_valid and not seed_peers.has (ea) then seed_peers.put_last (ea) end end end element.forth end end end initialize is -- Initialize data structures local seed_peers_equality_tester: KL_EQUALITY_TESTER [P2P_ENDPOINT_ADDRESS] seed_uris_equality_tester: KL_EQUALITY_TESTER [STRING] do Precursor create {DS_LINKED_LIST [P2P_ENDPOINT_ADDRESS]} seed_peers.make create seed_peers_equality_tester seed_peers.set_equality_tester (seed_peers_equality_tester) create {DS_LINKED_LIST [STRING]} seed_uris.make create seed_uris_equality_tester seed_uris.set_equality_tester (seed_uris_equality_tester) type := type_client end invariant Seeds_existent: seed_peers /= Void and seed_uris /= Void end