FileDocCategorySizeDatePackage
BayeuxCometHandler.javaAPI DocGlassfish v2 API9157Fri May 04 22:36:56 BST 2007com.sun.grizzly.cometd

BayeuxCometHandler

public class BayeuxCometHandler extends BayeuxCometHandlerBase
This class implement the Bauyeux Server side protocol.
author
Jeanfrancois Arcand
author
TAKAI, Naot

Fields Summary
public static final String
DEFAULT_CONTENT_TYPE
public static final String
BAYEUX_COMET_HANDLER
private ConcurrentLinkedQueue
clientIds
private Random
random
private ConcurrentHashMap
activeHandler
Constructors Summary
Methods Summary
public java.lang.StringgetChannel()

        return BAYEUX_COMET_HANDLER;
    
public static final CometdContextnewCometdContext(CometdRequest req, CometdResponse res, com.sun.grizzly.cometd.bayeux.Verb verb)

        return new CometdContext(){
            
            public Verb getVerb(){
                return verb;
            }
            
            public CometdRequest getRequest(){
                return req;
            }
                      
            public CometdResponse getResponse(){
                return res;
            }            
        };
    
public voidonConnect(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

        CometdContext cometdContext = (CometdContext)event.attachment();
        
        CometdRequest req = cometdContext.getRequest();
        CometdResponse res = cometdContext.getResponse();
        Connect connect = (Connect)cometdContext.getVerb();            
         
        res.setContentType(DEFAULT_CONTENT_TYPE);            
        res.write(connect.toJSON());
        res.flush();
    
public voidonData(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

        CometdContext cometdContext = (CometdContext)event.attachment();
        
        CometdRequest req = cometdContext.getRequest();
        CometdResponse res = cometdContext.getResponse();
        Data data = (Data)cometdContext.getVerb();           
         
        res.setContentType(DEFAULT_CONTENT_TYPE);            
        res.write(data.toJSON());
        res.flush(); 

        event.getCometContext().notify(data);
    
public voidonDisconnect(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

        CometdContext cometdContext = (CometdContext)event.attachment();
        
        CometdRequest req = cometdContext.getRequest();
        CometdResponse res = cometdContext.getResponse();
        Disconnect disconnect = (Disconnect)cometdContext.getVerb();      
        activeHandler.remove(disconnect.getClientId());
        
        res.setContentType(DEFAULT_CONTENT_TYPE);            
        res.write(disconnect.toJSON());
        res.flush();
    
public voidonHandshake(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

    
    
                      
        CometdContext cometdContext = (CometdContext)event.attachment();
        
        CometdRequest req = cometdContext.getRequest();
        CometdResponse res = cometdContext.getResponse();
        Handshake handshake = (Handshake)cometdContext.getVerb();
        
        boolean handshakeOK = true;
        String clientId  = "";
        synchronized(random){
            clientId = String.valueOf(Long.toHexString(random.nextLong()));
        }
        clientIds.add(clientId);
        
        // XXX Why do we need to cache the ID. Memory leak right now
        if (handshakeOK){
            handshake.setClientId(clientId);
        }

        res.setContentType(DEFAULT_CONTENT_TYPE);            
        res.write(handshake.toJSON());
        res.flush();
    
public voidonInterrupt(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

        // TODO: Use the SelectionKey to find which Handler needs to be cancelled.   
    
public voidonPing(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

    
public voidonReconnect(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

        CometdContext cometdContext = (CometdContext)event.attachment();
        
        CometdRequest req = cometdContext.getRequest();
        CometdResponse res = cometdContext.getResponse();
        Reconnect reconnect = (Reconnect)cometdContext.getVerb();                    
                       
        CometContext cometContext = event.getCometContext();
        DataHandler requestHandler = new DataHandler();
        requestHandler.attach(new Object[]{req,res});
        DataHandler prevHandler = activeHandler
                .putIfAbsent(reconnect.getClientId(),requestHandler);
        // Set the previous channel value.
        if (prevHandler != null){
            requestHandler.setChannel(prevHandler.getChannel());
        }        
        cometContext.addCometHandler(requestHandler);   
        
        res.setContentType(DEFAULT_CONTENT_TYPE);            
        res.write(reconnect.toJSON());
        res.flush();
    
public voidonStatus(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

    
public voidonSubscribe(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

        CometdContext cometdContext = (CometdContext)event.attachment();
        
        CometdRequest req = cometdContext.getRequest();
        CometdResponse res = cometdContext.getResponse();
        Subscribe subscribe = (Subscribe)cometdContext.getVerb();   
        
        DataHandler dataHandler = activeHandler.get(subscribe.getClientId());
        if (dataHandler != null){
            dataHandler.setChannel(subscribe.getSubscription());
        }         
        res.setContentType(DEFAULT_CONTENT_TYPE);            
        res.write(subscribe.toJSON());
        res.flush();
    
public voidonTerminate(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

        onInterrupt(event);    
    
public voidonUnsubscribe(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

        CometdContext cometdContext = (CometdContext)event.attachment();
        
        CometdRequest req = cometdContext.getRequest();
        CometdResponse res = cometdContext.getResponse();
        Unsubscribe unsubscribe = (Unsubscribe)cometdContext.getVerb();   
        
        activeHandler.remove(unsubscribe.getClientId());
        
        res.setContentType(DEFAULT_CONTENT_TYPE);            
        res.write(unsubscribe.toJSON());
        res.flush();
    
public voidsetChannel(java.lang.String channel)

        // Not supported