Skip to content

Commit c4d5ca6

Browse files
committed
Added tempfile, so minecartd -S would work even if port= changes in config.
This allows for the `systemd` service to be `restart`ed after a config edit (instead of having to `stop` service, edit cfg and then `start` again)
1 parent da22105 commit c4d5ca6

File tree

5 files changed

+62
-8
lines changed

5 files changed

+62
-8
lines changed

src/main/csokicraft/minecartd/MineCartD.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,40 @@
66
import csokicraft.minecartd.manager.MineManagerHost;
77

88
public class MineCartD{
9+
public static final String APP_NAME_VER="MineCartD v1.1";
910

1011
public static void main(String[] args) throws IOException{
1112
if(args.length>0&&("--help".equals(args[0])||"-h".equals(args[0]))){
1213
displayHelp();
1314
System.exit(0);
1415
}
1516

16-
System.out.println("Loading MineCartD v1.0, by CsokiCraft");
17+
System.out.println("Loading "+APP_NAME_VER+", by CsokiCraft");
1718
boolean discover=true, stop=false;
18-
String cfg=null;
19+
String cfg=null, tmpf=null;
1920
for(int i=0;i<args.length;i++){
2021
if("--gen-cfg".equals(args[i])||"-C".equals(args[i]))
2122
discover=false;
2223
if("--cfgfile".equals(args[i])||"-f".equals(args[i]))
2324
cfg=args[++i];
2425
if("--stop".equals(args[i])||"-S".equals(args[i]))
2526
stop=true;
27+
if("--tmpfile".equals(args[i])||"-t".equals(args[i]))
28+
tmpf=args[++i];
2629
}
2730

2831
MineConfigHandler cfgMan;
2932
if(cfg==null)
3033
cfgMan=new MineConfigHandler();
3134
else
3235
cfgMan=new MineConfigHandler(new File(cfg));
36+
if(tmpf!=null)
37+
cfgMan.setTempFile(tmpf);
3338

3439
if(stop){
35-
Socket sock=new Socket("localhost", cfgMan.port);
40+
int port=cfgMan.lastPort;
41+
if(port==-1) port=cfgMan.port;
42+
Socket sock=new Socket("localhost", port);
3643
PrintWriter out=new PrintWriter(sock.getOutputStream());
3744
out.println("STOP");
3845
out.flush();
@@ -41,6 +48,7 @@ public static void main(String[] args) throws IOException{
4148
MineManagerHost host=new MineManagerHost(cfgMan);
4249
while(host.isAlive())
4350
Thread.yield();
51+
cfgMan.onServerStop();
4452
}
4553
}
4654

src/main/csokicraft/minecartd/MineConfigHandler.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ public class MineConfigHandler{
1010
DEFAULT_DIR="/var/lib/minecartd/servers";
1111
public static final int DEFAULT_PORT=40960;
1212

13+
/** Config file */
1314
protected File cfgFile;
1415

16+
/** Config entry: Servers' directory */
1517
public File serversDir;
18+
/** Config entry: The port of the server */
1619
public int port;
1720

21+
/** Temp file */
22+
protected File tmpFile;
23+
24+
/** Tempfile entry: last known port of the server. '-1' means unknown */
25+
protected int lastPort=-1;
26+
1827
public MineConfigHandler() throws IOException{
1928
this(new File(DEFAULT_CFG));
2029
}
@@ -25,7 +34,7 @@ protected MineConfigHandler(File file) throws IOException{
2534
}
2635

2736
private void genCfg() throws IOException{
28-
System.out.println("Creating config at "+cfgFile.getAbsolutePath());
37+
System.out.println("Creating config...");
2938
PrintWriter fout=new PrintWriter(cfgFile);
3039
fout.println("# This is the default config for MineCartD");
3140
fout.println();
@@ -36,7 +45,7 @@ private void genCfg() throws IOException{
3645
}
3746

3847
private void readCfg() throws IOException{
39-
System.out.println("Reading config at "+cfgFile.getAbsolutePath());
48+
System.out.println("Config at "+cfgFile.getAbsolutePath());
4049
if(!cfgFile.exists())
4150
genCfg();
4251
if(!cfgFile.canRead())
@@ -74,4 +83,33 @@ public List<MineCraftServer> discoverServers(){
7483
System.out.println("Found "+servers.size()+" servers");
7584
return servers;
7685
}
86+
87+
public void setTempFile(String tmp) throws IOException{
88+
tmpFile=new File(tmp);
89+
if(!tmpFile.exists()) return;
90+
BufferedReader in=new BufferedReader(new FileReader(tmpFile));
91+
String ln=in.readLine();
92+
while(ln!=null){
93+
if(ln.startsWith("port="))
94+
lastPort=Integer.parseInt(ln.substring(5));
95+
else System.err.println("Temp file has invalid line: "+ln);
96+
ln=in.readLine();
97+
}
98+
in.close();
99+
}
100+
101+
public void onServerStart() throws IOException{
102+
if(tmpFile!=null){
103+
PrintWriter out=new PrintWriter(tmpFile);
104+
out.println("port="+port);
105+
out.close();
106+
}
107+
}
108+
109+
public void onServerStop(){
110+
if(tmpFile!=null){
111+
tmpFile.delete();
112+
lastPort=-1;
113+
}
114+
}
77115
}

src/main/csokicraft/minecartd/manager/MineManagerHost.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public MineManagerHost(List<MineCraftServer> l, int port) throws IOException{
2727

2828
public MineManagerHost(MineConfigHandler cfg) throws IOException{
2929
this(cfg.discoverServers(), cfg.port);
30+
cfg.onServerStart();
3031
}
3132

3233
public synchronized void newWorker() throws IOException{

src/main/csokicraft/minecartd/manager/MineManagerWorker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.*;
44
import java.net.*;
55

6+
import csokicraft.minecartd.MineCartD;
7+
68
public class MineManagerWorker extends Thread{
79
protected MineManagerHost host;
810

@@ -20,7 +22,7 @@ public void run(){
2022
host.newWorker();
2123
BufferedReader in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
2224
PrintWriter out=new PrintWriter(sock.getOutputStream());
23-
out.println("MineCartD v1.0 Command Interface. Type 'help' for a list of commands");
25+
out.println(MineCartD.APP_NAME_VER+" Command Interface. Type 'help' for a list of commands");
2426
out.flush();
2527
String cmd=in.readLine();
2628
while(cmd!=null){

src/rsrc/minecartd_install

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ if [ $(id -u) != 0 ]; then
55
exit 1
66
fi
77

8+
# If minecartd service is installed, first stop it
9+
if [ -s /etc/systemd/system/minecartd.service ]; then
10+
systemctl stop minecartd
11+
fi
12+
813
# Add user and working directory
914
useradd -U -r -d /var/lib/minecartd -m minecartd
1015

1116
# Download JAR
12-
wget -O /usr/lib/minecartd.jar http://csokicraft.agyklub.hu/downloads?file=minecartd_1.0.jar
17+
wget -O /usr/lib/minecartd.jar http://csokicraft.agyklub.hu/downloads?file=minecartd_1.1.jar
1318

1419
# Add runscript
1520
cat > /usr/bin/minecartd <<'EOF'
1621
#!/bin/sh
17-
java -jar /usr/lib/minecartd.jar "$@"
22+
java -jar /usr/lib/minecartd.jar -t ~/minecartd.tmp "$@"
1823
EOF
1924
chmod 655 /usr/bin/minecartd
2025

0 commit comments

Comments
 (0)