Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Side by Side Diff: talk/examples/objc/AppRTCDemo/ARDAppEngineClient.m

Issue 1235563006: Move talk/examples/* to webrtc/examples. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: 201507141427 Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "ARDJoinResponse.h"
31 #import "ARDLogging.h"
32 #import "ARDMessageResponse.h"
33 #import "ARDSignalingMessage.h"
34 #import "ARDUtilities.h"
35
36 // TODO(tkchin): move these to a configuration object.
37 static NSString * const kARDRoomServerHostUrl =
38 @"https://apprtc.appspot.com";
39 static NSString * const kARDRoomServerJoinFormat =
40 @"https://apprtc.appspot.com/join/%@";
41 static NSString * const kARDRoomServerMessageFormat =
42 @"https://apprtc.appspot.com/message/%@/%@";
43 static NSString * const kARDRoomServerLeaveFormat =
44 @"https://apprtc.appspot.com/leave/%@/%@";
45
46 static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient";
47 static NSInteger const kARDAppEngineClientErrorBadResponse = -1;
48
49 @implementation ARDAppEngineClient
50
51 #pragma mark - ARDRoomServerClient
52
53 - (void)joinRoomWithRoomId:(NSString *)roomId
54 completionHandler:(void (^)(ARDJoinResponse *response,
55 NSError *error))completionHandler {
56 NSParameterAssert(roomId.length);
57
58 NSString *urlString =
59 [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId];
60 NSURL *roomURL = [NSURL URLWithString:urlString];
61 ARDLog(@"Joining room:%@ on room server.", roomId);
62 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 }
76 ARDJoinResponse *joinResponse =
77 [ARDJoinResponse responseFromJSONData:data];
78 if (!joinResponse) {
79 if (completionHandler) {
80 NSError *error = [[self class] badResponseError];
81 completionHandler(nil, error);
82 }
83 return;
84 }
85 if (completionHandler) {
86 completionHandler(joinResponse, nil);
87 }
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];
105 ARDLog(@"C->RS POST: %@", message);
106 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
136 - (void)leaveRoomWithRoomId:(NSString *)roomId
137 clientId:(NSString *)clientId
138 completionHandler:(void (^)(NSError *error))completionHandler {
139 NSParameterAssert(roomId.length);
140 NSParameterAssert(clientId.length);
141
142 NSString *urlString =
143 [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId];
144 NSURL *url = [NSURL URLWithString:urlString];
145 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
146 request.HTTPMethod = @"POST";
147 NSURLResponse *response = nil;
148 NSError *error = nil;
149 // 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.
151 ARDLog(@"C->RS: BYE");
152 [NSURLConnection sendSynchronousRequest:request
153 returningResponse:&response
154 error:&error];
155 if (error) {
156 ARDLog(@"Error leaving room %@ on room server: %@",
157 roomId, error.localizedDescription);
158 if (completionHandler) {
159 completionHandler(error);
160 }
161 return;
162 }
163 ARDLog(@"Left room:%@ on room server.", roomId);
164 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698