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

DataHandler

public class DataHandler extends Object implements com.sun.enterprise.web.connector.grizzly.comet.CometHandler
CometHandler used to support the meta channel Verb Connect and Reconnect. The DataHandler is holding the state of the long polled (Comet) connection.
author
Jeanfrancois Arcand
author
TAKAI, Naoto

Fields Summary
private static final Logger
logger
private CometdRequest
req
private CometdResponse
res
public String
clientIP
private String
channel
Constructors Summary
Methods Summary
public voidattach(java.lang.Object[] reqRes)

    
       
        this.req = (CometdRequest) reqRes[0];
        this.res = (CometdResponse) reqRes[1];
    
public java.lang.StringgetChannel()

        return channel;
    
public voidonEvent(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

 
        Object obj = event.attachment();
        try{                   
            if (obj instanceof Data){
                Data data = (Data)obj;
                String sdata = data.getJSONData();
                res.write(sdata);
                res.flush();
                event.getCometContext().resumeCometHandler(this);
            }
        }  catch (Throwable t){
           logger.log(Level.SEVERE,"Data.onEvent",t);
        } 
        
        if (event.getType()  == CometEvent.READ){
            CometInputStream is = (CometInputStream)obj;
            
            // XXX This is dangerous...
            byte[] dataStream = new byte[2 * 8192];
            is.setReadTimeout(2000);
            while (is.read(dataStream) > 0){
            }
            String sdata = new String(dataStream).trim();
            
            if (sdata.length() <=1) return;  
            
            // XXX What abou the header.
            try{
                final Verb verb = 
                        VerbUtils.parse(JSONParser.parse(sdata));                
                // Notify our listener;
                CometContext cometContext = event.getCometContext();
                cometContext.notify(
                        BayeuxCometHandler.newCometdContext(req,res,verb),
                        CometEvent.NOTIFY,
                        (Integer)cometContext.getAttribute(
                            BayeuxCometHandler.BAYEUX_COMET_HANDLER));
                event.getCometContext().removeAttribute(this);
            } catch (Throwable t){
                logger.log(Level.SEVERE,"Data.onEvent",t);
            }
        }            
    
public voidonInitialize(com.sun.enterprise.web.connector.grizzly.comet.CometEvent event)

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

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

    
public voidsetChannel(java.lang.String channel)

        this.channel = channel;