pongc and pongd
This commit is contained in:
parent
25058bc708
commit
dd9f94ae78
@ -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
|
|
||||||
|
|
||||||
server_fd = client.server_fd
|
|
||||||
|
|
||||||
if server_fd.nil?
|
|
||||||
puts "there is no server_fd!!"
|
puts "there is no server_fd!!"
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def info(message : String)
|
client << 0
|
||||||
puts message unless CLI.verbosity
|
|
||||||
end
|
|
||||||
|
|
||||||
message = IPC::Message.new server_fd, CLI.type.to_u8, CLI.user_type.to_u8, CLI.message
|
client.loop do |event|
|
||||||
|
puts "CLIENT LOOP"
|
||||||
client.send message
|
|
||||||
client << 0
|
|
||||||
|
|
||||||
client.loop do |event|
|
|
||||||
case event
|
case event
|
||||||
when IPC::Event::ExtraSocket
|
when IPC::Event::ExtraSocket
|
||||||
|
puts "extra socket fd #{event.fd}"
|
||||||
info "reading on #{event.fd}"
|
info "reading on #{event.fd}"
|
||||||
if event.fd == 0
|
if event.fd == 0
|
||||||
puts "reading on STDIN"
|
puts "reading on STDIN"
|
||||||
end
|
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
|
when IPC::Event::MessageReceived
|
||||||
puts "\033[32mthere is a message\033[00m"
|
puts "\033[32mthere is a message\033[00m"
|
||||||
puts event.message.to_s
|
puts event.message.to_s
|
||||||
client.close
|
client.close
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
main
|
||||||
|
@ -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}"
|
|
||||||
if verbosity >= 2
|
|
||||||
m = String.new event.message.payload
|
m = String.new event.message.payload
|
||||||
puts "#{CBLUE}message type #{event.message.utype}: #{m} #{CRESET}"
|
debug "message type #{event.message.utype}: #{m}"
|
||||||
end
|
unless no_response
|
||||||
end
|
service.send event.message
|
||||||
service.send event.message unless no_response
|
debug "sending message..."
|
||||||
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
13
tests/prints.cr
Normal 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
|
Reference in New Issue
Block a user