-
Notifications
You must be signed in to change notification settings - Fork 0
/
kipatra.rb
74 lines (59 loc) · 1.68 KB
/
kipatra.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'kipatra/cipango'
module Kipatra
class Server
include Cipango
def initialize(opts)
@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
@server = Cipango::Server.new
@server.connector_manager.connectors = manage_connectors
handler = SipContextHandlerCollection.new
handler.add_handler sip_app
@server.application_router = DefaultApplicationRouter.new
@server.handler = handler
@server.start
end
def join
@server.join
end
private
def manage_connectors
conns = []
@udp.each do |args|
conn = UdpConnector.new
conn.host, conn.port = args[:host], args[:port]
conns << conn
end
@tcp.each do |args|
conn = TcpConnector.new
conn.host, conn.port = args[:host], args[:port]
conns << conn
end
conns
end
def sip_app
ctxt = nil
if @war
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 {}
servlet = eval(File.read(@app_file), proc.binding, @app_file)
ctxt.add_sip_servlet SipServletHolder::new(servlet)
end
ctxt
end
end
end