| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2014 Google Inc. | |
| 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 | |
| 30 #import "RTCLogging.h" | |
| 31 | |
| 32 #import "ARDJoinResponse.h" | |
| 33 #import "ARDMessageResponse.h" | |
| 34 #import "ARDSignalingMessage.h" | |
| 35 #import "ARDUtilities.h" | |
| 36 | |
| 37 // TODO(tkchin): move these to a configuration object. | |
| 38 static NSString * const kARDRoomServerHostUrl = | |
| 39 @"https://apprtc.appspot.com"; | |
| 40 static NSString * const kARDRoomServerJoinFormat = | |
| 41 @"https://apprtc.appspot.com/join/%@"; | |
| 42 static NSString * const kARDRoomServerMessageFormat = | |
| 43 @"https://apprtc.appspot.com/message/%@/%@"; | |
| 44 static NSString * const kARDRoomServerLeaveFormat = | |
| 45 @"https://apprtc.appspot.com/leave/%@/%@"; | |
| 46 | |
| 47 static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient"; | |
| 48 static NSInteger const kARDAppEngineClientErrorBadResponse = -1; | |
| 49 | |
| 50 @implementation ARDAppEngineClient | |
| 51 | |
| 52 #pragma mark - ARDRoomServerClient | |
| 53 | |
| 54 - (void)joinRoomWithRoomId:(NSString *)roomId | |
| 55 completionHandler:(void (^)(ARDJoinResponse *response, | |
| 56 NSError *error))completionHandler { | |
| 57 NSParameterAssert(roomId.length); | |
| 58 | |
| 59 NSString *urlString = | |
| 60 [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId]; | |
| 61 NSURL *roomURL = [NSURL URLWithString:urlString]; | |
| 62 RTCLog(@"Joining room:%@ on room server.", roomId); | |
| 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 } | |
| 77 ARDJoinResponse *joinResponse = | |
| 78 [ARDJoinResponse responseFromJSONData:data]; | |
| 79 if (!joinResponse) { | |
| 80 if (completionHandler) { | |
| 81 NSError *error = [[self class] badResponseError]; | |
| 82 completionHandler(nil, error); | |
| 83 } | |
| 84 return; | |
| 85 } | |
| 86 if (completionHandler) { | |
| 87 completionHandler(joinResponse, nil); | |
| 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]; | |
| 106 RTCLog(@"C->RS POST: %@", message); | |
| 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 | |
| 137 - (void)leaveRoomWithRoomId:(NSString *)roomId | |
| 138 clientId:(NSString *)clientId | |
| 139 completionHandler:(void (^)(NSError *error))completionHandler { | |
| 140 NSParameterAssert(roomId.length); | |
| 141 NSParameterAssert(clientId.length); | |
| 142 | |
| 143 NSString *urlString = | |
| 144 [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId]; | |
| 145 NSURL *url = [NSURL URLWithString:urlString]; | |
| 146 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
| 147 request.HTTPMethod = @"POST"; | |
| 148 NSURLResponse *response = nil; | |
| 149 NSError *error = nil; | |
| 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. | |
| 152 RTCLog(@"C->RS: BYE"); | |
| 153 [NSURLConnection sendSynchronousRequest:request | |
| 154 returningResponse:&response | |
| 155 error:&error]; | |
| 156 if (error) { | |
| 157 RTCLogError(@"Error leaving room %@ on room server: %@", | |
| 158 roomId, error.localizedDescription); | |
| 159 if (completionHandler) { | |
| 160 completionHandler(error); | |
| 161 } | |
| 162 return; | |
| 163 } | |
| 164 RTCLog(@"Left room:%@ on room server.", roomId); | |
| 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 | |
| OLD | NEW |