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