Search in sources :

Example 1 with WebSocketEventListenerAdapter

use of org.atmosphere.websocket.WebSocketEventListenerAdapter in project atmosphere by Atmosphere.

the class WebSocketProcessorTest method inClosePhaseTest.

@Test
public void inClosePhaseTest() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    final AtomicBoolean closed = new AtomicBoolean();
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(final AtmosphereResource resource) throws IOException {
            resource.addEventListener(new WebSocketEventListenerAdapter() {

                @Override
                public void onClose(WebSocketEvent event) {
                    closed.set(true);
                }
            });
            AtmosphereResourceEventImpl.class.cast(resource.getAtmosphereResourceEvent()).isClosedByClient(true);
            AsynchronousProcessor.class.cast(framework.getAsyncSupport()).completeLifecycle(resource, false);
            processor.close(w, 1005);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    assertFalse(closed.get());
}
Also used : WebSocketEventListenerAdapter(org.atmosphere.websocket.WebSocketEventListenerAdapter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocket(org.atmosphere.websocket.WebSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) Test(org.testng.annotations.Test)

Example 2 with WebSocketEventListenerAdapter

use of org.atmosphere.websocket.WebSocketEventListenerAdapter in project atmosphere by Atmosphere.

the class WebSocketProcessorTest method inClosePhaseTest.

@Test
public void inClosePhaseTest() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    final AtomicBoolean closed = new AtomicBoolean();
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(final AtmosphereResource resource) throws IOException {
            resource.addEventListener(new WebSocketEventListenerAdapter() {

                @Override
                public void onClose(WebSocketEvent event) {
                    closed.set(true);
                }
            });
            AtmosphereResourceEventImpl.class.cast(resource.getAtmosphereResourceEvent()).isClosedByClient(true);
            AsynchronousProcessor.class.cast(framework.getAsyncSupport()).completeLifecycle(resource, false);
            processor.close(w, 1005);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    assertFalse(closed.get());
}
Also used : WebSocketEventListenerAdapter(org.atmosphere.websocket.WebSocketEventListenerAdapter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocket(org.atmosphere.websocket.WebSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) Test(org.testng.annotations.Test)

Example 3 with WebSocketEventListenerAdapter

use of org.atmosphere.websocket.WebSocketEventListenerAdapter in project atmosphere by Atmosphere.

the class WebSocketProcessorTest method onCloseAtmosphereRequestAttribute.

@Test
public void onCloseAtmosphereRequestAttribute() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    final AtomicReference<String> uuid = new AtomicReference<String>();
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            resource.addEventListener(new WebSocketEventListenerAdapter() {

                @Override
                public void onClose(WebSocketEvent event) {
                    uuid.set((String) event.webSocket().resource().getRequest().getAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID));
                }
            });
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w, "yoWebSocket");
    processor.notifyListener(w, new WebSocketEventListener.WebSocketEvent("Close", WebSocketEventListener.WebSocketEvent.TYPE.CLOSE, w));
    assertNotNull(uuid.get());
    assertEquals(uuid.get(), request.getAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID));
}
Also used : WebSocketEventListenerAdapter(org.atmosphere.websocket.WebSocketEventListenerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocketEventListener(org.atmosphere.websocket.WebSocketEventListener) WebSocket(org.atmosphere.websocket.WebSocket) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) Test(org.testng.annotations.Test)

Example 4 with WebSocketEventListenerAdapter

use of org.atmosphere.websocket.WebSocketEventListenerAdapter in project atmosphere by Atmosphere.

the class WebSocketProcessorTest method onDisconnectAtmosphereRequestAttribute.

@Test
public void onDisconnectAtmosphereRequestAttribute() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    final AtomicReference<String> uuid = new AtomicReference<String>();
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            resource.addEventListener(new WebSocketEventListenerAdapter() {

                @Override
                public void onDisconnect(WebSocketEvent event) {
                    uuid.set((String) event.webSocket().resource().getRequest().getAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID));
                }
            });
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w, "yoWebSocket");
    processor.notifyListener(w, new WebSocketEventListener.WebSocketEvent("Disconnect", DISCONNECT, w));
    assertNotNull(uuid.get());
    assertEquals(uuid.get(), request.getAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID));
}
Also used : WebSocketEventListenerAdapter(org.atmosphere.websocket.WebSocketEventListenerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocketEventListener(org.atmosphere.websocket.WebSocketEventListener) WebSocket(org.atmosphere.websocket.WebSocket) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) Test(org.testng.annotations.Test)

