blob: 93f0d36a316aa740bf8ddad8a7924ffc80fba2ca [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"
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000033#import "RTCICEServer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
35@interface APPRTCAppClient ()
36
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000037@property(nonatomic, assign) dispatch_queue_t backgroundQueue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038@property(nonatomic, copy) NSString *baseURL;
39@property(nonatomic, strong) GAEChannelClient *gaeChannel;
40@property(nonatomic, copy) NSString *postMessageUrl;
41@property(nonatomic, copy) NSString *pcConfig;
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000042@property(nonatomic, strong) NSMutableString *roomHtml;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043@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);
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000055 dispatch_retain(_backgroundQueue);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 _sendQueue = [NSMutableArray array];
57 // Uncomment to see Request/Response logging.
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000058 // _verboseLogging = YES;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 }
60 return self;
61}
62
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000063- (void)dealloc {
64 dispatch_release(_backgroundQueue);
65}
66
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067#pragma mark - Public methods
68
69- (void)connectToRoom:(NSURL *)url {
70 NSURLRequest *request = [self getRequestFromUrl:url];
71 [NSURLConnection connectionWithRequest:request delegate:self];
72}
73
74- (void)sendData:(NSData *)data {
75 @synchronized(self) {
76 [self maybeLogMessage:@"Send message"];
77 [self.sendQueue addObject:[data copy]];
78 }
79 [self requestQueueDrainInBackground];
80}
81
82#pragma mark - Internal methods
83
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000084- (NSString*)findVar:(NSString*)name
85 strippingQuotes:(BOOL)strippingQuotes {
86 NSError* error;
87 NSString* pattern =
88 [NSString stringWithFormat:@".*\n *var %@ = ([^\n]*);\n.*", name];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 NSRegularExpression *regexp =
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000090 [NSRegularExpression regularExpressionWithPattern:pattern
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 options:0
92 error:&error];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000093 NSAssert(!error, @"Unexpected error compiling regex: ",
94 error.localizedDescription);
95
96 NSRange fullRange = NSMakeRange(0, [self.roomHtml length]);
97 NSArray *matches =
98 [regexp matchesInString:self.roomHtml options:0 range:fullRange];
99 if ([matches count] != 1) {
100 [self showMessage:[NSString stringWithFormat:@"%d matches for %@ in %@",
101 [matches count], name, self.roomHtml]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 return nil;
103 }
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000104 NSRange matchRange = [matches[0] rangeAtIndex:1];
105 NSString* value = [self.roomHtml substringWithRange:matchRange];
106 if (strippingQuotes) {
107 NSAssert([value length] > 2,
108 @"Can't strip quotes from short string: [%@]", value);
109 NSAssert(([value characterAtIndex:0] == '\'' &&
110 [value characterAtIndex:[value length] - 1] == '\''),
111 @"Can't strip quotes from unquoted string: [%@]", value);
112 value = [value substringWithRange:NSMakeRange(1, [value length] - 2)];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 }
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000114 return value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115}
116
117- (NSURLRequest *)getRequestFromUrl:(NSURL *)url {
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000118 self.roomHtml = [NSMutableString stringWithCapacity:20000];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 NSString *path =
120 [NSString stringWithFormat:@"https:%@", [url resourceSpecifier]];
121 NSURLRequest *request =
122 [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
123 return request;
124}
125
126- (void)maybeLogMessage:(NSString *)message {
127 if (self.verboseLogging) {
128 NSLog(@"%@", message);
129 }
130}
131
132- (void)requestQueueDrainInBackground {
133 dispatch_async(self.backgroundQueue, ^(void) {
134 // TODO(hughv): This can block the UI thread. Fix.
135 @synchronized(self) {
136 if ([self.postMessageUrl length] < 1) {
137 return;
138 }
139 for (NSData *data in self.sendQueue) {
140 NSString *url = [NSString stringWithFormat:@"%@/%@",
141 self.baseURL,
142 self.postMessageUrl];
143 [self sendData:data withUrl:url];
144 }
145 [self.sendQueue removeAllObjects];
146 }
147 });
148}
149
150- (void)sendData:(NSData *)data withUrl:(NSString *)url {
151 NSMutableURLRequest *request =
152 [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
153 request.HTTPMethod = @"POST";
154 [request setHTTPBody:data];
155 NSURLResponse *response;
156 NSError *error;
157 NSData *responseData = [NSURLConnection sendSynchronousRequest:request
158 returningResponse:&response
159 error:&error];
160 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
161 int status = [httpResponse statusCode];
162 NSAssert(status == 200,
163 @"Bad response [%d] to message: %@\n\n%@",
164 status,
165 [NSString stringWithUTF8String:[data bytes]],
166 [NSString stringWithUTF8String:[responseData bytes]]);
167}
168
169- (void)showMessage:(NSString *)message {
170 NSLog(@"%@", message);
171 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Unable to join"
172 message:message
173 delegate:nil
174 cancelButtonTitle:@"OK"
175 otherButtonTitles:nil];
176 [alertView show];
177}
178
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000179- (void)updateICEServers:(NSMutableArray *)ICEServers
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 withTurnServer:(NSString *)turnServerUrl {
181 if ([turnServerUrl length] < 1) {
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000182 [self.ICEServerDelegate onICEServers:ICEServers];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 return;
184 }
185 dispatch_async(self.backgroundQueue, ^(void) {
186 NSMutableURLRequest *request = [NSMutableURLRequest
187 requestWithURL:[NSURL URLWithString:turnServerUrl]];
188 [request addValue:@"Mozilla/5.0" forHTTPHeaderField:@"user-agent"];
189 [request addValue:@"https://apprtc.appspot.com"
190 forHTTPHeaderField:@"origin"];
191 NSURLResponse *response;
192 NSError *error;
193 NSData *responseData = [NSURLConnection sendSynchronousRequest:request
194 returningResponse:&response
195 error:&error];
196 if (!error) {
197 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
198 options:0
199 error:&error];
200 NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
201 NSString *username = json[@"username"];
202 NSString *turnServer = json[@"turn"];
203 NSString *password = json[@"password"];
204 NSString *fullUrl =
205 [NSString stringWithFormat:@"turn:%@@%@", username, turnServer];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000206 RTCICEServer *ICEServer =
207 [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:fullUrl]
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 password:password];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000209 [ICEServers addObject:ICEServer];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 } else {
211 NSLog(@"Unable to get TURN server. Error: %@", error.description);
212 }
213
214 dispatch_async(dispatch_get_main_queue(), ^(void) {
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000215 [self.ICEServerDelegate onICEServers:ICEServers];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 });
217 });
218}
219
220#pragma mark - NSURLConnectionDataDelegate methods
221
222- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
223 NSString *roomHtml = [NSString stringWithUTF8String:[data bytes]];
224 [self maybeLogMessage:
225 [NSString stringWithFormat:@"Received %d chars", [roomHtml length]]];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000226 [self.roomHtml appendString:roomHtml];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227}
228
229- (void)connection:(NSURLConnection *)connection
230 didReceiveResponse:(NSURLResponse *)response {
231 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
232 int statusCode = [httpResponse statusCode];
233 [self maybeLogMessage:
234 [NSString stringWithFormat:
235 @"Response received\nURL\n%@\nStatus [%d]\nHeaders\n%@",
236 [httpResponse URL],
237 statusCode,
238 [httpResponse allHeaderFields]]];
239 NSAssert(statusCode == 200, @"Invalid response of %d received.", statusCode);
240}
241
242- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
243 [self maybeLogMessage:[NSString stringWithFormat:@"finished loading %d chars",
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000244 [self.roomHtml length]]];
245 NSRegularExpression* fullRegex =
246 [NSRegularExpression regularExpressionWithPattern:@"room is full"
247 options:0
248 error:nil];
249 if ([fullRegex numberOfMatchesInString:self.roomHtml
250 options:0
251 range:NSMakeRange(0, [self.roomHtml length])]) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 [self showMessage:@"Room full"];
253 return;
254 }
255
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000256
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 NSString *fullUrl = [[[connection originalRequest] URL] absoluteString];
258 NSRange queryRange = [fullUrl rangeOfString:@"?"];
259 self.baseURL = [fullUrl substringToIndex:queryRange.location];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000260 [self maybeLogMessage:[NSString stringWithFormat:@"Base URL: %@", self.baseURL]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000262 self.token = [self findVar:@"channelToken" strippingQuotes:YES];
263 if (!self.token)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 return;
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000265 [self maybeLogMessage:[NSString stringWithFormat:@"Token: %@", self.token]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000267 NSString* roomKey = [self findVar:@"roomKey" strippingQuotes:YES];
268 NSString* me = [self findVar:@"me" strippingQuotes:YES];
269 if (!roomKey || !me)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 self.postMessageUrl =
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000272 [NSString stringWithFormat:@"/message?r=%@&u=%@", roomKey, me];
273 [self maybeLogMessage:[NSString stringWithFormat:@"POST message URL: %@",
274 self.postMessageUrl]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000276 NSString* pcConfig = [self findVar:@"pcConfig" strippingQuotes:NO];
277 if (!pcConfig)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 [self maybeLogMessage:
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000280 [NSString stringWithFormat:@"PC Config JSON: %@", pcConfig]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000282 NSString *turnServerUrl = [self findVar:@"turnUrl" strippingQuotes:YES];
283 if (turnServerUrl) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 [self maybeLogMessage:
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000285 [NSString stringWithFormat:@"TURN server request URL: %@",
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 turnServerUrl]];
287 }
288
289 NSError *error;
290 NSData *pcData = [pcConfig dataUsingEncoding:NSUTF8StringEncoding];
291 NSDictionary *json =
292 [NSJSONSerialization JSONObjectWithData:pcData options:0 error:&error];
293 NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000294 NSArray *servers = [json objectForKey:@"ICEServers"];
295 NSMutableArray *ICEServers = [NSMutableArray array];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 for (NSDictionary *server in servers) {
297 NSString *url = [server objectForKey:@"url"];
298 NSString *credential = [server objectForKey:@"credential"];
299 if (!credential) {
300 credential = @"";
301 }
302 [self maybeLogMessage:
303 [NSString stringWithFormat:@"url [%@] - credential [%@]",
304 url,
305 credential]];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000306 RTCICEServer *ICEServer =
307 [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:url]
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 password:credential];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000309 [ICEServers addObject:ICEServer];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 }
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000311 [self updateICEServers:ICEServers withTurnServer:turnServerUrl];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312
313 [self maybeLogMessage:
314 [NSString stringWithFormat:@"About to open GAE with token: %@",
315 self.token]];
316 self.gaeChannel =
317 [[GAEChannelClient alloc] initWithToken:self.token
318 delegate:self.messageHandler];
319}
320
321@end