Hi, I've written a patch that allows configurable number of attempts to connect to remote MPD server if there are any errors.
I'm running MPD proxy on a headless single board computer. It often happens that the first connection attempt fails, usually because of the delays in configuring the (wifi) network interface, but the following attempts succeed. This patch saves me a lot of time: I do not have to ssh to the board and do "service mpd start", so I think it may be useful to others, too. Thanks, Piotr Kozlowski
From 559f65cc8ec3ea37b8df27efaa307caa02678838 Mon Sep 17 00:00:00 2001 From: pkozlows <pi...@pkozlowski.pl> Date: Sun, 30 Nov 2014 19:02:47 +0100 Subject: [PATCH] ProxyDatabase: retry connection attempts to remote MPD server on startup --- doc/mpdconf.example | 4 ++++ src/db/plugins/ProxyDatabasePlugin.cxx | 41 ++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/doc/mpdconf.example b/doc/mpdconf.example index 4b55f88..1eb9a1f 100644 --- a/doc/mpdconf.example +++ b/doc/mpdconf.example @@ -182,6 +182,10 @@ # plugin "proxy" # host "other.mpd.host" # port "6600" +## Number of attempts MPD should take to connect to remote server. Default is 1. +# max_connection_retries "3" +## How long should MPD wait between attempts (in seconds). Default is 0. +# sleep_time_between_retries "3" #} # Input ####################################################################### diff --git a/src/db/plugins/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx index fba7221..38e9a36 100644 --- a/src/db/plugins/ProxyDatabasePlugin.cxx +++ b/src/db/plugins/ProxyDatabasePlugin.cxx @@ -89,6 +89,9 @@ class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor { */ bool is_idle; + unsigned max_connection_retries; + unsigned sleep_time_between_retries; + public: ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener) :Database(proxy_db_plugin), @@ -336,6 +339,8 @@ ProxyDatabase::Configure(const config_param ¶m, gcc_unused Error &error) { host = param.GetBlockValue("host", ""); port = param.GetBlockValue("port", 0u); + max_connection_retries = param.GetBlockValue("max_connection_retries", 1); + sleep_time_between_retries = param.GetBlockValue("sleep_time_between_retries", 0); return true; } @@ -362,17 +367,35 @@ bool ProxyDatabase::Connect(Error &error) { const char *_host = host.empty() ? nullptr : host.c_str(); - connection = mpd_connection_new(_host, port, 0); - if (connection == nullptr) { - error.Set(libmpdclient_domain, (int)MPD_ERROR_OOM, - "Out of memory"); - return false; - } - if (!CheckError(connection, error)) { - mpd_connection_free(connection); - connection = nullptr; + bool connected = false; + unsigned attempt = 0; + do + { + FormatInfo( db_domain, "Trying to connect to remote MPD server, attempt %d", attempt+1 ); + if( 0 != attempt ) { + sleep(sleep_time_between_retries); + } + + connection = mpd_connection_new(_host, port, 0); + if (connection == nullptr) { + error.Set(libmpdclient_domain, (int)MPD_ERROR_OOM, + "Out of memory"); + return false; + } + + if (!CheckError(connection, error)) { + mpd_connection_free(connection); + connection = nullptr; + ++attempt; + } + else { + connected = true; + } + } + while( false == connected && attempt < max_connection_retries ); + if( false == connected ) { return false; } -- 1.9.1
_______________________________________________ mpd-devel mailing list mpd-devel@musicpd.org http://mailman.blarg.de/listinfo/mpd-devel