blob: 760a1715034dd9fc86f9b171215ba3ba123d17df [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"
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000031#import "ARDMessageResponse.h"
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000032#import "ARDSignalingMessage.h"
33#import "ARDUtilities.h"
34
35// TODO(tkchin): move these to a configuration object.
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000036static NSString * const kARDRoomServerHostUrl =
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000037 @"https://apprtc.appspot.com";
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000038static NSString * const kARDRoomServerJoinFormat =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000039 @"https://apprtc.appspot.com/join/%@";
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000040static NSString * const kARDRoomServerMessageFormat =
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000041 @"https://apprtc.appspot.com/message/%@/%@";
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000042static NSString * const kARDRoomServerLeaveFormat =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000043 @"https://apprtc.appspot.com/leave/%@/%@";
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000044
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000045static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient";
46static NSInteger const kARDAppEngineClientErrorBadResponse = -1;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000047
48@implementation ARDAppEngineClient
49
50#pragma mark - ARDRoomServerClient
51
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000052- (void)joinRoomWithRoomId:(NSString *)roomId
53 completionHandler:(void (^)(ARDJoinResponse *response,
54 NSError *error))completionHandler {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000055 NSParameterAssert(roomId.length);
56
57 NSString *urlString =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000058 [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000059 NSURL *roomURL = [NSURL URLWithString:urlString];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000060 NSLog(@"Joining room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000061 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 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000075 ARDJoinResponse *joinResponse =
76 [ARDJoinResponse responseFromJSONData:data];
77 if (!joinResponse) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000078 if (completionHandler) {
79 NSError *error = [[self class] badResponseError];
80 completionHandler(nil, error);
81 }
82 return;
83 }
84 if (completionHandler) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000085 completionHandler(joinResponse, nil);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000086 }
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
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000135- (void)leaveRoomWithRoomId:(NSString *)roomId
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000136 clientId:(NSString *)clientId
137 completionHandler:(void (^)(NSError *error))completionHandler {
138 NSParameterAssert(roomId.length);
139 NSParameterAssert(clientId.length);
140
141 NSString *urlString =
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000142 [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000143 NSURL *url = [NSURL URLWithString:urlString];
Chuck Hayscaae5d42015-03-25 12:59:52 -0700144 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
145 request.HTTPMethod = @"POST";
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000146 NSURLResponse *response = nil;
147 NSError *error = nil;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000148 // We want a synchronous request so that we know that we've left the room on
149 // room server before we do any further work.
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000150 NSLog(@"C->RS: BYE");
151 [NSURLConnection sendSynchronousRequest:request
152 returningResponse:&response
153 error:&error];
154 if (error) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000155 NSLog(@"Error leaving room %@ on room server: %@",
156 roomId, error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000157 if (completionHandler) {
158 completionHandler(error);
159 }
160 return;
161 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000162 NSLog(@"Left room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000163 if (completionHandler) {
164 completionHandler(nil);
165 }
166}
167
168#pragma mark - Private
169
170+ (NSError *)badResponseError {
171 NSError *error =
172 [[NSError alloc] initWithDomain:kARDAppEngineClientErrorDomain
173 code:kARDAppEngineClientErrorBadResponse
174 userInfo:@{
175 NSLocalizedDescriptionKey: @"Error parsing response.",
176 }];
177 return error;
178}
179
180@end