New configuration system.
This commit is contained in:
parent
cad5a776cc
commit
a74bed308b
@ -28,45 +28,61 @@ class Tracking::Request
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Context
|
class Baguette::Configuration
|
||||||
# service instance parameters
|
class Websocket < Base
|
||||||
# they can be changed via the cli
|
# service instance parameters
|
||||||
class_property service_name = "websocket"
|
# they can be changed via the cli
|
||||||
class_property host = "0.0.0.0"
|
property service_name : String = "websocket"
|
||||||
class_property port_to_listen : UInt16 = 1234
|
property host : String = "0.0.0.0"
|
||||||
class_property timer_delay : Int32 = 30_000.to_i32
|
property port : UInt16 = 1234
|
||||||
|
|
||||||
class_property print_messages = false
|
property ipc_timer : Int32 = 30_000.to_i32
|
||||||
class_property print_timer = false
|
property print_messages : Bool = false
|
||||||
|
property print_ipc_timer : Bool = false
|
||||||
|
|
||||||
|
property verbosity : Int32 = 3
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
end
|
||||||
|
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|
|
OptionParser.parse do |parser|
|
||||||
parser.on "-l host", "--l host", "IP address to listen on." do |h|
|
parser.on "-l host", "--l host", "IP address to listen on." do |h|
|
||||||
Context.host = h
|
configuration.host = h
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-p port", "--port port", "Port to listen on." do |port|
|
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
|
end
|
||||||
|
|
||||||
parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
|
parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
|
||||||
Context.service_name = name
|
configuration.service_name = name
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t|
|
parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t|
|
||||||
Context.timer_delay = t.to_i32 * 1000
|
configuration.ipc_timer = t.to_i32 * 1000
|
||||||
end
|
|
||||||
|
|
||||||
parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt|
|
|
||||||
Baguette::Context.verbosity = opt.to_i
|
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-T", "--print-timer", "Print timer." do
|
parser.on "-T", "--print-timer", "Print timer." do
|
||||||
Context.print_timer = true
|
configuration.print_ipc_timer = true
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-M", "--print-messages", "Print messages received and sent." do
|
parser.on "-M", "--print-messages", "Print messages received and sent." do
|
||||||
Context.print_messages = true
|
configuration.print_messages = true
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-h", "--help", "Show this help" do
|
parser.on "-h", "--help", "Show this help" do
|
||||||
@ -169,12 +185,30 @@ class InstanceStorage
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Context
|
class Context
|
||||||
class_property service = IPC::SwitchingService.new service_name
|
class_property service_name = "websocketd"
|
||||||
class_property context = InstanceStorage.new service
|
|
||||||
end
|
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
|
# 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
|
Context.service << server.fd
|
||||||
|
|
||||||
def websocket_client_connection(client)
|
def websocket_client_connection(client)
|
||||||
@ -500,7 +534,7 @@ end
|
|||||||
|
|
||||||
# Every few seconds, the service should trigger the timer
|
# Every few seconds, the service should trigger the timer
|
||||||
# Allowing the sending of Ping messages to clients
|
# 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|
|
Context.service.loop do |event|
|
||||||
begin
|
begin
|
||||||
|
Loading…
Reference in New Issue
Block a user