indexing description: "Endpoint service message handler" license: "MIT license (see ../../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" class ENDPOINT_MESSAGE_SENDER inherit L4E_PRIORITY_CONSTANTS THREAD_CONTROL ARGUMENTS create make feature {NONE} -- Initialization make is -- Create endpoint service message handler local vampeer: P2P_PLATFORM failed: BOOLEAN do if not failed and argument_count >= 2 or argument_count <= 3 then -- Configuration directory path if argument_count = 3 then config_path := argument (1) else config_path := ".vampeer_sender" end -- Create stdout logger create_logger -- create vampeer platform vampeer := configure_platform if vampeer.module_status /= vampeer.init_failed then -- add incoming message filter for catching replies npg.endpoint_service.extend_service ("SimpleSender", "WaitForReply", agent reply_listener) -- now start platform vampeer.start if vampeer.module_status = vampeer.start_ok then -- send message send_message (argument (argument_count - 1), argument (argument_count)) -- stay idle idle_loop end end elseif argument_count < 2 or argument_count > 3 then io.put_string ("Usage: endpoint_message_sender [ConfigDirectory] remote_host remote_port") io.new_line end -- stop platform if vampeer /= Void and vampeer.module_status = vampeer.start_ok then vampeer.stop end rescue failed := True retry end feature -- Access config_path: STRING logger: L4E_LOGGER npg: P2P_PEERGROUP stopping: BOOLEAN feature {NONE} -- Implementation create_logger is -- Create log hierarchy and set `logger' local log_hierarchy: L4E_HIERARCHY log_appender: L4E_APPENDER log_layout: L4E_DATE_TIME_LAYOUT do create log_hierarchy.make (info_p) logger := log_hierarchy.root create {L4E_STDOUT_APPENDER} log_appender.make ("stdoutapp") create log_layout log_appender.set_layout (log_layout) logger.add_appender (log_appender) ensure Logger_set: logger /= Void end configure_platform: P2P_PLATFORM is -- Configure platform and initialize net peer group local conf: P2P_CONFIGURATION rdvconf: P2P_RENDEZVOUS_CONFIGURATION do create Result.make (config_path, logger) if not Result.is_configured then conf := Result.default_configuration conf.set_name ("endpoint_message_sender") conf.set_description ("Sends a message to a remote peer and waits for reply") create rdvconf.make conf.replace_service_parameter (Result.rendezvous_mcid, rdvconf) Result.configure (conf) end if Result.is_configured then npg := Result.standard_net_peergroup end end send_message (host, port: STRING) is -- Send a message to `host' with `port' local msg: P2P_MESSAGE msgel: P2P_MESSAGE_ELEMENT dest: P2P_ENDPOINT_ADDRESS do create dest.make_with_service ("tcp", host + ":" + port, "reply", Void) if dest.is_valid then create msg.make create msgel.make_string (msg.namespace_user, "SimpleSenderHeader", Void, "Dummy message") msg.extend (msgel) npg.endpoint_service.send_message (dest, msg) else logger.error ("Invalid destination address") stopping := True end end reply_listener (msg: P2P_MESSAGE; src, dest: P2P_ENDPOINT_ADDRESS) is -- Listens for message reply do if msg.element_by_namespace_and_name (msg.namespace_user, "SimpleSenderHeader") /= Void and msg.element_by_namespace_and_name (msg.namespace_user, "EndpointMessageReplyHandler") /= Void then logger.info ("Received reply") stopping := True end end idle_loop is -- Stay idle until received stopping command do from until stopping loop sleep (2000000000) end end end