This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AMWebserviceClient.m
267 lines (227 loc) · 9.73 KB
/
AMWebserviceClient.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// AMWebserviceClient.m
// Amua
//
// Created by Mathis & Simon Hofer on 28.11.06.
// Copyright 2005-2007 Mathis & Simon Hofer.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#import "AMWebserviceClient.h"
#define CLIENT @"Amua"
#define CLIENT_VERSION @"1.3.2.13"
#define CLIENT_PLATFORM @"mac"
#define CLIENT_LANGUAGE @"en"
#define HANDSHAKE_REQUEST @"handshake_request"
#define COMMAND_REQUEST @"command_request"
#define STATION_TUNING_REQUEST @"station_tuning_request"
#define UPDATE_PLAYLIST_REQUEST @"update_playlist_request"
#define DISCOVERY_MODE_REQUEST @"discovery_mode_request"
#define RECORD_TO_PROFILE_REQUEST @"record_to_profile_request"
@implementation AMWebserviceClient
- (id)init:(id<AMWebserviceClientDelegate>)webserviceDelegate
{
self = [super init];
delegate = webserviceDelegate;
requestPool = [[NSMutableDictionary alloc] init];
return self;
}
- (void)handshake:(NSString *)server withUser:(NSString *)username
withPasswordHash:(NSString *)passwordMD5
{
AMWebserviceRequest *request = [requestPool objectForKey:HANDSHAKE_REQUEST];
if (!request) {
request = [AMWebserviceRequest plainRequestWithDelegate:self];
[requestPool setObject:request forKey:HANDSHAKE_REQUEST];
} else if ([request isProcessing]) {
[request cancel];
}
AmuaLog(LOG_MSG, [[NSString stringWithString:@"handshake with username: "]
stringByAppendingString: username]);
NSString *user = [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *sessionURL = [NSString stringWithFormat:@"http://%@/radio/handshake.php?version=%@&platform=%@&username=%@&passwordmd5=%@&language=%@",
server, CLIENT_VERSION, CLIENT_PLATFORM, user, passwordMD5, CLIENT_LANGUAGE];
[request startWithURL:[NSURL URLWithString:sessionURL]];
}
- (void)executeCommand:(NSString *)command
{
AMWebserviceRequest *request = [requestPool objectForKey:COMMAND_REQUEST];
if (!request) {
request = [AMWebserviceRequest plainRequestWithDelegate:self];
[requestPool setObject:request forKey:COMMAND_REQUEST];
} else if ([request isProcessing]) {
[request cancel];
}
AmuaLogf(LOG_MSG, @"executing command: %@", command);
NSString *commandURL = [NSString stringWithFormat:@"http://%@%@/control.php?session=%@&command=%@",
baseURL, basePath, sessionID, command];
[request startWithURL:[NSURL URLWithString:commandURL]];
}
- (void)tuneToStation:(NSString *)stationURL
{
AMWebserviceRequest *request = [requestPool objectForKey:STATION_TUNING_REQUEST];
if (!request) {
request = [AMWebserviceRequest plainRequestWithDelegate:self];
[requestPool setObject:request forKey:STATION_TUNING_REQUEST];
} else if ([request isProcessing]) {
[request cancel];
}
AmuaLogf(LOG_MSG, @"tuning to station: %@", stationURL);
NSString *url = [NSString stringWithFormat:@"http://%@%@/adjust.php?session=%@&url=%@&lang=%@",
baseURL, basePath, sessionID, stationURL, CLIENT_LANGUAGE];
[request startWithURL:[NSURL URLWithString:url]];
}
- (void)updatePlaylist:(BOOL)discoveryMode
{
AMWebserviceRequest *request = [requestPool objectForKey:UPDATE_PLAYLIST_REQUEST];
if (!request) {
request = [AMWebserviceRequest xmlRequestWithDelegate:self];
[requestPool setObject:request forKey:UPDATE_PLAYLIST_REQUEST];
} else if ([request isProcessing]) {
[request cancel];
}
AmuaLog(LOG_MSG, @"updating playlist");
NSString *url = [NSString stringWithFormat:@"http://%@%@/xspf.php?sk=%@&discovery=%@&desktop=%@",
baseURL, basePath, sessionID, discoveryMode ? @"1" : @"0", CLIENT_VERSION];
[request startWithURL:[NSURL URLWithString:url]];
}
- (void)cancelAllRequests
{
NSEnumerator *keys = [requestPool keyEnumerator];
NSObject *requestKey;
while (requestKey = [keys nextObject]) {
AMWebserviceRequest *request = [requestPool objectForKey:requestKey];
if (request != nil && [request isProcessing]) {
[request cancel];
}
}
[requestPool removeAllObjects];
}
- (void)dealloc
{
if (baseURL != nil) {
[baseURL release];
}
if (basePath != nil) {
[basePath release];
}
if (sessionID != nil) {
[sessionID release];
}
if (requestPool != nil) {
[requestPool release];
}
[super dealloc];
}
// AMWebserviceRequest delegate implementation
- (void)requestHasFinished:(AMWebserviceRequest *)request withData:(NSDictionary *)data
{
if (request == [requestPool objectForKey:HANDSHAKE_REQUEST]) {
// handshake finished
[requestPool removeObjectForKey:HANDSHAKE_REQUEST];
if ([data objectForKey:@"session"] == nil || [[[data objectForKey:@"session"] lowercaseString] isEqualToString:@"failed"]) {
AmuaLog(LOG_ERROR, @"handshake failed");
if (delegate) {
[delegate webserviceHandshakeFailed];
}
} else {
if (sessionID != nil) {
[sessionID release];
}
sessionID = [[data objectForKey:@"session"] retain];
if (baseURL != nil) {
[baseURL release];
}
baseURL = [[data objectForKey:@"base_url"] retain];
if (basePath != nil) {
[basePath release];
}
basePath = [[data objectForKey:@"base_path"] retain];
bool subscriberMode = (bool)[[data objectForKey:@"subscriber"] intValue];
AmuaLogf(LOG_MSG, @"handshake done, sessionid: %@", sessionID);
if (delegate) {
[delegate webserviceHandshakeFinishedWithURL:[data objectForKey:@"stream_url"] subscriberMode:subscriberMode];
}
}
} else if (request == [requestPool objectForKey:COMMAND_REQUEST]) {
// command execution finished
[requestPool removeObjectForKey:COMMAND_REQUEST];
if (delegate) {
[delegate webserviceCommandExecutionFinished];
}
} else if (request == [requestPool objectForKey:STATION_TUNING_REQUEST]) {
// station tuning finished
[requestPool removeObjectForKey:STATION_TUNING_REQUEST];
int error = [[data objectForKey:@"error"] intValue];
if (error == 0) {
AmuaLog(LOG_MSG, @"station tuned");
if (delegate) {
[delegate webserviceStationTuningFinished];
}
} else {
AmuaLog(LOG_ERROR, @"station tuning error");
if (delegate) {
[delegate webserviceStationTuningFailed];
}
}
} else if (request == [requestPool objectForKey:UPDATE_PLAYLIST_REQUEST]) {
// playlist update finished
[requestPool removeObjectForKey:UPDATE_PLAYLIST_REQUEST];
AMXMLNode *xml = (AMXMLNode *)data;
if ([xml childElementsCount] > 0) {
AMXMLNode *trackListNode = [xml childWithName:@"tracklist"];
AMXMLNode *titleNode = [xml childWithName:@"title"];
NSString *station = [titleNode content];
if (trackListNode != nil && [trackListNode childElementsCount] > 0) {
NSMutableArray *songs = [NSMutableArray arrayWithCapacity:[trackListNode childElementsCount]];
int i;
for (i=0; i<[trackListNode childElementsCount]; ++i) {
AMXMLNode *node = [trackListNode childElementAtIndex:i];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
int j;
for (j=0; j<[node childElementsCount]; ++j) {
AMXMLNode *nodeChild = [node childElementAtIndex:j];
if ([nodeChild content] != nil) {
[dict setObject:[nodeChild content] forKey:[nodeChild name]];
}
}
if (station != nil) {
NSMutableString *mutableStation = [[station mutableCopy] autorelease];
[mutableStation replaceOccurrencesOfString:@"+"
withString:@" " options:NSLiteralSearch
range:NSMakeRange(0, [mutableStation length])];
[dict setObject:[mutableStation stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"station"];
}
AMSongInformation *songInfo = [[AMSongInformation alloc] initWithDictionary:dict];
[songs addObject:songInfo];
[songInfo release];
}
AmuaLog(LOG_MSG, @"playlist updated");
[delegate webservicePlaylistUpdateFinished:songs];
return;
}
}
AmuaLog(LOG_ERROR, @"playlist update failed");
[delegate webservicePlaylistUpdateFailed];
}
}
- (void)requestHasFailed:(AMWebserviceRequest *)request
{
AmuaLog(LOG_ERROR, @"connection error");
if (delegate) {
[delegate webserviceConnectionError];
}
}
@end