-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathServer.java
61 lines (52 loc) · 1.39 KB
/
Server.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
import java.io.IOException;
import java.net.Socket;
public class Server {
private String startCommand;
private String directory;
private String host;
private String port;
static private Process process;
public void setStartCommand(String command) {
startCommand = command;
}
public void setDirectory(String directory) {
this.directory = directory;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(String port) {
this.port = port;
}
public void startServer() throws Exception {
String command = startCommand + " -p " + port + " -d " + directory;
process = Runtime.getRuntime().exec(command);
while (!serverAvailable()) {
Thread.sleep(2000);
}
}
public boolean serverAvailable() {
Socket socket = null;
try {
socket = new Socket(host, Integer.parseInt(port));
return true;
} catch (IOException ex) {
/* ignore */
} finally {
closeSocket(socket);
}
return false;
}
private void closeSocket(Socket socket) {
try {
if (socket != null) {
socket.close();
}
} catch (IOException ex) {
/* ignore */
}
}
public void stopServer() throws Exception {
process.destroy();
}
}