Zond: задействование файла настроек
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
group = 'asys'
|
||||
version = '0.7.5-SNAPSHOT'
|
||||
version = '0.7.6-SNAPSHOT'
|
||||
|
||||
apply plugin: 'application'
|
||||
|
||||
|
||||
40
zond/src/main/java/asys/zond/Config.java
Normal file
40
zond/src/main/java/asys/zond/Config.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* DmitriyMX <d.mihailov@samson-rus.com>
|
||||
* 2017-06-08
|
||||
*/
|
||||
package asys.zond;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class Config {
|
||||
private static final Config instance = new Config();
|
||||
private Properties properties = new Properties();
|
||||
|
||||
public static Config getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Config(){
|
||||
}
|
||||
|
||||
void load(InputStream inputStream) throws IOException {
|
||||
properties.load(inputStream);
|
||||
if (properties.size() == 0) {
|
||||
throw new IOException("Config empty!");
|
||||
}
|
||||
}
|
||||
|
||||
public String getString(String key) {
|
||||
return properties.getProperty(key);
|
||||
}
|
||||
|
||||
public int getInt(String key) {
|
||||
try {
|
||||
return Integer.parseInt(properties.getProperty(key));
|
||||
} catch (NumberFormatException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,7 @@ import org.apache.commons.exec.DefaultExecutor;
|
||||
import org.apache.commons.exec.Executor;
|
||||
import org.apache.commons.exec.PumpStreamHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -25,6 +24,13 @@ public class Main {
|
||||
}
|
||||
|
||||
private void start(String[] args) {
|
||||
try {
|
||||
loadConfig();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(-2);
|
||||
return;
|
||||
}
|
||||
ZondCommandHandler commandHandler = new ZondCommandHandler();
|
||||
startShell(commandHandler);
|
||||
initExecCommand(args, Shell.getInstance().getOutput());
|
||||
@@ -48,4 +54,23 @@ public class Main {
|
||||
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout);
|
||||
executor.setStreamHandler(pumpStreamHandler);
|
||||
}
|
||||
|
||||
private void loadConfig() throws IOException {
|
||||
File zondPropertiesFile = new File("zond.properties");
|
||||
if (!zondPropertiesFile.exists()) {
|
||||
InputStream stream = Main.class.getResourceAsStream("/zond.properties");
|
||||
FileOutputStream fos = new FileOutputStream(zondPropertiesFile);
|
||||
byte[] buff = new byte[65536];
|
||||
int len;
|
||||
while ((len = stream.read(buff)) > 0) {
|
||||
fos.write(buff, 0, len);
|
||||
}
|
||||
fos.flush();
|
||||
fos.close();
|
||||
}
|
||||
|
||||
FileInputStream fis = new FileInputStream(zondPropertiesFile);
|
||||
Config.getInstance().load(fis);
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public class ZondCommandHandler implements CommandHandler {
|
||||
threadExec.interrupt();
|
||||
}
|
||||
} else if (line.equalsIgnoreCase("connect")) {
|
||||
Connector.getInstance().startReconnect("127.0.0.1", 8779);
|
||||
Connector.getInstance().startReconnect();
|
||||
} else if (line.equalsIgnoreCase("disconnect")) {
|
||||
Connector.getInstance().shutdown();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package asys.zond.proxy;
|
||||
|
||||
import asys.mcsmanager.packets.*;
|
||||
import asys.zond.Config;
|
||||
import asys.zond.shell.Shell;
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.ImmutableBiMap;
|
||||
@@ -42,7 +43,10 @@ public class ClientPacketHandler extends ChannelInboundHandlerAdapter implements
|
||||
ctx.channel().attr(KNOWN_PACKETS).set(handshakePackets);
|
||||
ctx.channel().attr(KNOWN_HANDLERS).set(handshakeHandlers);
|
||||
|
||||
ctx.channel().writeAndFlush(new CS_Handshake("00","11"));
|
||||
ctx.channel().writeAndFlush(new CS_Handshake(
|
||||
Config.getInstance().getString("serverId"),
|
||||
Config.getInstance().getString("passcode")
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package asys.zond.proxy;
|
||||
|
||||
import asys.mcsmanager.packets.Packet;
|
||||
import asys.zond.Config;
|
||||
import asys.zond.shell.Shell;
|
||||
|
||||
public class Connector {
|
||||
@@ -21,14 +22,15 @@ public class Connector {
|
||||
private Connector() {
|
||||
}
|
||||
|
||||
public void startReconnect(final String host, final int port) {
|
||||
public void startReconnect() {
|
||||
if (connectTicker != null && connectTicker.isActive()) return;
|
||||
client = new Client();
|
||||
connectTicker = new TaskTicker().setStepTimeMs(5000L);
|
||||
connectTicker.setTask(() -> {
|
||||
Shell.getInstance().getOutput()
|
||||
.println(String.format("Connect(%d) to ASys...", ++tryConnect));
|
||||
client.connect(host, port);
|
||||
client.connect(Config.getInstance().getString("host"),
|
||||
Config.getInstance().getInt("port"));
|
||||
if (client.isConnected()) {
|
||||
stopReconnect();
|
||||
} else {
|
||||
|
||||
4
zond/src/main/resources/zond.properties
Normal file
4
zond/src/main/resources/zond.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
serverId = SpigotServer0
|
||||
host = 127.0.0.1
|
||||
port = 8779
|
||||
passcode = testpassphrase
|
||||
Reference in New Issue
Block a user