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 "../src/ipc.cr"
require "./prints.cr"
class CLI class CLI
class_property service_name = "pong" class_property service_name = "pong"
@ -45,38 +47,50 @@ OptionParser.parse do |parser|
end end
end end
def main
client = IPC::Client.new CLI.service_name
client.base_timer = 30_000 # 30 seconds
client.timer = 30_000 # 30 seconds
client = IPC::Client.new service_name server_fd = client.server_fd
client.base_timer = 30_000 # 30 seconds if server_fd.nil?
client.timer = 30_000 # 30 seconds puts "there is no server_fd!!"
exit 1
end
server_fd = client.server_fd client << 0
if server_fd.nil? client.loop do |event|
puts "there is no server_fd!!" puts "CLIENT LOOP"
exit 1 case event
end when IPC::Event::ExtraSocket
puts "extra socket fd #{event.fd}"
info "reading on #{event.fd}"
if event.fd == 0
puts "reading on STDIN"
end
def info(message : String) mstr = if CLI.message.nil?
puts message unless CLI.verbosity if event.fd == 0 STDIN.gets_to_end else "coucou" end
end else
CLI.message.not_nil!
end
message = IPC::Message.new server_fd, CLI.type.to_u8, CLI.user_type.to_u8, CLI.message message = IPC::Message.new server_fd.not_nil!,
CLI.type.to_u8,
CLI.user_type.to_u8,
mstr
client.send message message.fd = server_fd.not_nil!
client << 0
client.loop do |event| client.send message
case event when IPC::Event::MessageReceived
when IPC::Event::ExtraSocket puts "\033[32mthere is a message\033[00m"
info "reading on #{event.fd}" puts event.message.to_s
if event.fd == 0 client.close
puts "reading on STDIN" exit
end end
when IPC::Event::MessageReceived
puts "\033[32mthere is a message\033[00m"
puts event.message.to_s
client.close
exit
end end
end end
main

View File

@ -1,6 +1,6 @@
require "option_parser" require "option_parser"
require "../src/ipc.cr" require "../src/ipc.cr"
require "./colors" require "./prints.cr"
verbosity = 1 verbosity = 1
service_name = "pong" service_name = "pong"
@ -26,23 +26,17 @@ OptionParser.parse do |parser|
end end
service = IPC::Server.new (service_name) service = IPC::Server.new (service_name)
service.base_timer = 5000 # 5 seconds service.base_timer = 30_000 # 30 seconds
service.timer = 5000 # 5 seconds service.timer = 30_000 # 30 seconds
service.loop do |event| service.loop do |event|
case event case event
when IPC::Event::Timer when IPC::Event::Timer
if verbosity >= 1 info "IPC::Event::Timer"
puts "#{CORANGE}IPC::Event::Timer#{CRESET}"
end
when IPC::Event::Connection when IPC::Event::Connection
if verbosity >= 1 info "IPC::Event::Connection, client: #{event.fd}"
puts "#{CBLUE}IPC::Event::Connection#{CRESET}, client: #{event.fd}"
end
when IPC::Event::Disconnection when IPC::Event::Disconnection
if verbosity >= 1 info "IPC::Event::Disconnection, client: #{event.fd}"
puts "#{CBLUE}IPC::Event::Disconnection#{CRESET}, client: #{event.fd}"
end
when IPC::Event::MessageSent when IPC::Event::MessageSent
begin begin
if verbosity >= 1 if verbosity >= 1
@ -54,25 +48,19 @@ service.loop do |event|
end end
when IPC::Event::MessageReceived when IPC::Event::MessageReceived
begin begin
if verbosity >= 1 info "IPC::Event::MessageReceived, client: #{event.fd}"
puts "#{CGREEN}IPC::Event::MessageReceived#{CRESET}, client: #{event.fd}" m = String.new event.message.payload
if verbosity >= 2 debug "message type #{event.message.utype}: #{m}"
m = String.new event.message.payload unless no_response
puts "#{CBLUE}message type #{event.message.utype}: #{m} #{CRESET}" service.send event.message
end debug "sending message..."
end
service.send event.message unless no_response
if verbosity >= 2 && ! no_response
puts "#{CBLUE}sending message...#{CRESET}"
end end
rescue e rescue e
puts "#{CRED}#{e.message}#{CRESET}" important "#{e.message}"
service.remove_fd event.fd service.remove_fd event.fd
end end
else else
if verbosity >= 1 important "Exception: message #{event}"
puts "#{CRED}Exception: message #{event} #{CRESET}"
end
end end
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