package com.sun.grizzly.samples.comet; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.sun.grizzly.comet.CometContext; import com.sun.grizzly.comet.CometEngine; import com.sun.grizzly.comet.CometEvent; import com.sun.grizzly.comet.CometHandler; /** * Test version for bug reproduction * @author akalnitski * */ public class MyCometServlet extends HttpServlet { //private String contextPath = null; private int agentId = 0; int deleverad = 0; private int clientid; class CometMsgHandler implements CometHandler { private String contextPath = null; private int agentId; public CometMsgHandler(String contexPath,int agentId) { this.contextPath = contexPath; this.agentId = agentId; } public CometMsgHandler(String contexPath) { this.contextPath = contexPath; } private HttpServletResponse response; //used when notifying public void onEvent(CometEvent event) throws IOException { if (CometEvent.NOTIFY == event.getType()) { System.out.println("SERVER: On Event notify "); PrintWriter writer = response.getWriter(); String msg = ((String) event.attachment()).replace("\n"," ").trim(); writer.write(msg.trim()+"\n"); writer.flush(); deleverad = 1; // event.getCometContext().resumeCometHandler(this); //tell that // is streaming comet type } } public void onInitialize(CometEvent event) throws IOException { } public void onInterrupt(CometEvent event) throws IOException { removeThisFromContext(); } public void onTerminate(CometEvent event) throws IOException { removeThisFromContext(); } public void attach(HttpServletResponse attachment) { this.response = attachment; } /** * Close when client disconnects * @throws IOException */ private void removeThisFromContext() throws IOException { if(response.hashCode() == agentId) { response.getWriter().close(); CometContext context = CometEngine.getEngine().getCometContext(contextPath); // context.removeCometHandler(this); CometEngine.getEngine().unregister(contextPath); System.out.println("Closing Agent connection as result"); context.removeCometHandler(this); } } } private static final long serialVersionUID = 1L; // simulates the cpu utilization @Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.println("SERVER: Do POST "); System.out.println("Got Post request from ip "+req.getRemoteAddr()); HttpSession id= req.getSession(); System.out.println("SERVER: Session id = "+id.getId()); String contextPath = req.getParameter("client"); System.out.println("SERVER: "+contextPath); String message = req.getParameter("command"); Map params = req.getParameterMap(); System.out.println(params.toString()); System.out.println("SERVER: "+message); CometEngine engine = CometEngine.getEngine(); CometContext cc; if (message == null) { cc = engine.getCometContext(contextPath); System.out.println("client not exit , register client"); agentId = res.hashCode(); if (cc == null) { cc = engine.register(contextPath); // how long to hold client cc.setExpirationDelay(-1); CometMsgHandler handler = new CometMsgHandler(contextPath,agentId); handler.attach(res); cc.addCometHandler(handler); System.out.println("Connected from ip "+req.getRemoteAddr()); } else{ System.out.println("Already registred remove the client from engine"); CometEngine.getEngine().unregister(contextPath); } } else { System.out.println("SERVER: "+res.toString()); } } }