lib/larynx.rb in larynx-0.1.5 vs lib/larynx.rb in larynx-0.1.6

- old
+ new

@@ -1,119 +1,30 @@ require 'rubygems' require 'eventmachine' require 'active_support' -require 'logger' -require 'daemons/daemonize' require 'larynx/version' require 'larynx/logger' +require 'larynx/server' require 'larynx/observable' require 'larynx/callbacks' require 'larynx/callbacks_with_async' require 'larynx/session' require 'larynx/response' require 'larynx/command' require 'larynx/commands' require 'larynx/prompt' require 'larynx/application' -require 'larynx/fields' +require 'larynx/field' require 'larynx/form' require 'larynx/restartable_timer' require 'larynx/call_handler' module Larynx class << self include Callbacks - define_callback :connect, :answer, :hungup - - def parse_options(args=ARGV) - @options = { - :ip => "0.0.0.0", - :port => 8084, - :pid_file => "./larynx.pid", - :log_file => "./larynx.log" - } - opts = OptionParser.new - opts.banner = "Usage: larynx [options]" - opts.separator '' - opts.separator "Larynx is a framework to develop FreeSWITCH IVR applications in Ruby." - opts.on('-i', '--ip IP', 'Listen for connections on this IP') {|ip| @options[:ip] = ip } - opts.on('-p', '--port PORT', 'Listen on this port', Integer) {|port| @options[:port] = port } - opts.on('-d', '--daemonize', 'Run as daemon') { @options[:daemonize] = true } - opts.on('-l', '--log-file FILE', 'Defaults to /app/root/larynx.log') {|log| @options[:log_file] = log } - opts.on( '--pid-file FILE', 'Defaults to /app/root/larynx.pid') {|pid| @options[:pid_file] = pid } - opts.on('-h', '--help', 'This is it') { $stderr.puts opts; exit 0 } - opts.on('-v', '--version') { $stderr.puts "Larynx version #{Larynx::VERSION}"; exit 0 } - opts.parse!(args) - end - - def setup_logger - logger = Larynx::Logger.new(@options[:log_file]) - logger.level = Logger::INFO - Object.const_set "LARYNX_LOGGER", logger - end - - def graceful_exit - msg = "Shutting down Larynx" - $stderr.puts msg unless @options[:daemon] - LARYNX_LOGGER.info msg - - EM.stop_server @em_signature - @em_signature = nil - remove_pid_file if @options[:daemonize] - exit 130 - end - - def daemonize - Daemonize.daemonize - Dir.chdir LARYNX_ROOT - File.open(@options[:pid_file], 'w+') {|f| f.write("#{Process.pid}\n") } - end - - def remove_pid_file - File.delete @options[:pid_file] - end - - def trap_signals - trap('TERM') { graceful_exit } - trap('INT') { graceful_exit } - end - - def setup_app - if ARGV[0].nil? - $stderr.puts "You must specify an application file" - exit -1 - end - Object.const_set "LARYNX_ROOT", File.expand_path(File.dirname(ARGV[0])) - require File.expand_path(ARGV[0]) - end - - def start_server - msg = "Larynx starting up on #{@options[:ip]}:#{@options[:port]}" - $stderr.puts msg unless @options[:daemon] - LARYNX_LOGGER.info msg - - EM::run { - @em_signature = EM::start_server @options[:ip], @options[:port], Larynx::CallHandler - } - end - - def run - parse_options(ARGV) - setup_app - daemonize if @options[:daemonize] - setup_logger - trap_signals - start_server - end - - def running? - !@em_signature.nil? - end end # Default connect callback is to answer call connect {|call| call.answer } end - -Larynx.run unless defined?(TEST)