mirror of
				https://github.com/SuperBFG7/ympd
				synced 2025-10-31 22:03:01 +00:00 
			
		
		
		
	Fix: better error handling in jukebox mode
This commit is contained in:
		| @@ -1015,8 +1015,7 @@ void mympd_jukebox() { | |||||||
|     char *album; |     char *album; | ||||||
|  |  | ||||||
|     if (!status) { |     if (!status) { | ||||||
|         printf("MPD mpd_run_status: %s\n", mpd_connection_get_error_message(mpd.conn)); |         LOG_ERROR_AND_RECOVER("mpd_run_status"); | ||||||
|         mpd.conn_state = MPD_FAILURE; |  | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|     queue_length = mpd_status_get_queue_length(status); |     queue_length = mpd_status_get_queue_length(status); | ||||||
| @@ -1025,24 +1024,36 @@ void mympd_jukebox() { | |||||||
|         return; |         return; | ||||||
|          |          | ||||||
|     srand((unsigned int)time(NULL)); |     srand((unsigned int)time(NULL)); | ||||||
|  |     num_songs = 0; | ||||||
|  |  | ||||||
|     if (mympd_state.jukeboxMode == 1 && strcmp(mympd_state.jukeboxPlaylist, "Database") == 0) { |     if (mympd_state.jukeboxMode == 1 && strcmp(mympd_state.jukeboxPlaylist, "Database") == 0) { | ||||||
|         struct mpd_stats *stats = mpd_run_stats(mpd.conn); |         struct mpd_stats *stats = mpd_run_stats(mpd.conn); | ||||||
|  |         if (stats == NULL) { | ||||||
|  |             LOG_ERROR_AND_RECOVER("mpd_run_stats"); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|         num_songs = mpd_stats_get_number_of_songs(stats); |         num_songs = mpd_stats_get_number_of_songs(stats); | ||||||
|         mpd_stats_free(stats); |         mpd_stats_free(stats); | ||||||
|     } |     } | ||||||
|     else if (mympd_state.jukeboxMode == 1) { |     else if (mympd_state.jukeboxMode == 1) { | ||||||
|         mpd_send_list_playlist(mpd.conn, mympd_state.jukeboxPlaylist); |         if (!mpd_send_list_playlist(mpd.conn, mympd_state.jukeboxPlaylist)) { | ||||||
|         num_songs = 0; |             LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|         while ((entity = mpd_recv_entity(mpd.conn)) != NULL) { |         while ((entity = mpd_recv_entity(mpd.conn)) != NULL) { | ||||||
|             num_songs++; |             num_songs++; | ||||||
|             mpd_entity_free(entity); |             mpd_entity_free(entity); | ||||||
|         }     |         }     | ||||||
|     } |     } | ||||||
|     else if (mympd_state.jukeboxMode == 2) { |     else if (mympd_state.jukeboxMode == 2) { | ||||||
|         mpd_search_db_tags(mpd.conn, MPD_TAG_ALBUM); |         if (!mpd_search_db_tags(mpd.conn, MPD_TAG_ALBUM)) { | ||||||
|         mpd_search_commit(mpd.conn); |             LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); | ||||||
|         num_songs = 0; |             return; | ||||||
|  |         } | ||||||
|  |         if (!mpd_search_commit(mpd.conn)) { | ||||||
|  |             LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|         while ((pair = mpd_recv_pair_tag(mpd.conn, MPD_TAG_ALBUM)) != NULL) { |         while ((pair = mpd_recv_pair_tag(mpd.conn, MPD_TAG_ALBUM)) != NULL) { | ||||||
|             num_songs++; |             num_songs++; | ||||||
|             mpd_return_pair(mpd.conn, pair); |             mpd_return_pair(mpd.conn, pair); | ||||||
| @@ -1059,10 +1070,18 @@ void mympd_jukebox() { | |||||||
|             rand_song = rand() % num_songs; |             rand_song = rand() % num_songs; | ||||||
|             if (mympd_state.jukeboxMode == 1) { |             if (mympd_state.jukeboxMode == 1) { | ||||||
|                 //add songs |                 //add songs | ||||||
|                 if (strcmp(mympd_state.jukeboxPlaylist, "Database") == 0) |                 if (strcmp(mympd_state.jukeboxPlaylist, "Database") == 0) { | ||||||
|                     mpd_send_list_all(mpd.conn, "/"); |                     if (!mpd_send_list_all(mpd.conn, "/")) { | ||||||
|                 else |                         LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); | ||||||
|                     mpd_send_list_playlist(mpd.conn, mympd_state.jukeboxPlaylist);             |                         return; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 else { | ||||||
|  |                     if (!mpd_send_list_playlist(mpd.conn, mympd_state.jukeboxPlaylist)) { | ||||||
|  |                         LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); | ||||||
|  |                         return; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|                 i = 0; |                 i = 0; | ||||||
|                 while ((entity = mpd_recv_entity(mpd.conn)) != NULL) { |                 while ((entity = mpd_recv_entity(mpd.conn)) != NULL) { | ||||||
|                     if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) { |                     if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) { | ||||||
| @@ -1076,14 +1095,22 @@ void mympd_jukebox() { | |||||||
|                 song = mpd_entity_get_song(entity);         |                 song = mpd_entity_get_song(entity);         | ||||||
|                 if (song != NULL) { |                 if (song != NULL) { | ||||||
|                     printf("Jukebox enabled, adding random song: %d/%d\n", rand_song, num_songs); |                     printf("Jukebox enabled, adding random song: %d/%d\n", rand_song, num_songs); | ||||||
|                     mpd_run_add(mpd.conn, mpd_song_get_uri(song)); |                     if (!mpd_run_add(mpd.conn, mpd_song_get_uri(song))) { | ||||||
|  |                         LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); | ||||||
|  |                     } | ||||||
|                 } |                 } | ||||||
|                 mpd_entity_free(entity); |                 mpd_entity_free(entity); | ||||||
|             } |             } | ||||||
|             else if (mympd_state.jukeboxMode == 2) { |             else if (mympd_state.jukeboxMode == 2) { | ||||||
|                 //add album |                 //add album | ||||||
|                 mpd_search_db_tags(mpd.conn, MPD_TAG_ALBUM); |                 if (!mpd_search_db_tags(mpd.conn, MPD_TAG_ALBUM)) { | ||||||
|                 mpd_search_commit(mpd.conn); |                     LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); | ||||||
|  |                     return; | ||||||
|  |                 } | ||||||
|  |                 if (!mpd_search_commit(mpd.conn)) { | ||||||
|  |                     LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); | ||||||
|  |                     return; | ||||||
|  |                 } | ||||||
|                 i = 0; |                 i = 0; | ||||||
|                 while ((pair = mpd_recv_pair_tag(mpd.conn, MPD_TAG_ALBUM )) != NULL)  { |                 while ((pair = mpd_recv_pair_tag(mpd.conn, MPD_TAG_ALBUM )) != NULL)  { | ||||||
|                     if (i == rand_song) { |                     if (i == rand_song) { | ||||||
| @@ -1096,7 +1123,10 @@ void mympd_jukebox() { | |||||||
|                 mpd_return_pair(mpd.conn, pair);             |                 mpd_return_pair(mpd.conn, pair);             | ||||||
|                 mpd_response_finish(mpd.conn); |                 mpd_response_finish(mpd.conn); | ||||||
|                 printf("Jukebox enabled, adding random album %s: %d/%d\n", album, rand_song, num_songs); |                 printf("Jukebox enabled, adding random album %s: %d/%d\n", album, rand_song, num_songs); | ||||||
|                 mpd_send_command(mpd.conn, "searchadd", "Album", album, NULL); |                 if (!mpd_send_command(mpd.conn, "searchadd", "Album", album, NULL)) { | ||||||
|  |                     LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); | ||||||
|  |                     return; | ||||||
|  |                 } | ||||||
|                 mpd_response_finish(mpd.conn);                 |                 mpd_response_finish(mpd.conn);                 | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| @@ -2264,6 +2294,7 @@ int mympd_smartpls_update(char *playlist, char *sticker, int maxentries) { | |||||||
|             LOG_ERROR_AND_RECOVER("mpd_run_playlist_add"); |             LOG_ERROR_AND_RECOVER("mpd_run_playlist_add"); | ||||||
|             fclose(fp); |             fclose(fp); | ||||||
|             free(uri); |             free(uri); | ||||||
|  |             unlink(tmpfile); | ||||||
|             return 1;         |             return 1;         | ||||||
|         } |         } | ||||||
|         i++; |         i++; | ||||||
| @@ -2326,8 +2357,9 @@ int mympd_smartpls_update_newest(char *playlist, int timerange, int maxentries) | |||||||
|         if (value >= value_max) |         if (value >= value_max) | ||||||
|             continue; |             continue; | ||||||
|         if (!mpd_run_playlist_add(mpd.conn, playlist, name)) { |         if (!mpd_run_playlist_add(mpd.conn, playlist, name)) { | ||||||
|             LOG_ERROR_AND_RECOVER("mpd_run_rm"); |             LOG_ERROR_AND_RECOVER("mpd_run_playlist_add"); | ||||||
|             fclose(fp); |             fclose(fp); | ||||||
|  |             unlink(tmpfile); | ||||||
|             free(uri); |             free(uri); | ||||||
|             return 1;         |             return 1;         | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ | |||||||
| #include "../dist/src/mongoose/mongoose.h" | #include "../dist/src/mongoose/mongoose.h" | ||||||
|  |  | ||||||
| #define RETURN_ERROR_AND_RECOVER(X) do { \ | #define RETURN_ERROR_AND_RECOVER(X) do { \ | ||||||
|     fprintf(stderr, "MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \ |     printf("MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \ | ||||||
|     len = json_printf(&out, "{ type:error, data : %Q }", \ |     len = json_printf(&out, "{ type:error, data : %Q }", \ | ||||||
|         mpd_connection_get_error_message(mpd.conn) \ |         mpd_connection_get_error_message(mpd.conn) \ | ||||||
|     ); \ |     ); \ | ||||||
| @@ -38,7 +38,7 @@ | |||||||
| } while (0) | } while (0) | ||||||
|  |  | ||||||
| #define LOG_ERROR_AND_RECOVER(X) do { \ | #define LOG_ERROR_AND_RECOVER(X) do { \ | ||||||
|     fprintf(stderr, "MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \ |     printf("MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \ | ||||||
|     if (!mpd_connection_clear_error(mpd.conn)) \ |     if (!mpd_connection_clear_error(mpd.conn)) \ | ||||||
|         mpd.conn_state = MPD_FAILURE; \ |         mpd.conn_state = MPD_FAILURE; \ | ||||||
| } while (0) | } while (0) | ||||||
|   | |||||||
| @@ -208,7 +208,7 @@ void read_statefiles() { | |||||||
|         mympd_state.jukeboxMode = 0; |         mympd_state.jukeboxMode = 0; | ||||||
|  |  | ||||||
|     if (mympd_state_get("jukeboxPlaylist", value)) |     if (mympd_state_get("jukeboxPlaylist", value)) | ||||||
|         mympd_state.jukeboxPlaylist = value; |         mympd_state.jukeboxPlaylist = strdup(value); | ||||||
|     else |     else | ||||||
|         mympd_state.jukeboxPlaylist = "Database"; |         mympd_state.jukeboxPlaylist = "Database"; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 jcorporation
					jcorporation