Class: RubyFS::Stream

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(host, port, secret, event_callback, events = true) ⇒ Stream

Returns a new instance of Stream.



20
21
22
23
24
25
# File 'lib/ruby_fs/stream.rb', line 20

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

Parameters:

  • action (#to_s)

    the API action to execute

Returns:



77
78
79
# File 'lib/ruby_fs/stream.rb', line 77

def api(action)
  command "api #{action}"
end

#application(call, appname, options = nil) ⇒ RubyFS::Response

Execute an application on a particular call

Parameters:

  • call (#to_s)

    the call ID on which to execute the application

  • appname (#to_s)

    the app to execute

  • options (optional, String) (defaults to: nil)

    the application options

Returns:



110
111
112
# File 'lib/ruby_fs/stream.rb', line 110

def application(call, appname, options = nil)
  sendmsg call, :call_command => 'execute', :execute_app_name => appname, :execute_app_arg => options
end

#bgapi(action) ⇒ RubyFS::Response

Send an API action in the background

Parameters:

  • action (#to_s)

    the API action to execute

Returns:



87
88
89
# File 'lib/ruby_fs/stream.rb', line 87

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

Parameters:

  • command (#to_s)

    the command to run

  • options (optional, Hash) (defaults to: {})

    the command’s options, where keys have _ substituted for -

Returns:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_fs/stream.rb', line 59

def command(command, options = {}, &callback)
  uuid = SecureRandom.uuid
  @command_callbacks << (callback || lambda { |reply| signal uuid, reply })
  string = "#{command}\n"
  options.each_pair do |key, value|
    string << "#{key.to_s.gsub '_', '-'}: #{value}\n" if value
  end
  string << "\n"
  send_data string
  wait uuid unless callback
end

#finalizeObject



119
120
121
122
123
124
# File 'lib/ruby_fs/stream.rb', line 119

def finalize
  logger.debug "Finalizing stream"
  @socket.close if @socket
  @state = :stopped
  fire_event Disconnected.new
end

#fire_event(event) ⇒ Object

Fire an event to the specified callback

Parameters:

  • event (Object)

    the event to fire



130
131
132
# File 'lib/ruby_fs/stream.rb', line 130

def fire_event(event)
  @event_callback.call event
end

#loggerObject

The stream’s logger object



136
137
138
139
140
141
142
143
144
# File 'lib/ruby_fs/stream.rb', line 136

def logger
  super
rescue
  @logger ||= begin
    logger = Logger
    logger.define_singleton_method :trace, logger.method(:debug)
    logger
  end
end

#runObject

Connect to the server and begin handling data



33
34
35
36
37
38
39
40
41
# File 'lib/ruby_fs/stream.rb', line 33

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.message}!"
  terminate
end

#send_data(data) ⇒ Object

Send raw string data to the FS server

Parameters:

  • data (#to_s)

    the data to send over the socket



47
48
49
50
# File 'lib/ruby_fs/stream.rb', line 47

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

Parameters:

  • call (#to_s)

    the call ID to send the message to

  • options (optional, Hash) (defaults to: {})

    the message options

Returns:



98
99
100
# File 'lib/ruby_fs/stream.rb', line 98

def sendmsg(call, options = {})
  command "SendMsg #{call}", options
end