blob: 87d58e0db1d6e2781b28e9051e18ab96aef6eefa [file] [log] [blame]
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00009 */
10
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000011#import "ARDJoinResponse+Internal.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000012
13#import "ARDSignalingMessage.h"
14#import "ARDUtilities.h"
hjon79858f82016-03-13 22:08:26 -070015#import "RTCIceServer+JSON.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000016
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000017static NSString const *kARDJoinResultKey = @"result";
18static NSString const *kARDJoinResultParamsKey = @"params";
19static NSString const *kARDJoinInitiatorKey = @"is_initiator";
20static NSString const *kARDJoinRoomIdKey = @"room_id";
21static NSString const *kARDJoinClientIdKey = @"client_id";
22static NSString const *kARDJoinMessagesKey = @"messages";
23static NSString const *kARDJoinWebSocketURLKey = @"wss_url";
24static NSString const *kARDJoinWebSocketRestURLKey = @"wss_post_url";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000025
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000026@implementation ARDJoinResponse
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000027
28@synthesize result = _result;
29@synthesize isInitiator = _isInitiator;
30@synthesize roomId = _roomId;
31@synthesize clientId = _clientId;
32@synthesize messages = _messages;
33@synthesize webSocketURL = _webSocketURL;
34@synthesize webSocketRestURL = _webSocketRestURL;
35
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000036+ (ARDJoinResponse *)responseFromJSONData:(NSData *)data {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000037 NSDictionary *responseJSON = [NSDictionary dictionaryWithJSONData:data];
38 if (!responseJSON) {
39 return nil;
40 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000041 ARDJoinResponse *response = [[ARDJoinResponse alloc] init];
42 NSString *resultString = responseJSON[kARDJoinResultKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000043 response.result = [[self class] resultTypeFromString:resultString];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000044 NSDictionary *params = responseJSON[kARDJoinResultParamsKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000045
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000046 response.isInitiator = [params[kARDJoinInitiatorKey] boolValue];
47 response.roomId = params[kARDJoinRoomIdKey];
48 response.clientId = params[kARDJoinClientIdKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000049
50 // Parse messages.
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000051 NSArray *messages = params[kARDJoinMessagesKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000052 NSMutableArray *signalingMessages =
53 [NSMutableArray arrayWithCapacity:messages.count];
54 for (NSString *message in messages) {
55 ARDSignalingMessage *signalingMessage =
56 [ARDSignalingMessage messageFromJSONString:message];
57 [signalingMessages addObject:signalingMessage];
58 }
59 response.messages = signalingMessages;
60
61 // Parse websocket urls.
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000062 NSString *webSocketURLString = params[kARDJoinWebSocketURLKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000063 response.webSocketURL = [NSURL URLWithString:webSocketURLString];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000064 NSString *webSocketRestURLString = params[kARDJoinWebSocketRestURLKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000065 response.webSocketRestURL = [NSURL URLWithString:webSocketRestURLString];
66
67 return response;
68}
69
70#pragma mark - Private
71
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000072+ (ARDJoinResultType)resultTypeFromString:(NSString *)resultString {
73 ARDJoinResultType result = kARDJoinResultTypeUnknown;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000074 if ([resultString isEqualToString:@"SUCCESS"]) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000075 result = kARDJoinResultTypeSuccess;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000076 } else if ([resultString isEqualToString:@"FULL"]) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000077 result = kARDJoinResultTypeFull;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000078 }
79 return result;
80}
81
82@end