tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #import "ARDAppEngineClient.h" |
| 12 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 13 | #import "WebRTC/RTCLogging.h" |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 14 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 15 | #import "ARDJoinResponse.h" |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 16 | #import "ARDMessageResponse.h" |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 17 | #import "ARDSignalingMessage.h" |
| 18 | #import "ARDUtilities.h" |
| 19 | |
| 20 | // TODO(tkchin): move these to a configuration object. |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 21 | static NSString * const kARDRoomServerHostUrl = |
jansson | 84f9b98 | 2016-05-19 02:43:33 -0700 | [diff] [blame] | 22 | @"https://appr.tc"; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 23 | static NSString * const kARDRoomServerJoinFormat = |
jansson | 84f9b98 | 2016-05-19 02:43:33 -0700 | [diff] [blame] | 24 | @"https://appr.tc/join/%@"; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 25 | static NSString * const kARDRoomServerJoinFormatLoopback = |
jansson | 84f9b98 | 2016-05-19 02:43:33 -0700 | [diff] [blame] | 26 | @"https://appr.tc/join/%@?debug=loopback"; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 27 | static NSString * const kARDRoomServerMessageFormat = |
jansson | 84f9b98 | 2016-05-19 02:43:33 -0700 | [diff] [blame] | 28 | @"https://appr.tc/message/%@/%@"; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 29 | static NSString * const kARDRoomServerLeaveFormat = |
jansson | 84f9b98 | 2016-05-19 02:43:33 -0700 | [diff] [blame] | 30 | @"https://appr.tc/leave/%@/%@"; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 31 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 32 | static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient"; |
| 33 | static NSInteger const kARDAppEngineClientErrorBadResponse = -1; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 34 | |
| 35 | @implementation ARDAppEngineClient |
| 36 | |
| 37 | #pragma mark - ARDRoomServerClient |
| 38 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 39 | - (void)joinRoomWithRoomId:(NSString *)roomId |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 40 | isLoopback:(BOOL)isLoopback |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 41 | completionHandler:(void (^)(ARDJoinResponse *response, |
| 42 | NSError *error))completionHandler { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 43 | NSParameterAssert(roomId.length); |
| 44 | |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 45 | NSString *urlString = nil; |
| 46 | if (isLoopback) { |
| 47 | urlString = |
| 48 | [NSString stringWithFormat:kARDRoomServerJoinFormatLoopback, roomId]; |
| 49 | } else { |
| 50 | urlString = |
| 51 | [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId]; |
| 52 | } |
| 53 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 54 | NSURL *roomURL = [NSURL URLWithString:urlString]; |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 55 | RTCLog(@"Joining room:%@ on room server.", roomId); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 56 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:roomURL]; |
| 57 | request.HTTPMethod = @"POST"; |
| 58 | __weak ARDAppEngineClient *weakSelf = self; |
| 59 | [NSURLConnection sendAsyncRequest:request |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 60 | completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { |
| 61 | ARDAppEngineClient *strongSelf = weakSelf; |
| 62 | if (error) { |
| 63 | if (completionHandler) { |
| 64 | completionHandler(nil, error); |
| 65 | } |
| 66 | return; |
| 67 | } |
| 68 | ARDJoinResponse *joinResponse = [ARDJoinResponse responseFromJSONData:data]; |
| 69 | if (!joinResponse) { |
| 70 | if (completionHandler) { |
| 71 | NSError *error = [[self class] badResponseError]; |
| 72 | completionHandler(nil, error); |
| 73 | } |
| 74 | return; |
| 75 | } |
| 76 | if (completionHandler) { |
| 77 | completionHandler(joinResponse, nil); |
| 78 | } |
| 79 | }]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | - (void)sendMessage:(ARDSignalingMessage *)message |
| 83 | forRoomId:(NSString *)roomId |
| 84 | clientId:(NSString *)clientId |
| 85 | completionHandler:(void (^)(ARDMessageResponse *response, |
| 86 | NSError *error))completionHandler { |
| 87 | NSParameterAssert(message); |
| 88 | NSParameterAssert(roomId.length); |
| 89 | NSParameterAssert(clientId.length); |
| 90 | |
| 91 | NSData *data = [message JSONData]; |
| 92 | NSString *urlString = |
| 93 | [NSString stringWithFormat: |
| 94 | kARDRoomServerMessageFormat, roomId, clientId]; |
| 95 | NSURL *url = [NSURL URLWithString:urlString]; |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 96 | RTCLog(@"C->RS POST: %@", message); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 97 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
| 98 | request.HTTPMethod = @"POST"; |
| 99 | request.HTTPBody = data; |
| 100 | __weak ARDAppEngineClient *weakSelf = self; |
| 101 | [NSURLConnection sendAsyncRequest:request |
| 102 | completionHandler:^(NSURLResponse *response, |
| 103 | NSData *data, |
| 104 | NSError *error) { |
| 105 | ARDAppEngineClient *strongSelf = weakSelf; |
| 106 | if (error) { |
| 107 | if (completionHandler) { |
| 108 | completionHandler(nil, error); |
| 109 | } |
| 110 | return; |
| 111 | } |
| 112 | ARDMessageResponse *messageResponse = |
| 113 | [ARDMessageResponse responseFromJSONData:data]; |
| 114 | if (!messageResponse) { |
| 115 | if (completionHandler) { |
| 116 | NSError *error = [[self class] badResponseError]; |
| 117 | completionHandler(nil, error); |
| 118 | } |
| 119 | return; |
| 120 | } |
| 121 | if (completionHandler) { |
| 122 | completionHandler(messageResponse, nil); |
| 123 | } |
| 124 | }]; |
| 125 | } |
| 126 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 127 | - (void)leaveRoomWithRoomId:(NSString *)roomId |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 128 | clientId:(NSString *)clientId |
| 129 | completionHandler:(void (^)(NSError *error))completionHandler { |
| 130 | NSParameterAssert(roomId.length); |
| 131 | NSParameterAssert(clientId.length); |
| 132 | |
| 133 | NSString *urlString = |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 134 | [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 135 | NSURL *url = [NSURL URLWithString:urlString]; |
Chuck Hays | caae5d4 | 2015-03-25 12:59:52 -0700 | [diff] [blame] | 136 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
| 137 | request.HTTPMethod = @"POST"; |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 138 | |
| 139 | RTCLog(@"C->RS: BYE"); |
| 140 | __block NSError *error = nil; |
| 141 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 142 | // We want a synchronous request so that we know that we've left the room on |
| 143 | // room server before we do any further work. |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 144 | dispatch_semaphore_t sem = dispatch_semaphore_create(0); |
| 145 | [NSURLConnection sendAsyncRequest:request |
| 146 | completionHandler:^(NSURLResponse *response, NSData *data, NSError *e) { |
| 147 | if (e) { |
| 148 | error = e; |
| 149 | } |
| 150 | dispatch_semaphore_signal(sem); |
| 151 | }]; |
| 152 | |
| 153 | dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 154 | if (error) { |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 155 | RTCLogError(@"Error leaving room %@ on room server: %@", roomId, error.localizedDescription); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 156 | if (completionHandler) { |
| 157 | completionHandler(error); |
| 158 | } |
| 159 | return; |
| 160 | } |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 161 | RTCLog(@"Left room:%@ on room server.", roomId); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 162 | if (completionHandler) { |
| 163 | completionHandler(nil); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | #pragma mark - Private |
| 168 | |
| 169 | + (NSError *)badResponseError { |
| 170 | NSError *error = |
| 171 | [[NSError alloc] initWithDomain:kARDAppEngineClientErrorDomain |
| 172 | code:kARDAppEngineClientErrorBadResponse |
| 173 | userInfo:@{ |
| 174 | NSLocalizedDescriptionKey: @"Error parsing response.", |
| 175 | }]; |
| 176 | return error; |
| 177 | } |
| 178 | |
| 179 | @end |