Class: Librevox::CommandSocket

Inherits:
Object
  • Object
show all
Includes:
Commands
Defined in:
lib/librevox/command_socket.rb

Instance Method Summary collapse

Methods included from Commands

#fsctl, #hash, #hupall, #originate, #status, #uuid_bridge, #uuid_park

Constructor Details

#initialize(args = {}) ⇒ CommandSocket

Returns a new instance of CommandSocket.



10
11
12
13
14
15
16
# File 'lib/librevox/command_socket.rb', line 10

def initialize args={}
  @server   = args[:server] || "127.0.0.1"
  @port     = args[:port] || "8021"
  @auth     = args[:auth] || "ClueCon"

  connect unless args[:connect] == false
end

Instance Method Details

#closeObject



51
52
53
# File 'lib/librevox/command_socket.rb', line 51

def close
  @socket.close
end

#command(*args) ⇒ Object



24
25
26
27
# File 'lib/librevox/command_socket.rb', line 24

def command *args
  @socket.print "#{super(*args)}\n\n"
  read_response
end

#connectObject



18
19
20
21
22
# File 'lib/librevox/command_socket.rb', line 18

def connect
  @socket = TCPSocket.open(@server, @port)
  @socket.print "auth #{@auth}\n\n"
  read_response
end

#read_headersObject



41
42
43
44
45
46
47
48
49
# File 'lib/librevox/command_socket.rb', line 41

def read_headers
  headers = ""

  while line = @socket.gets and !line.chomp.empty?
    headers += line
  end

  headers
end

#read_responseObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/librevox/command_socket.rb', line 29

def read_response
  response = Librevox::Response.new
  until response.command_reply? or response.api_response?
    response.headers = read_headers 
  end

  length = response.headers[:content_length].to_i
  response.content = @socket.read(length) if length > 0

  response
end