From dd9f94ae78d72cc8f2d962d5c87be5be14f4e298 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Mon, 20 Jul 2020 16:26:58 +0200 Subject: [PATCH] pongc and pongd --- tests/pongc.cr | 64 ++++++++++++++++++++++++++++++------------------- tests/pongd.cr | 40 +++++++++++-------------------- tests/prints.cr | 13 ++++++++++ 3 files changed, 66 insertions(+), 51 deletions(-) create mode 100644 tests/prints.cr diff --git a/tests/pongc.cr b/tests/pongc.cr index 4518597..18f01bf 100644 --- a/tests/pongc.cr +++ b/tests/pongc.cr @@ -1,4 +1,6 @@ +require "option_parser" require "../src/ipc.cr" +require "./prints.cr" class CLI class_property service_name = "pong" @@ -45,38 +47,50 @@ OptionParser.parse do |parser| end end +def main + client = IPC::Client.new CLI.service_name + client.base_timer = 30_000 # 30 seconds + client.timer = 30_000 # 30 seconds -client = IPC::Client.new service_name -client.base_timer = 30_000 # 30 seconds -client.timer = 30_000 # 30 seconds + server_fd = client.server_fd + if server_fd.nil? + puts "there is no server_fd!!" + exit 1 + end -server_fd = client.server_fd + client << 0 -if server_fd.nil? - puts "there is no server_fd!!" - exit 1 -end + client.loop do |event| + puts "CLIENT LOOP" + case event + when IPC::Event::ExtraSocket + puts "extra socket fd #{event.fd}" + info "reading on #{event.fd}" + if event.fd == 0 + puts "reading on STDIN" + end -def info(message : String) - puts message unless CLI.verbosity -end + mstr = if CLI.message.nil? + if event.fd == 0 STDIN.gets_to_end else "coucou" end + else + CLI.message.not_nil! + end -message = IPC::Message.new server_fd, CLI.type.to_u8, CLI.user_type.to_u8, CLI.message + message = IPC::Message.new server_fd.not_nil!, + CLI.type.to_u8, + CLI.user_type.to_u8, + mstr -client.send message -client << 0 + message.fd = server_fd.not_nil! -client.loop do |event| - case event - when IPC::Event::ExtraSocket - info "reading on #{event.fd}" - if event.fd == 0 - puts "reading on STDIN" + client.send message + when IPC::Event::MessageReceived + puts "\033[32mthere is a message\033[00m" + puts event.message.to_s + client.close + exit end - when IPC::Event::MessageReceived - puts "\033[32mthere is a message\033[00m" - puts event.message.to_s - client.close - exit end end + +main diff --git a/tests/pongd.cr b/tests/pongd.cr index b3c3314..e11c29b 100644 --- a/tests/pongd.cr +++ b/tests/pongd.cr @@ -1,6 +1,6 @@ require "option_parser" require "../src/ipc.cr" -require "./colors" +require "./prints.cr" verbosity = 1 service_name = "pong" @@ -26,23 +26,17 @@ OptionParser.parse do |parser| end service = IPC::Server.new (service_name) -service.base_timer = 5000 # 5 seconds -service.timer = 5000 # 5 seconds +service.base_timer = 30_000 # 30 seconds +service.timer = 30_000 # 30 seconds service.loop do |event| case event when IPC::Event::Timer - if verbosity >= 1 - puts "#{CORANGE}IPC::Event::Timer#{CRESET}" - end + info "IPC::Event::Timer" when IPC::Event::Connection - if verbosity >= 1 - puts "#{CBLUE}IPC::Event::Connection#{CRESET}, client: #{event.fd}" - end + info "IPC::Event::Connection, client: #{event.fd}" when IPC::Event::Disconnection - if verbosity >= 1 - puts "#{CBLUE}IPC::Event::Disconnection#{CRESET}, client: #{event.fd}" - end + info "IPC::Event::Disconnection, client: #{event.fd}" when IPC::Event::MessageSent begin if verbosity >= 1 @@ -54,25 +48,19 @@ service.loop do |event| end when IPC::Event::MessageReceived begin - if verbosity >= 1 - puts "#{CGREEN}IPC::Event::MessageReceived#{CRESET}, client: #{event.fd}" - if verbosity >= 2 - m = String.new event.message.payload - puts "#{CBLUE}message type #{event.message.utype}: #{m} #{CRESET}" - end - end - service.send event.message unless no_response - if verbosity >= 2 && ! no_response - puts "#{CBLUE}sending message...#{CRESET}" + info "IPC::Event::MessageReceived, client: #{event.fd}" + m = String.new event.message.payload + debug "message type #{event.message.utype}: #{m}" + unless no_response + service.send event.message + debug "sending message..." end rescue e - puts "#{CRED}#{e.message}#{CRESET}" + important "#{e.message}" service.remove_fd event.fd end else - if verbosity >= 1 - puts "#{CRED}Exception: message #{event} #{CRESET}" - end + important "Exception: message #{event}" end end diff --git a/tests/prints.cr b/tests/prints.cr new file mode 100644 index 0000000..7147c52 --- /dev/null +++ b/tests/prints.cr @@ -0,0 +1,13 @@ +require "./colors" + +def important(message : String) + puts "#{CRED}#{message}#{CRESET}" unless CLI.verbosity > 0 +end + +def info(message : String) + puts "#{CGREEN}#{message}#{CRESET}" unless CLI.verbosity > 1 +end + +def debug(message : String) + puts "#{CBLUE}#{message}#{CRESET}" unless CLI.verbosity > 2 +end