Archived
3
0

pongc and pongd

This commit is contained in:
Karchnu 2020-07-20 16:26:58 +02:00
parent 25058bc708
commit dd9f94ae78
3 changed files with 66 additions and 51 deletions

View File

@ -1,4 +1,6 @@
require "option_parser"
require "../src/ipc.cr"
require "./prints.cr"
class CLI
class_property service_name = "pong"
@ -45,34 +47,43 @@ OptionParser.parse do |parser|
end
end
client = IPC::Client.new service_name
def main
client = IPC::Client.new CLI.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
def info(message : String)
puts message unless CLI.verbosity
end
message = IPC::Message.new server_fd, CLI.type.to_u8, CLI.user_type.to_u8, CLI.message
client.send message
client << 0
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
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.not_nil!,
CLI.type.to_u8,
CLI.user_type.to_u8,
mstr
message.fd = server_fd.not_nil!
client.send message
when IPC::Event::MessageReceived
puts "\033[32mthere is a message\033[00m"
puts event.message.to_s
@ -80,3 +91,6 @@ client.loop do |event|
exit
end
end
end
main

View File

@ -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
info "IPC::Event::MessageReceived, client: #{event.fd}"
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}"
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

13
tests/prints.cr Normal file
View File

@ -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