1/*
2 *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 *
4 *  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.
9 */
10
11#import "ARDJoinResponse+Internal.h"
12
13#import "ARDSignalingMessage.h"
14#import "ARDUtilities.h"
15#import "RTCICEServer+JSON.h"
16
17static 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";
25
26@implementation ARDJoinResponse
27
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
36+ (ARDJoinResponse *)responseFromJSONData:(NSData *)data {
37  NSDictionary *responseJSON = [NSDictionary dictionaryWithJSONData:data];
38  if (!responseJSON) {
39    return nil;
40  }
41  ARDJoinResponse *response = [[ARDJoinResponse alloc] init];
42  NSString *resultString = responseJSON[kARDJoinResultKey];
43  response.result = [[self class] resultTypeFromString:resultString];
44  NSDictionary *params = responseJSON[kARDJoinResultParamsKey];
45
46  response.isInitiator = [params[kARDJoinInitiatorKey] boolValue];
47  response.roomId = params[kARDJoinRoomIdKey];
48  response.clientId = params[kARDJoinClientIdKey];
49
50  // Parse messages.
51  NSArray *messages = params[kARDJoinMessagesKey];
52  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.
62  NSString *webSocketURLString = params[kARDJoinWebSocketURLKey];
63  response.webSocketURL = [NSURL URLWithString:webSocketURLString];
64  NSString *webSocketRestURLString = params[kARDJoinWebSocketRestURLKey];
65  response.webSocketRestURL = [NSURL URLWithString:webSocketRestURLString];
66
67  return response;
68}
69
70#pragma mark - Private
71
72+ (ARDJoinResultType)resultTypeFromString:(NSString *)resultString {
73  ARDJoinResultType result = kARDJoinResultTypeUnknown;
74  if ([resultString isEqualToString:@"SUCCESS"]) {
75    result = kARDJoinResultTypeSuccess;
76  } else if ([resultString isEqualToString:@"FULL"]) {
77    result = kARDJoinResultTypeFull;
78  }
79  return result;
80}
81
82@end
83