Class: RubyFS::Stream
- Inherits:
-
Object
- Object
- RubyFS::Stream
- Includes:
- Celluloid::IO
- Defined in:
- lib/ruby_fs/stream.rb
Defined Under Namespace
Classes: ConnectionStatus
Constant Summary collapse
- Connected =
Class.new ConnectionStatus
- Disconnected =
Class.new ConnectionStatus
Instance Method Summary collapse
-
#api(action) ⇒ RubyFS::Response
Send an API action.
-
#application(call, appname, options = nil) ⇒ RubyFS::Response
Execute an application on a particular call.
-
#bgapi(action) ⇒ RubyFS::Response
Send an API action in the background.
-
#command(command, options = {}, &callback) ⇒ RubyFS::Response
Send a FreeSWITCH command with options and a callback for the response.
-
#fire_event(event) ⇒ Object
Fire an event to the specified callback.
-
#initialize(host, port, secret, event_callback, events = true) ⇒ Stream
constructor
A new instance of Stream.
-
#logger ⇒ Object
The stream’s logger object.
-
#run ⇒ Object
Connect to the server and begin handling data.
-
#send_data(data) ⇒ Object
Send raw string data to the FS server.
-
#sendmsg(call, options = {}) ⇒ RubyFS::Response
Send a message to a particular call.
Constructor Details
#initialize(host, port, secret, event_callback, events = true) ⇒ Stream
Returns a new instance of Stream.
22 23 24 25 26 27 |
# File 'lib/ruby_fs/stream.rb', line 22 def initialize(host, port, secret, event_callback, events = true) super() @host, @port, @secret, @event_callback, @events = host, port, secret, event_callback, events @command_callbacks = [] @lexer = Lexer.new method(:receive_request) end |
Instance Method Details
#api(action) ⇒ RubyFS::Response
Send an API action
81 82 83 |
# File 'lib/ruby_fs/stream.rb', line 81 def api(action) command "api #{action}" end |
#application(call, appname, options = nil) ⇒ RubyFS::Response
Execute an application on a particular call
114 115 116 117 118 119 120 121 122 |
# File 'lib/ruby_fs/stream.rb', line 114 def application(call, appname, = nil) opts = {call_command: 'execute', execute_app_name: appname} if opts[:content_type] = 'text/plain' opts[:content_length] = .bytesize opts[:command_body_value] = end sendmsg call, opts end |
#bgapi(action) ⇒ RubyFS::Response
Send an API action in the background
91 92 93 |
# File 'lib/ruby_fs/stream.rb', line 91 def bgapi(action) command "bgapi #{action}" end |
#command(command, options = {}, &callback) ⇒ RubyFS::Response
Send a FreeSWITCH command with options and a callback for the response
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ruby_fs/stream.rb', line 61 def command(command, = {}, &callback) uuid = SecureRandom.uuid @command_callbacks << (callback || lambda { |reply| signal uuid, reply }) string = "#{command}\n" body_value = .delete :command_body_value .each_pair do |key, value| string << "#{key.to_s.gsub '_', '-'}: #{value}\n" if value end string << "\n" << body_value << "\n" if body_value string << "\n" send_data string wait uuid unless callback end |
#fire_event(event) ⇒ Object
Fire an event to the specified callback
132 133 134 |
# File 'lib/ruby_fs/stream.rb', line 132 def fire_event(event) @event_callback.call event end |
#logger ⇒ Object
The stream’s logger object
138 139 140 141 142 143 144 145 146 |
# File 'lib/ruby_fs/stream.rb', line 138 def logger super rescue @logger ||= begin logger = Logger logger.define_singleton_method :trace, logger.method(:debug) logger end end |
#run ⇒ Object
Connect to the server and begin handling data
35 36 37 38 39 40 41 42 43 |
# File 'lib/ruby_fs/stream.rb', line 35 def run logger.debug "Starting up..." @socket = TCPSocket.from_ruby_socket ::TCPSocket.new(@host, @port) post_init loop { receive_data @socket.readpartial(4096) } rescue EOFError, IOError, Errno::ECONNREFUSED => e logger.info "Client socket closed due to (#{e.class}) #{e.}!" terminate end |
#send_data(data) ⇒ Object
Send raw string data to the FS server
49 50 51 52 |
# File 'lib/ruby_fs/stream.rb', line 49 def send_data(data) logger.trace "[SEND] #{data.to_s}" @socket.write data.to_s end |
#sendmsg(call, options = {}) ⇒ RubyFS::Response
Send a message to a particular call
102 103 104 |
# File 'lib/ruby_fs/stream.rb', line 102 def sendmsg(call, = {}) command "SendMsg #{call}", end |