home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / jmach08.zip / qfile.java < prev    next >
Text File  |  2000-05-18  |  5KB  |  238 lines

  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public final class qfile implements Runnable
  6. {
  7.  // location INFO
  8.  private String name; // jmeno souboru
  9.  private String localname; // jmeno pro ulozeni na hadr
  10.  private String URL[]; // na kterych URL je
  11.  
  12.  // retry status
  13.  private int URLstatus[]; // kolikrat se z nej downloadovalo
  14.  private short retry;  // kolikrat se nahravalo celkem
  15.  private int mindown;
  16.  
  17.  private boolean ok;  // already downloaded?
  18.  private boolean hold; // HOLD=fatal error or retry exhausted
  19.  
  20.  private Thread downloader;  // pro killnuti
  21.  
  22.  qfile(String fname,String url)
  23.  {
  24.   name=fname;
  25.   URL=new String[1];
  26.   URL[0]=url;
  27.   URLstatus=new int[1];
  28.   URLstatus[0]=0;
  29.   ok=false;
  30.   hold=false;
  31.   downloader=null;
  32.   retry=-1;
  33.   mindown=0;
  34.   
  35.   if(!isGoodFilename(fname)) localname=genAutoName(fname);
  36.    else localname=fname;
  37.  
  38.   //check for - OK file downloaded.
  39.   checkOK();  
  40.  }
  41.  
  42.  private final void checkOK()
  43.  {
  44.    if(new File(dmachine.download_dir,localname).exists()) 
  45.     {
  46.      ok=true;
  47.      System.out.println("[OK] File "+name+(localname.equals(name)?"":" ("+localname+")")+" already sucessfully downloaded.");
  48.     }
  49.  }
  50.  public final void addURL(String URL)
  51.  {
  52.   // dupe check
  53.   for(int i=this.URL.length-1;i>=0;i--)
  54.    if(this.URL[i].equals(URL)) return;
  55.    
  56.   System.out.println("[QUEUE] Alternate URL "+URL+" added for file "+name);
  57.   
  58.   this.URL=util.addStringToArray(URL,this.URL);
  59.   this.URLstatus=util.incIntArraySize(this.URLstatus);
  60.   mindown=0;
  61.   // unhold
  62.   hold=false; 
  63.   
  64.   if(retry+dmachine.uretry>dmachine.retry)
  65.   {
  66.     retry-=dmachine.uretry; 
  67.     retry=(short)Math.max(0,retry);
  68.   }
  69.   
  70.  }
  71.  
  72.  public final void run()
  73.  {
  74.   downloader=Thread.currentThread();
  75.   int siteidx=-1;
  76.   try
  77.   {
  78.    retry++;
  79.    
  80.    // najit vhodny sajt. Zatim bereme ten prvni co se namane
  81.    if(mindown>=dmachine.uretry || retry>=dmachine.retry) 
  82.       { 
  83.         hold=true; // no more retries
  84.     downloader=null;
  85.     return;
  86.       }
  87.  
  88.    idxscan:while(siteidx==-1)
  89.    {
  90.    for(int i=0;i<URLstatus.length;i++)
  91.     if(URLstatus[i]<=mindown) {siteidx=i;break idxscan;}
  92.    mindown++;
  93.    if(mindown>=dmachine.uretry) 
  94.      { 
  95.        hold=true; // no more retries
  96.        downloader=null;
  97.        return;
  98.      }
  99.    }
  100.    System.out.println("[TRYING] URL="+URL[siteidx]);
  101.    URLstatus[siteidx]++;
  102.    int i; 
  103.    switch(downloadfactory.downloadFactory(URL[siteidx],localname,this))
  104.    {
  105.     case downloadfactory.DOWNLOAD_OK:
  106.        ok=true;hold=true;break;
  107.     case downloadfactory.DOWNLOAD_FATAL:
  108.        URLstatus[siteidx]=dmachine.uretry;break;
  109.     case downloadfactory.DOWNLOAD_BUSY:
  110.        URLstatus[siteidx]--;break; // BUSY FTP server
  111.    }
  112.    
  113.   }
  114.   catch(java.net.UnknownHostException e)
  115.   {
  116.     dmachine.log_fatal("URL="+URL[siteidx]+" err="+
  117.         e.getMessage()+": Host unknown.");
  118.     
  119.     URLstatus[siteidx]=dmachine.uretry;
  120.   }
  121.   catch(IOException e)
  122.    {
  123.     System.out.println("[ERR] "+name+" I/O err="+e);
  124.    }
  125.   catch(Exception e)
  126.    {e.printStackTrace();}
  127.   finally
  128.    {
  129.     downloader=null;
  130.    }
  131.  }
  132.  
  133.  public final boolean equals(Object o)
  134.  {
  135.   qfile q;
  136.   if(o instanceof qfile) { q=(qfile)o; return name.equals(q.name);}
  137.    else return false;
  138.  }
  139.  
  140.  public final void redirect(String from,String to)
  141.  {
  142.   int i=0;
  143.   for(;i<URL.length;i++)
  144.     if(URL[i].equals(from))
  145.       {
  146.         URLstatus[i]=dmachine.uretry; // no longer needed
  147.         System.out.println("[REDIRECT] "+from+" => "+to);
  148.     addURL(to);
  149.         if(!isGoodFilename(name) && genLocalName(to)!=null)
  150.      {
  151.       localname=genLocalName(to);
  152.           checkOK();
  153.       if(ok==true) touch();
  154.      }
  155.     return;
  156.       }
  157.  }
  158.  
  159.  private final static boolean isGoodFilename(String n)
  160.  {
  161.   if(n==null) return false;
  162.   if(n.length()==0) return false;
  163.   if(n.indexOf("/")>-1) return false;
  164.   if(n.indexOf("\\")>-1) return false;
  165.   if(n.indexOf(":")>-1) return false;
  166.   return true;
  167.  }
  168.  
  169.  public final static String genAutoName(String uname)
  170.  {
  171.     java.util.zip.CRC32 crc;
  172.     crc=new java.util.zip.CRC32();
  173.     crc.update(uname.getBytes());
  174.     return dmachine.auto_prefix+Long.toHexString(crc.getValue())+dmachine.auto_suffix;
  175.  }
  176.  
  177.  private final static String genLocalName(String uname)
  178.  {
  179.   String result;
  180.   try
  181.   {
  182.    URL u;
  183.    u=new URL(uname);
  184.    result=dmachine.getFilename(u.getFile());
  185.    if(result==null || result.length()==0) return null; else return result;
  186.   }
  187.   catch (Exception e)
  188.    { return null; }
  189.  }
  190.  
  191.  /* info methods */
  192.  public final String getName()
  193.  {
  194.    return name;
  195.  }
  196.  
  197.  public final String getLocalName()
  198.  {
  199.    return localname;
  200.  }
  201.  
  202. public final boolean isActive()
  203. {
  204.   if(downloader!=null) return true;
  205.    else return false;
  206. }
  207.  
  208.  public final boolean needsDownload()
  209.  {
  210.   if(ok==true) return false;
  211.   if(isActive()) return false;
  212.   if(hold==true) return false;
  213.   return true;
  214.  }
  215.  
  216. /* create auto-touch generated name */
  217.  
  218.  public final void touch()
  219.  {
  220.     File f=new File(dmachine.download_dir,genAutoName(name));
  221.     try
  222.      {
  223.       FileOutputStream fos;
  224.       fos=new FileOutputStream(f.toString(),true);
  225.       for(int i=0;i<URL.length;i++)
  226.       {
  227.         fos.write(URL[i].getBytes());
  228.         fos.write('\n');
  229.       }
  230.       fos.write(localname.getBytes());
  231.       fos.write('\n');
  232.       fos.close();
  233.       System.out.println("[INFO] Touched "+genAutoName(name)+" for "+name);
  234.      }
  235.      catch (IOException io) {}
  236.   }
  237. }
  238.