mev-4.0.01/source/org/tigr/remote/gateway/MEVPollAcceptStrategy.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2003, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4 */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * $RCSfile: MEVPollAcceptStrategy.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.4 $
2 26 Feb 07 jari 8  * $Date: 2005/03/10 15:34:10 $
2 26 Feb 07 jari 9  * $Author: braistedj $
2 26 Feb 07 jari 10  */
2 26 Feb 07 jari 11 package org.tigr.remote.gateway;
2 26 Feb 07 jari 12
2 26 Feb 07 jari 13 import java.io.BufferedInputStream;
2 26 Feb 07 jari 14 import java.io.BufferedOutputStream;
2 26 Feb 07 jari 15 import java.io.IOException;
2 26 Feb 07 jari 16 import java.io.PrintWriter;
2 26 Feb 07 jari 17
2 26 Feb 07 jari 18 import javax.servlet.http.HttpServletRequest;
2 26 Feb 07 jari 19 import javax.servlet.http.HttpServletResponse;
2 26 Feb 07 jari 20
2 26 Feb 07 jari 21 import org.tigr.util.ConfMap;
2 26 Feb 07 jari 22
2 26 Feb 07 jari 23 class MEVPollAcceptStrategy {
2 26 Feb 07 jari 24     
2 26 Feb 07 jari 25     /**
2 26 Feb 07 jari 26      * Construct a <code>MEVPollAcceptStrategy</code> with specified session.
2 26 Feb 07 jari 27      */
2 26 Feb 07 jari 28     public MEVPollAcceptStrategy( SessionState session, ConfMap cfg ) {
2 26 Feb 07 jari 29   m_session = session;
2 26 Feb 07 jari 30     }
2 26 Feb 07 jari 31     
2 26 Feb 07 jari 32     /**
2 26 Feb 07 jari 33      * Accepts a polling request.
2 26 Feb 07 jari 34      */
2 26 Feb 07 jari 35     public void acceptPollRequest( HttpServletRequest req, HttpServletResponse resp ) throws IOException {
2 26 Feb 07 jari 36   if (!m_session.isLocked()) { // no PVM waiting for this poll
2 26 Feb 07 jari 37       PrintWriter p = new PrintWriter( resp.getOutputStream() );
2 26 Feb 07 jari 38       if (m_session.getMessageQueue().getSize() > 0) { // message queue is not empty
2 26 Feb 07 jari 39     String response = (String)m_session.getMessageQueue().getHead();
2 26 Feb 07 jari 40     p.print( response );
2 26 Feb 07 jari 41       } else {
2 26 Feb 07 jari 42     p.println("<response/>");
2 26 Feb 07 jari 43       }
2 26 Feb 07 jari 44       p.flush();
2 26 Feb 07 jari 45       p = null;
2 26 Feb 07 jari 46   } else { // PVM response arrived
2 26 Feb 07 jari 47       // need to clear message queue
2 26 Feb 07 jari 48       m_session.getMessageQueue().clear();
2 26 Feb 07 jari 49       synchronized( m_session ) {
2 26 Feb 07 jari 50     m_session.connect();
2 26 Feb 07 jari 51     m_session.notify();
2 26 Feb 07 jari 52       }
2 26 Feb 07 jari 53       m_session.releaseMonitor();
2 26 Feb 07 jari 54       BufferedInputStream in = new BufferedInputStream( m_session.getInputPipe() );
2 26 Feb 07 jari 55       BufferedOutputStream out = new BufferedOutputStream( resp.getOutputStream() );
2 26 Feb 07 jari 56       
2 26 Feb 07 jari 57       byte[] b = new byte[1024*100];
2 26 Feb 07 jari 58       int cnt;
2 26 Feb 07 jari 59       while ((cnt = in.read(b)) >= 0) {
2 26 Feb 07 jari 60     out.write(b, 0, cnt);
2 26 Feb 07 jari 61       }
2 26 Feb 07 jari 62       out.flush();
2 26 Feb 07 jari 63       out = null;
2 26 Feb 07 jari 64       in.close();
2 26 Feb 07 jari 65       m_session.getInputPipe().close();
2 26 Feb 07 jari 66       m_session.free();
2 26 Feb 07 jari 67   }
2 26 Feb 07 jari 68     }
2 26 Feb 07 jari 69     
2 26 Feb 07 jari 70     SessionState m_session;
2 26 Feb 07 jari 71 }