Sha256: e1912b9060931802774234bf038e4d1a6546c671a07e25b13c0d61c24b2380cf

Contents?: true

Size: 1.58 KB

Versions: 7

Compression:

Stored size: 1.58 KB

Contents

# encoding: utf-8
# Author: kimoto
require 'vrowser'
require 'webrick'

class Vrowser::HTTPDaemon
  def self.start(options={})
    self.new(options) do |instance|
      instance.start
    end
  end

  public
  def initialize(options={})
    @config_path = options[:config_path] or raise ArgumentError("config_path")

    @server = WEBrick::HTTPServer.new(options)
    @server.mount_proc("/api/updated/json"){ |req, res|
      res.header["Content-Type"] = "application/json"
      res.body = get_active_servers_nary.to_json
    }
    @server.mount_proc("/api/connected/json"){ |req, res|
      res.header["Content-Type"] = "application/json"
      res.body = get_active_servers.to_json
    }

    yield(self) if block_given?
    self
  end

  def start
    @th = Thread.start do
      fetch_and_update @config_path
    end
    regist_stop
    @server.start
  end

  def stop
    @server.shutdown if @server
    Thread.kill(@th) if @th
  end

  private
  def regist_stop
    trap("INT") do
      stop
    end
  end

  def fetch_and_update(config_path)
    Vrowser.load_file(config_path) do |vrowser|
      while true
        puts "update server list"
        vrowser.fetch

        (60 / 5).times do
          puts "try update"
          vrowser.update
          sleep (60 * 5)
        end
      end
    end
  end

  def get_active_servers
    Vrowser.load_file(@config_path){ |v|
      query = v.active_servers.select(:name, :host, :ping, :num_players, :type, :map, :players)
      return query.order(:host).map(&:values)
    }
    nil
  end

  def get_active_servers_nary
    get_active_servers.map(&:values)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vrowser-0.0.9 lib/vrowser/http_daemon.rb
vrowser-0.0.8 lib/vrowser/http_daemon.rb
vrowser-0.0.7 lib/vrowser/http_daemon.rb
vrowser-0.0.6 lib/vrowser/http_daemon.rb
vrowser-0.0.5 lib/vrowser/http_daemon.rb
vrowser-0.0.4 lib/vrowser/http_daemon.rb
vrowser-0.0.3 lib/vrowser/http_daemon.rb