Sha256: 66091ae24f078ba6f7dfba1acde940a885a5a6328177d045f307d014ecabdeb9
Contents?: true
Size: 1.61 KB
Versions: 2
Compression:
Stored size: 1.61 KB
Contents
require "socket" module Rmpd class Connection include Socket::Constants include Rmpd::Commands MAX_RETRIES = 5 attr_reader :socket def initialize(config_file=nil) @config = Rmpd::Config.new(config_file) @socket = nil end def close @socket.close end def connect return unless @socket.nil? || @socket.closed? Socket::getaddrinfo(@config.hostname, @config.port, nil, SOCK_STREAM).each do |info| begin sockaddr = Socket.pack_sockaddr_in(info[1], info[3]) @socket = Socket.new(info[4], info[5], 0) @socket.connect(sockaddr) rescue StandardError => error $stderr.puts "Failed to connect to #{info[3]}: #{error}" @socket = nil raise MpdConnRefusedError.new(error) else break end end read_response # protocol version, ignore for now password(@config.password) if @config.password end def send_command(command, *args) tries = 0 begin connect @socket.puts("#{command} #{quote(args).join(" ")}".strip) rescue Errno::EPIPE, EOFError @socket.close if (tries += 1) < MAX_RETRIES retry else raise MpdError.new("Retry count exceeded") end end end def read_response response = [] while (line = @socket.readline) response << line.strip break if END_RE === line end response end def mpd self end def quote(args) args.collect {|arg| "\"#{arg.to_s.gsub(/"/, "\\\"")}\""} end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rmpd-1.1.1 | lib/rmpd/connection.rb |
rmpd-1.1.0 | lib/rmpd/connection.rb |