blob: 55c467e8762ed5233322ee75f983810532cec326 [file] [log] [blame]
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00001/*
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
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000028#import "ARDRegisterResponse+Internal.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000029
30#import "ARDSignalingMessage.h"
31#import "ARDUtilities.h"
32#import "RTCICEServer+JSON.h"
33
34static NSString const *kARDRegisterResultKey = @"result";
35static NSString const *kARDRegisterResultParamsKey = @"params";
36static NSString const *kARDRegisterInitiatorKey = @"is_initiator";
37static NSString const *kARDRegisterRoomIdKey = @"room_id";
38static NSString const *kARDRegisterClientIdKey = @"client_id";
39static NSString const *kARDRegisterMessagesKey = @"messages";
40static NSString const *kARDRegisterWebSocketURLKey = @"wss_url";
41static NSString const *kARDRegisterWebSocketRestURLKey = @"wss_post_url";
42
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000043@implementation ARDRegisterResponse
44
45@synthesize result = _result;
46@synthesize isInitiator = _isInitiator;
47@synthesize roomId = _roomId;
48@synthesize clientId = _clientId;
49@synthesize messages = _messages;
50@synthesize webSocketURL = _webSocketURL;
51@synthesize webSocketRestURL = _webSocketRestURL;
52
53+ (ARDRegisterResponse *)responseFromJSONData:(NSData *)data {
54 NSDictionary *responseJSON = [NSDictionary dictionaryWithJSONData:data];
55 if (!responseJSON) {
56 return nil;
57 }
58 ARDRegisterResponse *response = [[ARDRegisterResponse alloc] init];
59 NSString *resultString = responseJSON[kARDRegisterResultKey];
60 response.result = [[self class] resultTypeFromString:resultString];
61 NSDictionary *params = responseJSON[kARDRegisterResultParamsKey];
62
63 response.isInitiator = [params[kARDRegisterInitiatorKey] boolValue];
64 response.roomId = params[kARDRegisterRoomIdKey];
65 response.clientId = params[kARDRegisterClientIdKey];
66
67 // Parse messages.
68 NSArray *messages = params[kARDRegisterMessagesKey];
69 NSMutableArray *signalingMessages =
70 [NSMutableArray arrayWithCapacity:messages.count];
71 for (NSString *message in messages) {
72 ARDSignalingMessage *signalingMessage =
73 [ARDSignalingMessage messageFromJSONString:message];
74 [signalingMessages addObject:signalingMessage];
75 }
76 response.messages = signalingMessages;
77
78 // Parse websocket urls.
79 NSString *webSocketURLString = params[kARDRegisterWebSocketURLKey];
80 response.webSocketURL = [NSURL URLWithString:webSocketURLString];
81 NSString *webSocketRestURLString = params[kARDRegisterWebSocketRestURLKey];
82 response.webSocketRestURL = [NSURL URLWithString:webSocketRestURLString];
83
84 return response;
85}
86
87#pragma mark - Private
88
89+ (ARDRegisterResultType)resultTypeFromString:(NSString *)resultString {
90 ARDRegisterResultType result = kARDRegisterResultTypeUnknown;
91 if ([resultString isEqualToString:@"SUCCESS"]) {
92 result = kARDRegisterResultTypeSuccess;
93 } else if ([resultString isEqualToString:@"FULL"]) {
94 result = kARDRegisterResultTypeFull;
95 }
96 return result;
97}
98
99@end