From a74bed308be3deedf98c0c96d863f087c76434a5 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Sun, 25 Oct 2020 06:22:37 +0100 Subject: [PATCH] New configuration system. --- src/websocketd.cr | 80 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/src/websocketd.cr b/src/websocketd.cr index 134bd82..d42962b 100644 --- a/src/websocketd.cr +++ b/src/websocketd.cr @@ -28,45 +28,61 @@ class Tracking::Request end end -class Context - # service instance parameters - # they can be changed via the cli - class_property service_name = "websocket" - class_property host = "0.0.0.0" - class_property port_to_listen : UInt16 = 1234 - class_property timer_delay : Int32 = 30_000.to_i32 +class Baguette::Configuration + class Websocket < Base + # service instance parameters + # they can be changed via the cli + property service_name : String = "websocket" + property host : String = "0.0.0.0" + property port : UInt16 = 1234 - class_property print_messages = false - class_property print_timer = false + property ipc_timer : Int32 = 30_000.to_i32 + property print_messages : Bool = false + property print_ipc_timer : Bool = false + + property verbosity : Int32 = 3 + + def initialize + end + end end +simulation, no_configuration, configuration_file = Baguette::Configuration.option_parser + +configuration = if no_configuration + Baguette::Log.info "do not load a configuration file." + Baguette::Configuration::Websocket.new +else + Baguette::Configuration::Websocket.get(configuration_file) || + Baguette::Configuration::Websocket.new +end + +Baguette::Context.verbosity = configuration.verbosity + + OptionParser.parse do |parser| parser.on "-l host", "--l host", "IP address to listen on." do |h| - Context.host = h + configuration.host = h end parser.on "-p port", "--port port", "Port to listen on." do |port| - Context.port_to_listen = port.to_u16 + configuration.port = port.to_u16 end parser.on "-s service-name", "--service-name service-name", "Service name." do |name| - Context.service_name = name + configuration.service_name = name end parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t| - Context.timer_delay = t.to_i32 * 1000 - end - - parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt| - Baguette::Context.verbosity = opt.to_i + configuration.ipc_timer = t.to_i32 * 1000 end parser.on "-T", "--print-timer", "Print timer." do - Context.print_timer = true + configuration.print_ipc_timer = true end parser.on "-M", "--print-messages", "Print messages received and sent." do - Context.print_messages = true + configuration.print_messages = true end parser.on "-h", "--help", "Show this help" do @@ -169,12 +185,30 @@ class InstanceStorage end class Context - class_property service = IPC::SwitchingService.new service_name - class_property context = InstanceStorage.new service + class_property service_name = "websocketd" end +Context.service_name = configuration.service_name + +class Context + class_property service = IPC::SwitchingService.new service_name + class_property context = InstanceStorage.new service + class_property print_messages = false + class_property print_timer = false +end + +Context.print_messages = configuration.print_messages +Context.print_timer = configuration.print_ipc_timer + + +if simulation + pp! configuration + exit 0 +end + + # by default, listen on any IP address -server = TCPServer.new(Context.host, Context.port_to_listen) +server = TCPServer.new configuration.host, configuration.port Context.service << server.fd def websocket_client_connection(client) @@ -500,7 +534,7 @@ end # Every few seconds, the service should trigger the timer # Allowing the sending of Ping messages to clients -Context.service.base_timer = Context.timer_delay +Context.service.base_timer = configuration.ipc_timer Context.service.loop do |event| begin