Cleaning up websocket connection open/close in favor of session open/close

This commit is contained in:
Joakim Erdfelt 2014-02-13 16:22:55 -07:00
parent 18e19260ff
commit cff7c9dbcb
3 changed files with 6 additions and 38 deletions

View File

@ -99,7 +99,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
client = new WebSocketClient(executor); client = new WebSocketClient(executor);
client.setEventDriverFactory(new JsrEventDriverFactory(client.getPolicy())); client.setEventDriverFactory(new JsrEventDriverFactory(client.getPolicy()));
client.setSessionFactory(new JsrSessionFactory(this,this)); client.setSessionFactory(new JsrSessionFactory(this,this,client));
addBean(client); addBean(client);
ShutdownThread.register(this); ShutdownThread.register(this);

View File

@ -31,18 +31,15 @@ import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection;
public class WebSocketServerConnection extends AbstractWebSocketConnection public class WebSocketServerConnection extends AbstractWebSocketConnection
{ {
private final WebSocketServerFactory factory;
private final AtomicBoolean opened = new AtomicBoolean(false); private final AtomicBoolean opened = new AtomicBoolean(false);
public WebSocketServerConnection(EndPoint endp, Executor executor, Scheduler scheduler, WebSocketPolicy policy, ByteBufferPool bufferPool, public WebSocketServerConnection(EndPoint endp, Executor executor, Scheduler scheduler, WebSocketPolicy policy, ByteBufferPool bufferPool)
WebSocketServerFactory factory)
{ {
super(endp,executor,scheduler,policy,bufferPool); super(endp,executor,scheduler,policy,bufferPool);
if (policy.getIdleTimeout() > 0) if (policy.getIdleTimeout() > 0)
{ {
endp.setIdleTimeout(policy.getIdleTimeout()); endp.setIdleTimeout(policy.getIdleTimeout());
} }
this.factory = factory;
} }
@Override @Override
@ -57,20 +54,13 @@ public class WebSocketServerConnection extends AbstractWebSocketConnection
return getEndPoint().getRemoteAddress(); return getEndPoint().getRemoteAddress();
} }
@Override
public void onClose()
{
super.onClose();
factory.sessionClosed(getSession());
}
@Override @Override
public void onOpen() public void onOpen()
{ {
boolean beenOpened = opened.getAndSet(true); boolean beenOpened = opened.getAndSet(true);
if (!beenOpened) if (!beenOpened)
{ {
factory.sessionOpened(getSession()); getSession().open();
} }
super.onOpen(); super.onOpen();
} }

View File

@ -91,7 +91,6 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
* Have the factory maintain 1 and only 1 scheduler. All connections share this scheduler. * Have the factory maintain 1 and only 1 scheduler. All connections share this scheduler.
*/ */
private final Scheduler scheduler = new ScheduledExecutorScheduler(); private final Scheduler scheduler = new ScheduledExecutorScheduler();
private final Queue<WebSocketSession> sessions = new ConcurrentLinkedQueue<>();
private final String supportedVersions; private final String supportedVersions;
private final WebSocketPolicy defaultPolicy; private final WebSocketPolicy defaultPolicy;
private final EventDriverFactory eventDriverFactory; private final EventDriverFactory eventDriverFactory;
@ -224,11 +223,11 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
protected void closeAllConnections() protected void closeAllConnections()
{ {
for (WebSocketSession session : sessions) for (WebSocketSession session : openSessions)
{ {
session.close(); session.close();
} }
sessions.clear(); openSessions.clear();
} }
@Override @Override
@ -423,27 +422,6 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
registeredSocketClasses.add(websocketPojo); registeredSocketClasses.add(websocketPojo);
} }
public boolean sessionClosed(WebSocketSession session)
{
return isRunning() && sessions.remove(session);
}
public boolean sessionOpened(WebSocketSession session)
{
if (LOG.isDebugEnabled())
{
LOG.debug("Session Opened: {}",session);
}
if (!isRunning())
{
LOG.warn("Factory is not running");
return false;
}
boolean ret = sessions.offer(session);
session.open();
return ret;
}
@Override @Override
public void setCreator(WebSocketCreator creator) public void setCreator(WebSocketCreator creator)
{ {
@ -536,7 +514,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
EndPoint endp = http.getEndPoint(); EndPoint endp = http.getEndPoint();
Executor executor = http.getConnector().getExecutor(); Executor executor = http.getConnector().getExecutor();
ByteBufferPool bufferPool = http.getConnector().getByteBufferPool(); ByteBufferPool bufferPool = http.getConnector().getByteBufferPool();
WebSocketServerConnection wsConnection = new WebSocketServerConnection(endp,executor,scheduler,driver.getPolicy(),bufferPool,this); WebSocketServerConnection wsConnection = new WebSocketServerConnection(endp,executor,scheduler,driver.getPolicy(),bufferPool);
connection = wsConnection; connection = wsConnection;
extensionStack.setPolicy(driver.getPolicy()); extensionStack.setPolicy(driver.getPolicy());