org/objectweb/proactive/filetransfer/FileTransfer.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.filetransfer;
00032 
00033 import java.io.File;
00034 import java.io.IOException;
00035 import java.util.Vector;
00036 
00037 import org.apache.log4j.Logger;
00038 import org.objectweb.proactive.core.ProActiveException;
00039 import org.objectweb.proactive.core.filetransfer.FileBlock;
00040 import org.objectweb.proactive.core.filetransfer.FileTransferEngine;
00041 import org.objectweb.proactive.core.filetransfer.FileTransferRequest;
00042 import org.objectweb.proactive.core.filetransfer.FileTransferService;
00043 import org.objectweb.proactive.core.filetransfer.OperationStatus;
00044 import org.objectweb.proactive.core.node.Node;
00045 import org.objectweb.proactive.core.util.log.Loggers;
00046 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00047 
00048 
00055 public class FileTransfer {
00056     static Logger logger = ProActiveLogger.getLogger(Loggers.FILETRANSFER);
00057 
00068     public static FileVector pullFile(Node node, File srcFile, File dstFile)
00069         throws IOException, ProActiveException {
00070         return pullFile(node, srcFile, dstFile, FileBlock.DEFAULT_BLOCK_SIZE,
00071             FileTransferService.DEFAULT_MAX_SIMULTANEOUS_BLOCKS);
00072     }
00073 
00074     public static FileVector pullFile(Node node, File srcFile, File dstFile,
00075         int bsize, int numFlyingBlocks) throws IOException, ProActiveException {
00076         File[] src = new File[1];
00077         File[] dst = new File[1];
00078         src[0] = srcFile;
00079         dst[0] = dstFile;
00080 
00081         return pullFiles(node, src, dst, bsize, numFlyingBlocks);
00082     }
00083 
00089     public static FileVector pullFiles(Node node, File[] srcFile, File[] dstFile)
00090         throws IOException, ProActiveException {
00091         return pullFiles(node, srcFile, dstFile, FileBlock.DEFAULT_BLOCK_SIZE,
00092             FileTransferService.DEFAULT_MAX_SIMULTANEOUS_BLOCKS);
00093     }
00094 
00095     public static FileVector pullFiles(Node node, File[] srcFile,
00096         File[] dstFile, int bsize, int numFlyingBlocks)
00097         throws IOException, ProActiveException {
00098         if (srcFile.length != dstFile.length) {
00099             throw new ProActiveException(
00100                 "Error, number destination and source file lists do not match in length");
00101         }
00102 
00103         FileVector fileWrapper = new FileVector();
00104         if (srcFile.length == 0) {
00105             return fileWrapper;
00106         }
00107 
00108         for (int i = 0; i < srcFile.length; i++) {
00109             // local verification
00110             if (dstFile[i].exists() && !dstFile[i].canWrite()) {
00111                 logger.error("Can't write to " + dstFile[i].getAbsoluteFile());
00112                 throw new IOException("Can't write to " +
00113                     dstFile[i].getAbsoluteFile());
00114             }
00115         }
00116         try {
00117             FileTransferService ftsRemote = FileTransferEngine.getFileTransferEngine()
00118                                                               .getFTS(node);
00119             FileTransferService ftsLocal = FileTransferEngine.getFileTransferEngine()
00120                                                              .getFTS();
00121 
00122             // We could ask the remote AO to send the file to us
00123             // futureFile = ftsRemote.sendFile(ftsLocal, srcFile, dstFile,
00124             // bsizem, numFlyingBlocks);
00125             for (int i = 0; i < srcFile.length; i++) {
00126                 OperationStatus futureFile = ftsLocal.receiveFile(ftsRemote,
00127                         srcFile[i], dstFile[i], bsize, numFlyingBlocks);
00128                 FileTransferRequest ftr = new FileTransferRequest(srcFile[i],
00129                         dstFile[i], futureFile, ftsRemote, ftsLocal);
00130                 fileWrapper.add(ftr);
00131             }
00132 
00133             //ftsLocal.putInThePool(ftsLocal);
00134             //TODO put the ftsRemote back into the pool
00135             return fileWrapper;
00136         } catch (Exception e) {
00137             e.printStackTrace();
00138             throw new ProActiveException(
00139                 "Unable to connect or use ProActive Node: " + node);
00140         }
00141     }
00142 
00153     public static FileVector pushFile(Node node, File srcFile, File dstFile)
00154         throws IOException, ProActiveException {
00155         return pushFile(node, srcFile, dstFile, FileBlock.DEFAULT_BLOCK_SIZE,
00156             FileTransferService.DEFAULT_MAX_SIMULTANEOUS_BLOCKS);
00157     }
00158 
00159     public static FileVector pushFile(Node node, File srcFile, File dstFile,
00160         int bsize, int numFlyingBlocks) throws IOException, ProActiveException {
00161         File[] src = new File[1];
00162         File[] dst = new File[1];
00163         src[0] = srcFile;
00164         dst[0] = dstFile;
00165 
00166         return pushFiles(node, src, dst, bsize, numFlyingBlocks);
00167     }
00168 
00173     public static FileVector pushFiles(Node node, File[] srcFile, File[] dstFile)
00174         throws IOException, ProActiveException {
00175         return pushFiles(node, srcFile, dstFile, FileBlock.DEFAULT_BLOCK_SIZE,
00176             FileTransferService.DEFAULT_MAX_SIMULTANEOUS_BLOCKS);
00177     }
00178 
00179     public static FileVector pushFiles(Node node, File[] srcFile,
00180         File[] dstFile, int bsize, int numFlyingBlocks)
00181         throws IOException, ProActiveException {
00182         if (srcFile.length != dstFile.length) {
00183             throw new ProActiveException(
00184                 "Error, destination and source file lists do not match in length");
00185         }
00186 
00187         FileVector fileVector = new FileVector();
00188 
00189         if (srcFile.length == 0) {
00190             return fileVector;
00191         }
00192 
00193         for (int i = 0; i < srcFile.length; i++) {
00194             // local verification
00195             if (!srcFile[i].canRead()) {
00196                 logger.error("Can't read or doesn't exist: " +
00197                     srcFile[i].getAbsoluteFile());
00198                 throw new IOException("Can't read or doesn't exist: " +
00199                     srcFile[i].getAbsoluteFile());
00200             }
00201         }
00202         try {
00203             FileTransferService ftsLocal = FileTransferEngine.getFileTransferEngine()
00204                                                              .getFTS();
00205             FileTransferService ftsRemote = FileTransferEngine.getFileTransferEngine()
00206                                                               .getFTS(node);
00207 
00208             // We ask the local AO to send the file to the remote AO
00209             for (int i = 0; i < srcFile.length; i++) {
00210                 OperationStatus f = ftsLocal.sendFile(ftsRemote, srcFile[i],
00211                         dstFile[i], bsize, numFlyingBlocks); // this call is asynchronous
00212                 fileVector.add(new FileTransferRequest(srcFile[i], dstFile[i],
00213                         f, ftsLocal, ftsRemote));
00214             }
00215 
00216             //ftsLocal.putInThePool(ftsLocal);
00217             //TODO put the ftsRemote back into the pool
00218             return fileVector;
00219         } catch (Exception e) {
00220             e.printStackTrace();
00221             throw new ProActiveException(
00222                 "Unable to connect or use ProActive Node: " + node);
00223         }
00224     }
00225 
00226     static public FileVector pushFile(Node node, FileVector srcFile, File dst)
00227         throws IOException, ProActiveException {
00228         File[] f = new File[1];
00229         f[0] = dst;
00230 
00231         return pushFiles(node, srcFile, f);
00232     }
00233 
00244     static public FileVector pushFiles(Node node, FileVector srcFile, File[] dst)
00245         throws ProActiveException {
00246         if (srcFile.size() != dst.length) {
00247             throw new ProActiveException(
00248                 "Error, destination and source file lists do not match in length");
00249         }
00250 
00251         FileTransferService ftsLocal = FileTransferEngine.getFileTransferEngine()
00252                                                          .getFTS();
00253         FileTransferService ftsRemote = FileTransferEngine.getFileTransferEngine()
00254                                                           .getFTS(node);
00255 
00256         //For each file, send
00257         Vector requests = srcFile.getFilesRequest();
00258         FileVector fileWrapper = new FileVector();
00259         for (int i = 0; i < requests.size(); i++) {
00260             FileTransferRequest ftr = (FileTransferRequest) requests.get(i);
00261 
00262             /* The source file is the previous operation's destination.
00263              * The future of the previous operation is passed to know
00264              * when the previous operation has finished.
00265              */
00266             FileTransferRequest newFTR = new FileTransferRequest(ftr.getDstFile(),
00267                     dst[i], null, ftr.getDestinationFTS(), ftsRemote);
00268             OperationStatus future = ftsLocal.submitFileTransferRequest(ftsLocal,
00269                     newFTR, ftr.getOperationFuture()); //async call
00270             newFTR.setDstFuture(future); //we keep a future on the remote file
00271             fileWrapper.add(newFTR);
00272         }
00273 
00274         return fileWrapper;
00275     }
00276 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1