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