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 | |
| 28 | #import "ARDWebSocketChannel.h" |
| 29 | |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 30 | #import "ARDLogging.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 31 | #import "ARDUtilities.h" |
| 32 | #import "SRWebSocket.h" |
| 33 | |
| 34 | // TODO(tkchin): move these to a configuration object. |
| 35 | static NSString const *kARDWSSMessageErrorKey = @"error"; |
| 36 | static NSString const *kARDWSSMessagePayloadKey = @"msg"; |
| 37 | |
| 38 | @interface ARDWebSocketChannel () <SRWebSocketDelegate> |
| 39 | @end |
| 40 | |
| 41 | @implementation ARDWebSocketChannel { |
| 42 | NSURL *_url; |
| 43 | NSURL *_restURL; |
| 44 | SRWebSocket *_socket; |
| 45 | } |
| 46 | |
| 47 | @synthesize delegate = _delegate; |
| 48 | @synthesize state = _state; |
| 49 | @synthesize roomId = _roomId; |
| 50 | @synthesize clientId = _clientId; |
| 51 | |
| 52 | - (instancetype)initWithURL:(NSURL *)url |
| 53 | restURL:(NSURL *)restURL |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 54 | delegate:(id<ARDSignalingChannelDelegate>)delegate { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 55 | if (self = [super init]) { |
| 56 | _url = url; |
| 57 | _restURL = restURL; |
| 58 | _delegate = delegate; |
| 59 | _socket = [[SRWebSocket alloc] initWithURL:url]; |
| 60 | _socket.delegate = self; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 61 | ARDLog(@"Opening WebSocket."); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 62 | [_socket open]; |
| 63 | } |
| 64 | return self; |
| 65 | } |
| 66 | |
| 67 | - (void)dealloc { |
| 68 | [self disconnect]; |
| 69 | } |
| 70 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 71 | - (void)setState:(ARDSignalingChannelState)state { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 72 | if (_state == state) { |
| 73 | return; |
| 74 | } |
| 75 | _state = state; |
| 76 | [_delegate channel:self didChangeState:_state]; |
| 77 | } |
| 78 | |
| 79 | - (void)registerForRoomId:(NSString *)roomId |
| 80 | clientId:(NSString *)clientId { |
| 81 | NSParameterAssert(roomId.length); |
| 82 | NSParameterAssert(clientId.length); |
| 83 | _roomId = roomId; |
| 84 | _clientId = clientId; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 85 | if (_state == kARDSignalingChannelStateOpen) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 86 | [self registerWithCollider]; |
| 87 | } |
| 88 | } |
| 89 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 90 | - (void)sendMessage:(ARDSignalingMessage *)message { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 91 | NSParameterAssert(_clientId.length); |
| 92 | NSParameterAssert(_roomId.length); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 93 | NSData *data = [message JSONData]; |
| 94 | if (_state == kARDSignalingChannelStateRegistered) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 95 | NSString *payload = |
| 96 | [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
| 97 | NSDictionary *message = @{ |
| 98 | @"cmd": @"send", |
| 99 | @"msg": payload, |
| 100 | }; |
| 101 | NSData *messageJSONObject = |
| 102 | [NSJSONSerialization dataWithJSONObject:message |
| 103 | options:NSJSONWritingPrettyPrinted |
| 104 | error:nil]; |
| 105 | NSString *messageString = |
| 106 | [[NSString alloc] initWithData:messageJSONObject |
| 107 | encoding:NSUTF8StringEncoding]; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 108 | ARDLog(@"C->WSS: %@", messageString); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 109 | [_socket send:messageString]; |
| 110 | } else { |
| 111 | NSString *dataString = |
| 112 | [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 113 | ARDLog(@"C->WSS POST: %@", dataString); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 114 | NSString *urlString = |
| 115 | [NSString stringWithFormat:@"%@/%@/%@", |
| 116 | [_restURL absoluteString], _roomId, _clientId]; |
| 117 | NSURL *url = [NSURL URLWithString:urlString]; |
| 118 | [NSURLConnection sendAsyncPostToURL:url |
| 119 | withData:data |
| 120 | completionHandler:nil]; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | - (void)disconnect { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 125 | if (_state == kARDSignalingChannelStateClosed || |
| 126 | _state == kARDSignalingChannelStateError) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | [_socket close]; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 130 | ARDLog(@"C->WSS DELETE rid:%@ cid:%@", _roomId, _clientId); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 131 | NSString *urlString = |
| 132 | [NSString stringWithFormat:@"%@/%@/%@", |
| 133 | [_restURL absoluteString], _roomId, _clientId]; |
| 134 | NSURL *url = [NSURL URLWithString:urlString]; |
| 135 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
| 136 | request.HTTPMethod = @"DELETE"; |
| 137 | request.HTTPBody = nil; |
| 138 | [NSURLConnection sendAsyncRequest:request completionHandler:nil]; |
| 139 | } |
| 140 | |
| 141 | #pragma mark - SRWebSocketDelegate |
| 142 | |
| 143 | - (void)webSocketDidOpen:(SRWebSocket *)webSocket { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 144 | ARDLog(@"WebSocket connection opened."); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 145 | self.state = kARDSignalingChannelStateOpen; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 146 | if (_roomId.length && _clientId.length) { |
| 147 | [self registerWithCollider]; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { |
| 152 | NSString *messageString = message; |
| 153 | NSData *messageData = [messageString dataUsingEncoding:NSUTF8StringEncoding]; |
| 154 | id jsonObject = [NSJSONSerialization JSONObjectWithData:messageData |
| 155 | options:0 |
| 156 | error:nil]; |
| 157 | if (![jsonObject isKindOfClass:[NSDictionary class]]) { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 158 | ARDLog(@"Unexpected message: %@", jsonObject); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | NSDictionary *wssMessage = jsonObject; |
| 162 | NSString *errorString = wssMessage[kARDWSSMessageErrorKey]; |
| 163 | if (errorString.length) { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 164 | ARDLog(@"WSS error: %@", errorString); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | NSString *payload = wssMessage[kARDWSSMessagePayloadKey]; |
| 168 | ARDSignalingMessage *signalingMessage = |
| 169 | [ARDSignalingMessage messageFromJSONString:payload]; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 170 | ARDLog(@"WSS->C: %@", payload); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 171 | [_delegate channel:self didReceiveMessage:signalingMessage]; |
| 172 | } |
| 173 | |
| 174 | - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 175 | ARDLog(@"WebSocket error: %@", error); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 176 | self.state = kARDSignalingChannelStateError; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | - (void)webSocket:(SRWebSocket *)webSocket |
| 180 | didCloseWithCode:(NSInteger)code |
| 181 | reason:(NSString *)reason |
| 182 | wasClean:(BOOL)wasClean { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 183 | ARDLog(@"WebSocket closed with code: %ld reason:%@ wasClean:%d", |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 184 | (long)code, reason, wasClean); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 185 | NSParameterAssert(_state != kARDSignalingChannelStateError); |
| 186 | self.state = kARDSignalingChannelStateClosed; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | #pragma mark - Private |
| 190 | |
| 191 | - (void)registerWithCollider { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 192 | if (_state == kARDSignalingChannelStateRegistered) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | NSParameterAssert(_roomId.length); |
| 196 | NSParameterAssert(_clientId.length); |
| 197 | NSDictionary *registerMessage = @{ |
| 198 | @"cmd": @"register", |
| 199 | @"roomid" : _roomId, |
| 200 | @"clientid" : _clientId, |
| 201 | }; |
| 202 | NSData *message = |
| 203 | [NSJSONSerialization dataWithJSONObject:registerMessage |
| 204 | options:NSJSONWritingPrettyPrinted |
| 205 | error:nil]; |
| 206 | NSString *messageString = |
| 207 | [[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding]; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 208 | ARDLog(@"Registering on WSS for rid:%@ cid:%@", _roomId, _clientId); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 209 | // Registration can fail if server rejects it. For example, if the room is |
| 210 | // full. |
| 211 | [_socket send:messageString]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 212 | self.state = kARDSignalingChannelStateRegistered; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @end |