use of org.atmosphere.cpr.AtmosphereResource in project atmosphere by Atmosphere.
the class AnnotationScanningTest method create.
@BeforeMethod
public void create() throws Throwable {
framework = new AtmosphereFramework();
framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
framework.addAnnotationPackage(AnnotationScanningTest.class);
framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {
@Override
public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
return suspended(req, res);
}
public void action(AtmosphereResourceImpl r) {
try {
resumed(r.getRequest(), r.getResponse());
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}
}).init().addAtmosphereHandler("/a", new AtmosphereHandlerAdapter() {
@Override
public void onRequest(AtmosphereResource resource) throws IOException {
resource.suspend();
}
});
}
use of org.atmosphere.cpr.AtmosphereResource in project atmosphere by Atmosphere.
the class DefaultWebSocketProcessor method executeClose.
public void executeClose(WebSocket webSocket, int closeCode) {
AtmosphereResource r = webSocket.resource();
boolean isClosedByClient = r == null ? true : r.getAtmosphereResourceEvent().isClosedByClient();
try {
if (r != null) {
asynchronousProcessor.endRequest(AtmosphereResourceImpl.class.cast(r), true);
}
} finally {
if (!isClosedByClient) {
notifyListener(webSocket, new WebSocketEventListener.WebSocketEvent(closeCode, CLOSE, webSocket));
}
}
}
use of org.atmosphere.cpr.AtmosphereResource in project atmosphere by Atmosphere.
the class DefaultWebSocketProcessor method notifyListener.
@Override
public void notifyListener(WebSocket webSocket, WebSocketEventListener.WebSocketEvent event) {
AtmosphereResource resource = webSocket.resource();
if (resource == null)
return;
AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(resource);
for (AtmosphereResourceEventListener l : r.atmosphereResourceEventListener()) {
if (WebSocketEventListener.class.isAssignableFrom(l.getClass())) {
try {
switch(event.type()) {
case CONNECT:
WebSocketEventListener.class.cast(l).onConnect(event);
break;
case DISCONNECT:
WebSocketEventListener.class.cast(l).onDisconnect(event);
onDisconnect(event, l);
break;
case CONTROL:
WebSocketEventListener.class.cast(l).onControl(event);
break;
case MESSAGE:
WebSocketEventListener.class.cast(l).onMessage(event);
break;
case HANDSHAKE:
WebSocketEventListener.class.cast(l).onHandshake(event);
break;
case CLOSE:
boolean isClosedByClient = r.getAtmosphereResourceEvent().isClosedByClient();
l.onDisconnect(new AtmosphereResourceEventImpl(r, !isClosedByClient, false, isClosedByClient, null));
onDisconnect(event, l);
WebSocketEventListener.class.cast(l).onClose(event);
break;
}
} catch (Throwable t) {
logger.debug("Listener error {}", t);
try {
WebSocketEventListener.class.cast(l).onThrowable(new AtmosphereResourceEventImpl(r, false, false, t));
} catch (Throwable t2) {
logger.warn("Listener error {}", t2);
}
}
} else {
switch(event.type()) {
case CLOSE:
boolean isClosedByClient = r.getAtmosphereResourceEvent().isClosedByClient();
l.onDisconnect(new AtmosphereResourceEventImpl(r, !isClosedByClient, false, isClosedByClient, null));
break;
}
}
}
}
use of org.atmosphere.cpr.AtmosphereResource in project atmosphere by Atmosphere.
the class DefaultWebSocketProcessor method close.
@Override
public void close(final WebSocket webSocket, int closeCode) {
WebSocketHandler webSocketHandler = webSocket.webSocketHandler();
// A message might be in the process of being processed and the websocket gets closed. In that corner
// case the webSocket.resource will be set to false and that might cause NPE in some WebSocketProcol implementation
// We could potentially synchronize on webSocket but since it is a rare case, it is better to not synchronize.
// synchronized (webSocket) {
final AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
if (resource == null) {
logger.trace("Already closed {}", webSocket);
} else {
final boolean allowedToClose = allowedCloseCode(closeCode);
final AtmosphereRequest r = resource.getRequest(false);
final AtmosphereResponse s = resource.getResponse(false);
boolean ff = r.getAttribute("firefox") != null;
boolean completeLifecycle = true;
try {
webSocketProtocol.onClose(webSocket);
if (webSocketHandler != null) {
webSocketHandler.onClose(webSocket);
}
logger.trace("About to close AtmosphereResource for {} with code {}", resource, closeCode);
if (!resource.getAtmosphereResourceEvent().isClosedByClient() && !resource.getAtmosphereResourceEvent().isClosedByApplication() && !resource.isCancelled()) {
// Better to call onDisconnect that onResume.
if (allowedToClose) {
if (ff || closingTime > 0) {
completeLifecycle = false;
logger.debug("Delaying closing operation for firefox and resource {}", resource.uuid());
ExecutorsFactory.getScheduler(framework.getAtmosphereConfig()).schedule(new Callable<Object>() {
@Override
public Object call() throws Exception {
AtmosphereResource currentResource = framework.atmosphereFactory().find(resource.uuid());
if (currentResource != null && currentResource.isSuspended()) {
// Do not close if the resource has reconnected already
executeClose(webSocket, 1005);
finish(webSocket, currentResource, r, s, !allowedToClose);
}
return null;
}
}, ff ? (closingTime == 0 ? 1000 : closingTime) : closingTime, TimeUnit.MILLISECONDS);
resource.getAndSetPendingClose();
} else {
executeClose(webSocket, closeCode);
}
} else {
logger.debug("Timeout {}", resource.uuid());
asynchronousProcessor.endRequest(AtmosphereResourceImpl.class.cast(webSocket.resource()), false);
}
} else {
logger.trace("Unable to properly complete {}", resource == null ? "null" : resource.uuid());
completeLifecycle = false;
}
} finally {
if (completeLifecycle) {
finish(webSocket, resource, r, s, !allowedToClose);
}
}
}
}
use of org.atmosphere.cpr.AtmosphereResource in project atmosphere by Atmosphere.
the class LifecycleHandler method offIfEmpty.
public LifecycleHandler offIfEmpty(DefaultBroadcaster broadcaster) {
BroadcasterConfig bc = broadcaster.getBroadcasterConfig();
Collection<AtmosphereResource> resources = broadcaster.getAtmosphereResources();
final BroadcasterLifeCyclePolicy lifeCyclePolicy = broadcaster.getBroadcasterLifeCyclePolicy();
if (resources.isEmpty()) {
notifyEmptyListener(broadcaster);
if (broadcaster.getScope() != Broadcaster.SCOPE.REQUEST && lifeCyclePolicy.getLifeCyclePolicy() == EMPTY) {
broadcaster.releaseExternalResources();
} else if (broadcaster.getScope() == Broadcaster.SCOPE.REQUEST || lifeCyclePolicy.getLifeCyclePolicy() == EMPTY_DESTROY) {
bc.getAtmosphereConfig().getBroadcasterFactory().remove(broadcaster, broadcaster.getID());
broadcaster.destroy();
}
}
return this;
}
Aggregations