blob: 5c15955c4af8cb44ace4f9c291344b5eeb39dc64 [file] [log] [blame]
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2014 Google Inc.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +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
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000028#import "ARDJoinResponse+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
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000034static NSString const *kARDJoinResultKey = @"result";
35static NSString const *kARDJoinResultParamsKey = @"params";
36static NSString const *kARDJoinInitiatorKey = @"is_initiator";
37static NSString const *kARDJoinRoomIdKey = @"room_id";
38static NSString const *kARDJoinClientIdKey = @"client_id";
39static NSString const *kARDJoinMessagesKey = @"messages";
40static NSString const *kARDJoinWebSocketURLKey = @"wss_url";
41static NSString const *kARDJoinWebSocketRestURLKey = @"wss_post_url";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000042
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000043@implementation ARDJoinResponse
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000044
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
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000053+ (ARDJoinResponse *)responseFromJSONData:(NSData *)data {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000054 NSDictionary *responseJSON = [NSDictionary dictionaryWithJSONData:data];
55 if (!responseJSON) {
56 return nil;
57 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000058 ARDJoinResponse *response = [[ARDJoinResponse alloc] init];
59 NSString *resultString = responseJSON[kARDJoinResultKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000060 response.result = [[self class] resultTypeFromString:resultString];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000061 NSDictionary *params = responseJSON[kARDJoinResultParamsKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000062
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000063 response.isInitiator = [params[kARDJoinInitiatorKey] boolValue];
64 response.roomId = params[kARDJoinRoomIdKey];
65 response.clientId = params[kARDJoinClientIdKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000066
67 // Parse messages.
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000068 NSArray *messages = params[kARDJoinMessagesKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000069 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.
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000079 NSString *webSocketURLString = params[kARDJoinWebSocketURLKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000080 response.webSocketURL = [NSURL URLWithString:webSocketURLString];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000081 NSString *webSocketRestURLString = params[kARDJoinWebSocketRestURLKey];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000082 response.webSocketRestURL = [NSURL URLWithString:webSocketRestURLString];
83
84 return response;
85}
86
87#pragma mark - Private
88
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000089+ (ARDJoinResultType)resultTypeFromString:(NSString *)resultString {
90 ARDJoinResultType result = kARDJoinResultTypeUnknown;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000091 if ([resultString isEqualToString:@"SUCCESS"]) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000092 result = kARDJoinResultTypeSuccess;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000093 } else if ([resultString isEqualToString:@"FULL"]) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000094 result = kARDJoinResultTypeFull;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000095 }
96 return result;
97}
98
99@end