blob: a089ff870705ed0f778ed763d441a9e174f62bad [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
28#import "ARDWebSocketChannel.h"
29
tkchinc3f46a92015-07-23 12:50:55 -070030#import "RTCLogging.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000031#import "SRWebSocket.h"
32
tkchinc3f46a92015-07-23 12:50:55 -070033#import "ARDUtilities.h"
34
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000035// TODO(tkchin): move these to a configuration object.
36static NSString const *kARDWSSMessageErrorKey = @"error";
37static 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.org3a63a3c2015-01-06 07:21:34 +000055 delegate:(id<ARDSignalingChannelDelegate>)delegate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000056 if (self = [super init]) {
57 _url = url;
58 _restURL = restURL;
59 _delegate = delegate;
60 _socket = [[SRWebSocket alloc] initWithURL:url];
61 _socket.delegate = self;
tkchinc3f46a92015-07-23 12:50:55 -070062 RTCLog(@"Opening WebSocket.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000063 [_socket open];
64 }
65 return self;
66}
67
68- (void)dealloc {
69 [self disconnect];
70}
71
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000072- (void)setState:(ARDSignalingChannelState)state {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000073 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.org3a63a3c2015-01-06 07:21:34 +000086 if (_state == kARDSignalingChannelStateOpen) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000087 [self registerWithCollider];
88 }
89}
90
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000091- (void)sendMessage:(ARDSignalingMessage *)message {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000092 NSParameterAssert(_clientId.length);
93 NSParameterAssert(_roomId.length);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000094 NSData *data = [message JSONData];
95 if (_state == kARDSignalingChannelStateRegistered) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000096 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];
tkchinc3f46a92015-07-23 12:50:55 -0700109 RTCLog(@"C->WSS: %@", messageString);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000110 [_socket send:messageString];
111 } else {
112 NSString *dataString =
113 [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
tkchinc3f46a92015-07-23 12:50:55 -0700114 RTCLog(@"C->WSS POST: %@", dataString);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000115 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.org3a63a3c2015-01-06 07:21:34 +0000126 if (_state == kARDSignalingChannelStateClosed ||
127 _state == kARDSignalingChannelStateError) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000128 return;
129 }
130 [_socket close];
tkchinc3f46a92015-07-23 12:50:55 -0700131 RTCLog(@"C->WSS DELETE rid:%@ cid:%@", _roomId, _clientId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000132 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 {
tkchinc3f46a92015-07-23 12:50:55 -0700145 RTCLog(@"WebSocket connection opened.");
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000146 self.state = kARDSignalingChannelStateOpen;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000147 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]]) {
tkchinc3f46a92015-07-23 12:50:55 -0700159 RTCLogError(@"Unexpected message: %@", jsonObject);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000160 return;
161 }
162 NSDictionary *wssMessage = jsonObject;
163 NSString *errorString = wssMessage[kARDWSSMessageErrorKey];
164 if (errorString.length) {
tkchinc3f46a92015-07-23 12:50:55 -0700165 RTCLogError(@"WSS error: %@", errorString);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000166 return;
167 }
168 NSString *payload = wssMessage[kARDWSSMessagePayloadKey];
169 ARDSignalingMessage *signalingMessage =
170 [ARDSignalingMessage messageFromJSONString:payload];
tkchinc3f46a92015-07-23 12:50:55 -0700171 RTCLog(@"WSS->C: %@", payload);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000172 [_delegate channel:self didReceiveMessage:signalingMessage];
173}
174
175- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
tkchinc3f46a92015-07-23 12:50:55 -0700176 RTCLogError(@"WebSocket error: %@", error);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000177 self.state = kARDSignalingChannelStateError;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000178}
179
180- (void)webSocket:(SRWebSocket *)webSocket
181 didCloseWithCode:(NSInteger)code
182 reason:(NSString *)reason
183 wasClean:(BOOL)wasClean {
tkchinc3f46a92015-07-23 12:50:55 -0700184 RTCLog(@"WebSocket closed with code: %ld reason:%@ wasClean:%d",
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000185 (long)code, reason, wasClean);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000186 NSParameterAssert(_state != kARDSignalingChannelStateError);
187 self.state = kARDSignalingChannelStateClosed;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000188}
189
190#pragma mark - Private
191
192- (void)registerWithCollider {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000193 if (_state == kARDSignalingChannelStateRegistered) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000194 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];
tkchinc3f46a92015-07-23 12:50:55 -0700209 RTCLog(@"Registering on WSS for rid:%@ cid:%@", _roomId, _clientId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000210 // Registration can fail if server rejects it. For example, if the room is
211 // full.
212 [_socket send:messageString];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000213 self.state = kARDSignalingChannelStateRegistered;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000214}
215
216@end