From: Florian Zeitz Date: Sat, 9 Jul 2011 01:24:20 +0000 (+0200) Subject: Reconnect to MPD using BEP on failure X-Git-Url: http://cgit.babelmonkeys.de/?p=mpdbot.git;a=commitdiff_plain;h=dd25d085ba32cb51af7819b73fceb3af501ecabb Reconnect to MPD using BEP on failure --- diff --git a/PEPThread.h b/PEPThread.h index 5e6cfb8..d3a0d14 100644 --- a/PEPThread.h +++ b/PEPThread.h @@ -1,3 +1,6 @@ @interface PEPThread: OFThread -- (OFMutableDictionary*)MPD_responeFromSocket: (OFTCPSocket*)sock; +- (void)MPD_connect; +- (OFMutableDictionary*)MPD_responseFromSocket: (OFTCPSocket*)sock; + +OFTCPSocket *sock; @end diff --git a/PEPThread.m b/PEPThread.m index 9aa58ad..496ebe6 100644 --- a/PEPThread.m +++ b/PEPThread.m @@ -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]; @@ -27,66 +55,76 @@ - (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]; }