lib/rmpd/command.rb in rmpd-1.1.15 vs lib/rmpd/command.rb in rmpd-1.1.16

- old
+ new

@@ -39,87 +39,113 @@ end module IdleStrategy def execute(connection, *args, &block) - connection.send_command(@name, *args) - if block_given? - yield connection.socket rescue nil - connection.send_command("noidle") + connection.synchronize do + connection.send_command(@name, *args) + if block_given? + yield connection.socket rescue nil + connection.send_command("noidle") + end + Response.factory(@name).parse(connection.read_response) end - Response.factory(@name).parse(connection.read_response) - rescue EOFError - puts "IdleStrategy EOFError received, retrying" if $DEBUG - connection.close - retry end - end module NoidleStrategy def execute(connection, *args) - connection.send_command(@name, *args) - # The MPD server will never respond to a noidle command. - # http://www.mail-archive.com/musicpd-dev-team@lists.sourceforge.net/msg02246.html - nil - rescue EOFError - puts "NoidleStrategy EOFError received, retrying" if $DEBUG - connection.close - retry + connection.synchronize do + connection.send_command(@name, *args) + # The MPD server will never respond to a noidle command. + # http://www.mail-archive.com/musicpd-dev-team@lists.sourceforge.net/msg02246.html + nil + end end end module CommandStrategy def execute(connection, *args) - connection.send_command(@name, *args) - Response.factory(@name).parse(connection.read_response) - rescue EOFError - puts "CommandStrategy EOFError received, retrying" if $DEBUG - connection.close - retry + tries = 0 + begin + connection.synchronize do + connection.send_command(@name, *args) + Response.factory(@name).parse(connection.read_response) + end + rescue MpdDisconnectedError => e + tries += 1 + if tries < 5 + puts "CommandStrategy MpdDisconnectedError received, retrying" if $DEBUG + connection.connect + retry + else + puts "CommandStrategy retries exceeded" if $DEBUG + raise e + end + end end - end module CommandListStrategy def execute(connection, *args, &block) + tries = 0 list = List.new yield list - connection.send_command("command_list_begin") - list.map do |command_w_args| - connection.send_command(*command_w_args) + begin + connection.synchronize do + connection.send_command("command_list_begin") + list.map do |command_w_args| + connection.send_command(*command_w_args) + end + connection.send_command("command_list_end") + Response.factory(@name).parse(connection.read_response) + end + rescue MpdDisconnectedError => e + tries += 1 + if tries < 5 + puts "CommandListStrategy MpdDisconnectedError received, retrying" if $DEBUG + connect + retry + else + puts "CommandListStrategy retries exceeded" if $DEBUG + raise e + end end - connection.send_command("command_list_end") - Response.factory(@name).parse(connection.read_response) - rescue EOFError - puts "CommandListStrategy EOFError received, retrying" if $DEBUG - connection.close - retry end - end module CommandListOkStrategy def execute(connection, *args, &block) + tries = 0 @list = List.new yield @list - connection.send_command("command_list_ok_begin") - @list.map do |command_w_args| - connection.send_command(*command_w_args) + begin + connection.synchronize do + connection.send_command("command_list_ok_begin") + @list.map do |command_w_args| + connection.send_command(*command_w_args) + end + connection.send_command("command_list_end") + handle_command_list_ok_response(connection.read_response) + end + rescue MpdDisconnectedError => e + tries += 1 + if tries < 5 + puts "CommandListOkStrategy MpdDisconnectedError received, retrying" if $DEBUG + connect + retry + else + puts "CommandListOkStrategy retries exceeded" if $DEBUG + raise e + end end - connection.send_command("command_list_end") - handle_command_list_ok_response(connection.read_response) - rescue EOFError - puts "CommandListOkStrategy EOFError received, retrying" if $DEBUG - connection.close - retry end private