Search in sources :

Example 1 with Broadcaster

use of org.atmosphere.runtime.Broadcaster in project atmosphere by Atmosphere.

the class ServiceInterceptor method mapAnnotatedService.

/**
     * Inspect the request and its mapped {@link org.atmosphere.runtime.AtmosphereHandler} to determine if the '{}' was used when defined the
     * annotation's path value. It will create a new {@link org.atmosphere.runtime.AtmosphereHandler} in case {} is detected .
     *
     * @param request
     * @param w
     * @return
     */
protected void mapAnnotatedService(AtmosphereRequest request, AtmosphereFramework.AtmosphereHandlerWrapper w) {
    Broadcaster b = w.broadcaster;
    String path;
    String pathInfo = null;
    boolean reMap = false;
    path = Utils.pathInfo(request);
    // Remove the Broadcaster with curly braces
    if (b.getID().contains("{")) {
        reMap = true;
        config.getBroadcasterFactory().remove(b.getID());
    }
    request.localAttributes().put(Named.class.getName(), path);
    mapAnnotatedService(reMap, path, request, w);
}
Also used : Named(javax.inject.Named) Broadcaster(org.atmosphere.runtime.Broadcaster)

Example 2 with Broadcaster

use of org.atmosphere.runtime.Broadcaster in project atmosphere by Atmosphere.

