struct mpd_stats * mpd_recv_stats(struct mpd_connection *connection) { struct mpd_stats * stats; struct mpd_pair *pair; assert(connection != NULL); if (mpd_error_is_defined(&connection->error)) /* refuse to receive a response if the connection's state is not clean */ return NULL; stats = mpd_stats_begin(); if (stats == NULL) { mpd_error_code(&connection->error, MPD_ERROR_OOM); return NULL; } /* read and parse all response lines */ while ((pair = mpd_recv_pair(connection)) != NULL) { mpd_stats_feed(stats, pair); mpd_return_pair(connection, pair); } if (mpd_error_is_defined(&connection->error)) { /* an error has occurred; roll back */ mpd_stats_free(stats); return NULL; } return stats; }
/* * Retrieves status about the state of MPD. */ bool Control::get_status() { mpd_status * status; mpd_stats * stats; EXIT_IDLE; pms->log(MSG_DEBUG, 0, "Retrieving MPD status from server.\n"); if ((status = mpd_run_status(conn->h())) == NULL) { /* FIXME: error handling? */ pms->log(MSG_DEBUG, 0, "mpd_run_status returned NULL pointer.\n"); delete _song; _song = NULL; st->song = MPD_SONG_NO_NUM; st->songid = MPD_SONG_NO_ID; return false; } st->assign_status(status); mpd_status_free(status); if ((stats = mpd_run_stats(conn->h())) == NULL) { /* FIXME ? */ pms->log(MSG_DEBUG, 0, "mpd_run_stats returned NULL pointer.\n"); return false; } st->assign_stats(stats); mpd_stats_free(stats); return true; }
void mpd_query_stats(struct mpd_connection *conn) { struct mpd_stats *stats; if (!conn) return; if (!mpd_command_list_begin(conn, true) || !mpd_send_stats(conn) || !mpd_command_list_end(conn)) { mpd_printerror("queue_commands"); return; } stats = mpd_recv_stats(conn); if (stats == NULL) { mpd_printerror("recv_stats"); return; } l_numberOfSongs = mpd_stats_get_number_of_songs(stats); l_uptime = mpd_stats_get_uptime(stats); l_playTime = mpd_stats_get_play_time(stats); l_dbPlayTime = mpd_stats_get_db_play_time(stats); mpd_stats_free(stats); if (!mpd_response_finish(conn)) { mpd_printerror("response_finish"); return; } }
int del_client(CLIENT_OBJECT *cli) { mpd_stats_free(cli->stats); if (cli->song) mpd_song_free(cli->song); mpd_status_free(cli->status); mpd_connection_free(cli->conn); free(cli); return 0; }
static int lmpdstats_gc(lua_State *L) { struct mpd_stats **stats; stats = luaL_checkudata(L, 1, MPD_STATS_T); if (*stats != NULL) mpd_stats_free(*stats); *stats = NULL; return 0; }
void get_random_song(struct mpd_connection *conn, char *str, char *path) { struct mpd_entity *entity; int listened0 = 65000, skipnum, numberofsongs = 0; struct mpd_stats *stats = mpd_run_stats(conn); if (stats == NULL) return; numberofsongs = mpd_stats_get_number_of_songs(stats); mpd_stats_free(stats); skipnum = rand() % numberofsongs; syslog(LOG_DEBUG, "%s: path %s; number of songs: %i skip: %i\n", __func__, path, numberofsongs, skipnum); if (!mpd_send_list_all_meta(conn, ""))//path)) { syslog(LOG_ERR, "%s: error: mpd_send_list_meta %s\n", __func__, path); return; } while((entity = mpd_recv_entity(conn)) != NULL) { const struct mpd_song *song; if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) { if (skipnum-- > 0) continue; int listened; song = mpd_entity_get_song(entity); listened = db_get_song_numplayed(mpd_get_title(song), mpd_get_artist(song)); if (listened < listened0) { listened0 = listened; syslog(LOG_DEBUG, "listened: %i ", listened); int probability = 50 + db_get_song_rating(mpd_get_title(song), mpd_get_artist(song)); syslog(LOG_DEBUG, "probability: %i ", probability); bool Yes = (rand() % 100) < probability; if (Yes) { sprintf(str, "%s", mpd_song_get_uri(song)); syslog(LOG_DEBUG, "uri: %s ", str); syslog(LOG_DEBUG, "title: %s ", mpd_get_title(song)); syslog(LOG_DEBUG, "artist: %s", mpd_get_artist(song)); } } } mpd_entity_free(entity); } }
int update_client(CLIENT_OBJECT *cli) { mpd_status_free(cli->status); cli->status = mpd_run_status(cli->conn); if (cli->song) mpd_song_free(cli->song); cli->song = mpd_run_current_song(cli->conn); mpd_stats_free(cli->stats); cli->stats = mpd_run_stats(cli->conn); return 0; }
int cmd_stats(gcc_unused int argc, gcc_unused char **argv, struct mpd_connection *conn) { struct mpd_stats *stats = mpd_run_stats(conn); if (stats == NULL) printErrorAndExit(conn); printf("Artists: %6d\n", mpd_stats_get_number_of_artists(stats)); printf("Albums: %6d\n", mpd_stats_get_number_of_albums(stats)); printf("Songs: %6d\n", mpd_stats_get_number_of_songs(stats)); printf("\n"); printf("Play Time: %s\n", DHMS(mpd_stats_get_play_time(stats))); printf("Uptime: %s\n", DHMS(mpd_stats_get_uptime(stats))); time_t t = mpd_stats_get_db_update_time(stats); printf("DB Updated: %s", ctime(&t)); /* no \n needed */ printf("DB Play Time: %s\n", DHMS(mpd_stats_get_db_play_time(stats))); mpd_stats_free(stats); return 0; }
/* * Returns: 0 - error; 1 - ok */ int mgr_mpd_fetch_status( struct mpd_connection *conn, int dump_status ) { struct mpd_status *status; struct mpd_stats *stats; if (DEBUG > 1) fprintf(stderr, "DEBUG: [mgr-thread] mpd: -status fetching...\n"); if (conn == NULL) { fprintf(stderr, "ERROR: MPD: Unable to retrieve the MPD status: not connected\n"); return 0; } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { fprintf(stderr, "ERROR: MPD: Unable to retrieve MPD status: connection error\n"); return 0; } if (DEBUG > 1) fprintf(stderr, "DEBUG: [mgr-thread] mpd: run status\n"); status = mpd_run_status(conn); if (!status) { fprintf(stderr, "ERROR: MPD: Unable to retrieve MPD status\n"); return 0; } cur_mpd_player_status = mpd_status_get_state(status); if (DEBUG > 1) { fprintf(stderr, "DEBUG: [mgr-thread] mpd: +status received:\n"); } if (DEBUG && dump_status) { fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => mode : %s\n", (cur_mpd_player_status == MPD_STATE_PLAY ? "Play" : "Other")); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => random : %d\n", mpd_status_get_random(status)); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => repeat : %d\n", mpd_status_get_random(status)); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => volume : %d\n", mpd_status_get_volume(status)); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => queue ver : %d\n", mpd_status_get_queue_version(status)); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => queue len : %d\n", mpd_status_get_queue_length(status)); } cur_mpd_random = mpd_status_get_random(status) ? 1 : 0; cur_mpd_volume = mpd_status_get_volume(status); cur_mpd_status_queue_len = mpd_status_get_queue_length(status); if (DEBUG > 1) fprintf(stderr, "DEBUG: [mgr-thread] mpd: release status\n"); if (mpd_status_get_error(status) != NULL) { fprintf(stderr, "WARNING: MPD: Error Received from MPD: %s\n", mpd_status_get_error(status)); // TODO - clear error } mpd_status_free(status); mpd_response_finish(conn); stats = mpd_run_stats(conn); if (stats == NULL) { fprintf(stderr, "ERROR: MPD: Unable to retrieve MPD statistics\n"); return 0; } cur_mpd_stats_number_of_songs = mpd_stats_get_number_of_songs( stats ); if (DEBUG && dump_status) { fprintf(stderr, "DEBUG: [mgr-thread] mpd stats : => # of songs: %d\n", cur_mpd_stats_number_of_songs); } mpd_stats_free( stats ); return 1; }
void CMPD::Update() { if(!_connected) { iprintf("Reconnecting in 10 seconds."); sleep(10); if(Connect()) iprintf("%s", "Reconnected!"); else { eprintf("%s", "Could not reconnect."); return; } } mpd_status *status = mpd_run_status(_conn); mpd_stats *stats = mpd_run_stats(_conn); if(status && stats) { int newsongid = mpd_status_get_song_id(status); int newsongpos = mpd_status_get_elapsed_time(status); int curplaytime = mpd_stats_get_play_time(stats); // new song if(newsongid != _songid) { _songid = newsongid; _songpos = newsongpos; _start = curplaytime; mpd_song *song = mpd_run_current_song(_conn); if(song) { GotNewSong(song); mpd_song_free(song); } } // song playing if(newsongpos != _songpos) { _songpos = newsongpos; CheckSubmit(curplaytime); } // check for client-to-client messages if(mpd_send_read_messages(_conn)) { mpd_message *msg; while((msg = mpd_recv_message(_conn)) != NULL) { const char *text = mpd_message_get_text(msg); if(_gotsong && text && !strncmp(text, "love", 4)) AudioScrobbler->LoveTrack(_song); mpd_message_free(msg); } mpd_response_finish(_conn); } mpd_status_free(status); mpd_stats_free(stats); } else { // we have most likely lost our connection eprintf("Could not query MPD server: %s", mpd_connection_get_error_message(_conn)); _connected = false; } }