Skip to content

Commit

Permalink
* modify kipatra options management
Browse files Browse the repository at this point in the history
* add sipatra option to load a sipatra app
* add a generic sipatra app with log4j
  • Loading branch information
Alexandre Moutot committed Jul 13, 2012
1 parent 9617588 commit 3771bc4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 45 deletions.
16 changes: 10 additions & 6 deletions bin/kipatra
Expand Up @@ -3,7 +3,7 @@
require 'kipatra'
require 'optparse'

options = {:udp => [], :tcp => []}
options = {:udp => [], :tcp => [], :sipatra => false}
option_parser = OptionParser.new do |opts|

opts.on("-u UDP","--udp UDP") do |udp|
Expand All @@ -17,6 +17,10 @@ option_parser = OptionParser.new do |opts|
port = port.nil? ? 5060 : port.to_i
options[:tcp] << {:host => host, :port => port}
end

opts.on("-s", "--sipatra") do
options[:sipatra] = true
end
end
option_parser.parse!

Expand All @@ -25,14 +29,14 @@ option_parser.parse!

dir_or_file = ARGV.shift

raise "No application to start" if dir_or_file.nil?
raise "No application to start." if dir_or_file.nil?

if(File.directory?(dir_or_file) || dir_or_file[-4..-1] == '.war')
opts = {:war => dir_or_file}
options[:war] = dir_or_file
else
opts = {:app_file => dir_or_file}
options[:app_file] = dir_or_file
end

server = Kipatra::Server.new(opts)
server.start(options)
server = Kipatra::Server.new(options)
server.start
server.join
19 changes: 13 additions & 6 deletions lib/kipatra.rb
Expand Up @@ -5,16 +5,17 @@ class Server
include Cipango

def initialize(opts)
@war, @app_file = opts[:war], opts[:app_file]
@war, @app_file, @sipatra, @tcp, @udp = opts[:war], opts[:app_file], opts[:sipatra], opts[:tcp], opts[:udp]
# useless
unless @war || @app_file
raise ArgumentError, "Either :war or :app_file must be defined"
end
end

def start(options = nil)
def start
@server = Cipango::Server.new

@server.connector_manager.connectors = manage_connectors(options)
@server.connector_manager.connectors = manage_connectors

handler = SipContextHandlerCollection.new
handler.add_handler sip_app
Expand All @@ -31,15 +32,16 @@ def join

private

def manage_connectors(connectors)
def manage_connectors
conns = []

connectors[:udp].each do |args|
@udp.each do |args|
conn = UdpConnector.new
conn.host, conn.port = args[:host], args[:port]
conns << conn
end
connectors[:tcp].each do |args|

@tcp.each do |args|
conn = TcpConnector.new
conn.host, conn.port = args[:host], args[:port]
conns << conn
Expand All @@ -54,6 +56,11 @@ def sip_app
ctxt = SipAppContext.new
ctxt.context_path = '/'
ctxt.war = @war
elsif @sipatra
ctxt = SipAppContext.new('/', '/')
proc = Proc.new {}
servlet = eval(File.read('lib/sipatra_app.rb'), proc.binding, 'lib/sipatra_app.rb')
ctxt.add_sip_servlet SipServletHolder::new(servlet)
else
ctxt = SipAppContext.new('/', '/')
proc = Proc.new {}
Expand Down
Binary file added lib/sipatra/log4j-1.2.17.jar
Binary file not shown.
39 changes: 39 additions & 0 deletions lib/sipatra_app.rb
@@ -0,0 +1,39 @@
require 'sipatra'
require 'sipatra/log4j-1.2.17.jar'
include_class "org.apache.log4j.Logger"

if File.exists?("log4j.properties")
include_class "org.apache.log4j.PropertyConfigurator"
PropertyConfigurator.configure("log4j.properties")
else
include_class "org.apache.log4j.BasicConfigurator"
BasicConfigurator.configure
end

begin
load @app_file
rescue Exception => e
puts "Load Error : #{e.inspect}\n#{e.backtrace.join("\n")}"
end

class SipatraApp < Kipatra::Cipango::SipServlet
def doRequest(req)
invoke req, :do_request
end

def doResponse(req)
invoke req, :do_response
end

private

def invoke(msg, method_name)
log = Logger.getLogger('Kipatra')
app = Sipatra::Application::new
session = msg.session
app.set_bindings session.servlet_context, session.servlet_context.getAttribute(Kipatra::Cipango::SipServlet::SIP_FACTORY), session, msg, log
app.send method_name
end
end

SipatraApp.new
5 changes: 5 additions & 0 deletions log4j.properties
@@ -0,0 +1,5 @@
log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n
33 changes: 0 additions & 33 deletions sample.rb

This file was deleted.

0 comments on commit 3771bc4

Please sign in to comment.