package webIM; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; /** * Title: Heartbeat * Description: This servlet periodically receives GET requests from the footer * frame, depending on a JavaScript timer. When new files arrive * they are assigned a sequence number, so the heartbeat can check * the sequence number to see if something new has arrived. If so, * a JavaScript location.reload() command is issued to cause a * GET request to be sent to refresh the Chat frame. * Copyright: Copyright (c) 2001 * Company: UCCS Computer Science Department * @author Merlin Vincent * @version 1.0 */ public class Heartbeat extends HttpServlet { private static final String CONTENT_TYPE = "text/html"; /**Initialize global variables*/ public void init(ServletConfig config) throws ServletException { super.init(config); } /**Process the HTTP Get request*/ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher dispatcher = null; response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); // Get the session and its variables. HttpSession session = request.getSession(false); if (session == null) throw new ServletException("heartbeat GET: no HttpSession"); String nickName = (String) session.getAttribute("nickname"); ServletContext context = getServletContext(); String buddyName = (String) context.getAttribute(nickName+"selectedBuddy"); // Get newFilesList and global message sequence number from servlet context. Integer CurSeqNo = null; ArrayList newFilesList = null; if (nickName != null && buddyName != null) { newFilesList = (ArrayList) context.getAttribute( nickName+buddyName+"NewChat"); if (newFilesList != null) { synchronized (newFilesList) { CurSeqNo = (Integer) context.getAttribute( nickName+buddyName+"SeqNo" ); } } } // If any needed variables are still undefined, just keep heartbeating. if (nickName == null || buddyName == null || newFilesList == null || CurSeqNo == null) { dispatcher = getServletContext().getRequestDispatcher( "/heartbeat.jsp" ); dispatcher.include(request, response); return; } // Get the last processed and current message sequence numbers. Integer LastSeqNum = (Integer) session.getAttribute("lastHeartbeatSeqNo"); if (LastSeqNum == null) { LastSeqNum = new Integer(0); } int lastSeqNo = LastSeqNum.intValue(); int currentSeqNo = CurSeqNo.intValue(); /** * If the chat files list has grown, i.e., the sequence number has * increased, we must reload the Chat servlet. */ if (currentSeqNo > lastSeqNo) { session.setAttribute( "lastHeartbeatSeqNo", new Integer( currentSeqNo )); // Include the JavaScript that reloads the Chat frame dispatcher = getServletContext().getRequestDispatcher( "/heartbeat_reload.jsp" ); } else { // Include the heartbeat Javascript dispatcher = getServletContext().getRequestDispatcher( "/heartbeat.jsp" ); } dispatcher.include(request, response); } /**Clean up resources*/ public void destroy() { } }