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 | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 13 | #import "RTCLogging.h" |
| 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 = |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 22 | @"https://apprtc.appspot.com"; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 23 | static NSString * const kARDRoomServerJoinFormat = |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 24 | @"https://apprtc.appspot.com/join/%@"; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 25 | static NSString * const kARDRoomServerMessageFormat = |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 26 | @"https://apprtc.appspot.com/message/%@/%@"; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 27 | static NSString * const kARDRoomServerLeaveFormat = |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 28 | @"https://apprtc.appspot.com/leave/%@/%@"; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 29 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 30 | static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient"; |
| 31 | static NSInteger const kARDAppEngineClientErrorBadResponse = -1; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 32 | |
| 33 | @implementation ARDAppEngineClient |
| 34 | |
| 35 | #pragma mark - ARDRoomServerClient |
| 36 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 37 | - (void)joinRoomWithRoomId:(NSString *)roomId |
| 38 | completionHandler:(void (^)(ARDJoinResponse *response, |
| 39 | NSError *error))completionHandler { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 40 | NSParameterAssert(roomId.length); |
| 41 | |
| 42 | NSString *urlString = |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 43 | [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 44 | NSURL *roomURL = [NSURL URLWithString:urlString]; |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 45 | RTCLog(@"Joining room:%@ on room server.", roomId); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 46 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:roomURL]; |
| 47 | request.HTTPMethod = @"POST"; |
| 48 | __weak ARDAppEngineClient *weakSelf = self; |
| 49 | [NSURLConnection sendAsyncRequest:request |
| 50 | completionHandler:^(NSURLResponse *response, |
| 51 | NSData *data, |
| 52 | NSError *error) { |
| 53 | ARDAppEngineClient *strongSelf = weakSelf; |
| 54 | if (error) { |
| 55 | if (completionHandler) { |
| 56 | completionHandler(nil, error); |
| 57 | } |
| 58 | return; |
| 59 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 60 | ARDJoinResponse *joinResponse = |
| 61 | [ARDJoinResponse responseFromJSONData:data]; |
| 62 | if (!joinResponse) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 63 | if (completionHandler) { |
| 64 | NSError *error = [[self class] badResponseError]; |
| 65 | completionHandler(nil, error); |
| 66 | } |
| 67 | return; |
| 68 | } |
| 69 | if (completionHandler) { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 70 | completionHandler(joinResponse, nil); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 71 | } |
| 72 | }]; |
| 73 | } |
| 74 | |
| 75 | - (void)sendMessage:(ARDSignalingMessage *)message |
| 76 | forRoomId:(NSString *)roomId |
| 77 | clientId:(NSString *)clientId |
| 78 | completionHandler:(void (^)(ARDMessageResponse *response, |
| 79 | NSError *error))completionHandler { |
| 80 | NSParameterAssert(message); |
| 81 | NSParameterAssert(roomId.length); |
| 82 | NSParameterAssert(clientId.length); |
| 83 | |
| 84 | NSData *data = [message JSONData]; |
| 85 | NSString *urlString = |
| 86 | [NSString stringWithFormat: |
| 87 | kARDRoomServerMessageFormat, roomId, clientId]; |
| 88 | NSURL *url = [NSURL URLWithString:urlString]; |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 89 | RTCLog(@"C->RS POST: %@", message); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 90 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
| 91 | request.HTTPMethod = @"POST"; |
| 92 | request.HTTPBody = data; |
| 93 | __weak ARDAppEngineClient *weakSelf = self; |
| 94 | [NSURLConnection sendAsyncRequest:request |
| 95 | completionHandler:^(NSURLResponse *response, |
| 96 | NSData *data, |
| 97 | NSError *error) { |
| 98 | ARDAppEngineClient *strongSelf = weakSelf; |
| 99 | if (error) { |
| 100 | if (completionHandler) { |
| 101 | completionHandler(nil, error); |
| 102 | } |
| 103 | return; |
| 104 | } |
| 105 | ARDMessageResponse *messageResponse = |
| 106 | [ARDMessageResponse responseFromJSONData:data]; |
| 107 | if (!messageResponse) { |
| 108 | if (completionHandler) { |
| 109 | NSError *error = [[self class] badResponseError]; |
| 110 | completionHandler(nil, error); |
| 111 | } |
| 112 | return; |
| 113 | } |
| 114 | if (completionHandler) { |
| 115 | completionHandler(messageResponse, nil); |
| 116 | } |
| 117 | }]; |
| 118 | } |
| 119 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 120 | - (void)leaveRoomWithRoomId:(NSString *)roomId |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 121 | clientId:(NSString *)clientId |
| 122 | completionHandler:(void (^)(NSError *error))completionHandler { |
| 123 | NSParameterAssert(roomId.length); |
| 124 | NSParameterAssert(clientId.length); |
| 125 | |
| 126 | NSString *urlString = |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 127 | [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 128 | NSURL *url = [NSURL URLWithString:urlString]; |
Chuck Hays | caae5d4 | 2015-03-25 12:59:52 -0700 | [diff] [blame] | 129 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
| 130 | request.HTTPMethod = @"POST"; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 131 | NSURLResponse *response = nil; |
| 132 | NSError *error = nil; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 133 | // We want a synchronous request so that we know that we've left the room on |
| 134 | // room server before we do any further work. |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 135 | RTCLog(@"C->RS: BYE"); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 136 | [NSURLConnection sendSynchronousRequest:request |
| 137 | returningResponse:&response |
| 138 | error:&error]; |
| 139 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 140 | RTCLogError(@"Error leaving room %@ on room server: %@", |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 141 | roomId, error.localizedDescription); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 142 | if (completionHandler) { |
| 143 | completionHandler(error); |
| 144 | } |
| 145 | return; |
| 146 | } |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 147 | RTCLog(@"Left room:%@ on room server.", roomId); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 148 | if (completionHandler) { |
| 149 | completionHandler(nil); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | #pragma mark - Private |
| 154 | |
| 155 | + (NSError *)badResponseError { |
| 156 | NSError *error = |
| 157 | [[NSError alloc] initWithDomain:kARDAppEngineClientErrorDomain |
| 158 | code:kARDAppEngineClientErrorBadResponse |
| 159 | userInfo:@{ |
| 160 | NSLocalizedDescriptionKey: @"Error parsing response.", |
| 161 | }]; |
| 162 | return error; |
| 163 | } |
| 164 | |
| 165 | @end |