blob: c01ee2507b41354239b5389343c54d786f358e50 [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.orgd0f4c212013-08-20 22:16:55 +000037@property(nonatomic, strong) 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
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000052@synthesize ICEServerDelegate = _ICEServerDelegate;
53@synthesize messageHandler = _messageHandler;
54
55@synthesize backgroundQueue = _backgroundQueue;
56@synthesize baseURL = _baseURL;
57@synthesize gaeChannel = _gaeChannel;
58@synthesize postMessageUrl = _postMessageUrl;
59@synthesize pcConfig = _pcConfig;
60@synthesize roomHtml = _roomHtml;
61@synthesize sendQueue = _sendQueue;
62@synthesize token = _token;
63@synthesize verboseLogging = _verboseLogging;
fischman@webrtc.org82387e42014-02-10 18:47:11 +000064@synthesize initiator = _initiator;
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000065
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066- (id)init {
67 if (self = [super init]) {
68 _backgroundQueue = dispatch_queue_create("RTCBackgroundQueue", NULL);
69 _sendQueue = [NSMutableArray array];
70 // Uncomment to see Request/Response logging.
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000071 // _verboseLogging = YES;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 }
73 return self;
74}
75
76#pragma mark - Public methods
77
78- (void)connectToRoom:(NSURL *)url {
79 NSURLRequest *request = [self getRequestFromUrl:url];
80 [NSURLConnection connectionWithRequest:request delegate:self];
81}
82
83- (void)sendData:(NSData *)data {
84 @synchronized(self) {
85 [self maybeLogMessage:@"Send message"];
86 [self.sendQueue addObject:[data copy]];
87 }
88 [self requestQueueDrainInBackground];
89}
90
91#pragma mark - Internal methods
92
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000093- (NSString*)findVar:(NSString*)name
94 strippingQuotes:(BOOL)strippingQuotes {
95 NSError* error;
96 NSString* pattern =
97 [NSString stringWithFormat:@".*\n *var %@ = ([^\n]*);\n.*", name];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 NSRegularExpression *regexp =
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000099 [NSRegularExpression regularExpressionWithPattern:pattern
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 options:0
101 error:&error];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000102 NSAssert(!error, @"Unexpected error compiling regex: ",
103 error.localizedDescription);
104
105 NSRange fullRange = NSMakeRange(0, [self.roomHtml length]);
106 NSArray *matches =
107 [regexp matchesInString:self.roomHtml options:0 range:fullRange];
108 if ([matches count] != 1) {
109 [self showMessage:[NSString stringWithFormat:@"%d matches for %@ in %@",
110 [matches count], name, self.roomHtml]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 return nil;
112 }
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000113 NSRange matchRange = [matches[0] rangeAtIndex:1];
114 NSString* value = [self.roomHtml substringWithRange:matchRange];
115 if (strippingQuotes) {
116 NSAssert([value length] > 2,
117 @"Can't strip quotes from short string: [%@]", value);
118 NSAssert(([value characterAtIndex:0] == '\'' &&
119 [value characterAtIndex:[value length] - 1] == '\''),
120 @"Can't strip quotes from unquoted string: [%@]", value);
121 value = [value substringWithRange:NSMakeRange(1, [value length] - 2)];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 }
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000123 return value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124}
125
126- (NSURLRequest *)getRequestFromUrl:(NSURL *)url {
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000127 self.roomHtml = [NSMutableString stringWithCapacity:20000];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 NSString *path =
129 [NSString stringWithFormat:@"https:%@", [url resourceSpecifier]];
130 NSURLRequest *request =
131 [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
132 return request;
133}
134
135- (void)maybeLogMessage:(NSString *)message {
136 if (self.verboseLogging) {
137 NSLog(@"%@", message);
138 }
139}
140
141- (void)requestQueueDrainInBackground {
142 dispatch_async(self.backgroundQueue, ^(void) {
143 // TODO(hughv): This can block the UI thread. Fix.
144 @synchronized(self) {
145 if ([self.postMessageUrl length] < 1) {
146 return;
147 }
148 for (NSData *data in self.sendQueue) {
149 NSString *url = [NSString stringWithFormat:@"%@/%@",
150 self.baseURL,
151 self.postMessageUrl];
152 [self sendData:data withUrl:url];
153 }
154 [self.sendQueue removeAllObjects];
155 }
156 });
157}
158
159- (void)sendData:(NSData *)data withUrl:(NSString *)url {
160 NSMutableURLRequest *request =
161 [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
162 request.HTTPMethod = @"POST";
163 [request setHTTPBody:data];
164 NSURLResponse *response;
165 NSError *error;
166 NSData *responseData = [NSURLConnection sendSynchronousRequest:request
167 returningResponse:&response
168 error:&error];
169 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
170 int status = [httpResponse statusCode];
171 NSAssert(status == 200,
172 @"Bad response [%d] to message: %@\n\n%@",
173 status,
174 [NSString stringWithUTF8String:[data bytes]],
175 [NSString stringWithUTF8String:[responseData bytes]]);
176}
177
178- (void)showMessage:(NSString *)message {
179 NSLog(@"%@", message);
180 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Unable to join"
181 message:message
182 delegate:nil
183 cancelButtonTitle:@"OK"
184 otherButtonTitles:nil];
185 [alertView show];
186}
187
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000188- (void)updateICEServers:(NSMutableArray *)ICEServers
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 withTurnServer:(NSString *)turnServerUrl {
190 if ([turnServerUrl length] < 1) {
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000191 [self.ICEServerDelegate onICEServers:ICEServers];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 return;
193 }
194 dispatch_async(self.backgroundQueue, ^(void) {
195 NSMutableURLRequest *request = [NSMutableURLRequest
196 requestWithURL:[NSURL URLWithString:turnServerUrl]];
197 [request addValue:@"Mozilla/5.0" forHTTPHeaderField:@"user-agent"];
198 [request addValue:@"https://apprtc.appspot.com"
199 forHTTPHeaderField:@"origin"];
200 NSURLResponse *response;
201 NSError *error;
202 NSData *responseData = [NSURLConnection sendSynchronousRequest:request
203 returningResponse:&response
204 error:&error];
205 if (!error) {
206 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
207 options:0
208 error:&error];
209 NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
210 NSString *username = json[@"username"];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 NSString *password = json[@"password"];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000212 NSArray* uris = json[@"uris"];
213 for (int i = 0; i < [uris count]; ++i) {
214 NSString *turnServer = [uris objectAtIndex:i];
215 RTCICEServer *ICEServer =
216 [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:turnServer]
217 username:username
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 password:password];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000219 NSLog(@"Added ICE Server: %@", ICEServer);
220 [ICEServers addObject:ICEServer];
221 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 } else {
223 NSLog(@"Unable to get TURN server. Error: %@", error.description);
224 }
225
226 dispatch_async(dispatch_get_main_queue(), ^(void) {
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000227 [self.ICEServerDelegate onICEServers:ICEServers];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 });
229 });
230}
231
232#pragma mark - NSURLConnectionDataDelegate methods
233
234- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
235 NSString *roomHtml = [NSString stringWithUTF8String:[data bytes]];
236 [self maybeLogMessage:
237 [NSString stringWithFormat:@"Received %d chars", [roomHtml length]]];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000238 [self.roomHtml appendString:roomHtml];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239}
240
241- (void)connection:(NSURLConnection *)connection
242 didReceiveResponse:(NSURLResponse *)response {
243 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
244 int statusCode = [httpResponse statusCode];
245 [self maybeLogMessage:
246 [NSString stringWithFormat:
247 @"Response received\nURL\n%@\nStatus [%d]\nHeaders\n%@",
248 [httpResponse URL],
249 statusCode,
250 [httpResponse allHeaderFields]]];
251 NSAssert(statusCode == 200, @"Invalid response of %d received.", statusCode);
252}
253
254- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
255 [self maybeLogMessage:[NSString stringWithFormat:@"finished loading %d chars",
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000256 [self.roomHtml length]]];
257 NSRegularExpression* fullRegex =
258 [NSRegularExpression regularExpressionWithPattern:@"room is full"
259 options:0
260 error:nil];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000261 if ([fullRegex
262 numberOfMatchesInString:self.roomHtml
263 options:0
264 range:NSMakeRange(0, [self.roomHtml length])]) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 [self showMessage:@"Room full"];
266 return;
267 }
268
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000269
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 NSString *fullUrl = [[[connection originalRequest] URL] absoluteString];
271 NSRange queryRange = [fullUrl rangeOfString:@"?"];
272 self.baseURL = [fullUrl substringToIndex:queryRange.location];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000273 [self maybeLogMessage:
274 [NSString stringWithFormat:@"Base URL: %@", self.baseURL]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275
fischman@webrtc.org82387e42014-02-10 18:47:11 +0000276 self.initiator = [[self findVar:@"initiator" strippingQuotes:NO] boolValue];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000277 self.token = [self findVar:@"channelToken" strippingQuotes:YES];
278 if (!self.token)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 return;
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000280 [self maybeLogMessage:[NSString stringWithFormat:@"Token: %@", self.token]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000282 NSString* roomKey = [self findVar:@"roomKey" strippingQuotes:YES];
283 NSString* me = [self findVar:@"me" strippingQuotes:YES];
284 if (!roomKey || !me)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 self.postMessageUrl =
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000287 [NSString stringWithFormat:@"/message?r=%@&u=%@", roomKey, me];
288 [self maybeLogMessage:[NSString stringWithFormat:@"POST message URL: %@",
289 self.postMessageUrl]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000291 NSString* pcConfig = [self findVar:@"pcConfig" strippingQuotes:NO];
292 if (!pcConfig)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 [self maybeLogMessage:
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000295 [NSString stringWithFormat:@"PC Config JSON: %@", pcConfig]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000297 NSString *turnServerUrl = [self findVar:@"turnUrl" strippingQuotes:YES];
298 if (turnServerUrl) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 [self maybeLogMessage:
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000300 [NSString stringWithFormat:@"TURN server request URL: %@",
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 turnServerUrl]];
302 }
303
304 NSError *error;
305 NSData *pcData = [pcConfig dataUsingEncoding:NSUTF8StringEncoding];
306 NSDictionary *json =
307 [NSJSONSerialization JSONObjectWithData:pcData options:0 error:&error];
308 NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000309 NSArray *servers = [json objectForKey:@"iceServers"];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000310 NSMutableArray *ICEServers = [NSMutableArray array];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 for (NSDictionary *server in servers) {
312 NSString *url = [server objectForKey:@"url"];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000313 NSString *username = json[@"username"];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 NSString *credential = [server objectForKey:@"credential"];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000315 if (!username) {
316 username = @"";
317 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 if (!credential) {
319 credential = @"";
320 }
321 [self maybeLogMessage:
322 [NSString stringWithFormat:@"url [%@] - credential [%@]",
323 url,
324 credential]];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000325 RTCICEServer *ICEServer =
326 [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:url]
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000327 username:username
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 password:credential];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000329 NSLog(@"Added ICE Server: %@", ICEServer);
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000330 [ICEServers addObject:ICEServer];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 }
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000332 [self updateICEServers:ICEServers withTurnServer:turnServerUrl];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333
334 [self maybeLogMessage:
335 [NSString stringWithFormat:@"About to open GAE with token: %@",
336 self.token]];
337 self.gaeChannel =
338 [[GAEChannelClient alloc] initWithToken:self.token
339 delegate:self.messageHandler];
340}
341
342@end