summaryrefslogtreecommitdiff
path: root/lib/freeswitch_event.rb
diff options
context:
space:
mode:
authorStefan Wintermeyer <stefan.wintermeyer@amooma.de>2012-12-17 12:01:45 +0100
committerStefan Wintermeyer <stefan.wintermeyer@amooma.de>2012-12-17 12:01:45 +0100
commitb80bd744ad873f6fc43018bc4bfb90677de167bd (patch)
tree072c4b0e33d442528555b82c415f5e7a1712b2b0 /lib/freeswitch_event.rb
parent3e706c2025ecc5523e81ad649639ef2ff75e7bac (diff)
Start of GS5.
Diffstat (limited to 'lib/freeswitch_event.rb')
-rw-r--r--lib/freeswitch_event.rb133
1 files changed, 133 insertions, 0 deletions
diff --git a/lib/freeswitch_event.rb b/lib/freeswitch_event.rb
new file mode 100644
index 0000000..b6e5cbc
--- /dev/null
+++ b/lib/freeswitch_event.rb
@@ -0,0 +1,133 @@
+class FreeswitchEventSocket
+ DEFAULT_HOST = "127.0.0.1"
+ DEFAULT_PORT = 8021
+ DEFAULT_PASSWORD = "ClueCon"
+ DEFAULT_SOCKET_TIMEOUT = 20
+ @socket = nil
+
+ def auth(password)
+ self.command("auth #{password}")
+ result = self.result()
+
+ if result && result["Reply-Text"] == "+OK accepted"
+ return true
+ end
+ return false
+ end
+
+ def connect(password = DEFAULT_PASSWORD, event_host = DEFAULT_HOST, event_port = DEFAULT_PORT)
+ begin
+ @socket = TCPSocket.open(event_host, event_port)
+ rescue
+ return false
+ end
+
+ if not @socket
+ return false
+ end
+
+ result = self.result()
+ if result && result["Content-Type"] == "auth/request" then
+ if not self.auth(password)
+ return false
+ end
+ end
+
+ return true
+ end
+
+ def command(command)
+ @socket.puts("#{command}\n\n")
+ end
+
+ def close()
+ @socket.close
+ end
+
+ def read()
+ return @socket.recv(1024)
+ end
+
+ def result()
+ reply = self.read()
+ if reply.class == String
+ message = Hash.new()
+ header, body = reply.split("\n\n", 2)
+ if ! body.blank?
+ message['_BODY'] = body
+ end
+ header.split("\n").each do |line|
+ key, value = line.split(/\: */, 2)
+ message[key] = value
+ end
+ return message
+ end
+ return nil
+ end
+end
+
+class FreeswitchEvent
+ def initialize(event_type)
+ @event_type = event_type
+ @event_header = Array.new()
+ @event_body = nil
+ @event_header.push("sendevent #{@event_type}")
+ end
+
+ def add_header(name, value)
+ @event_header.push("#{name}: #{value}")
+ end
+
+ def add_body(body)
+ @event_body = body
+ end
+
+ def fire()
+ if @event_body && @event_body.length > 0
+ self.add_header("content-length", @event_body.length);
+ end
+
+ event = FreeswitchEventSocket.new()
+ if event && event.connect()
+ event.command(@event_header.join("\n"))
+ event.command( @event_body)
+ result = event.result()
+ event.close()
+
+ if result && result["Reply-Text"] == "+OK"
+ return true
+ end
+ end
+ return false
+ end
+end
+
+class FreeswitchAPI
+ def self.execute(command, arguments, bgapi = false)
+
+ event = FreeswitchEventSocket.new()
+ if event && event.connect()
+ api = bgapi ? 'bgapi' : 'api'
+ event.command( "#{api} #{command} #{arguments}")
+ result = event.result()
+ if result && result["Content-Type"] == 'api/response' && result["_BODY"].blank?
+ result = event.result()
+ end
+ event.close()
+
+ if result
+ if result.has_key?('Reply-Text') && result['Reply-Text'] =~ /^\+OK/
+ if result.has_key?('Job-UUID') && ! result['Job-UUID'].blank?
+ return result['Job-UUID'];
+ else
+ return true;
+ end
+ elsif result.has_key?('_BODY') && result['_BODY'] =~ /^\+OK/
+ return true;
+ end
+ end
+ end
+
+ return false
+ end
+end