tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 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 "ARDMessageResponse.h" |
| 31 | #import "ARDRegisterResponse.h" |
| 32 | #import "ARDSignalingMessage.h" |
| 33 | #import "ARDUtilities.h" |
| 34 | |
| 35 | // TODO(tkchin): move these to a configuration object. |
| 36 | static NSString *kARDRoomServerHostUrl = |
| 37 | @"https://apprtc.appspot.com"; |
| 38 | static NSString *kARDRoomServerRegisterFormat = |
| 39 | @"https://apprtc.appspot.com/register/%@"; |
| 40 | static NSString *kARDRoomServerMessageFormat = |
| 41 | @"https://apprtc.appspot.com/message/%@/%@"; |
| 42 | static NSString *kARDRoomServerByeFormat = |
| 43 | @"https://apprtc.appspot.com/bye/%@/%@"; |
| 44 | |
| 45 | static NSString *kARDAppEngineClientErrorDomain = @"ARDAppEngineClient"; |
| 46 | static NSInteger kARDAppEngineClientErrorBadResponse = -1; |
| 47 | |
| 48 | @implementation ARDAppEngineClient |
| 49 | |
| 50 | #pragma mark - ARDRoomServerClient |
| 51 | |
| 52 | - (void)registerForRoomId:(NSString *)roomId |
| 53 | completionHandler:(void (^)(ARDRegisterResponse *response, |
| 54 | NSError *error))completionHandler { |
| 55 | NSParameterAssert(roomId.length); |
| 56 | |
| 57 | NSString *urlString = |
| 58 | [NSString stringWithFormat:kARDRoomServerRegisterFormat, roomId]; |
| 59 | NSURL *roomURL = [NSURL URLWithString:urlString]; |
| 60 | NSLog(@"Registering with room server."); |
| 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 | } |
| 75 | ARDRegisterResponse *registerResponse = |
| 76 | [ARDRegisterResponse responseFromJSONData:data]; |
| 77 | if (!registerResponse) { |
| 78 | if (completionHandler) { |
| 79 | NSError *error = [[self class] badResponseError]; |
| 80 | completionHandler(nil, error); |
| 81 | } |
| 82 | return; |
| 83 | } |
| 84 | if (completionHandler) { |
| 85 | completionHandler(registerResponse, nil); |
| 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 | |
| 135 | - (void)deregisterForRoomId:(NSString *)roomId |
| 136 | clientId:(NSString *)clientId |
| 137 | completionHandler:(void (^)(NSError *error))completionHandler { |
| 138 | NSParameterAssert(roomId.length); |
| 139 | NSParameterAssert(clientId.length); |
| 140 | |
| 141 | NSString *urlString = |
| 142 | [NSString stringWithFormat:kARDRoomServerByeFormat, roomId, clientId]; |
| 143 | NSURL *url = [NSURL URLWithString:urlString]; |
| 144 | NSURLRequest *request = [NSURLRequest requestWithURL:url]; |
| 145 | NSURLResponse *response = nil; |
| 146 | NSError *error = nil; |
| 147 | // We want a synchronous request so that we know that we're unregistered from |
| 148 | // room server before we do any further unregistration. |
| 149 | NSLog(@"C->RS: BYE"); |
| 150 | [NSURLConnection sendSynchronousRequest:request |
| 151 | returningResponse:&response |
| 152 | error:&error]; |
| 153 | if (error) { |
| 154 | NSLog(@"Error unregistering from room server: %@", error); |
| 155 | if (completionHandler) { |
| 156 | completionHandler(error); |
| 157 | } |
| 158 | return; |
| 159 | } |
| 160 | NSLog(@"Unregistered from room server."); |
| 161 | if (completionHandler) { |
| 162 | completionHandler(nil); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | #pragma mark - Private |
| 167 | |
| 168 | + (NSError *)badResponseError { |
| 169 | NSError *error = |
| 170 | [[NSError alloc] initWithDomain:kARDAppEngineClientErrorDomain |
| 171 | code:kARDAppEngineClientErrorBadResponse |
| 172 | userInfo:@{ |
| 173 | NSLocalizedDescriptionKey: @"Error parsing response.", |
| 174 | }]; |
| 175 | return error; |
| 176 | } |
| 177 | |
| 178 | @end |