-
Notifications
You must be signed in to change notification settings - Fork 15
/
CheckAliveThread.java
72 lines (64 loc) · 2.02 KB
/
CheckAliveThread.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* CheckAliveThread checks all dead servers in the server list and updates the
* list when some dead server becomes alive. Checking is done on a beforehand
* specified time itrervals. A server is considered alive if it accepts client
* connections on the specified port.
*/
import java.io.IOException;
import java.net.Socket;
public class CheckAliveThread extends Thread
{
private NakovForwardServer mNakovForwardServer = null;
/**
* Creates a check alive thread. NakovForwardServer object is needed
* for obtaining the servers list.
*/
public CheckAliveThread(NakovForwardServer aNakovForwardServer)
{
mNakovForwardServer = aNakovForwardServer;
}
/**
* Until stopped checks all dead servers if they are alive and waits
* specified time interval
*/
public void run()
{
while (!interrupted()) {
try {
Thread.sleep(mNakovForwardServer.getCheckAliveIntervalMs());
} catch (InterruptedException ie) {
ie.printStackTrace();
}
checkAllDeadServers();
}
}
/**
* Checks all dead servers if they are alive and updates their state if needed.
*/
private void checkAllDeadServers()
{
NakovForwardServer.ServerDescription[] servers = mNakovForwardServer.getServersList();
for (int i=0; i<servers.length; i++) {
if (!servers[i].isAlive)
if (alive(servers[i].host, servers[i].port)) {
servers[i].isAlive = true;
}
}
}
/**
* Checks if given server is alive (if accepts client connections on specified port)
*/
private boolean alive(String host, int port)
{
boolean result = false;
try {
Socket s = new Socket(host, port);
result = true;
s.close();
} catch (IOException ioe) {
ioe.printStackTrace();
// Ignore unsuccessfull connect attempts
}
return result;
}
}