]> git.babelmonkeys.de Git - mpdbot.git/commitdiff
Reconnect to MPD using BEP on failure
authorFlorian Zeitz <florob@babelmonkeys.de>
Sat, 9 Jul 2011 01:24:20 +0000 (03:24 +0200)
committerFlorian Zeitz <florob@babelmonkeys.de>
Sat, 9 Jul 2011 01:24:20 +0000 (03:24 +0200)
PEPThread.h
PEPThread.m

index 5e6cfb8be9cc7de44f2f8f7bb4fcddb449d397a0..d3a0d14d8a47881e8d734694ddada2a91993670f 100644 (file)
@@ -1,3 +1,6 @@
 @interface PEPThread: OFThread
-- (OFMutableDictionary*)MPD_responeFromSocket: (OFTCPSocket*)sock;
+- (void)MPD_connect;
+- (OFMutableDictionary*)MPD_responseFromSocket: (OFTCPSocket*)sock;
+
+OFTCPSocket *sock;
 @end
index 9aa58ad631cf270ba7a6a24abdd9b039dee5a75b..496ebe6110d0b0bc2e278fb8d6b8fd3730787998 100644 (file)
@@ -7,7 +7,35 @@
 #define NS_TUNE @"http://jabber.org/protocol/tune"
 
 @implementation PEPThread: OFThread
