class Airplay::Server
Attributes
port[R]
Public Class Methods
new()
click to toggle source
# File lib/airplay/server.rb, line 15 def initialize @port = Airplay.configuration.port || find_free_port @logger = Airplay::Logger.new("airplay::server") @server = Rack::Server.new( server: :reel, Host: private_ip, Port: @port, Logger: @logger, AccessLog: [], quiet: true, app: App.app ) start! end
Public Instance Methods
serve(file)
click to toggle source
Public: Adds a file to serve
file - The file path to be served
Returns the url that the file will have
# File lib/airplay/server.rb, line 37 def serve(file) sleep 0.1 until running? asset_id = App.settings[:assets][file] "http://#{private_ip}:#{@port}/assets/#{asset_id}" end
start!()
click to toggle source
Public: Starts the server in a new thread
Returns nothing
# File lib/airplay/server.rb, line 48 def start! Thread.start { @server.start } end
Private Instance Methods
find_free_port()
click to toggle source
Private: Finds a free port by asking the kernel for a free one
Returns a free port number
# File lib/airplay/server.rb, line 82 def find_free_port socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) socket.listen(1) port = socket.local_address.ip_port socket.close port end
private_ip()
click to toggle source
Private: The local ip of the machine
Returns the ip address of the current machine
# File lib/airplay/server.rb, line 72 def private_ip @_ip ||= Socket.ip_address_list.detect do |addr| addr.ipv4_private? end.ip_address end
running?(port = @port)
click to toggle source
Private: Checks the state if the server by attempting a connection to it
Returns a boolean with the state
# File lib/airplay/server.rb, line 58 def running?(port = @port) begin socket = TCPSocket.new(private_ip, port) socket.close unless socket.nil? true rescue Errno::ECONNREFUSED, Errno::EBADF, Errno::EADDRNOTAVAIL false end end