blob: 0b33325c893030ef917c9347b34f63ae3a34ce13 [file] [log] [blame]
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2014 Google Inc.
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +00004 *
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.org36401ab2015-01-27 21:34:39 +000030#import "ARDJoinResponse.h"
Zeke Chin2d3b7e22015-07-14 12:55:44 -070031#import "ARDLogging.h"
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000032#import "ARDMessageResponse.h"
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000033#import "ARDSignalingMessage.h"
34#import "ARDUtilities.h"
35
36// TODO(tkchin): move these to a configuration object.
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000037static NSString * const kARDRoomServerHostUrl =
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000038 @"https://apprtc.appspot.com";
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000039static NSString * const kARDRoomServerJoinFormat =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000040 @"https://apprtc.appspot.com/join/%@";
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000041static NSString * const kARDRoomServerMessageFormat =
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000042 @"https://apprtc.appspot.com/message/%@/%@";
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000043static NSString * const kARDRoomServerLeaveFormat =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000044 @"https://apprtc.appspot.com/leave/%@/%@";
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000045
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000046static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient";
47static NSInteger const kARDAppEngineClientErrorBadResponse = -1;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000048
49@implementation ARDAppEngineClient
50
51#pragma mark - ARDRoomServerClient
52
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000053- (void)joinRoomWithRoomId:(NSString *)roomId
54 completionHandler:(void (^)(ARDJoinResponse *response,
55 NSError *error))completionHandler {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000056 NSParameterAssert(roomId.length);
57
58 NSString *urlString =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000059 [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000060 NSURL *roomURL = [NSURL URLWithString:urlString];
Zeke Chin2d3b7e22015-07-14 12:55:44 -070061 ARDLog(@"Joining room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000062 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:roomURL];
63 request.HTTPMethod = @"POST";
64 __weak ARDAppEngineClient *weakSelf = self;
65 [NSURLConnection sendAsyncRequest:request
66 completionHandler:^(NSURLResponse *response,
67 NSData *data,
68 NSError *error) {
69 ARDAppEngineClient *strongSelf = weakSelf;
70 if (error) {
71 if (completionHandler) {
72 completionHandler(nil, error);
73 }
74 return;
75 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000076 ARDJoinResponse *joinResponse =
77 [ARDJoinResponse responseFromJSONData:data];
78 if (!joinResponse) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000079 if (completionHandler) {
80 NSError *error = [[self class] badResponseError];
81 completionHandler(nil, error);
82 }
83 return;
84 }
85 if (completionHandler) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000086 completionHandler(joinResponse, nil);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000087 }
88 }];
89}
90
91- (void)sendMessage:(ARDSignalingMessage *)message
92 forRoomId:(NSString *)roomId
93 clientId:(NSString *)clientId
94 completionHandler:(void (^)(ARDMessageResponse *response,
95 NSError *error))completionHandler {
96 NSParameterAssert(message);
97 NSParameterAssert(roomId.length);
98 NSParameterAssert(clientId.length);
99
100 NSData *data = [message JSONData];
101 NSString *urlString =
102 [NSString stringWithFormat:
103 kARDRoomServerMessageFormat, roomId, clientId];
104 NSURL *url = [NSURL URLWithString:urlString];
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700105 ARDLog(@"C->RS POST: %@", message);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000106 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
107 request.HTTPMethod = @"POST";
108 request.HTTPBody = data;
109 __weak ARDAppEngineClient *weakSelf = self;
110 [NSURLConnection sendAsyncRequest:request
111 completionHandler:^(NSURLResponse *response,
112 NSData *data,
113 NSError *error) {
114 ARDAppEngineClient *strongSelf = weakSelf;
115 if (error) {
116 if (completionHandler) {
117 completionHandler(nil, error);
118 }
119 return;
120 }
121 ARDMessageResponse *messageResponse =
122 [ARDMessageResponse responseFromJSONData:data];
123 if (!messageResponse) {
124 if (completionHandler) {
125 NSError *error = [[self class] badResponseError];
126 completionHandler(nil, error);
127 }
128 return;
129 }
130 if (completionHandler) {
131 completionHandler(messageResponse, nil);
132 }
133 }];
134}
135
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000136- (void)leaveRoomWithRoomId:(NSString *)roomId
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000137 clientId:(NSString *)clientId
138 completionHandler:(void (^)(NSError *error))completionHandler {
139 NSParameterAssert(roomId.length);
140 NSParameterAssert(clientId.length);
141
142 NSString *urlString =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000143 [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000144 NSURL *url = [NSURL URLWithString:urlString];
Chuck Hayscaae5d42015-03-25 12:59:52 -0700145 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
146 request.HTTPMethod = @"POST";
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000147 NSURLResponse *response = nil;
148 NSError *error = nil;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000149 // 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.
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700151 ARDLog(@"C->RS: BYE");
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000152 [NSURLConnection sendSynchronousRequest:request
153 returningResponse:&response
154 error:&error];
155 if (error) {
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700156 ARDLog(@"Error leaving room %@ on room server: %@",
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000157 roomId, error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000158 if (completionHandler) {
159 completionHandler(error);
160 }
161 return;
162 }
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700163 ARDLog(@"Left room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000164 if (completionHandler) {
165 completionHandler(nil);
166 }
167}
168
169#pragma mark - Private
170
171+ (NSError *)badResponseError {
172 NSError *error =
173 [[NSError alloc] initWithDomain:kARDAppEngineClientErrorDomain
174 code:kARDAppEngineClientErrorBadResponse
175 userInfo:@{
176 NSLocalizedDescriptionKey: @"Error parsing response.",
177 }];
178 return error;
179}
180
181@end