diff options
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/DownloadTask.java | 171 | ||||
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java | 135 |
2 files changed, 306 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/DownloadTask.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/DownloadTask.java new file mode 100644 index 00000000..aebaf732 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/DownloadTask.java @@ -0,0 +1,171 @@ +package org.openslx.dozmod.filetransfer; + +import java.io.File; +import java.io.RandomAccessFile; + +import javax.swing.JOptionPane; +import javax.swing.SwingWorker; + +import org.apache.log4j.Logger; +import org.openslx.filetransfer.DataReceivedCallback; +import org.openslx.filetransfer.Downloader; +import org.openslx.filetransfer.FileRange; +import org.openslx.filetransfer.WantRangeCallback; + +/** + * Execute file download in a background thread and update the progress. + * + * @author www.codejava.net + * + */ +public class DownloadTask extends SwingWorker<Void, Void> { + + /** + * Logger instance for this class. + */ + private final static Logger LOGGER = Logger.getLogger(DownloadTask.class); + + private static final double UPDATE_INTERVAL_SECONDS = 0.6; + private static final double UPDATE_INTERVAL_MS = UPDATE_INTERVAL_SECONDS * 1000; + private static final double BYTES_PER_MIB = 1024 * 1024; + private static final long CHUNK_SIZE = 16 * 1024 * 1024; + + private final String host; + private final int port; + private final String downloadToken; + private final String saveDir; + private final long fileSize; + private boolean success = false; + + public DownloadTask(String host, int port, String downloadToken, String saveDir, long fileSize) { + this.host = host; + this.port = port; + this.downloadToken = downloadToken; + this.saveDir = saveDir; + this.fileSize = fileSize; + } + + class Callbacks implements WantRangeCallback, DataReceivedCallback { + // initialize the counters needed for speed calculations + private long currentRequestedOffset = -1; + private long totalBytesRead = 0; + private long lastUpdate = 0; + private long lastBytes = 0; + private long currentBytes = 0; + private final RandomAccessFile file; + + public Callbacks(RandomAccessFile file) { + this.file = file; + } + + @Override + public FileRange get() { + if (currentRequestedOffset == -1) + currentRequestedOffset = 0; + else + currentRequestedOffset += CHUNK_SIZE; + if (currentRequestedOffset >= fileSize) + return null; + long end = currentRequestedOffset + CHUNK_SIZE; + if (end > fileSize) + end = fileSize; + return new FileRange(currentRequestedOffset, end); + } + + @Override + public boolean dataReceived(final long fileOffset, final int dataLength, final byte[] data) { + try { + file.seek(fileOffset); + file.write(data, 0, dataLength); + } catch (Exception e) { + LOGGER.error("Could not write to file at offset " + fileOffset, e); + return false; + } + currentBytes += dataLength; + totalBytesRead += dataLength; + final long now = System.currentTimeMillis(); + if (lastUpdate + UPDATE_INTERVAL_MS < now) { + final int percentCompleted = (int) ((totalBytesRead * 100) / fileSize); + setProgress(percentCompleted); + lastBytes = (lastBytes * 2 + currentBytes) / 3; + final double speed = lastBytes / UPDATE_INTERVAL_SECONDS; + firePropertyChange("speed", 0, speed / BYTES_PER_MIB); + firePropertyChange("bytesread", 0, totalBytesRead); + lastUpdate = now; + currentBytes = 0; + } + return true; + } + + } + + /** + * Executed in background thread + */ + @Override + protected Void doInBackground() throws Exception { + boolean ret = false; + // show filesize in the GUI + firePropertyChange("filesize", 0, fileSize); + + Downloader download = null; + RandomAccessFile file = null; + try { + download = new Downloader(host, port, null, downloadToken); // TODO: SSL + try { + file = new RandomAccessFile(new File(saveDir), "rw"); + } catch (Exception e2) { + JOptionPane.showMessageDialog(null, "Could not open destination file:\n" + saveDir + "\n" + + e2.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); + e2.printStackTrace(); + setProgress(0); + return null; + } + + Callbacks cb = new Callbacks(file); + + ret = download.download(cb, cb); + } finally { + if (file != null) { + try { + file.close(); + } catch (Exception e) { + } + } + if (download != null) + download.close(null); + } + + // if the download succeeded, set the progress to 100% manually again here to make + // sure the GUI knows about it. + if (ret) { + setProgress(100); + firePropertyChange("bytesread", 0, fileSize); + firePropertyChange("success", false, true); + success = true; + } + + return null; + } + + /** + * Executed in Swing's event dispatching thread + */ + @Override + protected void done() { + if (isCancelled()) + return; + if (success) { + LOGGER.info("Datei erfolgreich heruntergeladen."); + String vmxResult = ""; + JOptionPane.showMessageDialog(null, "Datei erfolgreich heruntergeladen. " + vmxResult, "Message", + JOptionPane.INFORMATION_MESSAGE); + } else { + LOGGER.error("Datei wurde unvollständig heruntergeladen."); + JOptionPane.showMessageDialog(null, + "Datei wurde unvollständig heruntergeladen. Bitte wiederholen.", "Message", + JOptionPane.INFORMATION_MESSAGE); + } + } + +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java new file mode 100644 index 00000000..c240c5d5 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java @@ -0,0 +1,135 @@ +package org.openslx.dozmod.filetransfer; + +import java.io.File; +import java.io.IOException; + +import javax.swing.JOptionPane; +import javax.swing.SwingWorker; + +import org.apache.log4j.Logger; +import org.openslx.filetransfer.UploadStatusCallback; +import org.openslx.filetransfer.Uploader; + +/** + * Executes the file upload in a background thread and updates progress to + * listeners that implement the java.beans.PropertyChangeListener interface. + * + * @author www.codejava.net + * + */ +public class UploadTask extends SwingWorker<Void, Void> { + + /** + * Logger instance for this class. + */ + private final static Logger LOGGER = Logger.getLogger(UploadTask.class); + + private static final double UPDATE_INTERVAL_SECONDS = 0.6; + private static final double UPDATE_INTERVAL_MS = UPDATE_INTERVAL_SECONDS * 1000; + private static final double BYTES_PER_MIB = 1024 * 1024; + + private final String host; + private final int port; + private final String uploadToken; + private final File uploadFile; + private boolean success = false; + + public UploadTask(String host, int port, String uploadToken, File uploadFile) { + // TODO: SSL + this.host = host; + this.port = port; + this.uploadToken = uploadToken; + this.uploadFile = uploadFile; + + } + + /** + * Executed in background thread + */ + @Override + protected Void doInBackground() { + + Uploader upload = null; + try { + upload = new Uploader(host, port, null, uploadToken); // TODO: SSL + + final Uploader ul = upload; + /* BROKEN CODE - FIXME class imports + final long fileSize = Image.Filesize = uploadFile.length(); + BROKEN CODE - FIXME class imports */ + final long fileSize = 1000000; + + + firePropertyChange("filesize", 0, fileSize); // Updates GUI + + final boolean ret = upload.upload(uploadFile.getAbsolutePath(), new UploadStatusCallback() { + // progress counter + private long totalBytesRead = 0; + // initialize the counters needed for speed calculations + private long lastUpdate = 0; + private long lastBytes = 0; + private long currentBytes = 0; + + @Override + public void uploadProgress(long bytesSent) { + totalBytesRead += bytesSent; + currentBytes += bytesSent; + final long now = System.currentTimeMillis(); + if (lastUpdate + UPDATE_INTERVAL_MS < now) { + final int percentCompleted = (int) ((totalBytesRead * 100) / fileSize); + setProgress(percentCompleted); + lastBytes = (lastBytes * 2 + currentBytes) / 3; + final double speed = lastBytes / UPDATE_INTERVAL_SECONDS; + firePropertyChange("speed", 0, speed / BYTES_PER_MIB); + firePropertyChange("bytesread", 0, totalBytesRead); + lastUpdate = now; + currentBytes = 0; + } + } + + @Override + public void uploadError(String message) { + JOptionPane.showMessageDialog(null, "Error uploading file: " + message, "Error", + JOptionPane.ERROR_MESSAGE); + ul.close(null); + } + }); + + // if the upload succeeded, set the progress to 100% manually again here to make + // sure the GUI knows about it. + if (ret) { + setProgress(100); + firePropertyChange("bytesread", 0, uploadFile.length()); + firePropertyChange("success", false, true); + success = true; + } + } catch (IOException e) { + JOptionPane.showMessageDialog(null, "Error uploading file: " + e.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + LOGGER.error("Upload of " + uploadFile.getAbsolutePath() + " failed!", e); + } finally { + if (upload != null) + upload.close(null); + } + + return null; + } + + /** + * Executed in Swing's event dispatching thread + */ + @Override + protected void done() { + if (isCancelled()) + return; + if (success) { + LOGGER.info("Datei erfolgreich hochgeladen."); + JOptionPane.showMessageDialog(null, "Datei erfolgreich hochgeladen.", "Message", + JOptionPane.INFORMATION_MESSAGE); + } else { + LOGGER.error("Datei wurde unvollständig hochgeladen."); + JOptionPane.showMessageDialog(null, "Datei wurde unvollständig hochgeladen. Bitte wiederholen.", + "Message", JOptionPane.INFORMATION_MESSAGE); + } + } +} |