Sha256: feb87687fd25c42baf174047f59dba1261e8facfe028a3f7ff6b3633e861c93a

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require 'eventmachine'
require 'em-synchrony'

$: << File.dirname(__FILE__)+'/radiodan/'

require 'em_additions'
require 'logging'
require 'builder'
require 'version'

class Radiodan
  include Logging

  def initialize(&blk)
    @builder = Builder.new(&blk)
  end
  
  def start
    # keep player running on schedule
    raise "no player set" unless player.adapter?
    
    stop_player_on_exit
    
    EM.synchrony do
      trap_signals!
      EventMachine::Synchrony.next_tick { @builder.call_middleware! }
      
      EM::Synchrony.add_periodic_timer(1) do
        logger.info "SYNC!"
        player.sync if player
      end
    end
  end

  def player
    @builder.player
  end
  
  def respond_to?(method)
    if player.respond_to? method
      true
    else
      super
    end
  end

  private
  
  def method_missing(method, *args, &block)
    if player.respond_to? method
      player.send method, *args, &block
    else
      super
    end
  end
  
  def stop_player_on_exit
    at_exit do
      logger.info 'Stopping player'
      stop
    end
  end
  
  def trap_signals!
    %w{INT TERM SIGHUP SIGINT SIGTERM}.each do |signal|
      Signal.trap(signal) do
        logger.info "Trapped #{signal}"
        EM.stop
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
radiodan-1.0.1 lib/radiodan.rb
radiodan-1.0.0 lib/radiodan.rb