Skip to content

Commit

Permalink
Feat: improve mpd error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed May 26, 2023
1 parent b8c2a04 commit 617cc8d
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 143 deletions.
17 changes: 8 additions & 9 deletions src/mympd_api/playlists.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ static void free_t_pl_data(void *data) {
bool mympd_api_playlist_content_move_to_playlist(struct t_partition_state *partition_state, sds src_plist, sds dst_plist,
struct t_list *positions, unsigned mode, sds *error)
{
//get source playlist
bool rc = mpd_send_list_playlist(partition_state->conn, src_plist);
if (mympd_check_rc_error_and_recover(partition_state, error, rc, "mpd_send_list_playlist") == false) {
return false;
}
struct mpd_song *song;
struct t_list src;
list_init(&src);
while ((song = mpd_recv_song(partition_state->conn)) != NULL) {
list_push(&src, mpd_song_get_uri(song), 0, NULL, NULL);
mpd_song_free(song);
//get source playlist
bool rc = mpd_send_list_playlist(partition_state->conn, src_plist);
if (rc == true) {
struct mpd_song *song;
while ((song = mpd_recv_song(partition_state->conn)) != NULL) {
list_push(&src, mpd_song_get_uri(song), 0, NULL, NULL);
mpd_song_free(song);
}
}
rc = mpd_response_finish(partition_state->conn);
if (mympd_check_rc_error_and_recover(partition_state, error, rc, "mpd_send_list_playlist") == false) {
Expand Down
50 changes: 24 additions & 26 deletions src/mympd_api/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,31 @@
sds mympd_api_stats_get(struct t_partition_state *partition_state, sds buffer, long request_id) {
enum mympd_cmd_ids cmd_id = MYMPD_API_STATS;
struct mpd_stats *stats = mpd_run_stats(partition_state->conn);
if (stats == NULL) {
mympd_check_error_and_recover_respond(partition_state, &buffer, cmd_id, request_id, "mpd_run_stats");
return buffer;
}

const unsigned *version = mpd_connection_get_server_version(partition_state->conn);
sds mpd_protocol_version = sdscatfmt(sdsempty(),"%u.%u.%u", version[0], version[1], version[2]);
sds mympd_uri = sdsnew("mympd://");
mympd_uri = resolv_mympd_uri(mympd_uri, partition_state->mpd_state->mpd_host, partition_state->mympd_state->config);
if (stats != NULL) {
const unsigned *version = mpd_connection_get_server_version(partition_state->conn);
sds mpd_protocol_version = sdscatfmt(sdsempty(),"%u.%u.%u", version[0], version[1], version[2]);
sds mympd_uri = sdsnew("mympd://");
mympd_uri = resolv_mympd_uri(mympd_uri, partition_state->mpd_state->mpd_host, partition_state->mympd_state->config);

buffer = jsonrpc_respond_start(buffer, cmd_id, request_id);
buffer = tojson_uint(buffer, "artists", mpd_stats_get_number_of_artists(stats), true);
buffer = tojson_uint(buffer, "albums", mpd_stats_get_number_of_albums(stats), true);
buffer = tojson_uint(buffer, "songs", mpd_stats_get_number_of_songs(stats), true);
buffer = tojson_ulong(buffer, "playtime", mpd_stats_get_play_time(stats), true);
buffer = tojson_ulong(buffer, "uptime", mpd_stats_get_uptime(stats), true);
buffer = tojson_time(buffer, "myMPDuptime", (time(NULL) - partition_state->mympd_state->config->startup_time), true);
buffer = tojson_ulong(buffer, "dbUpdated", mpd_stats_get_db_update_time(stats), true);
buffer = tojson_ulong(buffer, "dbPlaytime", mpd_stats_get_db_play_time(stats), true);
buffer = tojson_char(buffer, "mympdVersion", MYMPD_VERSION, true);
buffer = tojson_char(buffer, "mpdProtocolVersion", mpd_protocol_version, true);
buffer = tojson_char(buffer, "myMPDuri", mympd_uri,false);
buffer = jsonrpc_end(buffer);

FREE_SDS(mympd_uri);
FREE_SDS(mpd_protocol_version);
mpd_stats_free(stats);
buffer = jsonrpc_respond_start(buffer, cmd_id, request_id);
buffer = tojson_uint(buffer, "artists", mpd_stats_get_number_of_artists(stats), true);
buffer = tojson_uint(buffer, "albums", mpd_stats_get_number_of_albums(stats), true);
buffer = tojson_uint(buffer, "songs", mpd_stats_get_number_of_songs(stats), true);
buffer = tojson_ulong(buffer, "playtime", mpd_stats_get_play_time(stats), true);
buffer = tojson_ulong(buffer, "uptime", mpd_stats_get_uptime(stats), true);
buffer = tojson_time(buffer, "myMPDuptime", (time(NULL) - partition_state->mympd_state->config->startup_time), true);
buffer = tojson_ulong(buffer, "dbUpdated", mpd_stats_get_db_update_time(stats), true);
buffer = tojson_ulong(buffer, "dbPlaytime", mpd_stats_get_db_play_time(stats), true);
buffer = tojson_char(buffer, "mympdVersion", MYMPD_VERSION, true);
buffer = tojson_char(buffer, "mpdProtocolVersion", mpd_protocol_version, true);
buffer = tojson_char(buffer, "myMPDuri", mympd_uri,false);
buffer = jsonrpc_end(buffer);

FREE_SDS(mympd_uri);
FREE_SDS(mpd_protocol_version);
mpd_stats_free(stats);
}
mpd_response_finish(partition_state->conn);
mympd_check_error_and_recover_respond(partition_state, &buffer, cmd_id, request_id, "mpd_run_stats");
return buffer;
}
Loading

0 comments on commit 617cc8d

Please sign in to comment.