indexing description: "Benchmark node" license: "MIT license (see ../../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" deferred class P2P_BENCHMARK inherit L4E_PRIORITY_CONSTANTS THREAD_CONTROL ARGUMENTS feature {NONE} -- Initialization make is -- Create a benchmark node local vampeer: P2P_PLATFORM failed: BOOLEAN wait_step: INTEGER do if not failed and argument_count >= 1 then -- create stdout logger create_logger -- create vampeer platform vampeer := configure_platform if vampeer.module_status /= vampeer.init_failed then -- register our benchmark service npg.endpoint_service.extend_service (benchmark_service_name, Void, agent benchmark_message_handler) -- register our discovery listener npg.discovery_service.extend_response_listener (agent discovery_response_handler) -- set our peer id if is_master then pid_master := npg.peer_id else pid_slave := npg.peer_id end -- now start platform vampeer.start if vampeer.module_status = vampeer.start_ok then -- wait for connection from until npg.rendezvous_service.is_connected loop sleep (250000000) end -- publish our peer advertisement npg.discovery_service.publish_advertisement_remotely (npg.peer_advertisement, Void) -- wait for other node from wait_step := 0 until other_node_found loop if is_master and wait_step = 0 then -- master: send query every 5 seconds query_for_slaves end wait_step := (wait_step + 1) \\ 20 sleep (250000000) end if is_master then -- master: publish peer advertisement to slave npg.discovery_service.publish_advertisement_remotely (npg.peer_advertisement, pid_slave) logger.info ("benchmark: Slave node found: " + pid_slave.out) sleep (100000000) else logger.info ("benchmark: Master node found: " + pid_master.out) end -- start benchmark start_benchmark end end elseif argument_count /= 1 then io.put_string ("Usage: " + argument (0) + " ConfigDirectory") 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 logger: L4E_LOGGER npg: P2P_PEERGROUP Peer_name_master: STRING is "benchmark/master" Peer_name_slave: STRING is "benchmark/slave" Benchmark_service_name: STRING is "BenchmarkTest" Benchmark_element_namespace: STRING is "benchmark" Benchmark_control: STRING is "control" Benchmark_control_stop: STRING is "stop" Benchmark_message_id: STRING is "ID" Benchmark_message_data: STRING is "data" peer_name: STRING is -- Peer name do if is_master then Result := peer_name_master else Result := peer_name_slave end end is_master: BOOLEAN is -- Is node the master? deferred end pid_slave: P2P_PEER_ID pid_master: P2P_PEER_ID address_slave: P2P_ENDPOINT_ADDRESS address_master: P2P_ENDPOINT_ADDRESS other_node_found: BOOLEAN is -- Did we already found the other node? do if is_master then Result := (pid_slave /= Void) else Result := (pid_master /= Void) end end 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 (error_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 do create Result.make (argument (1), logger) if not Result.is_configured then conf := Result.default_configuration conf.set_name (peer_name) conf.set_description ("Benchmark node") Result.configure (conf) end if Result.is_configured then npg := Result.standard_net_peergroup end end query_for_slaves is -- Send discovery query for slave nodes local query: P2P_DISCOVERY_QUERY do create query.make ({P2P_DISCOVERY_SERVICE}.type_peer) query.set_restriction ("Name", peer_name_slave) query.set_threshold (10) npg.discovery_service.query_remote_advertisements (query, Void, Void) end discovery_response_handler (response: P2P_DISCOVERY_RESPONSE) is -- Handles discovery responses require Response_valid: response /= Void and response.is_valid local cursor: DS_LIST_CURSOR [P2P_PEER_ADVERTISEMENT] do if response.type = {P2P_DISCOVERY_SERVICE}.type_peer then from cursor := response.all_peer_advertisements.new_cursor cursor.start until cursor.after loop if is_master then if cursor.item.name /= Void and peer_name_slave.is_equal (cursor.item.name) then -- master found slave npg.discovery_service.remove_response_listener (Current) cursor.item.unset_lifetime -- make it eternally valid npg.discovery_service.publish_advertisement_locally (cursor.item) create address_slave.make_with_id (cursor.item.peer_id, benchmark_service_name, Void) pid_slave := cursor.item.peer_id cursor.go_after else cursor.forth end else if cursor.item.name /= Void and peer_name_master.is_equal (cursor.item.name) then -- slave found master npg.discovery_service.remove_response_listener (Current) cursor.item.unset_lifetime -- make it eternally valid npg.discovery_service.publish_advertisement_locally (cursor.item) create address_master.make_with_id (cursor.item.peer_id, benchmark_service_name, Void) pid_master := cursor.item.peer_id cursor.go_after else cursor.forth end end end end end start_benchmark is -- Main benchmark part deferred end benchmark_message_handler (a_msg: P2P_MESSAGE; a_source, a_destination: P2P_ENDPOINT_ADDRESS) is -- Benchmark message handler require Message_valid: a_msg /= Void Source_valid: a_source /= Void and a_source.is_valid Destination_valid: a_destination /= Void and a_destination.is_valid and a_destination.service_name.is_equal (benchmark_service_name) deferred end send_message (a_msg: P2P_MESSAGE) is -- Send message to other node do if is_master then npg.endpoint_service.send_message (address_slave, a_msg) else npg.endpoint_service.send_message (address_master, a_msg) end end end