Class: VLC::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/vlc-client/connection.rb

Overview

Manages the connection to a VLC server

Constant Summary collapse

DEFAULT_READ_TIMEOUT =

secs

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, read_timeout = nil) ⇒ Connection

Returns a new instance of Connection.


13
14
15
16
17
# File 'lib/vlc-client/connection.rb', line 13

def initialize(host, port, read_timeout=nil)
  @host, @port = host, port
  @socket = NullObject.new
  @read_timeout = read_timeout || DEFAULT_READ_TIMEOUT
end

Instance Attribute Details

#hostObject

Returns the value of attribute host


11
12
13
# File 'lib/vlc-client/connection.rb', line 11

def host
  @host
end

#portObject

Returns the value of attribute port


11
12
13
# File 'lib/vlc-client/connection.rb', line 11

def port
  @port
end

#read_timeoutObject

Returns the value of attribute read_timeout


11
12
13
# File 'lib/vlc-client/connection.rb', line 11

def read_timeout
  @read_timeout
end

Instance Method Details

#connectObject

Connects to VLC RC interface on Client#host and Client#port


20
21
22
23
24
25
26
# File 'lib/vlc-client/connection.rb', line 20

def connect
  @socket = TCPSocket.new(@host, @port)
  2.times { read(0.1) rescue nil } #Channel cleanup: some vlc versions echo two lines of text on connect.
  true
rescue Errno::ECONNREFUSED => e
  raise VLC::ConnectionRefused, "Could not connect to #{@host}:#{@port}: #{e}"
end

#connected?Boolean

Queries if there is a connection to VLC RC interface

Returns:

  • (Boolean)

    true is connected, false otherwise


32
33
34
# File 'lib/vlc-client/connection.rb', line 32

def connected?
  not @socket.nil?
end

#disconnectObject Also known as: close

Disconnects from VLC RC interface


37
38
39
40
# File 'lib/vlc-client/connection.rb', line 37

def disconnect
  @socket.close
  @socket = NullObject.new
end

#parse_raw_data(data) ⇒ Object


90
91
92
93
# File 'lib/vlc-client/connection.rb', line 90

def parse_raw_data(data)
  return nil if data.nil?
  data.match(%r{^[>*\s*]*(.*)$})
end

#read(timeout = nil) ⇒ String

Reads data from the TCP server

Parameters:

  • timeout (defaults to: nil)

    read timeout value for a read operation. If omited the configured value or DEFAULT_READ_TIMEOUT will be used.

Returns:

  • (String)

    the data


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vlc-client/connection.rb', line 73

def read(timeout=nil)
  timeout = read_timeout if timeout.nil?
  raw_data = nil

  Timeout.timeout(timeout) do
    raw_data = @socket.gets.chomp
  end

  if (data = parse_raw_data(raw_data))
    data[1]
  else
    raise VLC::ProtocolError, "could not interpret the playload: #{raw_data}"
  end
rescue Timeout::Error
  raise VLC::ReadTimeoutError, "read timeout"
end

#write(data, fire_and_forget = true) ⇒ Object

Writes data to the TCP server socket

Parameters:

  • data

    the data to write

  • fire_and_forget (defaults to: true)

    if true, no response response is expected from server, when false, a response from the server will be returned.

Returns:

  • the server response data if there is one


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vlc-client/connection.rb', line 52

def write(data, fire_and_forget = true)
  raise NotConnectedError, "no connection to server" unless connected?

  @socket.puts(data)
  @socket.flush

  return true if fire_and_forget
  read
rescue Errno::EPIPE
  disconnect
  raise BrokenConnectionError, "the connection to the server is lost"
end