Sha256: b6c3fb478ed4bdb773dd3e5ba970deeb2c1e72486005b561dda6110e487c47e4

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

require 'rack'
require 'socket'

module AirPlayer
  class Server
    attr_reader :uri

    def initialize(video_path)
      @server = Rack::Server.new(server: :webrick, Host: local_ip, Port: 7070, app: Rack::File.new(video_path))
      @uri  = "http://#{@server.options[:Host]}:#{@server.options[:Port]}"
    end

    def start
      Thread.start do
        # Output WEBrick access log to file
        $stderr = File.open(Logger.path, File::WRONLY | File::APPEND | File::CREAT)
        @server.start
        $stderr = STDERR
      end
    end

    def stop
      @server.server.shutdown
    end

    private
      # networking - Getting the Hostname or IP in Ruby on Rails - Stack Overflow
      #   http://stackoverflow.com/questions/42566/getting-the-hostname-or-ip-in-ruby-on-rails
      def local_ip
        # turn off reverse DNS resolution temporarily
        orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true

        UDPSocket.open do |s|
          s.connect('8.8.8.8', 1)
          s.addr.last
        end
      ensure
        Socket.do_not_reverse_lookup = orig
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
airplayer-0.0.8 lib/airplayer/server.rb
airplayer-0.0.7 lib/airplayer/server.rb