2 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
4 * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice is present in all copies.
10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
11 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
14 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
15 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
16 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
17 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
18 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20 * POSSIBILITY OF SUCH DAMAGE.
23 #import <ObjFW/ObjFW.h>
25 #import "MPDConnection.h"
27 @implementation MPDConnection: OFObject
28 - initWithHost: (OFString*)host
33 mpd_host = [host copy];
53 sock = [[OFTCPSocket alloc] init];
54 [sock connectToHost: mpd_host
59 [of_stderr writeFormat: @"Connection failed, retrying"
60 @" in %" PRIi64 @" seconds\n", pause];
61 [OFThread sleepForTimeInterval: pause];
69 - (void)send: (OFString*)message
71 [sock writeLine: message];
76 [sock writeLine: @"idle player"];
80 - (OFMutableDictionary*)response
83 OFMutableDictionary *response = [OFMutableDictionary dictionary];
84 while ((answer = [sock readLine]) && ![answer hasPrefix: @"OK"]) {
86 index = [answer indexOfFirstOccurrenceOfString: @":"];
87 if (index == OF_INVALID_INDEX)
89 [response setObject: [answer substringWithRange:
90 of_range(index + 2, answer.length - (index + 2))]
92 substringWithRange: of_range(0, index)]];