From 92b9c735c28a3a296cd4dd2f10bbdbac5febb2f8 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 3 Dec 2015 11:01:50 +0000 Subject: mpd_emu: preliminary arg checking + stats We can rely on Ruby-only techniques for simplifying our arg checks. --- lib/dtas/mpd_emu_client.rb | 31 +++++++++++++++++++++++++++++-- test/test_mpd_emu.rb | 2 ++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/dtas/mpd_emu_client.rb b/lib/dtas/mpd_emu_client.rb index 01258d1..25c96a0 100644 --- a/lib/dtas/mpd_emu_client.rb +++ b/lib/dtas/mpd_emu_client.rb @@ -46,17 +46,44 @@ class DTAS::MpdEmuClient # :nodoc: def dispatch(argv) cmd = argv.shift or return err(:ERROR_UNKNOWN) cmd = "mpdcmd_#{cmd}" - respond_to?(cmd) ? __send__(cmd, argv) : err(:ERROR_UNKNOWN) + if respond_to?(cmd) + m = method(cmd) + params = m.parameters + rest = params.any? { |x| x[0] == :rest } + req = params.count { |x| x[0] == :req } + opt = params.count { |x| x[0] == :opt } + argc = argv.size + return err(:ERROR_ARG) if argc < req + return err(:ERROR_ARG) if !rest && (argc > (req + opt)) + m.call(*argv) + else + err(:ERROR_UNKNOWN) + end end def err(sym) "[#{ACK[sym]}@#@cmd_listnum {}" end - def mpdcmd_ping(argv) + def mpdcmd_ping; out("OK\n"); end + def mpdcmd_close(*); nil; end + + def mpdcmd_clearerror + # player_clear_error out("OK\n") end + def mpdcmd_stats + out("artists: \n" \ + "albums: \n" \ + "songs: \n" \ + "uptime: \n" \ + "playtime: \n" \ + "db_playtime: \n" \ + "db_update: \n" \ + "OK\n") + end + # returns true on complete, :wait_writable when blocked, or nil on error def out(buf) buf = buf.b diff --git a/test/test_mpd_emu.rb b/test/test_mpd_emu.rb index 9c73ae9..487e743 100644 --- a/test/test_mpd_emu.rb +++ b/test/test_mpd_emu.rb @@ -46,6 +46,8 @@ class TestMlib < Testcase @c.write "ping\n" assert_equal "OK\n", @c.gets assert_nil IO.select([@c], nil, nil, 0) + @c.write "close\n" + assert_nil @c.read(1) end # to ensure output buffering works: -- cgit v1.2.3-24-ge0c7