indexing description: "Represents an Origo service control message" license: "MIT license (see ../../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" class O_SERVICE_CONTROL_MESSAGE inherit P2P_XML_CACHE redefine document, validate, initialize end create make, make_from_element, parse_from_string feature {NONE} -- Initialization make (a_control_command: like control_command) is -- Create new Origo service control message require Command_valid: a_control_command > 0 do initialize set_control_command (a_control_command) is_valid := True ensure Command_set: control_command = a_control_command end feature -- Access control_command: INTEGER service_type: INTEGER service_name: STRING service_role: STRING parameters: DS_LIST [STRING] Command_request_status_start: INTEGER is 1 -- Core requests status START for a service (role) Command_request_status_suspend: INTEGER is 2 -- Core requests status SUSPEND for a service (role) Command_received_service_configurations: INTEGER is 3 -- Core is notified of new available service configurations Command_unregister_service: INTEGER is 4 -- Service unregister message to core match (an_element_name, an_element_value: STRING): BOOLEAN is -- Is given element's value equal with `an_element_value'? do if element_control_command.is_equal (an_element_name) then Result := does_element_match (control_command, an_element_value) elseif element_service_type.is_equal (an_element_name) then Result := does_element_match (service_type, an_element_value) elseif element_service_name.is_equal (an_element_name) then Result := does_element_match (service_name, an_element_value) elseif element_service_role.is_equal (an_element_name) then Result := does_element_match (service_role, an_element_value) elseif element_parameter.is_equal (an_element_name) then Result := parameters.has (an_element_value) end end feature -- Element change set_control_command (a_control_command: like control_command) is -- Set control_command require control_command_valid: a_control_command > 0 do control_command := a_control_command renew_document ensure control_command_set: control_command = a_control_command end set_service_type (a_service_type: like service_type) is -- Set message's destination service type require Service_type_valid: a_service_type >= 0 do service_type := a_service_type renew_document ensure Service_type_set: service_type = a_service_type end set_service_name (a_service_name: like service_name) is -- Set message's destination service name require Service_name_valid: a_service_name /= Void and not a_service_name.is_empty do service_name := a_service_name renew_document ensure Service_name_set: service_name.is_equal (a_service_name) end set_service_role (a_service_role: like service_role) is -- Set message's destination service role require Service_role_valid: a_service_role /= Void and not a_service_role.is_empty do service_role := a_service_role renew_document ensure Service_role_set: service_role.is_equal (a_service_role) end add_parameter (a_parameter: STRING) is -- Add parameter require Parameter_valid: a_parameter /= Void and not a_parameter.is_empty do if not parameters.has (a_parameter) then parameters.put_last (a_parameter) renew_document end ensure Parameter_set: parameters.has (a_parameter) end feature -- Removal prune_parameter (a_parameter: STRING) is -- Prune parameter require Parameter_existent: a_parameter /= Void and parameters.has (a_parameter) do parameters.start parameters.search_forth (a_parameter) if not parameters.after then parameters.remove_at renew_document end ensure Parameter_pruned: not parameters.has (a_parameter) 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 Cmd Result.create_root_child_element (element_control_command, Result.namespace_empty) Result.create_content (Result.last_element, control_command.out) -- Element Type if service_type >= 0 then Result.create_root_child_element (element_service_type, Result.namespace_empty) Result.create_content (Result.last_element, service_type.out) end -- Element Name if service_name /= Void then Result.create_root_child_element (element_service_name, Result.namespace_empty) Result.create_content (Result.last_element, service_name) end -- Element Role if service_role /= Void then Result.create_root_child_element (element_service_role, Result.namespace_empty) Result.create_content (Result.last_element, service_role) end -- Parameters from parameters.start until parameters.after loop -- Element Parm Result.create_root_child_element (element_parameter, Result.namespace_empty) Result.create_content (Result.last_element, parameters.item_for_iteration) parameters.forth end end end feature {NONE} -- Implementation Element_control_command: STRING is "Cmd" Element_service_type: STRING is "Type" Element_service_name: STRING is "Name" Element_service_role: STRING is "Role" Element_parameter: STRING is "Parm" root_element_name: STRING is "OrigoServiceControl" 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, parm: STRING do if element_service_type.is_equal (element.name) then int_value := trimmed (element.text) if int_value /= Void and int_value.is_integer then service_type := int_value.to_integer end elseif element_service_name.is_equal (element.name) then service_name := trimmed (element.text) elseif element_service_role.is_equal (element.name) then service_role := trimmed (element.text) elseif element_control_command.is_equal (element.name) then int_value := trimmed (element.text) if int_value /= Void and int_value.is_integer then control_command := int_value.to_integer else control_command := -1 -- invalid end elseif element_parameter.is_equal (element.name) then parm := trimmed (element.text) if parm /= Void and not parm.is_empty then parameters.put_last (parm) end end end validate is -- Validate message and set `is_valid' do is_valid := is_valid and control_command > 0 end initialize is -- Initialize data structure local equality_tester: KL_EQUALITY_TESTER [STRING] do Precursor service_type := -1 create {DS_LINKED_LIST [STRING]} parameters.make create equality_tester parameters.set_equality_tester (equality_tester) end invariant Parameters_valid: parameters /= Void and parameters.equality_tester /= Void end