protocol/Ack: add exception class wrapping enum ack

This commit is contained in:
Max Kellermann
2015-12-18 09:50:48 +01:00
parent 8bb5a565cd
commit e939d667d9
13 changed files with 289 additions and 377 deletions

View File

@@ -165,6 +165,8 @@ PrintError(Response &r, const std::exception &e)
try {
throw e;
} catch (const ProtocolError &pe) {
r.Error(pe.GetCode(), pe.what());
} catch (const PlaylistError &pe) {
r.Error(ToAck(pe.GetCode()), pe.what());
} catch (const std::system_error &) {

View File

@@ -69,8 +69,7 @@ handle_match(Client &client, Request args, Response &r, bool fold_case)
{
RangeArg window;
if (args.size >= 2 && StringIsEqual(args[args.size - 2], "window")) {
if (!args.Parse(args.size - 1, window, r))
return CommandResult::ERROR;
window = args.ParseRange(args.size - 1);
args.pop_back();
args.pop_back();

View File

@@ -352,9 +352,7 @@ handle_rescan(Client &client, Request args, Response &r)
CommandResult
handle_setvol(Client &client, Request args, Response &r)
{
unsigned level;
if (!args.Parse(0, level, r, 100))
return CommandResult::ERROR;
unsigned level = args.ParseUnsigned(0, 100);
if (!volume_level_change(client.partition.outputs, level)) {
r.Error(ACK_ERROR_SYSTEM, "problems setting volume");
@@ -367,9 +365,7 @@ handle_setvol(Client &client, Request args, Response &r)
CommandResult
handle_volume(Client &client, Request args, Response &r)
{
int relative;
if (!args.Parse(0, relative, r, -100, 100))
return CommandResult::ERROR;
int relative = args.ParseInt(0, -100, 100);
const int old_volume = volume_level_get(client.partition.outputs);
if (old_volume < 0) {

View File

@@ -31,9 +31,7 @@ CommandResult
handle_enableoutput(Client &client, Request args, Response &r)
{
assert(args.size == 1);
unsigned device;
if (!args.Parse(0, device, r))
return CommandResult::ERROR;
unsigned device = args.ParseUnsigned(0);
if (!audio_output_enable_index(client.partition.outputs, device)) {
r.Error(ACK_ERROR_NO_EXIST, "No such audio output");
@@ -47,9 +45,7 @@ CommandResult
handle_disableoutput(Client &client, Request args, Response &r)
{
assert(args.size == 1);
unsigned device;
if (!args.Parse(0, device, r))
return CommandResult::ERROR;
unsigned device = args.ParseUnsigned(0);
if (!audio_output_disable_index(client.partition.outputs, device)) {
r.Error(ACK_ERROR_NO_EXIST, "No such audio output");
@@ -63,9 +59,7 @@ CommandResult
handle_toggleoutput(Client &client, Request args, Response &r)
{
assert(args.size == 1);
unsigned device;
if (!args.Parse(0, device, r))
return CommandResult::ERROR;
unsigned device = args.ParseUnsigned(0);
if (!audio_output_toggle_index(client.partition.outputs, device)) {
r.Error(ACK_ERROR_NO_EXIST, "No such audio output");

View File

@@ -57,23 +57,17 @@
#define COMMAND_STATUS_UPDATING_DB "updating_db"
CommandResult
handle_play(Client &client, Request args, Response &r)
handle_play(Client &client, Request args, gcc_unused Response &r)
{
int song = -1;
if (!args.ParseOptional(0, song, r))
return CommandResult::ERROR;
int song = args.ParseOptional(0, -1);
client.partition.PlayPosition(song);
return CommandResult::OK;
}
CommandResult
handle_playid(Client &client, Request args, Response &r)
handle_playid(Client &client, Request args, gcc_unused Response &r)
{
int id = -1;
if (!args.ParseOptional(0, id, r))
return CommandResult::ERROR;
int id = args.ParseOptional(0, -1);
client.partition.PlayId(id);
return CommandResult::OK;
}
@@ -93,13 +87,10 @@ handle_currentsong(Client &client, gcc_unused Request args, Response &r)
}
CommandResult
handle_pause(Client &client, Request args, Response &r)
handle_pause(Client &client, Request args, gcc_unused Response &r)
{
if (!args.IsEmpty()) {
bool pause_flag;
if (!args.Parse(0, pause_flag, r))
return CommandResult::ERROR;
bool pause_flag = args.ParseBool(0);
client.player_control.LockSetPause(pause_flag);
} else
client.player_control.LockPause();
@@ -236,45 +227,33 @@ handle_previous(Client &client, gcc_unused Request args,
}
CommandResult
handle_repeat(Client &client, Request args, Response &r)
handle_repeat(Client &client, Request args, gcc_unused Response &r)
{
bool status;
if (!args.Parse(0, status, r))
return CommandResult::ERROR;
bool status = args.ParseBool(0);
client.partition.SetRepeat(status);
return CommandResult::OK;
}
CommandResult
handle_single(Client &client, Request args, Response &r)
handle_single(Client &client, Request args, gcc_unused Response &r)
{
bool status;
if (!args.Parse(0, status, r))
return CommandResult::ERROR;
bool status = args.ParseBool(0);
client.partition.SetSingle(status);
return CommandResult::OK;
}
CommandResult
handle_consume(Client &client, Request args, Response &r)
handle_consume(Client &client, Request args, gcc_unused Response &r)
{
bool status;
if (!args.Parse(0, status, r))
return CommandResult::ERROR;
bool status = args.ParseBool(0);
client.partition.SetConsume(status);
return CommandResult::OK;
}
CommandResult
handle_random(Client &client, Request args, Response &r)
handle_random(Client &client, Request args, gcc_unused Response &r)
{
bool status;
if (!args.Parse(0, status, r))
return CommandResult::ERROR;
bool status = args.ParseBool(0);
client.partition.SetRandom(status);
client.partition.outputs.SetReplayGainMode(replay_gain_get_real_mode(client.partition.GetRandom()));
return CommandResult::OK;
@@ -291,10 +270,8 @@ handle_clearerror(Client &client, gcc_unused Request args,
CommandResult
handle_seek(Client &client, Request args, Response &r)
{
unsigned song;
SongTime seek_time;
if (!args.Parse(0, song, r) || !args.Parse(1, seek_time, r))
return CommandResult::ERROR;
unsigned song = args.ParseUnsigned(0);
SongTime seek_time = args.ParseSongTime(1);
Error error;
return client.partition.SeekSongPosition(song, seek_time, error)
@@ -305,12 +282,8 @@ handle_seek(Client &client, Request args, Response &r)
CommandResult
handle_seekid(Client &client, Request args, Response &r)
{
unsigned id;
SongTime seek_time;
if (!args.Parse(0, id, r))
return CommandResult::ERROR;
if (!args.Parse(1, seek_time, r))
return CommandResult::ERROR;
unsigned id = args.ParseUnsigned(0);
SongTime seek_time = args.ParseSongTime(1);
Error error;
return client.partition.SeekSongId(id, seek_time, error)
@@ -323,9 +296,7 @@ handle_seekcur(Client &client, Request args, Response &r)
{
const char *p = args.front();
bool relative = *p == '+' || *p == '-';
SignedSongTime seek_time;
if (!ParseCommandArg(r, seek_time, p))
return CommandResult::ERROR;
SignedSongTime seek_time = ParseCommandArgSignedSongTime(p);
Error error;
return client.partition.SeekCurrent(seek_time, relative, error)
@@ -334,36 +305,26 @@ handle_seekcur(Client &client, Request args, Response &r)
}
CommandResult
handle_crossfade(Client &client, Request args, Response &r)
handle_crossfade(Client &client, Request args, gcc_unused Response &r)
{
unsigned xfade_time;
if (!args.Parse(0, xfade_time, r))
return CommandResult::ERROR;
unsigned xfade_time = args.ParseUnsigned(0);
client.player_control.SetCrossFade(xfade_time);
return CommandResult::OK;
}
CommandResult
handle_mixrampdb(Client &client, Request args, Response &r)
handle_mixrampdb(Client &client, Request args, gcc_unused Response &r)
{
float db;
if (!args.Parse(0, db, r))
return CommandResult::ERROR;
float db = args.ParseFloat(0);
client.player_control.SetMixRampDb(db);
return CommandResult::OK;
}
CommandResult
handle_mixrampdelay(Client &client, Request args, Response &r)
handle_mixrampdelay(Client &client, Request args, gcc_unused Response &r)
{
float delay_secs;
if (!args.Parse(0, delay_secs, r))
return CommandResult::ERROR;
float delay_secs = args.ParseFloat(0);
client.player_control.SetMixRampDelay(delay_secs);
return CommandResult::OK;
}

View File

@@ -70,9 +70,7 @@ handle_save(Client &client, Request args, Response &r)
CommandResult
handle_load(Client &client, Request args, Response &r)
{
RangeArg range = RangeArg::All();
if (!args.ParseOptional(1, range, r))
return CommandResult::ERROR;
RangeArg range = args.ParseOptional(1, RangeArg::All());
const ScopeBulkEdit bulk_edit(client.partition);
@@ -144,9 +142,7 @@ CommandResult
handle_playlistdelete(gcc_unused Client &client, Request args, Response &r)
{
const char *const name = args[0];
unsigned from;
if (!args.Parse(1, from, r))
return CommandResult::ERROR;
unsigned from = args.ParseUnsigned(1);
Error error;
return spl_remove_index(name, from, error)
@@ -158,9 +154,8 @@ CommandResult
handle_playlistmove(gcc_unused Client &client, Request args, Response &r)
{
const char *const name = args.front();
unsigned from, to;
if (!args.Parse(1, from, r) || !args.Parse(2, to, r))
return CommandResult::ERROR;
unsigned from = args.ParseUnsigned(1);
unsigned to = args.ParseUnsigned(2);
Error error;
return spl_move_index(name, from, to, error)

View File

@@ -128,9 +128,7 @@ handle_addid(Client &client, Request args, Response &r)
return print_error(r, error);
if (args.size == 2) {
unsigned to;
if (!args.Parse(1, to, r))
return CommandResult::ERROR;
unsigned to = args.ParseUnsigned(1);
try {
client.partition.MoveId(added_id, to);
@@ -180,9 +178,7 @@ parse_time_range(const char *p, SongTime &start_r, SongTime &end_r)
CommandResult
handle_rangeid(Client &client, Request args, Response &r)
{
unsigned id;
if (!args.Parse(0, id, r))
return CommandResult::ERROR;
unsigned id = args.ParseUnsigned(0);
SongTime start, end;
if (!parse_time_range(args[1], start, end)) {
@@ -200,23 +196,17 @@ handle_rangeid(Client &client, Request args, Response &r)
}
CommandResult
handle_delete(Client &client, Request args, Response &r)
handle_delete(Client &client, Request args, gcc_unused Response &r)
{
RangeArg range;
if (!args.Parse(0, range, r))
return CommandResult::ERROR;
RangeArg range = args.ParseRange(0);
client.partition.DeleteRange(range.start, range.end);
return CommandResult::OK;
}
CommandResult
handle_deleteid(Client &client, Request args, Response &r)
handle_deleteid(Client &client, Request args, gcc_unused Response &r)
{
unsigned id;
if (!args.Parse(0, id, r))
return CommandResult::ERROR;
unsigned id = args.ParseUnsigned(0);
client.partition.DeleteId(id);
return CommandResult::OK;
}
@@ -229,12 +219,9 @@ handle_playlist(Client &client, gcc_unused Request args, Response &r)
}
CommandResult
handle_shuffle(gcc_unused Client &client, Request args, Response &r)
handle_shuffle(gcc_unused Client &client, Request args, gcc_unused Response &r)
{
RangeArg range = RangeArg::All();
if (!args.ParseOptional(0, range, r))
return CommandResult::ERROR;
RangeArg range = args.ParseOptional(0, RangeArg::All());
client.partition.Shuffle(range.start, range.end);
return CommandResult::OK;
}
@@ -249,14 +236,8 @@ handle_clear(Client &client, gcc_unused Request args, gcc_unused Response &r)
CommandResult
handle_plchanges(Client &client, Request args, Response &r)
{
uint32_t version;
if (!ParseCommandArg32(r, version, args.front()))
return CommandResult::ERROR;
RangeArg range = RangeArg::All();
if (!args.ParseOptional(1, range, r))
return CommandResult::ERROR;
uint32_t version = ParseCommandArgU32(args.front());
RangeArg range = args.ParseOptional(1, RangeArg::All());
playlist_print_changes_info(r, client.partition,
client.playlist, version,
range.start, range.end);
@@ -266,14 +247,8 @@ handle_plchanges(Client &client, Request args, Response &r)
CommandResult
handle_plchangesposid(Client &client, Request args, Response &r)
{
uint32_t version;
if (!ParseCommandArg32(r, version, args.front()))
return CommandResult::ERROR;
RangeArg range = RangeArg::All();
if (!args.ParseOptional(1, range, r))
return CommandResult::ERROR;
uint32_t version = ParseCommandArgU32(args.front());
RangeArg range = args.ParseOptional(1, RangeArg::All());
playlist_print_changes_position(r, client.playlist, version,
range.start, range.end);
return CommandResult::OK;
@@ -282,9 +257,7 @@ handle_plchangesposid(Client &client, Request args, Response &r)
CommandResult
handle_playlistinfo(Client &client, Request args, Response &r)
{
RangeArg range = RangeArg::All();
if (!args.ParseOptional(0, range, r))
return CommandResult::ERROR;
RangeArg range = args.ParseOptional(0, RangeArg::All());
if (!playlist_print_info(r, client.partition, client.playlist,
range.start, range.end))
@@ -298,10 +271,7 @@ CommandResult
handle_playlistid(Client &client, Request args, Response &r)
{
if (!args.IsEmpty()) {
unsigned id;
if (!args.Parse(0, id, r))
return CommandResult::ERROR;
unsigned id = args.ParseUnsigned(0);
bool ret = playlist_print_id(r, client.partition,
client.playlist, id);
if (!ret)
@@ -341,17 +311,13 @@ handle_playlistsearch(Client &client, Request args, Response &r)
}
CommandResult
handle_prio(Client &client, Request args, Response &r)
handle_prio(Client &client, Request args, gcc_unused Response &r)
{
unsigned priority;
if (!args.ParseShift(0, priority, r, 0xff))
return CommandResult::ERROR;
unsigned priority = args.ParseUnsigned(0, 0xff);
args.shift();
for (const char *i : args) {
RangeArg range;
if (!ParseCommandArg(r, range, i))
return CommandResult::ERROR;
RangeArg range = ParseCommandArgRange(i);
client.partition.SetPriorityRange(range.start, range.end,
priority);
}
@@ -360,17 +326,13 @@ handle_prio(Client &client, Request args, Response &r)
}
CommandResult
handle_prioid(Client &client, Request args, Response &r)
handle_prioid(Client &client, Request args, gcc_unused Response &r)
{
unsigned priority;
if (!args.ParseShift(0, priority, r, 0xff))
return CommandResult::ERROR;
unsigned priority = args.ParseUnsigned(0, 0xff);
args.shift();
for (const char *i : args) {
unsigned song_id;
if (!ParseCommandArg(r, song_id, i))
return CommandResult::ERROR;
unsigned song_id = ParseCommandArgUnsigned(i);
client.partition.SetPriorityId(song_id, priority);
}
@@ -378,48 +340,37 @@ handle_prioid(Client &client, Request args, Response &r)
}
CommandResult
handle_move(Client &client, Request args, Response &r)
handle_move(Client &client, Request args, gcc_unused Response &r)
{
RangeArg range;
int to;
if (!args.Parse(0, range, r) || !args.Parse(1, to, r))
return CommandResult::ERROR;
RangeArg range = args.ParseRange(0);
int to = args.ParseInt(1);
client.partition.MoveRange(range.start, range.end, to);
return CommandResult::OK;
}
CommandResult
handle_moveid(Client &client, Request args, Response &r)
handle_moveid(Client &client, Request args, gcc_unused Response &r)
{
unsigned id;
int to;
if (!args.Parse(0, id, r) || !args.Parse(1, to, r))
return CommandResult::ERROR;
unsigned id = args.ParseUnsigned(0);
int to = args.ParseInt(1);
client.partition.MoveId(id, to);
return CommandResult::OK;
}
CommandResult
handle_swap(Client &client, Request args, Response &r)
handle_swap(Client &client, Request args, gcc_unused Response &r)
{
unsigned song1, song2;
if (!args.Parse(0, song1, r) || !args.Parse(1, song2, r))
return CommandResult::ERROR;
unsigned song1 = args.ParseUnsigned(0);
unsigned song2 = args.ParseUnsigned(1);
client.partition.SwapPositions(song1, song2);
return CommandResult::OK;
}
CommandResult
handle_swapid(Client &client, Request args, Response &r)
handle_swapid(Client &client, Request args, gcc_unused Response &r)
{
unsigned id1, id2;
if (!args.Parse(0, id1, r) || !args.Parse(1, id2, r))
return CommandResult::ERROR;
unsigned id1 = args.ParseUnsigned(0);
unsigned id2 = args.ParseUnsigned(1);
client.partition.SwapIds(id1, id2);
return CommandResult::OK;
}

View File

@@ -22,6 +22,7 @@
#include "check.h"
#include "protocol/ArgParser.hxx"
#include "Chrono.hxx"
#include "util/ConstBuffer.hxx"
#include <utility>
@@ -44,30 +45,72 @@ public:
: default_value;
}
template<typename T, typename... Args>
bool Parse(unsigned idx, T &value_r, Response &r,
Args&&... args) {
gcc_pure
int ParseInt(unsigned idx) const {
assert(idx < size);
return ParseCommandArg(r, value_r, data[idx],
std::forward<Args>(args)...);
return ParseCommandArgInt(data[idx]);
}
template<typename T, typename... Args>
bool ParseOptional(unsigned idx, T &value_r, Response &r,
Args&&... args) {
return idx >= size ||
Parse(idx, value_r, r,
std::forward<Args>(args)...);
gcc_pure
int ParseInt(unsigned idx, int min_value, int max_value) const {
assert(idx < size);
return ParseCommandArgInt(data[idx], min_value, max_value);
}
template<typename T, typename... Args>
bool ParseShift(unsigned idx, T &value_r, Response &r,
Args&&... args) {
bool success = Parse(idx, value_r, r,
std::forward<Args>(args)...);
shift();
return success;
gcc_pure
int ParseUnsigned(unsigned idx) const {
assert(idx < size);
return ParseCommandArgUnsigned(data[idx]);
}
gcc_pure
int ParseUnsigned(unsigned idx, unsigned max_value) const {
assert(idx < size);
return ParseCommandArgUnsigned(data[idx], max_value);
}
gcc_pure
bool ParseBool(unsigned idx) const {
assert(idx < size);
return ParseCommandArgBool(data[idx]);
}
gcc_pure
RangeArg ParseRange(unsigned idx) const {
assert(idx < size);
return ParseCommandArgRange(data[idx]);
}
gcc_pure
float ParseFloat(unsigned idx) const {
assert(idx < size);
return ParseCommandArgFloat(data[idx]);
}
gcc_pure
SongTime ParseSongTime(unsigned idx) const {
assert(idx < size);
return ParseCommandArgSongTime(data[idx]);
}
gcc_pure
SignedSongTime ParseSignedSongTime(unsigned idx) const {
assert(idx < size);
return ParseCommandArgSignedSongTime(data[idx]);
}
gcc_pure
int ParseOptional(unsigned idx, int default_value) const {
return idx < size
? ParseInt(idx)
: default_value;
}
gcc_pure
RangeArg ParseOptional(unsigned idx, RangeArg default_value) const {
return idx < size
? ParseRange(idx)
: default_value;
}
};

View File

@@ -30,9 +30,7 @@
CommandResult
handle_addtagid(Client &client, Request args, Response &r)
{
unsigned song_id;
if (!args.Parse(0, song_id, r))
return CommandResult::ERROR;
unsigned song_id = args.ParseUnsigned(0);
const char *const tag_name = args[1];
const TagType tag_type = tag_name_parse_i(tag_name);
@@ -54,9 +52,7 @@ handle_addtagid(Client &client, Request args, Response &r)
CommandResult
handle_cleartagid(Client &client, Request args, Response &r)
{
unsigned song_id;
if (!args.Parse(0, song_id, r))
return CommandResult::ERROR;
unsigned song_id = args.ParseUnsigned(0);
TagType tag_type = TAG_NUM_OF_ITEM_TYPES;
if (args.size >= 2) {