diff options
| author | Simon Rettberg | 2015-07-21 12:41:11 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-07-21 12:41:11 +0200 |
| commit | 0b99078ca318bb8ef79884994ee8835b72b195e0 (patch) | |
| tree | 5f2b951c8e929f11de0172a88958f6253da91585 /dozentenmodul/src/main/java/org | |
| parent | [client] checkbox in tablecolumns [WIP] far from ready (diff) | |
| download | tutor-module-0b99078ca318bb8ef79884994ee8835b72b195e0.tar.gz tutor-module-0b99078ca318bb8ef79884994ee8835b72b195e0.tar.xz tutor-module-0b99078ca318bb8ef79884994ee8835b72b195e0.zip | |
[client] Created TransferTask base class for uploads and downloads
Diffstat (limited to 'dozentenmodul/src/main/java/org')
7 files changed, 372 insertions, 205 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/Config.java b/dozentenmodul/src/main/java/org/openslx/dozmod/Config.java index df1894bc..3ef0edcf 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/Config.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/Config.java @@ -9,9 +9,9 @@ import java.io.InputStream; import java.util.Properties; import org.apache.log4j.Logger; -import org.openslx.dozmod.util.FormatHelper; import org.openslx.util.QuickTimer; import org.openslx.util.QuickTimer.Task; +import org.openslx.util.Util; /** * Represents the configuration of the client @@ -334,7 +334,7 @@ public class Config { * @return */ private static int getInteger(String key, int defaultValue) { - return FormatHelper.parseInt(prop.getProperty(key), defaultValue); + return Util.parseInt(prop.getProperty(key), defaultValue); } /** diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferEvent.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferEvent.java index ff86e3ac..890b4193 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferEvent.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferEvent.java @@ -4,7 +4,7 @@ import org.openslx.bwlp.thrift.iface.TransferState; import org.openslx.dozmod.util.FormatHelper; public class TransferEvent { - + /** * Block-based progress of transfer */ @@ -14,7 +14,7 @@ public class TransferEvent { */ public final String speed; /** - * Estimated remaining time, human readable + * Estimated remaining time, human readable. <code>null</code> == not updated */ public final String remaining; /** @@ -22,7 +22,7 @@ public class TransferEvent { */ public final long speedRaw; /** - * Estimated remaining time, in milliseconds + * Estimated remaining time, in milliseconds. 0 == not updated */ public final long remainingRaw; /** @@ -33,14 +33,19 @@ public class TransferEvent { * An optional error message */ public final String errorMessage; - - public TransferEvent(TransferState state, byte[] progress, long speedRaw, long remainingRaw, String errorMessage) { + + public TransferEvent(TransferState state, byte[] progress, long speedRaw, long remainingRaw, + String errorMessage) { this.state = state; this.progress = progress; this.speedRaw = speedRaw; - this.remainingRaw = remainingRaw; this.speed = FormatHelper.byteToGigabyte(speedRaw, false) + "/s"; - this.remaining = FormatHelper.formatMilliseconds(remainingRaw); + this.remainingRaw = remainingRaw; + if (remainingRaw == 0) { + this.remaining = null; + } else { + this.remaining = FormatHelper.formatMilliseconds(remainingRaw, false); + } this.errorMessage = errorMessage; } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferTask.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferTask.java new file mode 100644 index 00000000..b54c2049 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferTask.java @@ -0,0 +1,198 @@ +package org.openslx.dozmod.filetransfer; + +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.openslx.bwlp.thrift.iface.TransferState; +import org.openslx.filetransfer.Transfer; +import org.openslx.util.Util; + +public abstract class TransferTask implements Runnable { + + protected static final double BYTES_PER_MIB = 1024 * 1024; + protected static final long CHUNK_SIZE = 16 * 1024 * 1024; + + /** + * Update interval of transfer status (speed only) + */ + protected static final double UPDATE_INTERVAL_SECONDS = 0.6; + protected static final int UPDATE_INTERVAL_MS = (int) (UPDATE_INTERVAL_SECONDS * 1000); + + protected final List<TransferThread> transfers = new ArrayList<>(); + private final List<TransferThread> connectingTransfers = new ArrayList<>(); + + /** + * List of listeners that want to get status updates about the transfer. + */ + private final List<TransferEventListener> listeners = new ArrayList<>(); + + private volatile boolean isRunning = true; + private volatile boolean isCancelled = false; + private boolean endgame = false; + private int minConnectionCount = 1; + private int failCount = 0; + + protected final File localFile; + protected final long fileSize; + + protected TransferTask(File localFile, long fileSize) { + this.localFile = localFile; + this.fileSize = fileSize; + } + + @Override + public final void run() { + while (!isCancelled && !Thread.interrupted()) { + TransferEvent event = getTransferEvent(); + if (event != null) { + fireEvent(event); + if (event.state == TransferState.ERROR || event.state == TransferState.FINISHED) + break; + } + ensureActivity(); + Util.sleep(UPDATE_INTERVAL_MS); + } + for (TransferThread t : transfers) { + t.getTransfer().cancel(); + t.interrupt(); + Util.joinThread(t); + } + for (TransferThread t : connectingTransfers) { + t.getTransfer().cancel(); + t.interrupt(); + Util.joinThread(t); + } + isRunning = false; + } + + public boolean isRunning() { + return isRunning; + } + + private void fireEvent(TransferEvent event) { + synchronized (listeners) { + for (TransferEventListener listener : listeners) { + listener.update(event); + } + } + } + + protected void fireErrorMessage(String message) { + TransferEvent event = new TransferEvent(null, null, 0, 0, message); + fireEvent(event); + } + + protected abstract TransferEvent getTransferEvent(); + + public final void setMinConnections(int count) { + synchronized (transfers) { + this.minConnectionCount = count; + } + } + + public void addListener(TransferEventListener listener) { + synchronized (listeners) { + listeners.add(listener); + } + } + + public void removeListener(TransferEventListener listener) { + synchronized (listeners) { + while (listeners.remove(listener)) { + } + } + } + + public void cancel() { + synchronized (transfers) { + for (TransferThread t : transfers) { + t.getTransfer().cancel(); + } + for (TransferThread t : connectingTransfers) { + t.getTransfer().cancel(); + } + isCancelled = true; + } + } + + private final void ensureActivity() { + synchronized (transfers) { + if (endgame && (!transfers.isEmpty() || !connectingTransfers.isEmpty())) + return; + Iterator<TransferThread> it = transfers.iterator(); + while (it.hasNext()) { + if (!it.next().getTransfer().isValid()) { + it.remove(); + } + } + + if (transfers.size() + connectingTransfers.size() < minConnectionCount) { + TransferThread thread = createNewThread(); + if (thread != null) { + connectingTransfers.add(thread); + } + } + } + } + + protected abstract TransferThread createNewThread(); + + protected final void connectFailed(TransferThread thread) { + synchronized (transfers) { + connectingTransfers.remove(thread); + if (transfers.size() > 0) + return; + failCount++; + } + } + + protected final void connectSucceeded(TransferThread thread) { + synchronized (transfers) { + connectingTransfers.remove(thread); + transfers.add(thread); + failCount = 0; + } + } + + protected final void transferEnded(TransferThread thread, boolean success) { + synchronized (transfers) { + transfers.remove(thread); + if (endgame && !success && transfers.isEmpty()) { + // We had a transfer that reported success before, so we assume there are no more pending blocks + // not being actively transfered already. Only trigger a new upload if this was the last active + // transfer and it failed. This also resets endgame mode. + endgame = false; + } else if (!endgame && success) { + endgame = true; + } + } + if (!endgame) { + ensureActivity(); + } + } + + /** + * Get the number of consecutive connection fails. This counter is only + * increased if there is no active transfer running, and is reset as soon as + * one transfer successfully connects. + * + * @return connect fails + */ + public final int getConnectFailCount() { + synchronized (transfers) { + return failCount; + } + } + + protected abstract static class TransferThread extends Thread { + @Override + public abstract void run(); + + protected abstract Transfer getTransfer(); + + public abstract long getCurrentSpeed(); + } + +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java index ad7a3eab..779344a3 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java @@ -1,9 +1,7 @@ package org.openslx.dozmod.filetransfer; import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.io.FileNotFoundException; import org.apache.log4j.Logger; import org.apache.thrift.TException; @@ -11,187 +9,149 @@ import org.openslx.bwlp.thrift.iface.TInvalidTokenException; import org.openslx.bwlp.thrift.iface.TransferState; import org.openslx.bwlp.thrift.iface.TransferStatus; import org.openslx.dozmod.Config; +import org.openslx.filetransfer.Transfer; import org.openslx.filetransfer.UploadStatusCallback; import org.openslx.filetransfer.Uploader; import org.openslx.thrifthelper.ThriftManager; -import org.openslx.util.QuickTimer; -import org.openslx.util.QuickTimer.Task; /** * Executes the file upload in a background thread and updates progress to * listeners. */ -public class UploadTask implements Runnable { +public class UploadTask extends TransferTask { /** * Logger instance for this class. */ private final static Logger LOGGER = Logger.getLogger(UploadTask.class); - - /** - * Update interval of transfer status (speed only) - */ - private static final double UPDATE_INTERVAL_SECONDS = 0.6; - private static final long UPDATE_INTERVAL_MS = (long) (UPDATE_INTERVAL_SECONDS * 1000); /** * Update interval of the block progress (needs thrift call to sat) */ private static final double THRIFT_INTERVAL_SECONDS = 1.7; - private static final long THRIFT_INTERVAL_MS = (long) (THRIFT_INTERVAL_SECONDS * 1000); + private static final int THRIFT_INTERVAL_MS = (int) (THRIFT_INTERVAL_SECONDS * 1000); private final String host; private final int port; private final String uploadToken; - private final File uploadFile; - Uploader upload = null; - - /** - * List of listeners that want to get status updates about the transfer. - */ - private final List<TransferEventListener> listeners = new ArrayList<>(); - - public UploadTask(String host, int port, String uploadToken, File uploadFile) { + public UploadTask(String host, int port, String uploadToken, File uploadFile) + throws FileNotFoundException { + super(uploadFile, uploadFile.length()); + if (!uploadFile.canRead()) + throw new FileNotFoundException(); // TODO: SSL this.host = host; this.port = port; this.uploadToken = uploadToken; - this.uploadFile = uploadFile; } - /** - * Executed in background thread - */ - @Override - public void run() { - try { - synchronized (this) { - if (upload != null) - throw new IllegalStateException("Cannot launch UploadTask twice!"); - upload = new Uploader(host, port, Config.TRANSFER_TIMEOUT, null, uploadToken); // TODO: SSL - } - final Uploader uploader = upload; - final long fileSize = uploadFile.length(); + private class UploadThread extends TransferThread { +// private long totalBytesRead = 0; + private long currentSpeed = 0; - final boolean ret = uploader.upload(uploadFile.getAbsolutePath(), new UploadStatusCallback() { + @Override + public void run() { + final Uploader uploader; + try { + uploader = new Uploader(host, port, Config.TRANSFER_TIMEOUT, null, uploadToken); + } catch (Exception e) { + LOGGER.warn("Could not initialize new uploader", e); + connectFailed(this); + return; + } // TODO: SSL + connectSucceeded(this); + final UploadThread thread = this; + + final boolean ret = uploader.upload(localFile.getAbsolutePath(), new UploadStatusCallback() { // progress counter - private long totalBytesRead = 0; - // initialize the counters needed for speed calculations - private long lastThriftUpdate = 0; + private long currentBytes = 0; 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) { - // Calculate updated speed - lastBytes = (lastBytes * 2 + currentBytes) / 3; - final long speed = (1000 * lastBytes) / (now - lastUpdate); - final long timeRemaining = (fileSize - totalBytesRead) / (speed + 1); + synchronized (thread) { + // Calculate updated speed +// totalBytesRead += currentBytes; + lastBytes = (lastBytes * 2 + currentBytes) / 3; + currentSpeed = (1000 * lastBytes) / (now - lastUpdate); + lastUpdate = now; + } // Reset counters - lastUpdate = now; currentBytes = 0; - // Fire event to all listeners - QuickTimer.scheduleOnce(new Task() { - @Override - public void fire() { - TransferState state = null; - byte[] blocks = null; - String error = null; - if (lastThriftUpdate + THRIFT_INTERVAL_MS < now) { - lastThriftUpdate = now; - try { - TransferStatus uploadStatus = ThriftManager.getSatClient().queryUploadStatus(uploadToken); - state = uploadStatus.getState(); - blocks = uploadStatus.getBlockStatus(); - } catch (TInvalidTokenException e) { - error = "Upload token unknown!?"; - } catch (TException e) { - error = "Exception quering upload status: " + e.toString(); - } - } - TransferEvent event = new TransferEvent(state, blocks, speed, timeRemaining, error); - fireEvent(event); - } - }); } } @Override public void uploadError(String message) { - TransferEvent event = new TransferEvent(null, null, 0, 0, message); - fireEvent(event); + fireErrorMessage(message); } }); - - if (!ret) { - TransferEvent event = new TransferEvent(TransferState.ERROR, null, 0, 0, null); - fireEvent(event); - return; - } - // Upload succeeded. Let's still wait until the sat reports FINISHED - // Do this in a blocking manner - for (;;) { - TransferState state = null; - byte[] blocks = null; - String error = null; - try { - TransferStatus uploadStatus = ThriftManager.getSatClient() - .queryUploadStatus(uploadToken); - state = uploadStatus.getState(); - blocks = uploadStatus.getBlockStatus(); - } catch (TInvalidTokenException e) { - error = "Upload token unknown!?"; - } catch (TException e) { - error = "Exception quering upload status: " + e.toString(); - } - TransferEvent event = new TransferEvent(state, blocks, 0, 0, error); - fireEvent(event); - if (state != TransferState.WORKING) - break; - try { - Thread.sleep(THRIFT_INTERVAL_MS); - } catch (InterruptedException e) { - event = new TransferEvent(TransferState.ERROR, null, 0, 0, "Upload thread interrupted"); - fireEvent(event); - break; - } + transferEnded(this, ret); + } + + @Override + public long getCurrentSpeed() { + synchronized (this) { + return currentSpeed; } - } catch (IOException e) { - LOGGER.error("Upload of " + uploadFile.getAbsolutePath() + " failed: ", e); - } finally { - if (upload != null) - upload.cancel(); } - return; + + @Override + protected Transfer getTransfer() { + // TODO Auto-generated method stub + return null; + } } - - private void fireEvent(TransferEvent event) { - synchronized (listeners) { - for (TransferEventListener listener : listeners) { - listener.update(event); + + private long lastThriftUpdate = 0; + + @Override + protected TransferEvent getTransferEvent() { + final long now = System.currentTimeMillis(); + TransferState state = null; + byte[] blocks = null; + String error = null; + if (lastThriftUpdate + THRIFT_INTERVAL_MS < now) { + lastThriftUpdate = now; + try { + TransferStatus uploadStatus = ThriftManager.getSatClient().queryUploadStatus(uploadToken); + state = uploadStatus.getState(); + blocks = uploadStatus.getBlockStatus(); + } catch (TInvalidTokenException e) { + error = "Upload token unknown!?"; + state = TransferState.ERROR; + } catch (TException e) { + error = "Exception quering upload status: " + e.toString(); } } - } - - public void addListener(TransferEventListener listener) { - synchronized (listeners) { - listeners.add(listener); + long speed = 0; + long timeRemaining = 0; + synchronized (transfers) { + for (TransferThread thread : transfers) { + speed += thread.getCurrentSpeed(); + } } - } - - public void removeListener(TransferEventListener listener) { - synchronized (listeners) { - while (listeners.remove(listener)){} + // 0 = complete, 1 = missing, 2 = uploading, 3 = queued for copying, 4 = copying + if (blocks != null) { + int missing = 0; + for (byte b : blocks) { + if (b != 0) { + missing++; + } + } + final long bytesRemaining = CHUNK_SIZE * (long)missing; + timeRemaining = (1000 * bytesRemaining) / (speed + 1); } + TransferEvent event = new TransferEvent(state, blocks, speed, timeRemaining, error); + return event; } - public void cancel() { - if (upload != null) - upload.cancel(); + @Override + protected TransferThread createNewThread() { + return new UploadThread(); } } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java index 57103748..c6656ac1 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java @@ -33,7 +33,7 @@ public class Gui { /** * Center the given shell on the {@link Monitor} it is displayed on. - * WARNING: Seems broken on Linux, SWT only sees the primary monitor. + * WARNING: Seems broken on Linux (depending on DE or WM) * * @param shell Some shell */ diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java index 70e5a98d..5374e370 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java @@ -1,6 +1,7 @@ package org.openslx.dozmod.gui.wizard.page; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; @@ -38,7 +39,7 @@ public class ImageUploadPage extends ImageUploadPageLayout { private UploadWizardState uploadWizardState; protected final boolean editExistingImage; private String lastDetectedName = null; - + public ImageUploadPage(UploadWizardState uploadWizardState, boolean editExistingImage) { super(); setPageComplete(false); @@ -98,7 +99,7 @@ public class ImageUploadPage extends ImageUploadPageLayout { blockProgressBar.setVisible(false); imageFileBrowseButton.setFocus(); } - + private void browseForVm() { FileDialog dialog = new FileDialog(getWizard().getContainer().getShell(), SWT.OPEN); dialog.setFilterNames(new String[] { "VMware Virtuelle Maschine (Beschreibungsdatei)" }); @@ -148,7 +149,7 @@ public class ImageUploadPage extends ImageUploadPageLayout { } if (vmDiskFileInfo.canRead()) { uploadWizardState.diskFile = vmDiskFileInfo; - } else { + } else { setErrorMessage("'" + vmDiskFileInfo.getPath() + "' kann nicht gelesen werden!"); setPageComplete(false); return; @@ -163,7 +164,7 @@ public class ImageUploadPage extends ImageUploadPageLayout { imageNameTextField.setText(meta.getDisplayName()); } lastDetectedName = meta.getDisplayName(); - + uploadWizardState.detectedOs = meta.getOs(); imageFileTextField.setText(file.getAbsolutePath()); // let the user know the upload is ready @@ -188,15 +189,20 @@ public class ImageUploadPage extends ImageUploadPageLayout { } /** - * This function starts the image creation process. It is triggered by the "Upload" button. - * - * Depending on the state, it will first try to get a UUID for the new image by calling - * createImage() of the thrift API. If a UUID is received, it will request an upload with - * requestImageVersionUpload(). If granted, it will finally start a thread for the UploadTask. - * - * Then a callback to the Gui is executed where we can process the upload state and give the + * This function starts the image creation process. It is triggered by the + * "Upload" button. + * + * Depending on the state, it will first try to get a UUID for the new image + * by calling + * createImage() of the thrift API. If a UUID is received, it will request + * an upload with + * requestImageVersionUpload(). If granted, it will finally start a thread + * for the UploadTask. + * + * Then a callback to the Gui is executed where we can process the upload + * state and give the * user feedback about it. - * + * */ private void createAndUploadImage() { // first get the image name from the field @@ -218,13 +224,13 @@ public class ImageUploadPage extends ImageUploadPageLayout { LOGGER.error("Error while creating image: ", e); } } - if (uploadWizardState.transferInformation == null - && uploadWizardState.uuid != null) { + if (uploadWizardState.transferInformation == null && uploadWizardState.uuid != null) { // -- request upload -- LOGGER.debug("Requesting upload..."); try { - uploadWizardState.transferInformation = ThriftManager.getSatClient().requestImageVersionUpload( - Session.getSatelliteToken(), uploadWizardState.uuid, uploadWizardState.diskFile.length(), null); + uploadWizardState.transferInformation = ThriftManager.getSatClient() + .requestImageVersionUpload(Session.getSatelliteToken(), + uploadWizardState.uuid, uploadWizardState.diskFile.length(), null); } catch (Exception e) { LOGGER.error("Error while requesting download for: " + uploadWizardState.uuid, e); } @@ -233,11 +239,15 @@ public class ImageUploadPage extends ImageUploadPageLayout { && uploadWizardState.uuid != null) { // do actually start the upload now LOGGER.debug("Starting upload for : " + uploadWizardState.diskFile.toPath()); - uploadWizardState.uploadTask = new UploadTask( - Session.getSatelliteAddress(), - uploadWizardState.transferInformation.getPlainPort(), - uploadWizardState.transferInformation.getToken(), - uploadWizardState.diskFile); + try { + uploadWizardState.uploadTask = new UploadTask(Session.getSatelliteAddress(), + uploadWizardState.transferInformation.getPlainPort(), + uploadWizardState.transferInformation.getToken(), uploadWizardState.diskFile); + } catch (FileNotFoundException e) { + MainWindow.showMessageBox("Cannot upload file: Not found", MessageType.ERROR, LOGGER, + e); + return; + } // -- add listener for upload status/progress -- uploadWizardState.uploadTask.addListener(new TransferEventListener() { @Override @@ -265,13 +275,17 @@ public class ImageUploadPage extends ImageUploadPageLayout { } /** - * Callback for the QuickTimer executing the thrift calls (see createAndUploadImage()) - * - * createAndUploadImage() starts the image upload process by creating the image, - * requesting upload transfer, starting the upload task and finally query the upload + * Callback for the QuickTimer executing the thrift calls (see + * createAndUploadImage()) + * + * createAndUploadImage() starts the image upload process by creating the + * image, + * requesting upload transfer, starting the upload task and finally query + * the upload * status to check if the upload is actually running. - * - * Each step will update the UploadWizardState which we will evaluate here to + * + * Each step will update the UploadWizardState which we will evaluate here + * to * know which step failed and handle the error accordingly. */ public void createAndUploadImageCallback() { @@ -297,8 +311,10 @@ public class ImageUploadPage extends ImageUploadPageLayout { } /** - * Evaluates the transfer's state and show feedback to the user based on the state. - * @param listener + * Evaluates the transfer's state and show feedback to the user based on the + * state. + * + * @param listener */ private void processTransferStatus(TransferEventListener listener, TransferEvent event) { if (getControl().isDisposed()) { @@ -314,24 +330,25 @@ public class ImageUploadPage extends ImageUploadPageLayout { } // TODO: Move user interaction to central listener when we have it switch (event.state) { - case FINISHED: - cancelUpload.setEnabled(false); - MainWindow.showMessageBox("Upload abgeschlossen.", MessageType.INFO, LOGGER, null); - break; - case ERROR: - if (MainWindow.showMessageBox("Fehler beim Upload: " + event.errorMessage + "\nNochmal versuchen?", MessageType.ERROR_RETRY, LOGGER, null)) { - // user wants to try again, just reset transferInformation ... - uploadWizardState.uploadTask = null; - // ... since this function will then do the upload process again. - createAndUploadImage(); - } - break; - case WORKING: - case IDLE: - break; - default: - LOGGER.warn("Unhandled transfer state: " + event.state); - break; + case FINISHED: + cancelUpload.setEnabled(false); + MainWindow.showMessageBox("Upload abgeschlossen.", MessageType.INFO, LOGGER, null); + break; + case ERROR: + if (MainWindow.showMessageBox("Fehler beim Upload: " + event.errorMessage + + "\nNochmal versuchen?", MessageType.ERROR_RETRY, LOGGER, null)) { + // user wants to try again, just reset transferInformation ... + uploadWizardState.uploadTask = null; + // ... since this function will then do the upload process again. + createAndUploadImage(); + } + break; + case WORKING: + case IDLE: + break; + default: + LOGGER.warn("Unhandled transfer state: " + event.state); + break; } } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java index 02449e4e..68b90661 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java @@ -64,24 +64,7 @@ public class FormatHelper { return String.format("%.1f" + THIN_SP + "%sB", bytes / Math.pow(unit, exp), pre); } - /** - * Parse the given String as a base10 integer. - * If the string does not represent a valid integer, return the given - * default value. - * - * @param value string representation to parse to an int - * @param defaultValue fallback value if given string can't be parsed - * @return - */ - public static int parseInt(String value, int defaultValue) { - try { - return Integer.parseInt(value); - } catch (Exception e) { - return defaultValue; - } - } - - public static String formatMilliseconds(long ms) { + public static String formatMilliseconds(long ms, boolean withSeconds) { String ret = ""; long seconds = ms / 1000; if (seconds >= 86400 * 365) { // More than a year... @@ -110,7 +93,11 @@ public class FormatHelper { if (!ret.isEmpty()) { ret += ", "; } - return ret + String.format("%02d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, seconds % 60); + if (withSeconds) { + return ret + String.format("%02d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, seconds % 60); + } else { + return ret + String.format("%02d:%02d", seconds / 3600, (seconds % 3600) / 60); + } } } |
