Zond: попытка перевести основной функционал плагина на "зонд"
что бы плагин занимался только апи сервера
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
package asys.zond;
|
||||
|
||||
import asys.zond.proxy.Client;
|
||||
import asys.zond.proxy.Connector;
|
||||
import org.apache.commons.exec.CommandLine;
|
||||
import org.apache.commons.exec.DefaultExecutor;
|
||||
import org.apache.commons.exec.ExecuteException;
|
||||
@@ -16,20 +16,9 @@ import org.fusesource.jansi.AnsiConsole;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.fusesource.jansi.Ansi.ansi;
|
||||
|
||||
public class Main {
|
||||
private static Client client;
|
||||
private static ScheduledExecutorService ses;
|
||||
private static ScheduledFuture<?> sesFuture;
|
||||
private static int tryConnect = 0;
|
||||
|
||||
private static void printLogo() {
|
||||
System.out.println(
|
||||
Ansi.ansi().reset()
|
||||
@@ -60,13 +49,13 @@ public class Main {
|
||||
}
|
||||
|
||||
loadConfig();
|
||||
startReconnect();
|
||||
Connector.getInstance().startReconnect();
|
||||
|
||||
int resultCode = executeProcess(args);
|
||||
|
||||
stopReconnect();
|
||||
ses.shutdown();
|
||||
client.disconnect();
|
||||
Connector.getInstance().setNeedReconnect(false);
|
||||
Connector.getInstance().stopReconnect();
|
||||
Connector.getInstance().disconnect();
|
||||
|
||||
System.out.println(
|
||||
Ansi.ansi().reset().newline()
|
||||
@@ -80,7 +69,7 @@ public class Main {
|
||||
CommandLine commandLine = CommandLine.parse(cmdLine);
|
||||
DefaultExecutor executor = new DefaultExecutor();
|
||||
|
||||
PrintStream proxySysOut = new ProxySysOut(System.err);
|
||||
PrintStream proxySysOut = new ProxySysOut(System.out);
|
||||
InputStream proxySysIn = new ProxySysIn();
|
||||
|
||||
PumpStreamHandler psh = new PumpStreamHandler(proxySysOut, proxySysOut, proxySysIn);
|
||||
@@ -118,55 +107,15 @@ public class Main {
|
||||
fis.close();
|
||||
}
|
||||
|
||||
public static void startReconnect() {
|
||||
client = new Client();
|
||||
ses = Executors.newScheduledThreadPool(2);
|
||||
sesFuture = ses.scheduleAtFixedRate(() -> {
|
||||
log(String.format("Connect(%d) to ASys...", ++tryConnect));
|
||||
client.connect(Config.getString("host"), Config.getInt("port"));
|
||||
if (client.isConnected()) {
|
||||
stopReconnect();
|
||||
} else {
|
||||
log(String.format("Connection(%d) fail. Try reconnect...", tryConnect));
|
||||
}
|
||||
}, 0L, 5L, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private static void stopReconnect() {
|
||||
if (sesFuture != null) {
|
||||
sesFuture.cancel(false);
|
||||
sesFuture = null;
|
||||
tryConnect = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ProxySysOut extends PrintStream {
|
||||
final byte[] prefix = Ansi.ansi().bg(Color.YELLOW).fg(Color.BLACK).boldOff().toString().getBytes();
|
||||
final byte[] suffix = Ansi.ansi().reset().toString().getBytes();
|
||||
|
||||
ProxySysOut(OutputStream out) {
|
||||
super(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] buf, int off, int len) {
|
||||
super.write(prefix, 0, prefix.length);
|
||||
super.write(buf, off, len);
|
||||
super.write(suffix, 0, suffix.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
super.write(prefix, 0, prefix.length);
|
||||
super.write(b);
|
||||
super.write(suffix, 0, suffix.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) throws IOException {
|
||||
super.write(prefix, 0, prefix.length);
|
||||
super.write(b);
|
||||
super.write(suffix, 0, suffix.length);
|
||||
Connector.getInstance().sendMessage(new String(buf, off, len));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,9 @@ public class Client {
|
||||
group = new NioEventLoopGroup();
|
||||
bootstrap = createBootstrap();
|
||||
}
|
||||
|
||||
channelFuture = bootstrap.connect(host, port);
|
||||
channelFuture.awaitUninterruptibly(5000);
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
|
||||
@@ -56,9 +56,11 @@ public class ClientPacketHandler extends ChannelInboundHandlerAdapter implements
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
log("Lost connection!");
|
||||
log("Try reconnect...");
|
||||
Main.startReconnect();
|
||||
Connector.getInstance().setChannel(null);
|
||||
if (Connector.getInstance().isNeedReconnect()) {
|
||||
log("Lost connection! Try reconnect...");
|
||||
Connector.getInstance().startReconnect();
|
||||
}
|
||||
|
||||
ctx.channel().attr(KNOWN_PACKETS).remove();
|
||||
ctx.channel().attr(KNOWN_HANDLERS).remove();
|
||||
@@ -73,6 +75,7 @@ public class ClientPacketHandler extends ChannelInboundHandlerAdapter implements
|
||||
log(String.format("Handshake: #%d %s", pkt.getErrorCode(), pkt.getMessage()));
|
||||
} else {
|
||||
context.channel().attr(KNOWN_PACKETS).set(knownPackets);
|
||||
Connector.getInstance().setChannel(context.channel());
|
||||
log("Handshake: OK");
|
||||
}
|
||||
}
|
||||
|
||||
74
zond/src/main/java/asys/zond/proxy/Connector.java
Normal file
74
zond/src/main/java/asys/zond/proxy/Connector.java
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-06-14
|
||||
*/
|
||||
package asys.zond.proxy;
|
||||
|
||||
import asys.mcsmanager.packets.CS_ConsoleMessage;
|
||||
import asys.zond.Config;
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
import static asys.zond.Main.log;
|
||||
|
||||
public class Connector {
|
||||
private static Connector instance = new Connector();
|
||||
private Client client;
|
||||
private TaskTicker connectTicker;
|
||||
private int tryConnect = 0;
|
||||
private boolean needReconnect = true;
|
||||
private Channel channel;
|
||||
|
||||
public static Connector getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Connector(){
|
||||
}
|
||||
|
||||
public void startReconnect() {
|
||||
client = new Client();
|
||||
connectTicker = new TaskTicker().setStepTimeMs(5000L);
|
||||
connectTicker.setTask(() -> {
|
||||
log(String.format("Connect(%d) to ASys...", ++tryConnect));
|
||||
client.connect(Config.getString("host"), Config.getInt("port"));
|
||||
if (client.isConnected()) {
|
||||
stopReconnect();
|
||||
} else {
|
||||
log(String.format("Connection(%d) fail. Try reconnect...", tryConnect));
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public boolean isNeedReconnect() {
|
||||
return needReconnect;
|
||||
}
|
||||
|
||||
public void setNeedReconnect(boolean value) {
|
||||
this.needReconnect = value;
|
||||
}
|
||||
|
||||
public void stopReconnect() {
|
||||
if (connectTicker != null) {
|
||||
connectTicker.stop();
|
||||
tryConnect = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
if (client.isConnected()) {
|
||||
log("Disconnect...");
|
||||
client.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public void setChannel(Channel channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public void sendMessage(String message) {
|
||||
if (channel != null) {
|
||||
channel.writeAndFlush(new CS_ConsoleMessage(
|
||||
System.currentTimeMillis(),0,"L",message));
|
||||
}
|
||||
}
|
||||
}
|
||||
48
zond/src/main/java/asys/zond/proxy/TaskTicker.java
Normal file
48
zond/src/main/java/asys/zond/proxy/TaskTicker.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* DmitriyMX <d.mihailov@samson-rus.com>
|
||||
* 2017-05-18
|
||||
*/
|
||||
package asys.zond.proxy;
|
||||
|
||||
public class TaskTicker implements Runnable {
|
||||
private Runnable task;
|
||||
private long stepTimeMs = 1000L;
|
||||
private Thread thread;
|
||||
private boolean loop = false;
|
||||
|
||||
TaskTicker setTask(Runnable task) {
|
||||
this.task = task;
|
||||
return this;
|
||||
}
|
||||
|
||||
TaskTicker setStepTimeMs(long stepTimeMs) {
|
||||
this.stepTimeMs = stepTimeMs;
|
||||
return this;
|
||||
}
|
||||
|
||||
void start() {
|
||||
thread = new Thread(this, "TaskTicker");
|
||||
loop = true;
|
||||
thread.start();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
loop = false;
|
||||
if (thread != null) {
|
||||
thread.interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (loop || !Thread.currentThread().isInterrupted()) {
|
||||
task.run();
|
||||
|
||||
try {
|
||||
Thread.sleep(stepTimeMs);
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user