summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/OutgoingDataTransfer.java
blob: 6ef4d4a6e6d913ee20a97d4baa59d4b52856a3c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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.concurrent.ExecutorService;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.bwlp.sat.util.Configuration;
import org.openslx.bwlp.thrift.iface.TInvalidTokenException;
import org.openslx.bwlp.thrift.iface.TransferInformation;
import org.openslx.filetransfer.Uploader;
import org.openslx.filetransfer.util.OutgoingTransferBase;
import org.openslx.thrifthelper.ThriftManager;

public class OutgoingDataTransfer extends OutgoingTransferBase {

	private static final Logger LOGGER = Logger.getLogger(OutgoingDataTransfer.class);

	private final TransferInformation masterTransferInfo;

	/**
	 * For downloads by clients.
	 * 
	 * @param uuid UUID of this transfer
	 * @param file file to send to client
	 */
	public OutgoingDataTransfer(String uuid, File file, int plainPort, int sslPort) {
		super(uuid, file, plainPort, sslPort);
		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(transferInfo.token, absFile, 0, 0);
		this.masterTransferInfo = transferInfo;
	}

	/**
	 * Called periodically if this is a transfer from the master server, so we
	 * can make sure the transfer is running.
	 */
	public synchronized void heartBeat(ExecutorService pool) {
		if (masterTransferInfo == null)
			return;
		if (connectFailCount() > 50)
			return;
		if (connectFailCount() > 5) {
			try {
				ThriftManager.getMasterClient().queryUploadStatus(masterTransferInfo.token);
			} catch (TInvalidTokenException e) {
				LOGGER.info("Master server forgot about upload " + masterTransferInfo.token + ", aborting...");
				connectFails.set(100);
				return;
			} catch (TException e) {
				LOGGER.warn("Cannot query master server for upload status of " + masterTransferInfo.token, e);
			}
		}
		if (getActiveConnectionCount() >= 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 {
			runConnectionInternal(uploader, pool);
		}
	}

	@Override
	public String getRelativePath() {
		throw new RuntimeException("Not implemented");
	}

}