-
Notifications
You must be signed in to change notification settings - Fork 9
/
LoadBalanceListener.java
56 lines (40 loc) · 1.13 KB
/
LoadBalanceListener.java
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
package loadBalance;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class LoadBalanceListener implements Runnable {
// Globals
private ServerSocket listenSock;
public static int serverSelector = 0;
public LoadBalanceListener(int port) {
// Construct listening socket
try {
this.listenSock = new ServerSocket(port);
} catch (IOException ex) {
System.err.println("Error opening listening port");
ex.printStackTrace();
}
}
// Method to configure load balance.
// TODO Add other options
public void configure() {
System.out.println("Configuration finished");
}
@Override
public void run() {
System.out.println("Listening for connections");
// Pass connections onto threads and relisten
while (!Thread.interrupted()) {
try {
Socket newSock = listenSock.accept();
System.out.println("Connection detected");
LoadBalanceSocket newCon = new LoadBalanceSocket(newSock);
Thread newThread = new Thread(newCon);
newThread.start();
} catch (Exception e) {
System.err.println("Server listen error");
e.printStackTrace();
}
}
}
}