lib/mpd_client.rb in mpd_client-0.0.3 vs lib/mpd_client.rb in mpd_client-0.0.4

- old
+ new

@@ -29,10 +29,11 @@ "repeat" => "fetch_nothing", "setvol" => "fetch_nothing", "single" => "fetch_nothing", "replay_gain_mode" => "fetch_nothing", "replay_gain_status" => "fetch_item", + "volume" => "fetch_nothing", # Playback Control Commands "next" => "fetch_nothing", "pause" => "fetch_nothing", "play" => "fetch_nothing", "playid" => "fetch_nothing", @@ -84,10 +85,11 @@ "search" => "fetch_songs", "searchadd" => "fetch_nothing", "searchaddp1" => "fetch_nothing", "update" => "fetch_item", "rescan" => "fetch_item", + "readcomments" => "fetch_item", # Sticker Commands "sticker get" => "fetch_sticker", "sticker set" => "fetch_nothing", "sticker delete" => "fetch_nothing", "sticker list" => "fetch_stickers", @@ -99,10 +101,11 @@ "ping" => "fetch_nothing", # Audio Output Commands "disableoutput" => "fetch_nothing", "enableoutput" => "fetch_nothing", "outputs" => "fetch_outputs", + "toggleoutput" => "fetch_nothing", # Reflection Commands "config" => "fetch_item", "commands" => "fetch_list", "notcommands" => "fetch_list", "tagtypes" => "fetch_list", @@ -149,26 +152,33 @@ remove_method name.to_sym end end def initialize + @mutex = Mutex.new reset end def connect(host = 'localhost', port = 6600) - log.info("MPD connect #{host}, #{port}") if log - if host.start_with?('/') - @socket = UNIXSocket.new(host) + @host = host + @port = port + reconnect + end + + def reconnect + log.info("MPD (re)connect #{host}, #{port}") if log + if @host.start_with?('/') + @socket = UNIXSocket.new(@host) hello else - @socket = TCPSocket.new(host, port) + @socket = TCPSocket.new(@host, @port) hello end end def disconnect - log.info("MPD disconnect") + log.info("MPD disconnect") if log @socket.close reset end # http://www.musicpd.org/doc/protocol/ch01s04.html @@ -179,11 +189,11 @@ end def command_list_end raise "Not in command list" if @command_list.nil? write_command('command_list_end') - + return fetch_command_list end # The current logger. If no logger has been set MPDClient.log is used # @@ -198,21 +208,28 @@ end private def execute(command, *args, retval) - if !@command_list.nil? - write_command(command, *args) - @command_list << retval - else - write_command(command, *args) - eval retval + @mutex.synchronize do + if !@command_list.nil? + write_command(command, *args) + @command_list << retval + else + write_command(command, *args) + eval retval + end end end def write_line(line) - @socket.puts line + begin + @socket.puts line + rescue Errno::EPIPE + reconnect + @socket.puts line + end @socket.flush end def write_command(command, *args) parts = [command] @@ -317,23 +334,23 @@ objs = fetch_objects return objs ? objs[0] : {} end def fetch_changes; fetch_objects(['cpos']); end - + def fetch_songs; fetch_objects(['file']); end def fetch_messages; fetch_objects('channel'); end def fetch_outputs; fetch_objects(['outputid']); end - + def fetch_plugins; fetch_objects(['plugin']); end def fetch_database; fetch_objects(['file', 'directory', 'playlist']); end - + def fetch_playlists; fetch_objects(['playlist']); end - + def fetch_playlist result = [] read_pairs(':').each do |key, value| result << value end @@ -361,10 +378,10 @@ result << (eval retval) end ensure @command_list = nil end - + return result end def hello