the class BroadcasterListenerServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<BroadcasterListener> annotatedClass) {
    try {
        BroadcasterListener l = framework.newClassInstance(BroadcasterListener.class, annotatedClass);
        framework.addBroadcasterListener(l);
        // We must reconfigure all existing Broadcaster
        Collection<Broadcaster> c = framework.getBroadcasterFactory().lookupAll();
        for (Broadcaster b : c) {
            l.onPostCreate(b);
        }
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : BroadcasterListener(org.atmosphere.runtime.BroadcasterListener) Broadcaster(org.atmosphere.runtime.Broadcaster)

Example 3 with Broadcaster

use of org.atmosphere.runtime.Broadcaster in project atmosphere by Atmosphere.

the class AtmosphereResourceStateRecovery method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    if (!Utils.pollableTransport(r.transport()) && !Utils.webSocketMessage(r)) {
        final BroadcasterTracker tracker = track(r).tick();
        List<Object> cachedMessages = retrieveCache(r, tracker, false);
        if (!cachedMessages.isEmpty()) {
            logger.trace("cached messages");
            writeCache(r, cachedMessages);
            return Action.CANCELLED;
        } else {
            r.addEventListener(new OnAlwaysSuspend() {

                public void onSuspend(AtmosphereResourceEvent event) {
                    r.removeEventListener(this);
                    logger.trace("onSuspend first");
                    final AtomicBoolean doNotSuspend = new AtomicBoolean(false);
                    /**
                         * If a message gets broadcasted during the execution of the code below, we don't need to
                         * suspend the connection. This code is needed to prevent the connection being suspended
                         * with messages already written.
                         */
                    r.addEventListener(new OnBroadcast() {

                        @Override
                        public void onBroadcast(AtmosphereResourceEvent event) {
                            r.removeEventListener(this);
                            doNotSuspend.set(true);
                            logger.trace("onBroadcast");
                        }
                    });
                    for (String broadcasterID : tracker.ids()) {
                        Broadcaster b = factory.lookup(broadcasterID, false);
                        logger.trace("About to associate resource {} with Broadcaster {}", r.uuid(), broadcasterID);
                        if (b != null && !b.getID().equalsIgnoreCase(r.getBroadcaster().getID())) {
                            logger.trace("Associate AtmosphereResource {} with Broadcaster {}", r.uuid(), broadcasterID);
                            b.addAtmosphereResource(r);
                        } else if (b == null) {
                            logger.trace("Broadcaster {} is no longer available for {}", broadcasterID, r);
                        } else {
                            logger.trace("AtmosphereResource {} already associated with {}", r.uuid(), broadcasterID);
                        }
                    }
                    /**
                         * Check the cache to see if messages has been added directly by using
                         * {@link BroadcasterCache#addToCache(String, org.atmosphere.runtime.AtmosphereResource, org.atmosphere.cache.BroadcastMessage)}
                         * after {@link Broadcaster#addAtmosphereResource(org.atmosphere.runtime.AtmosphereResource)} has been
                         * invoked.
                         */
                    final List<Object> cachedMessages = retrieveCache(r, tracker, true);
                    if (logger.isTraceEnabled()) {
                        logger.trace("message size {}", cachedMessages.size());
                    }
                    if (!cachedMessages.isEmpty()) {
                        logger.trace("About to write to the cache {}", r.uuid());
                        writeCache(r, cachedMessages);
                        doNotSuspend.set(true);
                    }
                    // Force doNotSuspend.
                    if (doNotSuspend.get()) {
                        AtmosphereResourceImpl.class.cast(r).action().type(Action.TYPE.CONTINUE);
                    }
                    if (logger.isTraceEnabled()) {
                        logger.trace("doNotSuspend {}", doNotSuspend.get());
                    }
                }
            });
        }
    }
    return Action.CONTINUE;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtmosphereResourceEvent(org.atmosphere.runtime.AtmosphereResourceEvent) Broadcaster(org.atmosphere.runtime.Broadcaster) LinkedList(java.util.LinkedList) List(java.util.List) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 4 with Broadcaster

use of org.atmosphere.runtime.Broadcaster in project atmosphere by Atmosphere.

the class AtmosphereResourceStateRecovery method retrieveCache.

public List<Object> retrieveCache(AtmosphereResource r, BroadcasterTracker tracker, boolean force) {
    List<Object> cachedMessages = new LinkedList<Object>();
    for (String broadcasterID : tracker.ids()) {
        Broadcaster b = factory.lookup(broadcasterID, false);
        BroadcasterCache cache;
        logger.trace("About to retrieve cached messages for resource {} with Broadcaster {}, tracked by " + b, r.uuid(), r.getBroadcaster());
        if (b != null && (force || !b.getID().equalsIgnoreCase(r.getBroadcaster().getID()))) {
            // We cannot add the resource now. we need to first make sure there is no cached message.
            cache = b.getBroadcasterConfig().getBroadcasterCache();
            List<Object> t = cache.retrieveFromCache(b.getID(), r.uuid());
            t = b.getBroadcasterConfig().applyFilters(r, t);
            if (!t.isEmpty()) {
                logger.trace("Found Cached Messages For AtmosphereResource {} with Broadcaster {}", r.uuid(), broadcasterID);
                cachedMessages.addAll(t);
            }
        } else {
            logger.trace("Broadcaster {} is no longer available for {}", broadcasterID, r);
        }
    }
    return cachedMessages;
}
Also used : Broadcaster(org.atmosphere.runtime.Broadcaster) LinkedList(java.util.LinkedList) BroadcasterCache(org.atmosphere.runtime.BroadcasterCache)

Example 5 with Broadcaster

use of org.atmosphere.runtime.Broadcaster in project atmosphere by Atmosphere.

the class BroadcasterIntrospector method injectable.

@Override
public Broadcaster injectable(AtmosphereResource r) {
    String named = nameLocal.get();
    if (named == null) {
        named = Broadcaster.ROOT_MASTER;
    }
    String s = (String) r.getRequest().getAttribute(Named.class.getName());
    int indx = named.indexOf("{");
    if (s != null && indx != -1) {
        named = named.substring(0, indx) + s;
    }
    final Broadcaster broadcaster = r.getAtmosphereConfig().getBroadcasterFactory().lookup(named, true);
    return (Broadcaster) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { Broadcaster.class }, new ThreadLocalInvoker() {

        {
            set(broadcaster);
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return method.invoke(broadcaster, args);
        }
    });
}
Also used : Broadcaster(org.atmosphere.runtime.Broadcaster) Method(java.lang.reflect.Method) ThreadLocalInvoker(org.atmosphere.util.ThreadLocalInvoker)

Aggregations

Broadcaster (org.atmosphere.runtime.Broadcaster)5 LinkedList (java.util.LinkedList)2 Method (java.lang.reflect.Method)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Named (javax.inject.Named)1 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)1 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)1 BroadcasterCache (org.atmosphere.runtime.BroadcasterCache)1 BroadcasterListener (org.atmosphere.runtime.BroadcasterListener)1 ThreadLocalInvoker (org.atmosphere.util.ThreadLocalInvoker)1