blob: bcc2329b260740ba5159ccf8956a46e3448b5a31 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2013, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#import "APPRTCAppClient.h"
29
30#import <dispatch/dispatch.h>
31
32#import "GAEChannelClient.h"
33#import "RTCIceServer.h"
34
35@interface APPRTCAppClient ()
36
37@property(nonatomic, strong) dispatch_queue_t backgroundQueue;
38@property(nonatomic, copy) NSString *baseURL;
39@property(nonatomic, strong) GAEChannelClient *gaeChannel;
40@property(nonatomic, copy) NSString *postMessageUrl;
41@property(nonatomic, copy) NSString *pcConfig;
42@property(nonatomic, strong) NSMutableString *receivedData;
43@property(atomic, strong) NSMutableArray *sendQueue;
44@property(nonatomic, copy) NSString *token;
45
46@property(nonatomic, assign) BOOL verboseLogging;
47
48@end
49
50@implementation APPRTCAppClient
51
52- (id)init {
53 if (self = [super init]) {
54 _backgroundQueue = dispatch_queue_create("RTCBackgroundQueue", NULL);
55 _sendQueue = [NSMutableArray array];
56 // Uncomment to see Request/Response logging.
57 //_verboseLogging = YES;
58 }
59 return self;
60}
61
62#pragma mark - Public methods
63
64- (void)connectToRoom:(NSURL *)url {
65 NSURLRequest *request = [self getRequestFromUrl:url];
66 [NSURLConnection connectionWithRequest:request delegate:self];
67}
68
69- (void)sendData:(NSData *)data {
70 @synchronized(self) {
71 [self maybeLogMessage:@"Send message"];
72 [self.sendQueue addObject:[data copy]];
73 }
74 [self requestQueueDrainInBackground];
75}
76
77#pragma mark - Internal methods
78
79- (NSTextCheckingResult *)findMatch:(NSString *)regexpPattern
80 withString:(NSString *)string
81 errorMessage:(NSString *)errorMessage {
82 NSError *error;
83 NSRegularExpression *regexp =
84 [NSRegularExpression regularExpressionWithPattern:regexpPattern
85 options:0
86 error:&error];
87 if (error) {
88 [self maybeLogMessage:
89 [NSString stringWithFormat:@"Failed to create regexp - %@",
90 [error description]]];
91 return nil;
92 }
93 NSRange fullRange = NSMakeRange(0, [string length]);
94 NSArray *matches = [regexp matchesInString:string options:0 range:fullRange];
95 if ([matches count] == 0) {
96 if ([errorMessage length] > 0) {
97 [self maybeLogMessage:string];
98 [self showMessage:
99 [NSString stringWithFormat:@"Missing %@ in HTML.", errorMessage]];
100 }
101 return nil;
102 } else if ([matches count] > 1) {
103 if ([errorMessage length] > 0) {
104 [self maybeLogMessage:string];
105 [self showMessage:[NSString stringWithFormat:@"Too many %@s in HTML.",
106 errorMessage]];
107 }
108 return nil;
109 }
110 return matches[0];
111}
112
113- (NSURLRequest *)getRequestFromUrl:(NSURL *)url {
114 self.receivedData = [NSMutableString stringWithCapacity:20000];
115 NSString *path =
116 [NSString stringWithFormat:@"https:%@", [url resourceSpecifier]];
117 NSURLRequest *request =
118 [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
119 return request;
120}
121
122- (void)maybeLogMessage:(NSString *)message {
123 if (self.verboseLogging) {
124 NSLog(@"%@", message);
125 }
126}
127
128- (void)requestQueueDrainInBackground {
129 dispatch_async(self.backgroundQueue, ^(void) {
130 // TODO(hughv): This can block the UI thread. Fix.
131 @synchronized(self) {
132 if ([self.postMessageUrl length] < 1) {
133 return;
134 }
135 for (NSData *data in self.sendQueue) {
136 NSString *url = [NSString stringWithFormat:@"%@/%@",
137 self.baseURL,
138 self.postMessageUrl];
139 [self sendData:data withUrl:url];
140 }
141 [self.sendQueue removeAllObjects];
142 }
143 });
144}
145
146- (void)sendData:(NSData *)data withUrl:(NSString *)url {
147 NSMutableURLRequest *request =
148 [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
149 request.HTTPMethod = @"POST";
150 [request setHTTPBody:data];
151 NSURLResponse *response;
152 NSError *error;
153 NSData *responseData = [NSURLConnection sendSynchronousRequest:request
154 returningResponse:&response
155 error:&error];
156 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
157 int status = [httpResponse statusCode];
158 NSAssert(status == 200,
159 @"Bad response [%d] to message: %@\n\n%@",
160 status,
161 [NSString stringWithUTF8String:[data bytes]],
162 [NSString stringWithUTF8String:[responseData bytes]]);
163}
164
165- (void)showMessage:(NSString *)message {
166 NSLog(@"%@", message);
167 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Unable to join"
168 message:message
169 delegate:nil
170 cancelButtonTitle:@"OK"
171 otherButtonTitles:nil];
172 [alertView show];
173}
174
175- (void)updateIceServers:(NSMutableArray *)iceServers
176 withTurnServer:(NSString *)turnServerUrl {
177 if ([turnServerUrl length] < 1) {
178 [self.iceServerDelegate onIceServers:iceServers];
179 return;
180 }
181 dispatch_async(self.backgroundQueue, ^(void) {
182 NSMutableURLRequest *request = [NSMutableURLRequest
183 requestWithURL:[NSURL URLWithString:turnServerUrl]];
184 [request addValue:@"Mozilla/5.0" forHTTPHeaderField:@"user-agent"];
185 [request addValue:@"https://apprtc.appspot.com"
186 forHTTPHeaderField:@"origin"];
187 NSURLResponse *response;
188 NSError *error;
189 NSData *responseData = [NSURLConnection sendSynchronousRequest:request
190 returningResponse:&response
191 error:&error];
192 if (!error) {
193 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
194 options:0
195 error:&error];
196 NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
197 NSString *username = json[@"username"];
198 NSString *turnServer = json[@"turn"];
199 NSString *password = json[@"password"];
200 NSString *fullUrl =
201 [NSString stringWithFormat:@"turn:%@@%@", username, turnServer];
202 RTCIceServer *iceServer =
203 [[RTCIceServer alloc] initWithUri:[NSURL URLWithString:fullUrl]
204 password:password];
205 [iceServers addObject:iceServer];
206 } else {
207 NSLog(@"Unable to get TURN server. Error: %@", error.description);
208 }
209
210 dispatch_async(dispatch_get_main_queue(), ^(void) {
211 [self.iceServerDelegate onIceServers:iceServers];
212 });
213 });
214}
215
216#pragma mark - NSURLConnectionDataDelegate methods
217
218- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
219 NSString *roomHtml = [NSString stringWithUTF8String:[data bytes]];
220 [self maybeLogMessage:
221 [NSString stringWithFormat:@"Received %d chars", [roomHtml length]]];
222 [self.receivedData appendString:roomHtml];
223}
224
225- (void)connection:(NSURLConnection *)connection
226 didReceiveResponse:(NSURLResponse *)response {
227 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
228 int statusCode = [httpResponse statusCode];
229 [self maybeLogMessage:
230 [NSString stringWithFormat:
231 @"Response received\nURL\n%@\nStatus [%d]\nHeaders\n%@",
232 [httpResponse URL],
233 statusCode,
234 [httpResponse allHeaderFields]]];
235 NSAssert(statusCode == 200, @"Invalid response of %d received.", statusCode);
236}
237
238- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
239 [self maybeLogMessage:[NSString stringWithFormat:@"finished loading %d chars",
240 [self.receivedData length]]];
241 NSTextCheckingResult *result =
242 [self findMatch:@".*\n *Sorry, this room is full\\..*"
243 withString:self.receivedData
244 errorMessage:nil];
245 if (result) {
246 [self showMessage:@"Room full"];
247 return;
248 }
249
250 NSString *fullUrl = [[[connection originalRequest] URL] absoluteString];
251 NSRange queryRange = [fullUrl rangeOfString:@"?"];
252 self.baseURL = [fullUrl substringToIndex:queryRange.location];
253 [self maybeLogMessage:[NSString stringWithFormat:@"URL\n%@", self.baseURL]];
254
255 result = [self findMatch:@".*\n *openChannel\\('([^']*)'\\);\n.*"
256 withString:self.receivedData
257 errorMessage:@"channel token"];
258 if (!result) {
259 return;
260 }
261 self.token = [self.receivedData substringWithRange:[result rangeAtIndex:1]];
262 [self maybeLogMessage:[NSString stringWithFormat:@"Token\n%@", self.token]];
263
264 result =
265 [self findMatch:@".*\n *path = '/(message\\?r=.+)' \\+ '(&u=[0-9]+)';\n.*"
266 withString:self.receivedData
267 errorMessage:@"postMessage URL"];
268 if (!result) {
269 return;
270 }
271 self.postMessageUrl =
272 [NSString stringWithFormat:@"%@%@",
273 [self.receivedData substringWithRange:[result rangeAtIndex:1]],
274 [self.receivedData substringWithRange:[result rangeAtIndex:2]]];
275 [self maybeLogMessage:[NSString stringWithFormat:@"POST message URL\n%@",
276 self.postMessageUrl]];
277
278 result = [self findMatch:@".*\n *var pc_config = (\\{[^\n]*\\});\n.*"
279 withString:self.receivedData
280 errorMessage:@"pc_config"];
281 if (!result) {
282 return;
283 }
284 NSString *pcConfig =
285 [self.receivedData substringWithRange:[result rangeAtIndex:1]];
286 [self maybeLogMessage:
287 [NSString stringWithFormat:@"PC Config JSON\n%@", pcConfig]];
288
289 result = [self findMatch:@".*\n *requestTurn\\('([^\n]*)'\\);\n.*"
290 withString:self.receivedData
291 errorMessage:@"channel token"];
292 NSString *turnServerUrl;
293 if (result) {
294 turnServerUrl =
295 [self.receivedData substringWithRange:[result rangeAtIndex:1]];
296 [self maybeLogMessage:
297 [NSString stringWithFormat:@"TURN server request URL\n%@",
298 turnServerUrl]];
299 }
300
301 NSError *error;
302 NSData *pcData = [pcConfig dataUsingEncoding:NSUTF8StringEncoding];
303 NSDictionary *json =
304 [NSJSONSerialization JSONObjectWithData:pcData options:0 error:&error];
305 NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
306 NSArray *servers = [json objectForKey:@"iceServers"];
307 NSMutableArray *iceServers = [NSMutableArray array];
308 for (NSDictionary *server in servers) {
309 NSString *url = [server objectForKey:@"url"];
310 NSString *credential = [server objectForKey:@"credential"];
311 if (!credential) {
312 credential = @"";
313 }
314 [self maybeLogMessage:
315 [NSString stringWithFormat:@"url [%@] - credential [%@]",
316 url,
317 credential]];
318 RTCIceServer *iceServer =
319 [[RTCIceServer alloc] initWithUri:[NSURL URLWithString:url]
320 password:credential];
321 [iceServers addObject:iceServer];
322 }
323 [self updateIceServers:iceServers withTurnServer:turnServerUrl];
324
325 [self maybeLogMessage:
326 [NSString stringWithFormat:@"About to open GAE with token: %@",
327 self.token]];
328 self.gaeChannel =
329 [[GAEChannelClient alloc] initWithToken:self.token
330 delegate:self.messageHandler];
331}
332
333@end