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