blob: de3f9f70461831051c20cbb7d5ba0cd3f0011b6a [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
tkchinc3f46a92015-07-23 12:50:55 -070030#import "RTCLogging.h"
31
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000032#import "ARDJoinResponse.h"
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000033#import "ARDMessageResponse.h"
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000034#import "ARDSignalingMessage.h"
35#import "ARDUtilities.h"
36
37// TODO(tkchin): move these to a configuration object.
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000038static NSString * const kARDRoomServerHostUrl =
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000039 @"https://apprtc.appspot.com";
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000040static NSString * const kARDRoomServerJoinFormat =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000041 @"https://apprtc.appspot.com/join/%@";
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000042static NSString * const kARDRoomServerMessageFormat =
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000043 @"https://apprtc.appspot.com/message/%@/%@";
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000044static NSString * const kARDRoomServerLeaveFormat =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000045 @"https://apprtc.appspot.com/leave/%@/%@";
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000046
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000047static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient";
48static NSInteger const kARDAppEngineClientErrorBadResponse = -1;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000049
50@implementation ARDAppEngineClient
51
52#pragma mark - ARDRoomServerClient
53
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000054- (void)joinRoomWithRoomId:(NSString *)roomId
55 completionHandler:(void (^)(ARDJoinResponse *response,
56 NSError *error))completionHandler {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000057 NSParameterAssert(roomId.length);
58
59 NSString *urlString =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000060 [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000061 NSURL *roomURL = [NSURL URLWithString:urlString];
tkchinc3f46a92015-07-23 12:50:55 -070062 RTCLog(@"Joining room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000063 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 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000077 ARDJoinResponse *joinResponse =
78 [ARDJoinResponse responseFromJSONData:data];
79 if (!joinResponse) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000080 if (completionHandler) {
81 NSError *error = [[self class] badResponseError];
82 completionHandler(nil, error);
83 }
84 return;
85 }
86 if (completionHandler) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000087 completionHandler(joinResponse, nil);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000088 }
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];
tkchinc3f46a92015-07-23 12:50:55 -0700106 RTCLog(@"C->RS POST: %@", message);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000107 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
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000137- (void)leaveRoomWithRoomId:(NSString *)roomId
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000138 clientId:(NSString *)clientId
139 completionHandler:(void (^)(NSError *error))completionHandler {
140 NSParameterAssert(roomId.length);
141 NSParameterAssert(clientId.length);
142
143 NSString *urlString =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000144 [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000145 NSURL *url = [NSURL URLWithString:urlString];
Chuck Hayscaae5d42015-03-25 12:59:52 -0700146 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
147 request.HTTPMethod = @"POST";
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000148 NSURLResponse *response = nil;
149 NSError *error = nil;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000150 // 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.
tkchinc3f46a92015-07-23 12:50:55 -0700152 RTCLog(@"C->RS: BYE");
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000153 [NSURLConnection sendSynchronousRequest:request
154 returningResponse:&response
155 error:&error];
156 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700157 RTCLogError(@"Error leaving room %@ on room server: %@",
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000158 roomId, error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000159 if (completionHandler) {
160 completionHandler(error);
161 }
162 return;
163 }
tkchinc3f46a92015-07-23 12:50:55 -0700164 RTCLog(@"Left room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000165 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