#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];
}