Class: VLC::Connection
- Inherits:
-
Object
- Object
- VLC::Connection
- 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
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
Instance Method Summary collapse
-
#connect ⇒ Object
Connects to VLC RC interface on Client#host and Client#port.
-
#connected? ⇒ Boolean
Queries if there is a connection to VLC RC interface.
-
#disconnect ⇒ Object
(also: #close)
Disconnects from VLC RC interface.
-
#initialize(host, port, read_timeout = nil) ⇒ Connection
constructor
A new instance of Connection.
- #parse_raw_data(data) ⇒ Object
-
#read(timeout = nil) ⇒ String
Reads data from the TCP server.
-
#write(data, fire_and_forget = true) ⇒ Object
Writes data to the TCP server socket.
Constructor Details
#initialize(host, port, read_timeout = nil) ⇒ 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
#host ⇒ Object
Returns the value of attribute host.
11 12 13 |
# File 'lib/vlc-client/connection.rb', line 11 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
11 12 13 |
# File 'lib/vlc-client/connection.rb', line 11 def port @port end |
#read_timeout ⇒ Object
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
#connect ⇒ Object
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
32 33 34 |
# File 'lib/vlc-client/connection.rb', line 32 def connected? not @socket.nil? end |
#disconnect ⇒ Object 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
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
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 |