/********************************************************************************
*File name: push.java
*Author   : LineNoise (Mark)
*Date     : 2/26/98
*
*Prog purpose: Creates a "server push" for the 
*             webcam2000 webcam web server.
*                         Allows someone to "stream" video on
*                         on a web page.
*Limitations: Server push technology is not
*                        supported by MSIE 5.0 or earlier
*
*Future additions: Support for multiple image
*                                 animations rather than just
*                                 webcam
*
*Contact info: lynoise@bigfoot.com
*
*Usage: 
*<img src=www.yourhost.com/servlet_Dir/push?URL=http://www.yourhost.com:port>
*
*******************************************************************************/


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;

public class push extends HttpServlet {

public void  service ( HttpServletRequest  req, HttpServletResponse  res )
        throws ServletException, IOException{
        OutputStream fw = res.getOutputStream();
        ServletOutputStream  out = res.getOutputStream();
        res.setContentType("multipart/x-mixed-replace;boundary=ThisIsIt");
        out.println();
        out.println("--ThisIsIt");
        String UrL=req.getParameterValues("URL")[0];
                byte[] g_IMG  = new byte[1024];
                try
                {
                        int begin = UrL.lastIndexOf("/") + 1;
                URL url = new URL(UrL);
                        URLConnection  urlConn; 
                        for(int l=0;l<100;l++)
                        {
                                out.println("Content-type: image/jpeg");
                                out.println();
                                urlConn = url.openConnection(); 
                        urlConn.setDoInput(true); 
                        urlConn.setUseCaches(true);
                                InputStream  dis = (urlConn.getInputStream());
                                int len = 0;
                                while((len = dis.read(g_IMG,0,1024)) > 0)
                                        fw.write(g_IMG,0,len);
                                dis.close();
                                out.println();
                                out.flush();
                                out.println("--ThisIsIt");
                                /*try
                                {
                                        (Thread.currentThread()).sleep(500);
                                }
                                catch(InterruptedException e){}*/
                        }
                        out.println("--ThisIsIt--");    	
                        out.close();
                }
                catch(FileNotFoundException e){}
                catch(IOException e){}	
}
}