Obsolete
/
ipc.cr-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
ipc.cr-old/tests/pongd.cr

67 lines
1.6 KiB
Crystal
Raw Normal View History

2020-07-14 17:04:51 +02:00
require "option_parser"
require "../src/ipc.cr"
2020-07-20 16:26:58 +02:00
require "./prints.cr"
2020-07-14 17:04:51 +02:00
verbosity = 1
service_name = "pong"
no_response = false
OptionParser.parse do |parser|
parser.on "-s service_name", "--service-name service_name", "URI" do |optsn|
service_name = optsn
end
parser.on "-n", "--no-response", "Do not provide any response back." do
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
end
parser.on "-h", "--help", "Show this help" do
puts parser
exit 0
end
end
service = IPC::Server.new (service_name)
2020-07-20 16:26:58 +02:00
service.base_timer = 30_000 # 30 seconds
service.timer = 30_000 # 30 seconds
2020-07-14 17:04:51 +02:00
service.loop do |event|
case event
when IPC::Event::Timer
2020-07-20 16:26:58 +02:00
info "IPC::Event::Timer"
2020-07-14 17:04:51 +02:00
when IPC::Event::Connection
2020-07-20 16:26:58 +02:00
info "IPC::Event::Connection, client: #{event.fd}"
2020-07-14 17:04:51 +02:00
when IPC::Event::Disconnection
2020-07-20 16:26:58 +02:00
info "IPC::Event::Disconnection, client: #{event.fd}"
2020-07-14 17:04:51 +02:00
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
2020-07-20 16:26:58 +02:00
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..."
2020-07-14 17:04:51 +02:00
end
rescue e
2020-07-20 16:26:58 +02:00
important "#{e.message}"
2020-07-14 17:04:51 +02:00
service.remove_fd event.fd
end
else
2020-07-20 16:26:58 +02:00
important "Exception: message #{event}"
2020-07-14 17:04:51 +02:00
end
end