tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2014 Google Inc. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 28 | #import "ARDJoinResponse+Internal.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 29 | |
| 30 | #import "ARDSignalingMessage.h" |
| 31 | #import "ARDUtilities.h" |
| 32 | #import "RTCICEServer+JSON.h" |
| 33 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 34 | static NSString const *kARDJoinResultKey = @"result"; |
| 35 | static NSString const *kARDJoinResultParamsKey = @"params"; |
| 36 | static NSString const *kARDJoinInitiatorKey = @"is_initiator"; |
| 37 | static NSString const *kARDJoinRoomIdKey = @"room_id"; |
| 38 | static NSString const *kARDJoinClientIdKey = @"client_id"; |
| 39 | static NSString const *kARDJoinMessagesKey = @"messages"; |
| 40 | static NSString const *kARDJoinWebSocketURLKey = @"wss_url"; |
| 41 | static NSString const *kARDJoinWebSocketRestURLKey = @"wss_post_url"; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 42 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 43 | @implementation ARDJoinResponse |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 53 | + (ARDJoinResponse *)responseFromJSONData:(NSData *)data { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 54 | NSDictionary *responseJSON = [NSDictionary dictionaryWithJSONData:data]; |
| 55 | if (!responseJSON) { |
| 56 | return nil; |
| 57 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 58 | ARDJoinResponse *response = [[ARDJoinResponse alloc] init]; |
| 59 | NSString *resultString = responseJSON[kARDJoinResultKey]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 60 | response.result = [[self class] resultTypeFromString:resultString]; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 61 | NSDictionary *params = responseJSON[kARDJoinResultParamsKey]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 62 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 63 | response.isInitiator = [params[kARDJoinInitiatorKey] boolValue]; |
| 64 | response.roomId = params[kARDJoinRoomIdKey]; |
| 65 | response.clientId = params[kARDJoinClientIdKey]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 66 | |
| 67 | // Parse messages. |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 68 | NSArray *messages = params[kARDJoinMessagesKey]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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. |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 79 | NSString *webSocketURLString = params[kARDJoinWebSocketURLKey]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 80 | response.webSocketURL = [NSURL URLWithString:webSocketURLString]; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 81 | NSString *webSocketRestURLString = params[kARDJoinWebSocketRestURLKey]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 82 | response.webSocketRestURL = [NSURL URLWithString:webSocketRestURLString]; |
| 83 | |
| 84 | return response; |
| 85 | } |
| 86 | |
| 87 | #pragma mark - Private |
| 88 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 89 | + (ARDJoinResultType)resultTypeFromString:(NSString *)resultString { |
| 90 | ARDJoinResultType result = kARDJoinResultTypeUnknown; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 91 | if ([resultString isEqualToString:@"SUCCESS"]) { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 92 | result = kARDJoinResultTypeSuccess; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 93 | } else if ([resultString isEqualToString:@"FULL"]) { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame^] | 94 | result = kARDJoinResultTypeFull; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 95 | } |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | @end |