blob: eaa8d4b5656da1fe3182e15a403bb31288c9a177 (
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
|
package org.openslx.filetransfer;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
// TODO: (all files) apply formatting using strg+shift+f *after* importing scheme from ./extras/
public class Listener extends Thread {
private IncomingEvent incomingEvent;
/*
private static String pathToKeyStore =
"/home/bjoern/javadev/DataTransfer/mySrvKeyStore.jks";
*/
private SSLContext context;
private int port;
/***********************************************************************//**
* Constructor for class Listener, which gets an instance of IncomingEvent.
* @param e
*/
public Listener(IncomingEvent e, SSLContext context, int port) {
this.incomingEvent = e;
this.context = context;
this.port = port;
/*
char[] passphrase = "test123".toCharArray();
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(new FileInputStream(pathToKeyStore), passphrase);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keystore, passphrase);
context = SSLContext.getInstance("SSLv3");
KeyManager[] keyManagers = kmf.getKeyManagers();
context.init(keyManagers, null, null);
*/
}
/***********************************************************************//**
* Method listen, should run from Master Server. Listen for incoming
* connection, and start Downloader or Uploader.
* @throws Exception
*/
private void listen() throws Exception { // TODO: Handle exceptions in function to keep going
SSLServerSocketFactory sslServerSocketFactory = context.getServerSocketFactory();
SSLServerSocket welcomeSocket =
(SSLServerSocket) sslServerSocketFactory.createServerSocket(this.port);
while (!isInterrupted()) {
SSLSocket connectionSocket = (SSLSocket) welcomeSocket.accept();
connectionSocket.setSoTimeout(2000); // 2 second timeout enough? Maybe even use a small thread pool for handling accepted connections
// TODO: Handle SocketTimeoutException for all reads and writes in Downloader and Uploader
byte[] b = new byte[1];
int length = connectionSocket.getInputStream().read(b);
System.out.println(length);
// Ascii - Code: 'U' = 85 ; 'D' = 68. TODO: byte constant as class member
if (b[0] == 85) {
System.out.println("U erkannt --> Downloader starten"); // TODO: Use Logger (see masterserver code for example)
// --> start Downloader(socket).
Downloader d = new Downloader(connectionSocket);
incomingEvent.incomingDownloader(d);
}
else if (b[0] == 68) {
System.out.println("D erkannt --> Uploader starten");
// --> start Uploader(socket).
Uploader u = new Uploader(connectionSocket);
incomingEvent.incomingUploader(u);
}
else {
System.out.println("Müll empfangen");
connectionSocket.close();
}
}
}
public int getPort()
{
return this.port;
}
@Override
public void run() {
try {
this.listen();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|