/
SubServer.java
308 lines (283 loc) · 11 KB
/
SubServer.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package net.ME1312.SubServers.Host.Executable;
import net.ME1312.Galaxi.Library.Container;
import net.ME1312.Galaxi.Library.UniversalFile;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Library.Version.Version;
import net.ME1312.SubServers.Host.Library.Exception.InvalidServerException;
import net.ME1312.SubServers.Host.Network.Packet.PacketExUpdateServer;
import net.ME1312.SubServers.Host.ExHost;
import net.ME1312.SubServers.Host.SubAPI;
import java.io.*;
import java.util.LinkedList;
import java.util.UUID;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
/**
* Internal SubServer Class
*/
public class SubServer {
private ExHost host;
private String name;
private boolean enabled;
private int port;
private Container<Boolean> log;
private String dir;
private File directory;
private String executable;
private Process process;
private SubLogger logger;
private Thread thread;
private BufferedWriter command;
private LinkedList<String> queue;
private String stopcmd;
private boolean allowrestart;
/**
* Creates a SubServer
*
* @param host SubServers.Host
* @param name Name
* @param enabled Enabled Status
* @param log Logging Status
* @param directory Directory
* @param executable Executable String
* @param stopcmd Stop Command
* @throws InvalidServerException
*/
public SubServer(ExHost host, String name, boolean enabled, int port, boolean log, String directory, String executable, String stopcmd) throws InvalidServerException {
if (Util.isNull(host, name, enabled, log, directory, executable)) throw new NullPointerException();
this.host = host;
this.name = name;
this.enabled = enabled;
this.port = port;
this.log = new Container<Boolean>(log);
this.dir = directory;
this.directory = new File(host.host.getRawString("Directory"), directory);
this.executable = executable;
this.process = null;
this.logger = new SubLogger(null, this, name, null, this.log, null);
this.thread = null;
this.command = null;
this.queue = new LinkedList<String>();
this.stopcmd = stopcmd;
if (new UniversalFile(this.directory, "plugins:SubServers.Client.jar").exists()) {
try {
JarInputStream updated = new JarInputStream(ExHost.class.getResourceAsStream("/net/ME1312/SubServers/Host/Library/Files/client.jar"));
JarFile existing = new JarFile(new UniversalFile(this.directory, "plugins:SubServers.Client.jar"));
if (existing.getManifest().getMainAttributes().getValue("Implementation-Title") != null && existing.getManifest().getMainAttributes().getValue("Implementation-Title").startsWith("SubServers.Client") && existing.getManifest().getMainAttributes().getValue("Specification-Title") != null &&
updated.getManifest().getMainAttributes().getValue("Implementation-Title") != null && updated.getManifest().getMainAttributes().getValue("Implementation-Title").startsWith("SubServers.Client") && updated.getManifest().getMainAttributes().getValue("Specification-Title") != null) {
if (new Version(existing.getManifest().getMainAttributes().getValue("Specification-Title")).compareTo(new Version(updated.getManifest().getMainAttributes().getValue("Specification-Title"))) < 0) {
new UniversalFile(this.directory, "plugins:SubServers.Client.jar").delete();
Util.copyFromJar(ExHost.class.getClassLoader(), "net/ME1312/SubServers/Host/Library/Files/client.jar", new UniversalFile(this.directory, "plugins:SubServers.Client.jar").getPath());
}
}
existing.close();
updated.close();
} catch (Throwable e) {
host.log.info.println("Couldn't auto-update SubServers.Client.jar for " + name);
host.log.error.println(e);
}
} else if (new UniversalFile(this.directory, "mods:SubServers.Client.jar").exists()) {
try {
JarInputStream updated = new JarInputStream(ExHost.class.getResourceAsStream("/net/ME1312/SubServers/Host/Library/Files/client.jar"));
JarFile existing = new JarFile(new UniversalFile(this.directory, "mods:SubServers.Client.jar"));
if (existing.getManifest().getMainAttributes().getValue("Implementation-Title") != null && existing.getManifest().getMainAttributes().getValue("Implementation-Title").startsWith("SubServers.Client") && existing.getManifest().getMainAttributes().getValue("Specification-Title") != null &&
updated.getManifest().getMainAttributes().getValue("Implementation-Title") != null && updated.getManifest().getMainAttributes().getValue("Implementation-Title").startsWith("SubServers.Client") && updated.getManifest().getMainAttributes().getValue("Specification-Title") != null) {
if (new Version(existing.getManifest().getMainAttributes().getValue("Specification-Title")).compareTo(new Version(updated.getManifest().getMainAttributes().getValue("Specification-Title"))) < 0) {
new UniversalFile(this.directory, "mods:SubServers.Client.jar").delete();
Util.copyFromJar(ExHost.class.getClassLoader(), "net/ME1312/SubServers/Host/Library/Files/client.jar", new UniversalFile(this.directory, "mods:SubServers.Client.jar").getPath());
}
}
existing.close();
updated.close();
} catch (Throwable e) {
host.log.info.println("Couldn't auto-update SubServers.Client.jar for " + name);
host.log.error.println(e);
}
}
}
private void run() {
boolean falsestart = true;
allowrestart = true;
try {
ProcessBuilder pb = new ProcessBuilder().command(Executable.parse(host.host.getRawString("Git-Bash"), executable)).directory(directory);
pb.environment().put("name", getName());
pb.environment().put("port", Integer.toString(getPort()));
process = pb.start();
falsestart = false;
host.log.info.println("Now starting " + name);
logger.process = process;
logger.start();
command = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
for (String command : queue) if (process.isAlive()) {
this.command.write(command);
this.command.newLine();
this.command.flush();
}
queue.clear();
if (process.isAlive()) process.waitFor();
} catch (IOException | InterruptedException e) {
host.log.error.println(e);
allowrestart = false;
if (falsestart) host.subdata.sendPacket(new PacketExUpdateServer(this, PacketExUpdateServer.UpdateType.LAUNCH_EXCEPTION));
}
host.subdata.sendPacket(new PacketExUpdateServer(this, PacketExUpdateServer.UpdateType.STOPPED, (Integer) process.exitValue(), (Boolean) allowrestart));
host.log.info.println(name + " has stopped");
process = null;
command = null;
}
/**
* Starts the Server
*
* @param address External Logging Address
*/
public void start(UUID address) {
if (isEnabled() && !(thread != null && thread.isAlive())) {
logger.address = address;
(thread = new Thread(this::run, SubAPI.getInstance().getAppInfo().getName() + "::Server_Process_Handler(" + name + ')')).start();
}
}
/**
* Stops the Server
*/
public void stop() {
if (thread != null && thread.isAlive()) {
try {
allowrestart = false;
if (process != null && process.isAlive()) {
command.write(stopcmd);
command.newLine();
command.flush();
}
} catch (IOException e) {
host.log.error.println(e);
}
}
}
/**
* Terminates the Server
*/
public void terminate() {
allowrestart = false;
if (process != null && process.isAlive()) Executable.terminate(process);
}
/**
* Commands the Server
*
* @param command Command to Send
*/
public void command(String command) {
if (Util.isNull(command)) throw new NullPointerException();
if (thread != null && thread.isAlive()) {
try {
if (command.equalsIgnoreCase(stopcmd)) allowrestart = false;
if (process != null && process.isAlive()) {
this.command.write(command);
this.command.newLine();
this.command.flush();
}
} catch (IOException e) {
host.log.error.println(e);
}
}
}
/**
* Waits for the Server to Stop
*
* @throws InterruptedException
*/
public void waitFor() throws InterruptedException {
while (thread != null && thread.isAlive()) {
Thread.sleep(250);
}
}
/**
* Gets the name of the Server
*
* @return Server Name
*/
public String getName() {
return name;
}
/**
* If the Server is Running
*
* @return Running Status
*/
public boolean isRunning() {
return process != null && process.isAlive();
}
/**
* If the Server is Enabled
*
* @return Enabled Status
*/
public boolean isEnabled() {
return enabled;
}
/**
* Set if the Server is Enabled
*
* @param value Value
*/
public void setEnabled(boolean value) {
if (Util.isNull(value)) throw new NullPointerException();
enabled = value;
}
/**
* Get the Port of the Server
*
* @return Server Port Number
*/
public int getPort() {
return port;
}
/**
* If the Server is Logging
*
* @return Logging Status
*/
public boolean isLogging() {
return log.get();
}
/**
* Set if the Server is Logging
*
* @param value Value
*/
public void setLogging(boolean value) {
if (Util.isNull(value)) throw new NullPointerException();
log.set(value);
}
/**
* Get Process Logger
*/
public SubLogger getLogger() {
return logger;
}
/**
* Get the Server Directory
*
* @return Server Directory
*/
public String getDirectory() {
return dir;
}
/**
* Grab the Command to Stop the Server
*
* @return Stop Command
*/
public String getStopCommand() {
return stopcmd;
}
/**
* Set the Command that Stops the Server
*
* @param value Value
*/
public void setStopCommand(String value) {
if (Util.isNull(value)) throw new NullPointerException();
stopcmd = value;
}
}