-- (OFMutableDictionary*)MPD_responeFromSocket: (OFTCPSocket*)sock
+- (void)dealloc
+{
+       [sock release];
+
+       [super dealloc];
+}
+
+- (void)MPD_connect
+{
+       int64_t pause = 1;
+       while(1) {
+               @try {
+                       [sock release];
+                       sock = [[OFTCPSocket alloc] init];
+                       [sock connectToHost: @"localhost"
+                                      port: 6600];
+                       return;
+               } @catch (id e) {
+                       of_log(@"Connection failed, retrying in %" PRIi64
+                                       " seconds", pause);
+                       [OFThread sleepForTimeInterval: pause];
+                       if (pause < 120)
+                               pause *= 2;
+                       [e release];
+               }
+       }
+}
+
+- (OFMutableDictionary*)MPD_responseFromSocket: (OFTCPSocket*)sock
 {
        OFString *answer;
        OFMutableDictionary *response = [OFMutableDictionary dictionary];
 
 - (id)main
 {
-       OFTCPSocket *sock = [OFTCPSocket socket];
-       [sock connectToHost: @"localhost"
-                      port: 6600];
-       [self MPD_responeFromSocket: sock];
+       [self MPD_connect];
+       [self MPD_responseFromSocket: sock];
        while (1) {
                OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
-               OFMutableDictionary *response;
-               OFString *answer;
-               XMPPIQ *tuneIQ = [XMPPIQ IQWithType: @"set"
+               @try {
+                       OFMutableDictionary *response;
+                       OFString *answer;
+                       XMPPIQ *tuneIQ;
+                       OFXMLElement *pubsub, *publish, *item, *tune;
+
+                       tuneIQ = [XMPPIQ IQWithType: @"set"
                                                 ID: [object generateStanzaID]];
-               OFXMLElement *pubsub = [OFXMLElement
-                   elementWithName: @"pubsub"
-                         namespace: NS_PUBSUB];
-               [tuneIQ addChild: pubsub];
-               OFXMLElement *publish = [OFXMLElement 
-                   elementWithName: @"publish"
-                         namespace: NS_PUBSUB];
-               [publish addAttributeWithName: @"node"
-                                 stringValue: NS_TUNE];
-               [pubsub addChild: publish];
-               OFXMLElement *item = [OFXMLElement elementWithName: @"item"
-                                                        namespace: NS_PUBSUB];
-               [publish addChild: item];
-               OFXMLElement *tune = [OFXMLElement elementWithName: @"tune"
-                                                        namespace: NS_TUNE];
-               [item addChild: tune];
-               [sock writeLine: @"status"];
-               response = [self MPD_responeFromSocket: sock];
-               if ([[response objectForKey: @"state"] isEqual: @"play"]) {
-                       [sock writeLine: @"currentsong"];
-                       response = [self MPD_responeFromSocket: sock];
-                       if ((answer = [response objectForKey: @"Artist"]))
-                               [tune addChild: [OFXMLElement
-                                   elementWithName: @"artist"
-                                         namespace: NS_TUNE
-                                       stringValue: answer]];
-                       if ((answer = [response objectForKey: @"Time"]))
-                               [tune addChild: [OFXMLElement
-                                   elementWithName: @"length"
-                                         namespace: NS_TUNE
-                                       stringValue: answer]];
-                       if ((answer = [response objectForKey: @"Album"]))
-                               [tune addChild: [OFXMLElement
-                                   elementWithName: @"source"
-                                         namespace: NS_TUNE
-                                       stringValue: answer]];
-                       if ((answer = [response objectForKey: @"Title"]))
-                               [tune addChild: [OFXMLElement
-                                   elementWithName: @"title"
-                                         namespace: NS_TUNE
-                                       stringValue: answer]];
-                       if ((answer = [response objectForKey: @"Track"]))
-                               [tune addChild: [OFXMLElement
-                                   elementWithName: @"track"
-                                         namespace: NS_TUNE
-                                       stringValue: answer]];
+                       pubsub = [OFXMLElement elementWithName: @"pubsub"
+                                                    namespace: NS_PUBSUB];
+                       [tuneIQ addChild: pubsub];
+                       publish = [OFXMLElement elementWithName: @"publish"
+                                                     namespace: NS_PUBSUB];
+                       [publish addAttributeWithName: @"node"
+                                         stringValue: NS_TUNE];
+                       [pubsub addChild: publish];
+                       item = [OFXMLElement elementWithName: @"item"
+                                                  namespace: NS_PUBSUB];
+                       [publish addChild: item];
+                       tune = [OFXMLElement elementWithName: @"tune"
+                                                  namespace: NS_TUNE];
+                       [item addChild: tune];
+                       [sock writeLine: @"status"];
+                       response = [self MPD_responseFromSocket: sock];
+                       if ([[response objectForKey: @"state"]
+                           isEqual: @"play"]) {
+                               [sock writeLine: @"currentsong"];
+                               response = [self MPD_responseFromSocket: sock];
+                               if ((answer =
+                                   [response objectForKey: @"Artist"]))
+                                       [tune addChild: [OFXMLElement
+                                           elementWithName: @"artist"
+                                                 namespace: NS_TUNE
+                                               stringValue: answer]];
+                               if ((answer = [response objectForKey: @"Time"]))
+                                       [tune addChild: [OFXMLElement
+                                           elementWithName: @"length"
+                                                 namespace: NS_TUNE
+                                               stringValue: answer]];
+                               if ((answer =
+                                   [response objectForKey: @"Album"]))
+                                       [tune addChild: [OFXMLElement
+                                           elementWithName: @"source"
+                                                 namespace: NS_TUNE
+                                               stringValue: answer]];
+                               if ((answer =
+                                   [response objectForKey: @"Title"]))
+                                       [tune addChild: [OFXMLElement
+                                           elementWithName: @"title"
+                                                 namespace: NS_TUNE
+                                               stringValue: answer]];
+                               if ((answer =
+                                   [response objectForKey: @"Track"]))
+                                       [tune addChild: [OFXMLElement
+                                           elementWithName: @"track"
+                                                 namespace: NS_TUNE
+                                               stringValue: answer]];
+                       }
+                       [object sendStanza: tuneIQ];
+                       [sock writeLine: @"idle player"];
+                       [self MPD_responseFromSocket: sock];
+               } @catch (id e) {
+                       [self MPD_connect];
+                       [self MPD_responseFromSocket: sock];
+                       [e release];
                }
-               [object sendStanza: tuneIQ];
-               [sock writeLine: @"idle player"];
-               [self MPD_responeFromSocket: sock];
                [pool release];
        }