ipcd/src/admind.cr

93 lines
1.9 KiB
Crystal

require "option_parser"
require "ipc"
require "./colors"
require "json"
verbosity = 1
service_name = "admind"
OptionParser.parse! do |parser|
parser.on "-s service_name", "--service-name service_name", "URI" do |optsn|
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|
verbosity = optsn.to_i
end
parser.on "-h", "--help", "Show this help" do
puts parser
exit 0
end
end
class Rectangle
JSON.mapping(
x: UInt32,
y: UInt32
)
def initialize(@x : UInt32, @y : UInt32)
end
end
class WidgetTree
JSON.mapping(
rectangles: Array(Rectangle)
)
def initialize(@rectangles : Array(Rectangle))
end
end
class Page
JSON.mapping(
page: String,
title: String,
tree: WidgetTree
)
def initialize(@page, @title, @tree)
end
end
IPC::Service.new (service_name) do |event|
case event
when IPC::Event::Timer
if verbosity >= 1
puts "#{CORANGE}IPC::Event::Timer#{CRESET}"
end
when IPC::Event::Connection
if verbosity >= 1
puts "#{CBLUE}IPC::Event::Connection#{CRESET}, client: #{event.connection.fd}"
end
when IPC::Event::Disconnection
if verbosity >= 1
puts "#{CBLUE}IPC::Event::Disconnection#{CRESET}, client: #{event.connection.fd}"
end
when IPC::Event::Message
if verbosity >= 1
puts "#{CGREEN}IPC::Event::Message#{CRESET}, client: #{event.connection.fd}"
if verbosity == 2
puts "#{CBLUE}message: #{event.message} #{CRESET}"
end
end
# JSON message
rectangles = Array(Rectangle).new
rectangles << Rectangle.new 1, 2
rectangles << Rectangle.new 2, 1
rectangles << Rectangle.new 3, 1
widgets = WidgetTree.new(rectangles)
page = Page.new "admin", "This is the Admin Page", widgets
event.connection.send 2, page.to_json
else
if verbosity >= 1
puts "#{CRED}Exception: message = #{event.message} #{CRESET}"
end
end
end