blob: c647ecb97f2e78039b3974ea3147a14c46cba263 [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.
36static NSString *kARDRoomServerHostUrl =
37 @"https://apprtc.appspot.com";
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000038static NSString *kARDRoomServerJoinFormat =
39 @"https://apprtc.appspot.com/join/%@";
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000040static NSString *kARDRoomServerMessageFormat =
41 @"https://apprtc.appspot.com/message/%@/%@";
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000042static NSString *kARDRoomServerLeaveFormat =
43 @"https://apprtc.appspot.com/leave/%@/%@";
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000044
45static NSString *kARDAppEngineClientErrorDomain = @"ARDAppEngineClient";
46static NSInteger kARDAppEngineClientErrorBadResponse = -1;
47
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];
144 NSURLRequest *request = [NSURLRequest requestWithURL:url];
145 NSURLResponse *response = nil;
146 NSError *error = nil;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000147 // We want a synchronous request so that we know that we've left the room on
148 // room server before we do any further work.
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000149 NSLog(@"C->RS: BYE");
150 [NSURLConnection sendSynchronousRequest:request
151 returningResponse:&response
152 error:&error];
153 if (error) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000154 NSLog(@"Error leaving room %@ on room server: %@",
155 roomId, error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000156 if (completionHandler) {
157 completionHandler(error);
158 }
159 return;
160 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000161 NSLog(@"Left room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000162 if (completionHandler) {
163 completionHandler(nil);
164 }
165}
166
167#pragma mark - Private
168
169+ (NSError *)badResponseError {
170 NSError *error =
171 [[NSError alloc] initWithDomain:kARDAppEngineClientErrorDomain
172 code:kARDAppEngineClientErrorBadResponse
173 userInfo:@{
174 NSLocalizedDescriptionKey: @"Error parsing response.",
175 }];
176 return error;
177}
178
179@end