static void mpd_disconnect_cb(GtkWidget *widget, gpointer data) { mpd_disconnect(mpd_info.obj); msi_clear(&mpd_info); mpd_info.msi.connected = FALSE; mpd_gui_hide_mpd_elements(); return; }
int mpd_check_error(MpdObj *mi) { if(mi == NULL) { debug_printf(DEBUG_ERROR, "mi == NULL?"); return MPD_ARGS_ERROR; } /* this shouldn't happen, ever */ if(mi->connection == NULL) { debug_printf(DEBUG_ERROR, "mi->connection == NULL?"); return MPD_FATAL_ERROR; } /* TODO: map these errors in the future */ mi->error = mi->connection->error; mi->error_mpd_code = mi->connection->errorCode; /*TODO: do I need to strdup this? */ mi->error_msg = strdup(mi->connection->errorStr); /* Check for permission */ /* First check for an error reported by MPD * Then check what type of error mpd reported */ if(mi->error == MPD_ERROR_ACK) { debug_printf(DEBUG_ERROR,"clearing errors in mpd_Connection: %i-%s", mi->connection->errorCode, mi->connection->errorStr); mpd_clearError(mi->connection); if (mi->the_error_callback) { debug_printf(DEBUG_ERROR, "Error callback 1 (ACK)"); mi->the_error_callback(mi, mi->error_mpd_code, mi->error_msg, mi->the_error_signal_userdata ); } free(mi->error_msg); mi->error_msg = NULL; return TRUE; } if(mi->error) { debug_printf(DEBUG_ERROR, "Following error occured: %i: code: %i msg: %s", mi->error,mi->connection->errorCode, mi->error_msg); if (mi->the_error_callback) { debug_printf(DEBUG_ERROR, "Error callback 2"); mi->the_error_callback(mi, mi->error, mi->error_msg, mi->the_error_signal_userdata ); } mpd_disconnect(mi); free(mi->error_msg); mi->error_msg = NULL; return MPD_SERVER_ERROR; } free(mi->error_msg); mi->error_msg = NULL; return MPD_OK; }
static gboolean mpd_reconnect(G_GNUC_UNUSED gpointer data) { if (!mpd_connect()) { mpd_disconnect(); return TRUE; // try again } mpd.reconnect_source = 0; return FALSE; // remove event }
static void mpd_report_error(void) { const gchar *error = mpd_connection_get_error_message(mpd.conn); g_warning("MPD error: %s", error); irc_say("MPD error: %s", error); if (mpd_connection_clear_error(mpd.conn)) { g_warning("Unable to recover, reconnecting"); mpd_disconnect(); mpd_schedule_reconnect(); } }
void mpd_free(MpdObj *mi) { debug_printf(DEBUG_INFO, "destroying MpdObj object\n"); if(mi->connected) { /* disconnect */ debug_printf(DEBUG_WARNING, "Connection still running, disconnecting\n"); mpd_disconnect(mi); } if(mi->hostname) { free(mi->hostname); } if(mi->password) { free(mi->password); } if(mi->error_msg) { free(mi->error_msg); } if(mi->connection) { /* obsolete */ mpd_closeConnection(mi->connection); } if(mi->status) { mpd_freeStatus(mi->status); } if(mi->stats) { mpd_freeStats(mi->stats); } if(mi->CurrentSong) { mpd_freeSong(mi->CurrentSong); } mpd_free_queue_ob(mi); mpd_server_free_commands(mi); free(mi); }
/* -------------------------------------------------------------------------- */ static bool mpd_start_connection(struct lcd_stuff_mpd *mpd) { int err; mpd->error = false; /* create the current song */ mpd_song_delete(mpd->current_song); mpd->current_song = mpd_song_new("", ""); /* create the object */ if (mpd->mpd) { mpd_free(mpd->mpd); mpd->mpd = NULL; } mpd->mpd = mpd_new(mpd->connection->host, mpd->connection->port, mpd->connection->password); /* connect signal handlers */ mpd_signal_connect_error(mpd->mpd, mpd_error_handler, mpd); mpd_signal_connect_status_changed(mpd->mpd, mpd_state_changed_handler, mpd); mpd_signal_connect_connection_changed(mpd->mpd, mpd_connection_changed_handler, mpd); /* set timeout */ mpd_set_connection_timeout(mpd->mpd, mpd->timeout); if (mpd->error) { mpd_disconnect(mpd->mpd); return false; } /* connect */ err = mpd_connect(mpd->mpd); if (err != MPD_OK || mpd->error) { report(RPT_ERR, "Failed to connect: %d", err); return false; } return true; }
int main(int argc, char **argv) { int n, option_index = 0; struct mg_server *server = mg_create_server(NULL, server_callback); unsigned int current_timer = 0, last_timer = 0; char *run_as_user = NULL; char const *error_msg = NULL; char *webport = "8080"; atexit(bye); #ifdef WITH_DYNAMIC_ASSETS mg_set_option(server, "document_root", SRC_PATH); #endif mpd.port = 6600; strcpy(mpd.host, "127.0.0.1"); static struct option long_options[] = { {"host", required_argument, 0, 'h'}, {"port", required_argument, 0, 'p'}, {"webport", required_argument, 0, 'w'}, {"dirbletoken", required_argument, 0, 'd'}, {"user", required_argument, 0, 'u'}, {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 0 }, {"mpdpass", required_argument, 0, 'm'}, {0, 0, 0, 0 } }; while((n = getopt_long(argc, argv, "h:p:w:u:vm:", long_options, &option_index)) != -1) { switch (n) { case 'h': strncpy(mpd.host, optarg, sizeof(mpd.host)); break; case 'p': mpd.port = atoi(optarg); break; case 'w': webport = strdup(optarg); break; case 'd': strncpy(dirble_api_token, optarg, sizeof(dirble_api_token)); break; case 'u': run_as_user = strdup(optarg); break; case 'm': if (strlen(optarg) > 0) mpd.password = strdup(optarg); break; case 'v': fprintf(stdout, "ympd %d.%d.%d\n" "Copyright (C) 2014 Andrew Karpow <*****@*****.**>\n" "built " __DATE__ " "__TIME__ " ("__VERSION__")\n", YMPD_VERSION_MAJOR, YMPD_VERSION_MINOR, YMPD_VERSION_PATCH); return EXIT_SUCCESS; break; default: fprintf(stderr, "Usage: %s [OPTION]...\n\n" " -h, --host <host>\t\tconnect to mpd at host [localhost]\n" " -p, --port <port>\t\tconnect to mpd at port [6600]\n" " -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\n" " -u, --user <username>\t\tdrop priviliges to user after socket bind\n" " -d, --dirbletoken <apitoken>\tDirble API token\n" " -V, --version\t\t\tget version\n" " -m, --mpdpass <password>\tspecifies the password to use when connecting to mpd\n" " --help\t\t\t\tthis help\n" , argv[0]); return EXIT_FAILURE; } if(error_msg) { fprintf(stderr, "Mongoose error: %s\n", error_msg); return EXIT_FAILURE; } } error_msg = mg_set_option(server, "listening_port", webport); if(error_msg) { fprintf(stderr, "Mongoose error: %s\n", error_msg); return EXIT_FAILURE; } /* drop privilges at last to ensure proper port binding */ if(run_as_user != NULL) { error_msg = mg_set_option(server, "run_as_user", run_as_user); free(run_as_user); if(error_msg) { fprintf(stderr, "Mongoose error: %s\n", error_msg); return EXIT_FAILURE; } } while (!force_exit) { mg_poll_server(server, 200); current_timer = time(NULL); if(current_timer - last_timer) { last_timer = current_timer; mpd_poll(server); } } mpd_disconnect(); mg_destroy_server(&server); return EXIT_SUCCESS; }
void mpd_cleanup(void) { if (mpd.idle_source > 0) g_source_remove(mpd.idle_source); mpd_disconnect(); }
int main(int argc, char *argv[]) { pid_t pid; struct sigaction sa; if (init_preferences(argc, argv) == FALSE) g_error("Config file parsing failed"); /* Open the log file before forking, so that if there is an error, the * user will get some idea what is going on */ open_log(prefs.log_file); g_log_set_default_handler(scmpc_log, NULL); /* Check if scmpc is already running */ if ((pid = scmpc_is_running()) > 0) { clear_preferences(); g_error("Daemon is already running with PID: %ld", (long)pid); } /* Daemonise if wanted */ if (prefs.fork) daemonise(); /* Signal handler */ open_signal_pipe(); sa.sa_handler = sighandler; sigfillset(&sa.sa_mask); sa.sa_flags = SA_RESTART; sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); if (as_connection_init() == FALSE) { scmpc_cleanup(); exit(EXIT_FAILURE); } as_authenticate(); queue_init(); queue_load(); // submit the loaded queue as_check_submit(); mpd.song_pos = g_timer_new(); mpd.idle_source = 0; if (!mpd_connect()) { mpd_disconnect(); mpd_schedule_reconnect(); } // set up main loop events loop = g_main_loop_new(NULL, FALSE); // save queue if (prefs.cache_interval > 0) { cache_save_source = g_timeout_add_seconds(prefs.cache_interval * 60, queue_save, NULL); } g_main_loop_run(loop); scmpc_cleanup(); }
int mpd_connect_real(MpdObj *mi,mpd_Connection *connection) { int retv; if(mi == NULL) { /* should return some spiffy error here */ debug_printf(DEBUG_ERROR, "mi != NULL failed"); return MPD_ARGS_ERROR; } /* reset errors */ mi->error = 0; mi->error_mpd_code = 0; if(mi->error_msg != NULL) { free(mi->error_msg); } mi->error_msg = NULL; debug_printf(DEBUG_INFO, "connecting\n"); mpd_init_MpdServerState(&(mi->CurrentState)); memcpy(&(mi->OldState), &(mi->CurrentState), sizeof(MpdServerState)); if(mi->connected) { /* disconnect */ mpd_disconnect(mi); } if(mi->hostname == NULL) { mpd_set_hostname(mi, "localhost"); } /* make sure this is locked */ if(!mi->connection_lock) { mpd_lock_conn(mi); } if(connection) { mi->connection = connection; } else { /* make timeout configurable */ mi->connection = mpd_newConnection(mi->hostname,mi->port,mi->connection_timeout); } if(mi->connection == NULL) { /* TODO: make seperate error message? */ return MPD_NOT_CONNECTED; } if(mpd_check_error(mi) != MPD_OK) { /* TODO: make seperate error message? */ return MPD_NOT_CONNECTED; } /* set connected state */ mi->connected = TRUE; if(mpd_unlock_conn(mi)) { return MPD_LOCK_FAILED; } /* get the commands we are allowed to use */ retv = mpd_server_get_allowed_commands(mi); if(retv!= MPD_OK) { return retv; } if(mi->the_connection_changed_callback != NULL) { mi->the_connection_changed_callback( mi, TRUE, mi->the_connection_changed_signal_userdata ); } debug_printf(DEBUG_INFO, "Connected to mpd"); return MPD_OK; }
int main(int argc, char **argv) { int n, option_index = 0; struct mg_server *server = mg_create_server(NULL); unsigned int current_timer = 0, last_timer = 0; char *run_as_user = NULL; char radio_song_name[512]; const char *radio_path = NULL; char *radio_url = NULL; char radio_added_song[512]; config_t cfg; config_setting_t *settings; char config_file_name[512]; struct passwd *pw = getpwuid(getuid()); char *homedir = pw->pw_dir; sprintf(config_file_name, "%s/%s/%s", homedir, project_name, ympd_conf_file); printf("conf = %s\n", config_file_name); atexit(bye); mg_set_option(server, "listening_port", "8080"); mpd.port = 6600; strcpy(mpd.host, "127.0.0.1"); config_init(&cfg); if(! config_read_file(&cfg, config_file_name)) { printf("config file error %s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return(EXIT_FAILURE); } if (!config_lookup_string(&cfg, "application.radio_path", &radio_path)) { fprintf(stderr, "No 'radio_path' setting in configuration file.\n"); } /* drop privilges at last to ensure proper port binding */ if(run_as_user != NULL) { mg_set_option(server, "run_as_user", run_as_user); free(run_as_user); } if (!config_lookup_string(&cfg, "streamripper.url", &radio_url)) { fprintf(stderr, "No 'radio_url' setting in configuration file.\n"); } printf("url = %s\n", radio_url); start_streamripper(); printf("start_streamripper\n"); /* init_watch_radio(); printf("init_watch_radio\n"); add_watch_radio(radio_path); printf("add_watch_radio\n");*/ mg_set_http_close_handler(server, mpd_close_handler); mg_set_request_handler(server, server_callback); while (!force_exit) { current_timer = mg_poll_server(server, 200); if(current_timer - last_timer) { last_timer = current_timer; mpd_poll(server); /* if (radio_poll(radio_song_name)) { sprintf(radio_added_song, "%s%s", "radio/", radio_song_name); printf("%s\n", radio_added_song); mpd_run_update(mpd.conn, radio_added_song); sleep(1); mpd_run_add(mpd.conn, radio_added_song); }*/ } } stop_streamripper(); close_watch_radio(); mpd_disconnect(); mg_destroy_server(&server); config_destroy(&cfg); return EXIT_SUCCESS; }