aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorKalle Wallin <kaw@linux.se>2004-03-20 08:42:32 +0000
committerKalle Wallin <kaw@linux.se>2004-03-20 08:42:32 +0000
commit362c73b143bc8bc1bb3658309fcf4737af0c8000 (patch)
treea4ffc0f78a609cc716286eb70358b7b145f7c10b /main.c
parentfff55a912af575a5ea0135a36a25b737cdfd761b (diff)
downloadmpd-362c73b143bc8bc1bb3658309fcf4737af0c8000.tar.gz
mpd-362c73b143bc8bc1bb3658309fcf4737af0c8000.tar.xz
mpd-362c73b143bc8bc1bb3658309fcf4737af0c8000.zip
Reconnect to the server when the connection is lost.
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@320 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to '')
-rw-r--r--main.c66
1 files changed, 46 insertions, 20 deletions
diff --git a/main.c b/main.c
index c3c6687fd..624734004 100644
--- a/main.c
+++ b/main.c
@@ -5,16 +5,19 @@
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include <signal.h>
#include <glib.h>
#include "config.h"
#include "libmpdclient.h"
+#include "support.h"
#include "mpc.h"
#include "options.h"
#include "command.h"
#include "screen.h"
+#define BUFSIZE 256
static mpd_client_t *mpc = NULL;
@@ -43,7 +46,7 @@ main(int argc, char *argv[])
{
options_t *options;
struct sigaction act;
- int counter;
+ int counter, connected;
/* parse command line options */
options_init();
@@ -93,35 +96,58 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
screen_init();
-#if 0
- mpc_update(mpc);
- mpc_update_filelist(mpc);
- screen_paint(mpc);
- // sleep(1);
-#endif
counter=0;
- while( !mpc_error(mpc) )
+ connected=1;
+ while( connected || options->reconnect )
{
command_t cmd;
-
- if( !counter )
+ char buf[BUFSIZE];
+
+ if( connected && counter==0 )
{
mpc_update(mpc);
- mpd_finishCommand(mpc->connection);
+ if( mpc_error(mpc) )
+ {
+ connected=0;
+ snprintf(buf, BUFSIZE, "Lost connection to %s", options->host);
+ screen_status_message(mpc, buf);
+ mpd_closeConnection(mpc->connection);
+ mpc->connection = NULL;
+ }
+ else
+ mpd_finishCommand(mpc->connection);
counter=10;
}
- else
- counter--;
-
- screen_update(mpc);
-
- if( (cmd=get_keyboard_command()) != CMD_NONE )
+
+ if( connected )
{
- screen_cmd(mpc, cmd);
- counter=0;
+ screen_update(mpc);
+ if( (cmd=get_keyboard_command()) != CMD_NONE )
+ {
+ screen_cmd(mpc, cmd);
+ counter=0;
+ }
}
- }
+ else if( options->reconnect )
+ {
+ sleep(3);
+ snprintf(buf, BUFSIZE,
+ "Connecting to %s... [Press Ctrl-C to abort]",
+ options->host);
+ screen_status_message(mpc, buf);
+ if( mpc_reconnect(mpc, options->host, options->port) == 0 )
+ {
+ snprintf(buf, BUFSIZE, "Connected to %s!", options->host);
+ screen_status_message(mpc, buf);
+ connected=1;
+ counter=0;
+ }
+ }
+
+ if( counter>0 )
+ counter--;
+ }
exit(EXIT_FAILURE);
}