Class: Rmpd::Connection
- Inherits:
-
Object
- Object
- Rmpd::Connection
- Includes:
- Commands, Socket::Constants
- Defined in:
- lib/rmpd/connection.rb
Constant Summary collapse
- MAX_RETRIES =
5
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
- #connect_inet_socket ⇒ Object
- #connect_unix_socket ⇒ Object
-
#initialize(config_file = nil) ⇒ Connection
constructor
A new instance of Connection.
- #mpd ⇒ Object
- #quote(args) ⇒ Object
- #read_response ⇒ Object
- #send_command(command, *args) ⇒ Object
- #send_command_without_reconnect(command, *args) ⇒ Object
Methods included from Commands
Constructor Details
#initialize(config_file = nil) ⇒ Connection
Returns a new instance of Connection.
16 17 18 19 20 |
# File 'lib/rmpd/connection.rb', line 16 def initialize(config_file=nil) @config = Rmpd::Config.new(config_file) @socket = nil @socket_mu = Mutex.new end |
Instance Attribute Details
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
13 14 15 |
# File 'lib/rmpd/connection.rb', line 13 def socket @socket end |
Instance Method Details
#close ⇒ Object
22 23 24 25 26 |
# File 'lib/rmpd/connection.rb', line 22 def close @socket_mu.lock @socket.close @socket_mu.unlock end |
#connect ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rmpd/connection.rb', line 28 def connect @socket_mu.lock unless @socket.nil? || @socket.closed? @socket_mu.unlock return end @socket_mu.unlock if %r{^/} === @config.hostname connect_unix_socket else connect_inet_socket end read_response # protocol version, ignore for now password(@config.password) if @config.password end |
#connect_inet_socket ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rmpd/connection.rb', line 58 def connect_inet_socket Socket::getaddrinfo(@config.hostname, @config.port, nil, SOCK_STREAM).each do |info| @socket_mu.lock begin sockaddr = Socket.pack_sockaddr_in(info[1], info[3]) @socket = Socket.new(info[4], info[5], 0) @socket.connect(sockaddr) rescue StandardError => error @socket = nil raise MpdConnRefusedError.new(error) else break ensure @socket_mu.unlock end end end |
#connect_unix_socket ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rmpd/connection.rb', line 46 def connect_unix_socket @socket_mu.lock begin @socket = UNIXSocket.new(@config.hostname) rescue StandardError => error @socket = nil raise MpdConnRefusedError.new(error) ensure @socket_mu.unlock end end |
#mpd ⇒ Object
114 115 116 |
# File 'lib/rmpd/connection.rb', line 114 def mpd self end |
#quote(args) ⇒ Object
118 119 120 |
# File 'lib/rmpd/connection.rb', line 118 def quote(args) args.collect {|arg| "\"#{arg.to_s.gsub(/"/, "\\\"").gsub(/\\/, "\\\\\\\\")}\""} end |
#read_response ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rmpd/connection.rb', line 101 def read_response response = [] @socket_mu.synchronize do while (line = @socket.readline.force_encoding("UTF-8")) response << line.strip break if END_RE === line end end response end |
#send_command(command, *args) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rmpd/connection.rb', line 76 def send_command(command, *args) tries = 0 begin connect send_command_without_reconnect(command, *args) rescue Errno::EPIPE, EOFError if (tries += 1) < MAX_RETRIES retry else raise MpdError.new("Retry count exceeded") end end end |
#send_command_without_reconnect(command, *args) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/rmpd/connection.rb', line 91 def send_command_without_reconnect(command, *args) @socket_mu.lock @socket.puts("#{command} #{quote(args).join(" ")}".strip) rescue => e @socket.close raise e ensure @socket_mu.unlock end |