]> git.babelmonkeys.de Git - mpdbot.git/blob - src/MPDConnection.m
Initial command support
[mpdbot.git] / src / MPDConnection.m
1 /*
2  * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
3  *
4  * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
5  *
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.
9  *
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.
21  */
22
23 #import <ObjFW/ObjFW.h>
24
25 #import "MPDConnection.h"
26
27 @implementation MPDConnection: OFObject
28 - initWithHost: (OFString*)host
29           port: (uint16_t)port
30 {
31         self = [super init];
32
33         mpd_host = [host copy];
34         mpd_port = port;
35
36         return self;
37 }
38
39 - (void)dealloc
40 {
41         [sock release];
42         [mpd_host release];
43
44         [super dealloc];
45 }
46
47 - (void)connect
48 {
49         int64_t pause = 1;
50         while(1) {
51                 @try {
52                         [sock release];
53                         sock = [[OFTCPSocket alloc] init];
54                         [sock connectToHost: mpd_host
55                                        port: mpd_port];
56                         [self response];
57                         return;
58                 } @catch (id e) {
59                         [of_stderr writeFormat: @"Connection failed, retrying"
60                                 @" in %" PRIi64 @" seconds\n", pause];
61                         [OFThread sleepForTimeInterval: pause];
62                         if (pause < 120)
63                                 pause *= 2;
64                         [e release];
65                 }
66         }
67 }
68
69 - (void)send: (OFString*)message
70 {
71         [sock writeLine: message];
72 }
73
74 - (void)idle
75 {
76         [sock writeLine: @"idle player"];
77         [self response];
78 }
79
80 - (OFMutableDictionary*)response
81 {
82         OFString *answer;
83         OFMutableDictionary *response = [OFMutableDictionary dictionary];
84         while ((answer = [sock readLine]) && ![answer hasPrefix: @"OK"]) {
85                 size_t index;
86                 index = [answer indexOfFirstOccurrenceOfString: @":"];
87                 if (index == OF_INVALID_INDEX)
88                         continue;
89                 [response setObject: [answer substringWithRange:
90                     of_range(index + 2, answer.length - (index + 2))]
91                              forKey: [answer
92                     substringWithRange: of_range(0, index)]];
93         }
94
95         return response;
96 }
97 @end