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.



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

Parameters:

  • action (#to_s)

    the API action to execute

Returns:



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

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:



114
115
116
117
118
119
120
121
122
# File 'lib/ruby_fs/stream.rb', line 114

def application(call, appname, options = nil)
  opts = {call_command: 'execute', execute_app_name: appname}
  if options
    opts[:content_type]       = 'text/plain'
    opts[:content_length]     = options.bytesize
    opts[:command_body_value] = options
  end
  sendmsg call, opts
end

#bgapi(action) ⇒ RubyFS::Response

Send an API action in the background

Parameters:

  • action (#to_s)

    the API action to execute

Returns:



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

Parameters:

  • command (#to_s)

    the command to run

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

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

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby_fs/stream.rb', line 61

def command(command, options = {}, &callback)
  uuid = SecureRandom.uuid
  @command_callbacks << (callback || lambda { |reply| signal uuid, reply })
  string = "#{command}\n"
  body_value = options.delete :command_body_value
  options.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

Parameters:

  • event (Object)

    the event to fire



132
133
134
# File 'lib/ruby_fs/stream.rb', line 132

def fire_event(event)
  @event_callback.call event
end

#loggerObject

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

#runObject

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.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



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

Parameters:

  • call (#to_s)

    the call ID to send the message to

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

    the message options

Returns:



102
103
104
# File 'lib/ruby_fs/stream.rb', line 102

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