From 17048dbd456088149f7b60516c93be26bef71a00 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Mon, 20 Jul 2020 16:31:17 +0200 Subject: [PATCH] Pongd fixed. --- tests/pongd.cr | 83 ++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/tests/pongd.cr b/tests/pongd.cr index e11c29b..3cd55c8 100644 --- a/tests/pongd.cr +++ b/tests/pongd.cr @@ -2,21 +2,23 @@ require "option_parser" require "../src/ipc.cr" require "./prints.cr" -verbosity = 1 -service_name = "pong" -no_response = false +class CLI + class_property service_name = "pong" + class_property verbosity = 1 + class_property no_response = false +end OptionParser.parse do |parser| parser.on "-s service_name", "--service-name service_name", "URI" do |optsn| - service_name = optsn + CLI.service_name = optsn end parser.on "-n", "--no-response", "Do not provide any response back." do - no_response = true + CLI.no_response = true end parser.on "-v verbosity", "--verbosity verbosity", "Verbosity (0 = nothing is printed, 1 = only events, 2 = events and messages). Default: 1" do |optsn| - verbosity = optsn.to_i + CLI.verbosity = optsn.to_i end parser.on "-h", "--help", "Show this help" do @@ -25,42 +27,45 @@ OptionParser.parse do |parser| end end -service = IPC::Server.new (service_name) -service.base_timer = 30_000 # 30 seconds -service.timer = 30_000 # 30 seconds +def main + service = IPC::Server.new CLI.service_name + service.base_timer = 30_000 # 30 seconds + service.timer = 30_000 # 30 seconds -service.loop do |event| - case event - when IPC::Event::Timer - info "IPC::Event::Timer" - when IPC::Event::Connection - info "IPC::Event::Connection, client: #{event.fd}" - when IPC::Event::Disconnection - info "IPC::Event::Disconnection, client: #{event.fd}" - when IPC::Event::MessageSent - begin - if verbosity >= 1 - puts "#{CGREEN}IPC::Event::MessageSent#{CRESET}, client: #{event.fd}" - end - rescue e - puts "#{CRED}#{e.message}#{CRESET}" - service.remove_fd event.fd - end - when IPC::Event::MessageReceived - begin - 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..." + service.loop do |event| + case event + when IPC::Event::Timer + info "IPC::Event::Timer" + when IPC::Event::Connection + info "IPC::Event::Connection, client: #{event.fd}" + when IPC::Event::Disconnection + info "IPC::Event::Disconnection, client: #{event.fd}" + when IPC::Event::MessageSent + begin + info "IPC::Event::MessageSent, client: #{event.fd}" + rescue e + important "#{e.message}" + service.remove_fd event.fd end + when IPC::Event::MessageReceived + begin + info "IPC::Event::MessageReceived, client: #{event.fd}" + m = String.new event.message.payload + debug "message type #{event.message.utype}: #{m}" - rescue e - important "#{e.message}" - service.remove_fd event.fd + unless CLI.no_response + service.send event.message + debug "sending message..." + end + + rescue e + important "#{e.message}" + service.remove_fd event.fd + end + else + important "Exception: message #{event}" end - else - important "Exception: message #{event}" end end + +main