| OLD | NEW |
| 1 /* | 1 /* |
| 2 * libjingle | 2 * libjingle |
| 3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 * this list of conditions and the following disclaimer in the documentation | 11 * this list of conditions and the following disclaimer in the documentation |
| 12 * and/or other materials provided with the distribution. | 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 | 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. | 14 * derived from this software without specific prior written permission. |
| 15 * | 15 * |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 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 | 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 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, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 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 | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #import "ARDAppEngineClient.h" | 28 #import "ARDAppEngineClient.h" |
| 29 | 29 |
| 30 #import "RTCLogging.h" |
| 31 |
| 30 #import "ARDJoinResponse.h" | 32 #import "ARDJoinResponse.h" |
| 31 #import "ARDLogging.h" | |
| 32 #import "ARDMessageResponse.h" | 33 #import "ARDMessageResponse.h" |
| 33 #import "ARDSignalingMessage.h" | 34 #import "ARDSignalingMessage.h" |
| 34 #import "ARDUtilities.h" | 35 #import "ARDUtilities.h" |
| 35 | 36 |
| 36 // TODO(tkchin): move these to a configuration object. | 37 // TODO(tkchin): move these to a configuration object. |
| 37 static NSString * const kARDRoomServerHostUrl = | 38 static NSString * const kARDRoomServerHostUrl = |
| 38 @"https://apprtc.appspot.com"; | 39 @"https://apprtc.appspot.com"; |
| 39 static NSString * const kARDRoomServerJoinFormat = | 40 static NSString * const kARDRoomServerJoinFormat = |
| 40 @"https://apprtc.appspot.com/join/%@"; | 41 @"https://apprtc.appspot.com/join/%@"; |
| 41 static NSString * const kARDRoomServerMessageFormat = | 42 static NSString * const kARDRoomServerMessageFormat = |
| 42 @"https://apprtc.appspot.com/message/%@/%@"; | 43 @"https://apprtc.appspot.com/message/%@/%@"; |
| 43 static NSString * const kARDRoomServerLeaveFormat = | 44 static NSString * const kARDRoomServerLeaveFormat = |
| 44 @"https://apprtc.appspot.com/leave/%@/%@"; | 45 @"https://apprtc.appspot.com/leave/%@/%@"; |
| 45 | 46 |
| 46 static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient"; | 47 static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient"; |
| 47 static NSInteger const kARDAppEngineClientErrorBadResponse = -1; | 48 static NSInteger const kARDAppEngineClientErrorBadResponse = -1; |
| 48 | 49 |
| 49 @implementation ARDAppEngineClient | 50 @implementation ARDAppEngineClient |
| 50 | 51 |
| 51 #pragma mark - ARDRoomServerClient | 52 #pragma mark - ARDRoomServerClient |
| 52 | 53 |
| 53 - (void)joinRoomWithRoomId:(NSString *)roomId | 54 - (void)joinRoomWithRoomId:(NSString *)roomId |
| 54 completionHandler:(void (^)(ARDJoinResponse *response, | 55 completionHandler:(void (^)(ARDJoinResponse *response, |
| 55 NSError *error))completionHandler { | 56 NSError *error))completionHandler { |
| 56 NSParameterAssert(roomId.length); | 57 NSParameterAssert(roomId.length); |
| 57 | 58 |
| 58 NSString *urlString = | 59 NSString *urlString = |
| 59 [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId]; | 60 [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId]; |
| 60 NSURL *roomURL = [NSURL URLWithString:urlString]; | 61 NSURL *roomURL = [NSURL URLWithString:urlString]; |
| 61 ARDLog(@"Joining room:%@ on room server.", roomId); | 62 RTCLog(@"Joining room:%@ on room server.", roomId); |
| 62 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:roomURL]; | 63 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:roomURL]; |
| 63 request.HTTPMethod = @"POST"; | 64 request.HTTPMethod = @"POST"; |
| 64 __weak ARDAppEngineClient *weakSelf = self; | 65 __weak ARDAppEngineClient *weakSelf = self; |
| 65 [NSURLConnection sendAsyncRequest:request | 66 [NSURLConnection sendAsyncRequest:request |
| 66 completionHandler:^(NSURLResponse *response, | 67 completionHandler:^(NSURLResponse *response, |
| 67 NSData *data, | 68 NSData *data, |
| 68 NSError *error) { | 69 NSError *error) { |
| 69 ARDAppEngineClient *strongSelf = weakSelf; | 70 ARDAppEngineClient *strongSelf = weakSelf; |
| 70 if (error) { | 71 if (error) { |
| 71 if (completionHandler) { | 72 if (completionHandler) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 95 NSError *error))completionHandler { | 96 NSError *error))completionHandler { |
| 96 NSParameterAssert(message); | 97 NSParameterAssert(message); |
| 97 NSParameterAssert(roomId.length); | 98 NSParameterAssert(roomId.length); |
| 98 NSParameterAssert(clientId.length); | 99 NSParameterAssert(clientId.length); |
| 99 | 100 |
| 100 NSData *data = [message JSONData]; | 101 NSData *data = [message JSONData]; |
| 101 NSString *urlString = | 102 NSString *urlString = |
| 102 [NSString stringWithFormat: | 103 [NSString stringWithFormat: |
| 103 kARDRoomServerMessageFormat, roomId, clientId]; | 104 kARDRoomServerMessageFormat, roomId, clientId]; |
| 104 NSURL *url = [NSURL URLWithString:urlString]; | 105 NSURL *url = [NSURL URLWithString:urlString]; |
| 105 ARDLog(@"C->RS POST: %@", message); | 106 RTCLog(@"C->RS POST: %@", message); |
| 106 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | 107 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
| 107 request.HTTPMethod = @"POST"; | 108 request.HTTPMethod = @"POST"; |
| 108 request.HTTPBody = data; | 109 request.HTTPBody = data; |
| 109 __weak ARDAppEngineClient *weakSelf = self; | 110 __weak ARDAppEngineClient *weakSelf = self; |
| 110 [NSURLConnection sendAsyncRequest:request | 111 [NSURLConnection sendAsyncRequest:request |
| 111 completionHandler:^(NSURLResponse *response, | 112 completionHandler:^(NSURLResponse *response, |
| 112 NSData *data, | 113 NSData *data, |
| 113 NSError *error) { | 114 NSError *error) { |
| 114 ARDAppEngineClient *strongSelf = weakSelf; | 115 ARDAppEngineClient *strongSelf = weakSelf; |
| 115 if (error) { | 116 if (error) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 141 | 142 |
| 142 NSString *urlString = | 143 NSString *urlString = |
| 143 [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId]; | 144 [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId]; |
| 144 NSURL *url = [NSURL URLWithString:urlString]; | 145 NSURL *url = [NSURL URLWithString:urlString]; |
| 145 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | 146 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
| 146 request.HTTPMethod = @"POST"; | 147 request.HTTPMethod = @"POST"; |
| 147 NSURLResponse *response = nil; | 148 NSURLResponse *response = nil; |
| 148 NSError *error = nil; | 149 NSError *error = nil; |
| 149 // We want a synchronous request so that we know that we've left the room on | 150 // We want a synchronous request so that we know that we've left the room on |
| 150 // room server before we do any further work. | 151 // room server before we do any further work. |
| 151 ARDLog(@"C->RS: BYE"); | 152 RTCLog(@"C->RS: BYE"); |
| 152 [NSURLConnection sendSynchronousRequest:request | 153 [NSURLConnection sendSynchronousRequest:request |
| 153 returningResponse:&response | 154 returningResponse:&response |
| 154 error:&error]; | 155 error:&error]; |
| 155 if (error) { | 156 if (error) { |
| 156 ARDLog(@"Error leaving room %@ on room server: %@", | 157 RTCLogError(@"Error leaving room %@ on room server: %@", |
| 157 roomId, error.localizedDescription); | 158 roomId, error.localizedDescription); |
| 158 if (completionHandler) { | 159 if (completionHandler) { |
| 159 completionHandler(error); | 160 completionHandler(error); |
| 160 } | 161 } |
| 161 return; | 162 return; |
| 162 } | 163 } |
| 163 ARDLog(@"Left room:%@ on room server.", roomId); | 164 RTCLog(@"Left room:%@ on room server.", roomId); |
| 164 if (completionHandler) { | 165 if (completionHandler) { |
| 165 completionHandler(nil); | 166 completionHandler(nil); |
| 166 } | 167 } |
| 167 } | 168 } |
| 168 | 169 |
| 169 #pragma mark - Private | 170 #pragma mark - Private |
| 170 | 171 |
| 171 + (NSError *)badResponseError { | 172 + (NSError *)badResponseError { |
| 172 NSError *error = | 173 NSError *error = |
| 173 [[NSError alloc] initWithDomain:kARDAppEngineClientErrorDomain | 174 [[NSError alloc] initWithDomain:kARDAppEngineClientErrorDomain |
| 174 code:kARDAppEngineClientErrorBadResponse | 175 code:kARDAppEngineClientErrorBadResponse |
| 175 userInfo:@{ | 176 userInfo:@{ |
| 176 NSLocalizedDescriptionKey: @"Error parsing response.", | 177 NSLocalizedDescriptionKey: @"Error parsing response.", |
| 177 }]; | 178 }]; |
| 178 return error; | 179 return error; |
| 179 } | 180 } |
| 180 | 181 |
| 181 @end | 182 @end |
| OLD | NEW |