Example 5 with WebSocketEventListenerAdapter

use of org.atmosphere.websocket.WebSocketEventListenerAdapter in project scheduling by ow2-proactive.

the class SchedulerStateRest method publish.

/**
 * Accepts an {@link EventSubscription} instance which specifies the types
 * of SchedulerEvents which interest the client. When such Scheduler event
 * occurs, it will be communicated to the client in the form of
 * {@link EventNotification} utilizing the WebSocket channel initialized
 * previously.
 */
@POST
@Path("/events")
@Produces("application/json")
public EventNotification publish(@Context HttpServletRequest req, EventSubscription subscription) throws RestException {
    HttpSession session = req.getSession();
    String broadcasterId = (String) session.getAttribute(ATM_BROADCASTER_ID);
    final SchedulerProxyUserInterface scheduler = checkAccess(broadcasterId);
    SchedulerEventBroadcaster eventListener = new SchedulerEventBroadcaster(broadcasterId);
    try {
        final SchedulerEventBroadcaster activedEventListener = PAActiveObject.turnActive(eventListener);
        scheduler.addEventListener(activedEventListener, subscription.isMyEventsOnly(), EventUtil.toSchedulerEvents(subscription.getEvents()));
        AtmosphereResource atmResource = getAtmosphereResourceFactory().find((String) session.getAttribute(ATM_RESOURCE_ID));
        atmResource.addEventListener(new WebSocketEventListenerAdapter() {

            @Override
            public void onDisconnect(@SuppressWarnings("rawtypes") WebSocketEvent event) {
                try {
                    logger.info("#### websocket disconnected remove listener ####");
                    scheduler.removeEventListener();
                } catch (Exception e) {
                    logger.error(e);
                }
                PAActiveObject.terminateActiveObject(activedEventListener, true);
            }
        });
    } catch (SchedulerException e) {
        throw RestException.wrapExceptionToRest(e);
    } catch (ActiveObjectCreationException | NodeException e) {
        throw new RuntimeException(e);
    }
    return new EventNotification(EventNotification.Action.NONE, null, null);
}
Also used : EventNotification(org.ow2.proactive_grid_cloud_portal.scheduler.dto.eventing.EventNotification) WebSocketEventListenerAdapter(org.atmosphere.websocket.WebSocketEventListenerAdapter) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) HttpSession(javax.servlet.http.HttpSession) SchedulerProxyUserInterface(org.ow2.proactive.scheduler.common.util.SchedulerProxyUserInterface) NodeException(org.objectweb.proactive.core.node.NodeException) LoginException(javax.security.auth.login.LoginException) ActiveObjectCreationException(org.objectweb.proactive.ActiveObjectCreationException) KeyException(java.security.KeyException) URISyntaxException(java.net.URISyntaxException) LogForwardingException(org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException) NodeException(org.objectweb.proactive.core.node.NodeException) ActiveObjectCreationException(org.objectweb.proactive.ActiveObjectCreationException)

Aggregations

WebSocketEventListenerAdapter (org.atmosphere.websocket.WebSocketEventListenerAdapter)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 WebSocket (org.atmosphere.websocket.WebSocket)6 WebSocketProcessor (org.atmosphere.websocket.WebSocketProcessor)6 Test (org.testng.annotations.Test)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 WebSocketEventListener (org.atmosphere.websocket.WebSocketEventListener)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 URISyntaxException (java.net.URISyntaxException)1 KeyException (java.security.KeyException)1 LoginException (javax.security.auth.login.LoginException)1 HttpSession (javax.servlet.http.HttpSession)1 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)1 ActiveObjectCreationException (org.objectweb.proactive.ActiveObjectCreationException)1 NodeException (org.objectweb.proactive.core.node.NodeException)1 SchedulerProxyUserInterface (org.ow2.proactive.scheduler.common.util.SchedulerProxyUserInterface)1 LogForwardingException (org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException)1 EventNotification (org.ow2.proactive_grid_cloud_portal.scheduler.dto.eventing.EventNotification)1