[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/imagex/postlet/rev_unkn/ -> UploadManager.java (source)

   1  /*    Copyright (C) 2005 Simon David Rycroft
   2  
   3      This program is free software; you can redistribute it and/or
   4      modify it under the terms of the GNU General Public License
   5      as published by the Free Software Foundation; either version 2
   6      of the License, or (at your option) any later version.
   7  
   8      This program is distributed in the hope that it will be useful,
   9      but WITHOUT ANY WARRANTY; without even the implied warranty of
  10      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11      GNU General Public License for more details.
  12  
  13      You should have received a copy of the GNU General Public License
  14      along with this program; if not, write to the Free Software
  15      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16  
  17  import java.io.File;
  18  import java.net.*;
  19  import java.io.IOException;
  20  
  21  // Note, the upload manager extends Thread so that the GUI is
  22  // still responsive, and updates.
  23  public class UploadManager extends Thread {
  24  
  25      File [] files;
  26      Main main;
  27      URL destination;
  28      private int maxThreads = 5;
  29      private UploadThread upThreads[];
  30      private boolean cancelUpload;
  31  
  32      /** Creates a new instance of Upload */
  33      public UploadManager(File [] f, Main m, URL d){
  34          files = f;
  35          main = m;
  36          destination = d;
  37          cancelUpload = false;
  38      }
  39  
  40      public UploadManager(File [] f, Main m, URL d, int max){
  41          try {
  42              if (max>5 || max < 1)
  43                  max = 5;
  44              maxThreads = max;
  45          }
  46          catch (NullPointerException npe){
  47              maxThreads = 5;// Leave the maxThreads as default
  48          }
  49          files = f;
  50          main = m;
  51          destination = d;
  52      }
  53      
  54      public void cancelUpload(){
  55          cancelUpload = true;
  56          for(int i=0;i<files.length;i++){
  57              try {
  58                  upThreads[i].cancelUpload();
  59              } catch (NullPointerException npe){
  60                  main.errorMessage("Cancelled unknown");//No need really to do anything here
  61              }
  62          }
  63      }
  64  
  65      public void run() {
  66          upThreads = new UploadThread[files.length];
  67          for(int i=0; i<files.length; i+=maxThreads) {
  68              if(!cancelUpload){
  69                  //UploadThread u = new UploadThread(destination,files[i], main);
  70                  //u.upload();
  71                  int j=0;
  72                  while(j<maxThreads && (i+j)<files.length)
  73                  {
  74                      try{
  75                          upThreads[i+j] = new UploadThread(destination,files[i+j], main);
  76                          upThreads[i+j].start();}
  77                      catch(UnknownHostException uhe) {System.out.println("*** UnknownHostException: UploadManager ***");}
  78                      catch(IOException ioe)            {System.out.println("*** IOException: UploadManager ***");}
  79                      j++;
  80                  }
  81                  // wait for the last one to started to finish (means there may be others running still FIXME!
  82                  while(upThreads[i+j-1].isAlive()){;}
  83              }
  84          }
  85      }
  86  
  87      private void urlFailure(){
  88          // Output a message explaining that the URL has failed.
  89          // This should stop all the threads!
  90      }
  91  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7