/
ConnectionManager.java
244 lines (212 loc) · 7.1 KB
/
ConnectionManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// ========================================================================
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.client.io;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
import java.nio.channels.SocketChannel;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.common.WebSocketSession;
import org.eclipse.jetty.websocket.common.events.EventDriver;
/**
* Internal Connection/Client Manager used to track active clients, their physical vs virtual connection information, and provide some means to create new
* physical or virtual connections.
*/
public class ConnectionManager extends ContainerLifeCycle
{
private class PhysicalConnect extends ConnectPromise
{
private SocketAddress bindAddress;
public PhysicalConnect(WebSocketClient client, EventDriver driver, ClientUpgradeRequest request)
{
super(client,driver,request);
this.bindAddress = client.getBindAddress();
}
@Override
public void run()
{
SocketChannel channel = null;
try
{
channel = SocketChannel.open();
if (bindAddress != null)
{
channel.bind(bindAddress);
}
URI wsUri = getRequest().getRequestURI();
channel.socket().setTcpNoDelay(true); // disable nagle
channel.configureBlocking(false); // async always
InetSocketAddress address = toSocketAddress(wsUri);
channel.connect(address);
getSelector().connect(channel,this);
}
catch (Throwable t)
{
// close the socket channel
if (channel != null)
{
try
{
channel.close();
}
catch (IOException ignore)
{
LOG.ignore(ignore);
}
}
// notify the future
failed(t);
}
}
}
private class VirtualConnect extends ConnectPromise
{
public VirtualConnect(WebSocketClient client, EventDriver driver, ClientUpgradeRequest request)
{
super(client,driver,request);
}
@Override
public void run()
{
failed(new WebSocketException("MUX Not yet supported"));
}
}
private static final Logger LOG = Log.getLogger(ConnectionManager.class);
public static InetSocketAddress toSocketAddress(URI uri)
{
if (!uri.isAbsolute())
{
throw new IllegalArgumentException("Cannot get InetSocketAddress of non-absolute URIs");
}
int port = uri.getPort();
String scheme = uri.getScheme().toLowerCase(Locale.ENGLISH);
if ("ws".equals(scheme))
{
if (port == (-1))
{
port = 80;
}
}
else if ("wss".equals(scheme))
{
if (port == (-1))
{
port = 443;
}
}
else
{
throw new IllegalArgumentException("Only support ws:// and wss:// URIs");
}
return new InetSocketAddress(uri.getHost(),port);
}
private final Queue<WebSocketSession> sessions = new ConcurrentLinkedQueue<>();
private final WebSocketClient client;
private WebSocketClientSelectorManager selector;
public ConnectionManager(WebSocketClient client)
{
this.client = client;
}
public void addSession(WebSocketSession session)
{
sessions.add(session);
}
private void closeAllConnections()
{
for (WebSocketSession session : sessions)
{
if (session.getConnection() != null)
{
try
{
session.getConnection().close();
}
catch (Throwable t)
{
LOG.debug("During Close All Connections",t);
}
}
}
}
public ConnectPromise connect(WebSocketClient client, EventDriver driver, ClientUpgradeRequest request)
{
URI toUri = request.getRequestURI();
String hostname = toUri.getHost();
if (isVirtualConnectionPossibleTo(hostname))
{
return new VirtualConnect(client,driver,request);
}
return new PhysicalConnect(client,driver,request);
}
@Override
protected void doStart() throws Exception
{
selector = newWebSocketClientSelectorManager(client);
selector.setSslContextFactory(client.getSslContextFactory());
selector.setConnectTimeout(client.getConnectTimeout());
addBean(selector);
super.doStart();
}
@Override
protected void doStop() throws Exception
{
closeAllConnections();
sessions.clear();
super.doStop();
removeBean(selector);
}
public WebSocketClientSelectorManager getSelector()
{
return selector;
}
public Collection<WebSocketSession> getSessions()
{
return Collections.unmodifiableCollection(sessions);
}
public boolean isVirtualConnectionPossibleTo(String hostname)
{
// TODO Auto-generated method stub
return false;
}
/**
* Factory method for new WebSocketClientSelectorManager (used by other projects like cometd)
*
* @param client
* the client used to create the WebSocketClientSelectorManager
* @return the new WebSocketClientSelectorManager
*/
protected WebSocketClientSelectorManager newWebSocketClientSelectorManager(WebSocketClient client)
{
return new WebSocketClientSelectorManager(client);
}
public void removeSession(WebSocketSession session)
{
sessions.remove(session);
}
}