OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
11 * this list of conditions and the following disclaimer in the documentation | 11 * this list of conditions and the following disclaimer in the documentation |
12 * and/or other materials provided with the distribution. | 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 | 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. | 14 * derived from this software without specific prior written permission. |
15 * | 15 * |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 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 | 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 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, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 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 | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #import "ARDWebSocketChannel.h" | 28 #import "ARDWebSocketChannel.h" |
29 | 29 |
30 #import "ARDLogging.h" | 30 #import "RTCLogging.h" |
| 31 #import "SRWebSocket.h" |
| 32 |
31 #import "ARDUtilities.h" | 33 #import "ARDUtilities.h" |
32 #import "SRWebSocket.h" | |
33 | 34 |
34 // TODO(tkchin): move these to a configuration object. | 35 // TODO(tkchin): move these to a configuration object. |
35 static NSString const *kARDWSSMessageErrorKey = @"error"; | 36 static NSString const *kARDWSSMessageErrorKey = @"error"; |
36 static NSString const *kARDWSSMessagePayloadKey = @"msg"; | 37 static NSString const *kARDWSSMessagePayloadKey = @"msg"; |
37 | 38 |
38 @interface ARDWebSocketChannel () <SRWebSocketDelegate> | 39 @interface ARDWebSocketChannel () <SRWebSocketDelegate> |
39 @end | 40 @end |
40 | 41 |
41 @implementation ARDWebSocketChannel { | 42 @implementation ARDWebSocketChannel { |
42 NSURL *_url; | 43 NSURL *_url; |
43 NSURL *_restURL; | 44 NSURL *_restURL; |
44 SRWebSocket *_socket; | 45 SRWebSocket *_socket; |
45 } | 46 } |
46 | 47 |
47 @synthesize delegate = _delegate; | 48 @synthesize delegate = _delegate; |
48 @synthesize state = _state; | 49 @synthesize state = _state; |
49 @synthesize roomId = _roomId; | 50 @synthesize roomId = _roomId; |
50 @synthesize clientId = _clientId; | 51 @synthesize clientId = _clientId; |
51 | 52 |
52 - (instancetype)initWithURL:(NSURL *)url | 53 - (instancetype)initWithURL:(NSURL *)url |
53 restURL:(NSURL *)restURL | 54 restURL:(NSURL *)restURL |
54 delegate:(id<ARDSignalingChannelDelegate>)delegate { | 55 delegate:(id<ARDSignalingChannelDelegate>)delegate { |
55 if (self = [super init]) { | 56 if (self = [super init]) { |
56 _url = url; | 57 _url = url; |
57 _restURL = restURL; | 58 _restURL = restURL; |
58 _delegate = delegate; | 59 _delegate = delegate; |
59 _socket = [[SRWebSocket alloc] initWithURL:url]; | 60 _socket = [[SRWebSocket alloc] initWithURL:url]; |
60 _socket.delegate = self; | 61 _socket.delegate = self; |
61 ARDLog(@"Opening WebSocket."); | 62 RTCLog(@"Opening WebSocket."); |
62 [_socket open]; | 63 [_socket open]; |
63 } | 64 } |
64 return self; | 65 return self; |
65 } | 66 } |
66 | 67 |
67 - (void)dealloc { | 68 - (void)dealloc { |
68 [self disconnect]; | 69 [self disconnect]; |
69 } | 70 } |
70 | 71 |
71 - (void)setState:(ARDSignalingChannelState)state { | 72 - (void)setState:(ARDSignalingChannelState)state { |
(...skipping 26 matching lines...) Expand all Loading... |
98 @"cmd": @"send", | 99 @"cmd": @"send", |
99 @"msg": payload, | 100 @"msg": payload, |
100 }; | 101 }; |
101 NSData *messageJSONObject = | 102 NSData *messageJSONObject = |
102 [NSJSONSerialization dataWithJSONObject:message | 103 [NSJSONSerialization dataWithJSONObject:message |
103 options:NSJSONWritingPrettyPrinted | 104 options:NSJSONWritingPrettyPrinted |
104 error:nil]; | 105 error:nil]; |
105 NSString *messageString = | 106 NSString *messageString = |
106 [[NSString alloc] initWithData:messageJSONObject | 107 [[NSString alloc] initWithData:messageJSONObject |
107 encoding:NSUTF8StringEncoding]; | 108 encoding:NSUTF8StringEncoding]; |
108 ARDLog(@"C->WSS: %@", messageString); | 109 RTCLog(@"C->WSS: %@", messageString); |
109 [_socket send:messageString]; | 110 [_socket send:messageString]; |
110 } else { | 111 } else { |
111 NSString *dataString = | 112 NSString *dataString = |
112 [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | 113 [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
113 ARDLog(@"C->WSS POST: %@", dataString); | 114 RTCLog(@"C->WSS POST: %@", dataString); |
114 NSString *urlString = | 115 NSString *urlString = |
115 [NSString stringWithFormat:@"%@/%@/%@", | 116 [NSString stringWithFormat:@"%@/%@/%@", |
116 [_restURL absoluteString], _roomId, _clientId]; | 117 [_restURL absoluteString], _roomId, _clientId]; |
117 NSURL *url = [NSURL URLWithString:urlString]; | 118 NSURL *url = [NSURL URLWithString:urlString]; |
118 [NSURLConnection sendAsyncPostToURL:url | 119 [NSURLConnection sendAsyncPostToURL:url |
119 withData:data | 120 withData:data |
120 completionHandler:nil]; | 121 completionHandler:nil]; |
121 } | 122 } |
122 } | 123 } |
123 | 124 |
124 - (void)disconnect { | 125 - (void)disconnect { |
125 if (_state == kARDSignalingChannelStateClosed || | 126 if (_state == kARDSignalingChannelStateClosed || |
126 _state == kARDSignalingChannelStateError) { | 127 _state == kARDSignalingChannelStateError) { |
127 return; | 128 return; |
128 } | 129 } |
129 [_socket close]; | 130 [_socket close]; |
130 ARDLog(@"C->WSS DELETE rid:%@ cid:%@", _roomId, _clientId); | 131 RTCLog(@"C->WSS DELETE rid:%@ cid:%@", _roomId, _clientId); |
131 NSString *urlString = | 132 NSString *urlString = |
132 [NSString stringWithFormat:@"%@/%@/%@", | 133 [NSString stringWithFormat:@"%@/%@/%@", |
133 [_restURL absoluteString], _roomId, _clientId]; | 134 [_restURL absoluteString], _roomId, _clientId]; |
134 NSURL *url = [NSURL URLWithString:urlString]; | 135 NSURL *url = [NSURL URLWithString:urlString]; |
135 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | 136 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
136 request.HTTPMethod = @"DELETE"; | 137 request.HTTPMethod = @"DELETE"; |
137 request.HTTPBody = nil; | 138 request.HTTPBody = nil; |
138 [NSURLConnection sendAsyncRequest:request completionHandler:nil]; | 139 [NSURLConnection sendAsyncRequest:request completionHandler:nil]; |
139 } | 140 } |
140 | 141 |
141 #pragma mark - SRWebSocketDelegate | 142 #pragma mark - SRWebSocketDelegate |
142 | 143 |
143 - (void)webSocketDidOpen:(SRWebSocket *)webSocket { | 144 - (void)webSocketDidOpen:(SRWebSocket *)webSocket { |
144 ARDLog(@"WebSocket connection opened."); | 145 RTCLog(@"WebSocket connection opened."); |
145 self.state = kARDSignalingChannelStateOpen; | 146 self.state = kARDSignalingChannelStateOpen; |
146 if (_roomId.length && _clientId.length) { | 147 if (_roomId.length && _clientId.length) { |
147 [self registerWithCollider]; | 148 [self registerWithCollider]; |
148 } | 149 } |
149 } | 150 } |
150 | 151 |
151 - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { | 152 - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { |
152 NSString *messageString = message; | 153 NSString *messageString = message; |
153 NSData *messageData = [messageString dataUsingEncoding:NSUTF8StringEncoding]; | 154 NSData *messageData = [messageString dataUsingEncoding:NSUTF8StringEncoding]; |
154 id jsonObject = [NSJSONSerialization JSONObjectWithData:messageData | 155 id jsonObject = [NSJSONSerialization JSONObjectWithData:messageData |
155 options:0 | 156 options:0 |
156 error:nil]; | 157 error:nil]; |
157 if (![jsonObject isKindOfClass:[NSDictionary class]]) { | 158 if (![jsonObject isKindOfClass:[NSDictionary class]]) { |
158 ARDLog(@"Unexpected message: %@", jsonObject); | 159 RTCLogError(@"Unexpected message: %@", jsonObject); |
159 return; | 160 return; |
160 } | 161 } |
161 NSDictionary *wssMessage = jsonObject; | 162 NSDictionary *wssMessage = jsonObject; |
162 NSString *errorString = wssMessage[kARDWSSMessageErrorKey]; | 163 NSString *errorString = wssMessage[kARDWSSMessageErrorKey]; |
163 if (errorString.length) { | 164 if (errorString.length) { |
164 ARDLog(@"WSS error: %@", errorString); | 165 RTCLogError(@"WSS error: %@", errorString); |
165 return; | 166 return; |
166 } | 167 } |
167 NSString *payload = wssMessage[kARDWSSMessagePayloadKey]; | 168 NSString *payload = wssMessage[kARDWSSMessagePayloadKey]; |
168 ARDSignalingMessage *signalingMessage = | 169 ARDSignalingMessage *signalingMessage = |
169 [ARDSignalingMessage messageFromJSONString:payload]; | 170 [ARDSignalingMessage messageFromJSONString:payload]; |
170 ARDLog(@"WSS->C: %@", payload); | 171 RTCLog(@"WSS->C: %@", payload); |
171 [_delegate channel:self didReceiveMessage:signalingMessage]; | 172 [_delegate channel:self didReceiveMessage:signalingMessage]; |
172 } | 173 } |
173 | 174 |
174 - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { | 175 - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { |
175 ARDLog(@"WebSocket error: %@", error); | 176 RTCLogError(@"WebSocket error: %@", error); |
176 self.state = kARDSignalingChannelStateError; | 177 self.state = kARDSignalingChannelStateError; |
177 } | 178 } |
178 | 179 |
179 - (void)webSocket:(SRWebSocket *)webSocket | 180 - (void)webSocket:(SRWebSocket *)webSocket |
180 didCloseWithCode:(NSInteger)code | 181 didCloseWithCode:(NSInteger)code |
181 reason:(NSString *)reason | 182 reason:(NSString *)reason |
182 wasClean:(BOOL)wasClean { | 183 wasClean:(BOOL)wasClean { |
183 ARDLog(@"WebSocket closed with code: %ld reason:%@ wasClean:%d", | 184 RTCLog(@"WebSocket closed with code: %ld reason:%@ wasClean:%d", |
184 (long)code, reason, wasClean); | 185 (long)code, reason, wasClean); |
185 NSParameterAssert(_state != kARDSignalingChannelStateError); | 186 NSParameterAssert(_state != kARDSignalingChannelStateError); |
186 self.state = kARDSignalingChannelStateClosed; | 187 self.state = kARDSignalingChannelStateClosed; |
187 } | 188 } |
188 | 189 |
189 #pragma mark - Private | 190 #pragma mark - Private |
190 | 191 |
191 - (void)registerWithCollider { | 192 - (void)registerWithCollider { |
192 if (_state == kARDSignalingChannelStateRegistered) { | 193 if (_state == kARDSignalingChannelStateRegistered) { |
193 return; | 194 return; |
194 } | 195 } |
195 NSParameterAssert(_roomId.length); | 196 NSParameterAssert(_roomId.length); |
196 NSParameterAssert(_clientId.length); | 197 NSParameterAssert(_clientId.length); |
197 NSDictionary *registerMessage = @{ | 198 NSDictionary *registerMessage = @{ |
198 @"cmd": @"register", | 199 @"cmd": @"register", |
199 @"roomid" : _roomId, | 200 @"roomid" : _roomId, |
200 @"clientid" : _clientId, | 201 @"clientid" : _clientId, |
201 }; | 202 }; |
202 NSData *message = | 203 NSData *message = |
203 [NSJSONSerialization dataWithJSONObject:registerMessage | 204 [NSJSONSerialization dataWithJSONObject:registerMessage |
204 options:NSJSONWritingPrettyPrinted | 205 options:NSJSONWritingPrettyPrinted |
205 error:nil]; | 206 error:nil]; |
206 NSString *messageString = | 207 NSString *messageString = |
207 [[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding]; | 208 [[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding]; |
208 ARDLog(@"Registering on WSS for rid:%@ cid:%@", _roomId, _clientId); | 209 RTCLog(@"Registering on WSS for rid:%@ cid:%@", _roomId, _clientId); |
209 // Registration can fail if server rejects it. For example, if the room is | 210 // Registration can fail if server rejects it. For example, if the room is |
210 // full. | 211 // full. |
211 [_socket send:messageString]; | 212 [_socket send:messageString]; |
212 self.state = kARDSignalingChannelStateRegistered; | 213 self.state = kARDSignalingChannelStateRegistered; |
213 } | 214 } |
214 | 215 |
215 @end | 216 @end |
OLD | NEW |