Author: chatellier Date: 2009-12-10 15:07:57 +0000 (Thu, 10 Dec 2009) New Revision: 2812 Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/ProgressMonitor.java Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SSHSimulatorLauncher.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUtils.java Log: Add progression monitor for ssh transfert Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SSHSimulatorLauncher.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SSHSimulatorLauncher.java 2009-12-10 14:30:26 UTC (rev 2811) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SSHSimulatorLauncher.java 2009-12-10 15:07:57 UTC (rev 2812) @@ -49,6 +49,7 @@ import fr.ifremer.isisfish.simulator.SimulationControl; import fr.ifremer.isisfish.util.ClasspathTemplateLoader; import fr.ifremer.isisfish.util.ssh.InvalidPassphraseException; +import fr.ifremer.isisfish.util.ssh.ProgressMonitor; import fr.ifremer.isisfish.util.ssh.SSHAgent; import fr.ifremer.isisfish.util.ssh.SSHException; import fr.ifremer.isisfish.util.ssh.SSHUserInfo; @@ -703,7 +704,8 @@ String remoteFile = getRemoteResultArchivePath(simulationId); try { - SSHUtils.scpFrom(session, remoteFile, localFile); + ProgressMonitor progress = new ControlProgressMonitor(simulationControl); + SSHUtils.scpFrom(session, remoteFile, localFile, progress); } catch(SSHException e) { localFile.delete(); @@ -725,6 +727,51 @@ } /** + * Redefine a custom progress monitor that update control. + */ + protected static class ControlProgressMonitor extends ProgressMonitor { + + /** Control to update. */ + protected SimulationControl control; + + /** + * Constructor with control. + * + * @param control control + */ + public ControlProgressMonitor(SimulationControl control) { + this.control = control; + } + + /* + * @see fr.ifremer.isisfish.util.ssh.ProgressMonitor#init(long) + */ + @Override + public void init(long max) { + super.init(max); + control.setProgressMax(initFileSize); + } + + /* + * @see fr.ifremer.isisfish.util.ssh.ProgressMonitor#count(long) + */ + @Override + public void count(long len) { + super.count(len); + control.setProgress(totalLength); + } + + /* + * @see fr.ifremer.isisfish.util.ssh.ProgressMonitor#end() + */ + @Override + public void end() { + super.end(); + control.setProgress(initFileSize); + } + } + + /** * Download remote simulation control file and store its content into temp * file. * Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/ProgressMonitor.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/ProgressMonitor.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/ProgressMonitor.java 2009-12-10 15:07:57 UTC (rev 2812) @@ -0,0 +1,50 @@ +/* *##% + * Copyright (C) 2009 Code Lutin, Chatellier Eric + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package fr.ifremer.isisfish.util.ssh; + +/** + * Progress monitor (sftp). + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class ProgressMonitor { + + /** Size of file to download. */ + protected long initFileSize = 0; + + /** Current dowloaded length. */ + protected long totalLength = 0; + + public void init(long max) { + initFileSize = max; + totalLength = 0; + } + + public void count(long len) { + totalLength += len; + } + + public void end() { + + } +} Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/ProgressMonitor.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUtils.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUtils.java 2009-12-10 14:30:26 UTC (rev 2811) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUtils.java 2009-12-10 15:07:57 UTC (rev 2812) @@ -238,11 +238,26 @@ * @param remoteFileName remote file name to download * @param localFile local file name to download into * - * @throws SSHException if transfer fail (include md5 control failed) + * @throws SSHException if transfer fail */ public static void scpFrom(Session session, String remoteFileName, File localFile) throws SSHException { + scpFrom(session, remoteFileName, localFile, null); + } + /** + * Download a local file from remote server and check md5sum. + * + * @param session opened valid jsch session + * @param remoteFileName remote file name to download + * @param localFile local file name to download into + * @param monitor progress monitor (can be null) + * + * @throws SSHException if transfer fail + */ + public static void scpFrom(Session session, String remoteFileName, + File localFile, ProgressMonitor monitor) throws SSHException { + String command = "scp -f -r \"" + remoteFileName + "\""; ChannelExec channel = null; @@ -257,7 +272,7 @@ channel.connect(); sendAck(out); - startRemoteCpProtocol(in, out, localFile); + startRemoteCpProtocol(in, out, localFile, monitor); } catch (IOException e) { throw new SSHException(e); } catch (JSchException e) { @@ -292,7 +307,7 @@ // scp protected static void startRemoteCpProtocol(InputStream in, - OutputStream out, File localFile) throws IOException, SSHException { + OutputStream out, File localFile, ProgressMonitor monitor) throws IOException, SSHException { File startFile = localFile; while (true) { // C0644 filesize filename - header for a regular file @@ -311,7 +326,7 @@ } String serverResponse = stream.toString("UTF-8"); if (serverResponse.charAt(0) == 'C') { - parseAndFetchFile(serverResponse, startFile, out, in); + parseAndFetchFile(serverResponse, startFile, out, in, monitor); } else if (serverResponse.charAt(0) == 'D') { startFile = parseAndCreateDirectory(serverResponse, startFile); sendAck(out); @@ -344,7 +359,7 @@ // scp protected static void parseAndFetchFile(String serverResponse, - File localFile, OutputStream out, InputStream in) + File localFile, OutputStream out, InputStream in, ProgressMonitor monitor) throws IOException, SSHException { int start = 0; int end = serverResponse.indexOf(" ", start + 1); @@ -355,29 +370,27 @@ log.debug("Receiving: " + filename + " : " + filesize); File transferFile = (localFile.isDirectory()) ? new File(localFile, filename) : localFile; - fetchFile(transferFile, filesize, out, in); + fetchFile(transferFile, filesize, out, in, monitor); waitForAck(in); sendAck(out); } // scp protected static void fetchFile(File localFile, long filesize, - OutputStream out, InputStream in) throws IOException { + OutputStream out, InputStream in, ProgressMonitor monitor) throws IOException { byte[] buf = new byte[BUFFER_SIZE]; sendAck(out); + // case were progression is requested + if (monitor != null) { + monitor.init(filesize); + } + // read a content of lfile FileOutputStream fos = new FileOutputStream(localFile); int length; long totalLength = 0; - // only track progress for files larger than 100kb in verbose mode - boolean trackProgress = filesize > HUNDRED_KILOBYTES; - // since filesize keeps on decreasing we have to store the - // initial filesize - long initFilesize = filesize; - int percentTransmitted = 0; - try { while (true) { length = in.read(buf, 0, (BUFFER_SIZE < filesize) ? BUFFER_SIZE @@ -392,15 +405,20 @@ break; } - if (trackProgress) { - percentTransmitted = trackProgress(initFilesize, - totalLength, percentTransmitted); + // case were progression is requested + if (monitor != null) { + monitor.count(length); } } } finally { fos.flush(); fos.close(); } + + // case were progression is requested + if (monitor != null) { + monitor.end(); + } } // scp @@ -444,13 +462,6 @@ byte[] buf = new byte[BUFFER_SIZE]; long totalLength = 0; - // only track progress for files larger than 100kb in verbose mode - boolean trackProgress = filesize > HUNDRED_KILOBYTES; - // since filesize keeps on decreasing we have to store the - // initial filesize - long initFilesize = filesize; - int percentTransmitted = 0; - try { while (true) { int len = fis.read(buf, 0, buf.length); @@ -459,11 +470,6 @@ } out.write(buf, 0, len); totalLength += len; - - if (trackProgress) { - percentTransmitted = trackProgress(initFilesize, - totalLength, percentTransmitted); - } } out.flush(); sendAck(out); @@ -476,7 +482,7 @@ /** * Progress monitor (sftp). */ - protected static class ProgressMonitor implements SftpProgressMonitor { + protected static class UtilsProgressMonitor implements SftpProgressMonitor { private long initFileSize = 0; private long totalLength = 0; private int percentTransmitted = 0; @@ -607,7 +613,7 @@ SftpProgressMonitor monitor = null; boolean trackProgress = totalLength > HUNDRED_KILOBYTES; if (trackProgress) { - monitor = new ProgressMonitor(); + monitor = new UtilsProgressMonitor(); } channel.get(remoteFile, localFile.getAbsolutePath(), monitor); } @@ -666,7 +672,7 @@ SftpProgressMonitor monitor = null; if (trackProgress) { - monitor = new ProgressMonitor(); + monitor = new UtilsProgressMonitor(); } channel.put(localFile.getAbsolutePath(), remotePath, monitor);