Sha256: d31e197df3ec92818c52cc1e6ccf29b03cc8f3ecded321fedddce6d3b8032e08

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'optparse'

module Kamerling class ServerRunner
  def initialize args, classes: def_classes, orm: Sequel, repos: Repos
    @args    = args
    repos.db = orm.connect settings.db
    @servers = { http: :TCP, tcp: :TCP, udp: :UDP }.map do |type, prot|
      addr = Addr[settings.host, settings.send(type), prot]
      classes[type].new addr: addr if addr.port
    end.compact
  end

  def join
    servers.each(&:join)
  end

  def start
    servers.each(&:start)
    self
  end

  attr_reader :args, :servers
  private     :args, :servers

  private

  Settings = Struct.new(*%i(db host http tcp udp))

  def def_classes
    { http: Server::HTTP, tcp: Server::TCP, udp: Server::UDP }
  end

  def defaults
    Settings.new 'sqlite::memory:', '127.0.0.1'
  end

  def settings
    @settings ||= defaults.tap do |set|
      OptionParser.new do |opt|
        opt.on("--db #{set.db}", String, 'database') { |db| set.db = db }
        opt.on("--host #{set.host}", String, 'host') { |host| set.host = host }
        opt.on('--http 0', Integer, 'HTTP port') { |http| set.http = http }
        opt.on('--tcp 0',  Integer, 'TCP port')  { |tcp|  set.tcp  = tcp  }
        opt.on('--udp 0',  Integer, 'UDP port')  { |udp|  set.udp  = udp  }
      end.parse! args
    end
  end
end end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kamerling-0.0.2 lib/kamerling/server_runner.rb