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/pongc.cr

103 lines
2.3 KiB
Crystal
Raw Normal View History

2020-07-20 16:26:58 +02:00
require "option_parser"
2020-07-14 17:04:51 +02:00
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 15:37:50 +02:00
class CLI
class_property service_name = "pong"
class_property message : String? = nil
class_property type = 1
class_property user_type = 42
class_property verbosity = 1
class_property rounds = 1
end
OptionParser.parse do |parser|
parser.on "-s service_name", "--service-name service_name", "URI" do |optsn|
CLI.service_name = optsn
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
parser.on "-t message_type",
"--type message_type",
"(internal) message type." do |opt|
CLI.type = opt.to_i
end
parser.on "-u user_message_type",
"--user-type user_message_type",
"Message type." do |opt|
CLI.user_type = opt.to_i
end
parser.on "-r rounds", "--rounds count", "Number of messages sent." do |opt|
CLI.rounds = opt.to_i
end
parser.on "-m message", "--message m", "Message to sent." do |opt|
CLI.message = opt
end
parser.on "-h", "--help", "Show this help" do
puts parser
exit 0
end
end
2020-07-20 16:26:58 +02:00
def main
client = IPC::Client.new CLI.service_name
client.base_timer = 30_000 # 30 seconds
client.timer = 30_000 # 30 seconds
2020-07-20 15:37:50 +02:00
2020-07-20 16:26:58 +02:00
server_fd = client.server_fd
if server_fd.nil?
puts "there is no server_fd!!"
exit 1
end
2020-07-14 17:04:51 +02:00
2020-07-20 18:54:57 +02:00
# Listening on STDIN.
2020-07-20 16:26:58 +02:00
client << 0
client.loop do |event|
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
mstr = if CLI.message.nil?
2020-07-20 18:54:57 +02:00
if event.fd == 0 STDIN.gets || "STDIN failed!" else "coucou" end
2020-07-20 16:26:58 +02:00
else
CLI.message.not_nil!
end
message = IPC::Message.new server_fd.not_nil!,
CLI.type.to_u8,
CLI.user_type.to_u8,
mstr
message.fd = server_fd.not_nil!
2020-07-20 18:54:57 +02:00
CLI.rounds.times do |i|
client.send message
end
2020-07-20 16:26:58 +02:00
when IPC::Event::MessageReceived
2020-07-20 18:54:57 +02:00
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}"
2020-07-20 15:37:50 +02:00
end
2020-07-14 17:04:51 +02:00
end
end
2020-07-20 16:26:58 +02:00
main