indexing description: "Represents a discovery query message" license: "MIT license (see ../../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" class P2P_DISCOVERY_QUERY inherit P2P_XML_CACHE redefine document, validate, initialize end create make, make_from_element, parse_from_string feature {NONE} -- Initialization make (a_type: like type) is -- Create new discovery query message require Type_valid: a_type >= {P2P_DISCOVERY_SERVICE}.type_peer and a_type <= {P2P_DISCOVERY_SERVICE}.type_general do initialize set_type (a_type) is_valid := True ensure Type_set: type = a_type end feature -- Access type: INTEGER threshold: INTEGER key: STRING value: STRING peer_advertisement: P2P_PEER_ADVERTISEMENT resolver_query_id: INTEGER -- Used query id in resolver query match (an_element_name, an_element_value: STRING): BOOLEAN is -- Is given element's value equal with `an_element_value'? do if element_type.is_equal (an_element_name) then Result := does_element_match (type, an_element_value) elseif element_threshold.is_equal (an_element_name) then Result := does_element_match (threshold, an_element_value) elseif element_key.is_equal (an_element_name) then Result := does_element_match (key, an_element_value) elseif element_value.is_equal (an_element_name) then Result := does_element_match (value, an_element_value) end end feature -- Element change set_type (a_type: like type) is -- Set queries advertisement type require Type_valid: a_type >= {P2P_DISCOVERY_SERVICE}.type_peer and a_type <= {P2P_DISCOVERY_SERVICE}.type_general do type := a_type renew_document ensure Type_set: type = a_type end set_threshold (a_threshold: like threshold) is -- Set threshold require Threshold_valid: a_threshold >= 0 do threshold := a_threshold renew_document ensure Threshold_set: threshold = a_threshold end set_restriction (a_key: like key; a_value: like value) is -- Set key and value require Key_valid: a_key /= Void Value_valid: a_value /= Void do key := a_key value := a_value renew_document ensure Key_set: key.is_equal (a_key) Value_set: value.is_equal (a_value) end set_peer_advertisement (an_adv: like peer_advertisement) is -- Set peer advertisement require Advertisement_valid: an_adv /= Void and an_adv.is_valid do peer_advertisement := an_adv renew_document ensure Advertisement_set: peer_advertisement.is_equal (an_adv) end set_resolver_query_id (a_query_id: like resolver_query_id) is -- Set resolver query id (not part of xml discovery query) require Id_valid: a_query_id >= 0 do resolver_query_id := a_query_id ensure Id_set: resolver_query_id = a_query_id 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 Type Result.create_root_child_element (element_type, Result.namespace_empty) Result.create_content (Result.last_element, type.out) if threshold >= 0 then -- Element Threshold Result.create_root_child_element (element_threshold, Result.namespace_empty) Result.create_content (Result.last_element, threshold.out) end if key /= Void and value /= Void then -- Element Attr Result.create_root_child_element (element_key, Result.namespace_empty) Result.create_content (Result.last_element, key) -- Element Value Result.create_root_child_element (element_value, Result.namespace_empty) Result.create_content (Result.last_element, value) end -- Element PeerAdv if peer_advertisement /= Void then Result.create_root_child_element (element_peer_advertisement, Result.namespace_empty) Result.create_content (Result.last_element, peer_advertisement.out) end end end feature {NONE} -- Implementation Element_type: STRING is "Type" Element_threshold: STRING is "Threshold" Element_key: STRING is "Attr" Element_value: STRING is "Value" Element_peer_advertisement: STRING is "PeerAdv" root_element_name: STRING is "DiscoveryQuery" attribute_handler (root_attribute: XM_ATTRIBUTE) is -- No attributes do end element_handler (element: XM_ELEMENT) is -- Handle root child element of parsed advertisement local int_value: STRING do if element_type.is_equal (element.name) then int_value := element_text (element) if int_value.is_integer then type := int_value.to_integer else type := -1 end elseif element_threshold.is_equal (element.name) then int_value := element_text (element) if int_value.is_integer then threshold := int_value.to_integer else threshold := -1 end elseif element_key.is_equal (element.name) then key := trimmed (element.text) elseif element_value.is_equal (element.name) then value := trimmed (element.text) elseif element_peer_advertisement.is_equal (element.name) then create peer_advertisement.parse_from_string (element_text (element)) if peer_advertisement.is_valid then peer_advertisement.set_lifetime_relative (peer_advertisement.expiration_time) else peer_advertisement := Void end end end validate is -- Validate message and set `is_valid' do is_valid := is_valid and type >= {P2P_DISCOVERY_SERVICE}.type_peer and type <= {P2P_DISCOVERY_SERVICE}.type_general and (key = Void implies value = Void) and (value = Void implies key = Void) end initialize is -- Initialize data structure do Precursor threshold := -1 -- (unlimited, when element not given) end end