From 725b23e4e59352814e6311d8887c06c7c4827d1e Mon Sep 17 00:00:00 2001 From: Karchnu Date: Mon, 20 Jul 2020 18:54:57 +0200 Subject: [PATCH] blah --- tests/pongc.cr | 20 +++++++++++++------- tests/pongd.cr | 11 +++++++++-- tests/prints.cr | 6 +++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/tests/pongc.cr b/tests/pongc.cr index 18f01bf..62d1658 100644 --- a/tests/pongc.cr +++ b/tests/pongc.cr @@ -58,10 +58,10 @@ def main exit 1 end + # Listening on STDIN. client << 0 client.loop do |event| - puts "CLIENT LOOP" case event when IPC::Event::ExtraSocket puts "extra socket fd #{event.fd}" @@ -71,7 +71,7 @@ def main end mstr = if CLI.message.nil? - if event.fd == 0 STDIN.gets_to_end else "coucou" end + if event.fd == 0 STDIN.gets || "STDIN failed!" else "coucou" end else CLI.message.not_nil! end @@ -83,12 +83,18 @@ def main message.fd = server_fd.not_nil! - client.send message + CLI.rounds.times do |i| + client.send message + end when IPC::Event::MessageReceived - puts "\033[32mthere is a message\033[00m" - puts event.message.to_s - client.close - exit + info "new message from #{event.fd}: #{event.message.to_s}" + when IPC::Event::Disconnection + info "Disconnection from #{event.fd}" + if event.fd == 0 + client.remove_fd 0 + end + else + info "unhandled event: #{event.class}" end end end diff --git a/tests/pongd.cr b/tests/pongd.cr index 3cd55c8..fc66f7d 100644 --- a/tests/pongd.cr +++ b/tests/pongd.cr @@ -5,6 +5,7 @@ require "./prints.cr" class CLI class_property service_name = "pong" class_property verbosity = 1 + class_property timer = 30_000 class_property no_response = false end @@ -17,6 +18,11 @@ OptionParser.parse do |parser| CLI.no_response = true end + parser.on "-t timer", "--timer ms", "Timer in ms. Default: 30 000" do |optsn| + CLI.timer = optsn.to_i + end + + parser.on "-v verbosity", "--verbosity verbosity", "Verbosity (0 = nothing is printed, 1 = only events, 2 = events and messages). Default: 1" do |optsn| CLI.verbosity = optsn.to_i end @@ -29,10 +35,11 @@ end def main service = IPC::Server.new CLI.service_name - service.base_timer = 30_000 # 30 seconds - service.timer = 30_000 # 30 seconds + service.base_timer = CLI.timer # default: 30 seconds + service.timer = CLI.timer # default: 30 seconds service.loop do |event| + service.pp case event when IPC::Event::Timer info "IPC::Event::Timer" diff --git a/tests/prints.cr b/tests/prints.cr index 7147c52..1770699 100644 --- a/tests/prints.cr +++ b/tests/prints.cr @@ -1,13 +1,13 @@ require "./colors" def important(message : String) - puts "#{CRED}#{message}#{CRESET}" unless CLI.verbosity > 0 + puts "#{CRED}#{message}#{CRESET}" if CLI.verbosity > 0 end def info(message : String) - puts "#{CGREEN}#{message}#{CRESET}" unless CLI.verbosity > 1 + puts "#{CGREEN}#{message}#{CRESET}" if CLI.verbosity > 1 end def debug(message : String) - puts "#{CBLUE}#{message}#{CRESET}" unless CLI.verbosity > 2 + puts "#{CBLUE}#{message}#{CRESET}" if CLI.verbosity > 2 end