diff options
author | Simon Rettberg | 2016-04-13 18:39:26 +0200 |
---|---|---|
committer | Simon Rettberg | 2016-04-13 18:39:26 +0200 |
commit | 5a2b7a8a2f0a9ea5d01895b00f75beaa7af55622 (patch) | |
tree | 5bdc5411cd9954577e5489d5e4271c800d826e9c /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/OutgoingDataTransfer.java | |
parent | [client] fix bad commit (diff) | |
download | tutor-module-5a2b7a8a2f0a9ea5d01895b00f75beaa7af55622.tar.gz tutor-module-5a2b7a8a2f0a9ea5d01895b00f75beaa7af55622.tar.xz tutor-module-5a2b7a8a2f0a9ea5d01895b00f75beaa7af55622.zip |
(WiP) Global image sync
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/OutgoingDataTransfer.java')
-rw-r--r-- | dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/OutgoingDataTransfer.java | 75 |
1 files changed, 70 insertions, 5 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/OutgoingDataTransfer.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/OutgoingDataTransfer.java index 31c219aa..581d7ba5 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/OutgoingDataTransfer.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/OutgoingDataTransfer.java @@ -1,12 +1,18 @@ package org.openslx.bwlp.sat.fileserv; import java.io.File; +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import java.util.concurrent.ExecutorService; import org.apache.log4j.Logger; +import org.openslx.bwlp.sat.util.Configuration; import org.openslx.bwlp.sat.util.Constants; +import org.openslx.bwlp.thrift.iface.TransferInformation; import org.openslx.filetransfer.Uploader; public class OutgoingDataTransfer extends AbstractTransfer { @@ -20,19 +26,75 @@ public class OutgoingDataTransfer extends AbstractTransfer { private final File sourceFile; + private final TransferInformation masterTransferInfo; + private boolean isCanceled = false; + /** + * For downloads by clients. + * + * @param uuid UUID of this transfer + * @param file file to send to client + */ public OutgoingDataTransfer(String uuid, File file) { super(uuid); this.sourceFile = file; + this.masterTransferInfo = null; + } + + /** + * For uploads to the master server. + * + * @param transferInfo TI received by master server when it granted the + * upload + * @param absFile file to send to master server + */ + public OutgoingDataTransfer(TransferInformation transferInfo, File absFile) { + super(UUID.randomUUID().toString()); + this.masterTransferInfo = transferInfo; + this.sourceFile = absFile; } /** * Called periodically if this is a transfer from the master server, so we * can make sure the transfer is running. */ - public void heartBeat(ExecutorService pool) { - // TODO + public synchronized void heartBeat(ExecutorService pool) { + if (isCanceled || masterTransferInfo == null) + return; + synchronized (uploads) { + if (uploads.size() >= 1) + return; + } + Uploader uploader = null; + Exception connectException = null; + if (masterTransferInfo.plainPort != 0) { + // Try plain + try { + uploader = new Uploader(Configuration.getMasterServerAddress(), masterTransferInfo.plainPort, + 10000, null, masterTransferInfo.token); + } catch (IOException e) { + uploader = null; + connectException = e; + } + } + if (uploader == null && masterTransferInfo.sslPort != 0 && Configuration.getMasterServerSsl()) { + // Try SSL + try { + uploader = new Uploader(Configuration.getMasterServerAddress(), masterTransferInfo.sslPort, + 10000, Configuration.getMasterServerSslContext(), masterTransferInfo.token); + } catch (KeyManagementException | NoSuchAlgorithmException | IOException e) { + connectException = e; + } + } + if (uploader == null) { + LOGGER.debug("Cannot connect to master server for uploading", connectException); + } else { + synchronized (uploads) { + uploads.add(uploader); + } + runConnectionInternal(uploader, pool); + } } /** @@ -43,7 +105,7 @@ public class OutgoingDataTransfer extends AbstractTransfer { * discarded */ public synchronized boolean addConnection(final Uploader connection, ExecutorService pool) { - if (isCanceled) + if (isCanceled || masterTransferInfo != null) return false; potentialFinishTime.set(0); synchronized (uploads) { @@ -51,16 +113,19 @@ public class OutgoingDataTransfer extends AbstractTransfer { return false; uploads.add(connection); } + return runConnectionInternal(connection, pool); + } + + private boolean runConnectionInternal(final Uploader connection, ExecutorService pool) { try { pool.execute(new Runnable() { @Override public void run() { - potentialFinishTime.set(0); boolean ret = connection.upload(sourceFile.getAbsolutePath()); synchronized (uploads) { uploads.remove(connection); } - if (ret && uploads.isEmpty()) { + if (ret && uploads.isEmpty() && potentialFinishTime.get() == 0) { potentialFinishTime.set(System.currentTimeMillis()); } lastActivityTime.set(System.currentTimeMillis()); |