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