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

79 lines
1.9 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
2020-07-20 16:31:17 +02:00
class CLI
class_property service_name = "pong"
class_property verbosity = 1
2020-07-20 18:54:57 +02:00
class_property timer = 30_000
2020-07-20 16:31:17 +02:00
class_property no_response = false
end
2020-07-14 17:04:51 +02:00
OptionParser.parse do |parser|
parser.on "-s service_name", "--service-name service_name", "URI" do |optsn|
2020-07-20 16:31:17 +02:00
CLI.service_name = optsn
2020-07-14 17:04:51 +02:00
end
parser.on "-n", "--no-response", "Do not provide any response back." do
2020-07-20 16:31:17 +02:00
CLI.no_response = true
2020-07-14 17:04:51 +02:00
end
2020-07-20 18:54:57 +02:00
parser.on "-t timer", "--timer ms", "Timer in ms. Default: 30 000" do |optsn|
CLI.timer = optsn.to_i
end
2020-07-14 17:04:51 +02:00
parser.on "-v verbosity", "--verbosity verbosity", "Verbosity (0 = nothing is printed, 1 = only events, 2 = events and messages). Default: 1" do |optsn|
2020-07-20 16:31:17 +02:00
CLI.verbosity = optsn.to_i
2020-07-14 17:04:51 +02:00
end
parser.on "-h", "--help", "Show this help" do
puts parser
exit 0
end
end
2020-07-20 16:31:17 +02:00
def main
service = IPC::Server.new CLI.service_name
2020-07-20 18:54:57 +02:00
service.base_timer = CLI.timer # default: 30 seconds
service.timer = CLI.timer # default: 30 seconds
2020-07-14 17:04:51 +02:00
2020-07-20 16:31:17 +02:00
service.loop do |event|
2020-07-23 19:10:16 +02:00
# service.pp
2020-07-20 16:31:17 +02:00
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
2020-07-14 17:04:51 +02:00
end
2020-07-20 16:31:17 +02:00
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}"
2020-07-14 17:04:51 +02:00
2020-07-20 16:31:17 +02:00
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}"
2020-07-14 17:04:51 +02:00
end
end
end
2020-07-20 16:31:17 +02:00
main