Zond: алгоритмы с командной строкой выделили в отдельный пакет
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
group = 'asys'
|
||||
version = '0.6.9-SNAPSHOT'
|
||||
version = '0.6.10-SNAPSHOT'
|
||||
|
||||
apply plugin: 'application'
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
package asys.zond;
|
||||
|
||||
import asys.zond.proxy.Connector;
|
||||
import jline.console.ConsoleReader;
|
||||
import asys.zond.shell.Shell;
|
||||
import org.apache.commons.exec.*;
|
||||
import org.fusesource.jansi.Ansi;
|
||||
import org.fusesource.jansi.Ansi.Color;
|
||||
@@ -17,7 +17,7 @@ import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
private static JlineProxySysOut proxySysOut;
|
||||
private static ExecuteWatchdog watchdog;
|
||||
|
||||
private static void printLogo() {
|
||||
System.out.println(
|
||||
@@ -31,8 +31,8 @@ public class Main {
|
||||
String msg = Ansi.ansi().reset()
|
||||
.bg(Color.BLUE).fg(Color.WHITE).a("[ASys Zond] ")
|
||||
.a(message).reset().toString();
|
||||
if (proxySysOut != null) {
|
||||
proxySysOut.println(msg);
|
||||
if (Shell.getInstance().isActive()) {
|
||||
Shell.getInstance().getOutput().println(msg);
|
||||
} else {
|
||||
System.out.println(msg);
|
||||
}
|
||||
@@ -53,56 +53,32 @@ public class Main {
|
||||
|
||||
loadConfig();
|
||||
|
||||
proxySysOut = new JlineProxySysOut(System.out);
|
||||
JlineProxySysIn proxySysIn = new JlineProxySysIn();
|
||||
ConsoleReader console = new ConsoleReader(System.in, proxySysOut);
|
||||
proxySysOut.setConsoleReader(console);
|
||||
console.setPrompt(ConsoleReader.RESET_LINE + ":");
|
||||
|
||||
Thread thread = new Thread(() -> {
|
||||
Thread current = Thread.currentThread();
|
||||
|
||||
try {
|
||||
while (!current.isInterrupted()) {
|
||||
String line;
|
||||
if ((line = console.readLine()) != null) {
|
||||
if (line.equalsIgnoreCase(":exit")) {
|
||||
proxySysOut.println("force exit");
|
||||
watchdog.destroyProcess();
|
||||
break;
|
||||
}
|
||||
proxySysIn.write(line+"\r\n");
|
||||
}
|
||||
}
|
||||
} catch (Throwable ignore) {
|
||||
PipeInputStream pipeInputStream = new PipeInputStream();
|
||||
Shell.getInstance().start(System.in, commandLine -> {
|
||||
if (commandLine.equalsIgnoreCase(":exit")) {
|
||||
Shell.getInstance().getOutput().println("force exit");
|
||||
watchdog.destroyProcess();
|
||||
Shell.getInstance().shutdown();
|
||||
}
|
||||
pipeInputStream.write(commandLine+"\r\n");
|
||||
});
|
||||
try {
|
||||
thread.start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();//FIXME
|
||||
}
|
||||
|
||||
|
||||
Connector.getInstance().startReconnect();
|
||||
|
||||
int resultCode = executeProcess(args, proxySysOut, proxySysIn);
|
||||
int resultCode = executeProcess(args, Shell.getInstance().getOutput(), pipeInputStream);
|
||||
|
||||
Connector.getInstance().setNeedReconnect(false);
|
||||
Connector.getInstance().stopReconnect();
|
||||
Connector.getInstance().disconnect();
|
||||
|
||||
thread.interrupt();
|
||||
console.shutdown();
|
||||
Shell.getInstance().shutdown();
|
||||
|
||||
System.out.println(
|
||||
Ansi.ansi().reset().newline()
|
||||
.fg(Color.GREEN).a("Process Finished. Code: ")
|
||||
.bold().fg(Color.WHITE).a(resultCode).reset()
|
||||
);
|
||||
System.out.println(Ansi.ansi().reset().newline()
|
||||
.fg(Color.GREEN).a("Process Finished. Code: ")
|
||||
.bold().fg(Color.WHITE).a(resultCode).reset());
|
||||
}
|
||||
|
||||
private static ExecuteWatchdog watchdog;
|
||||
private static int executeProcess(String[] args, PrintStream printStream, InputStream stdin) throws IOException {
|
||||
String cmdLine = Arrays.stream(args).collect(Collectors.joining(" "));
|
||||
CommandLine commandLine = CommandLine.parse(cmdLine);
|
||||
|
||||
@@ -7,8 +7,8 @@ package asys.zond;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class JlineProxySysIn extends InputStream {
|
||||
private byte[] buffer = new byte[15];
|
||||
public class PipeInputStream extends InputStream {
|
||||
private byte[] buffer = new byte[1024];
|
||||
private int lastWritePos = 0,
|
||||
lastReadPos = 0,
|
||||
wallPos = buffer.length-1;
|
||||
@@ -0,0 +1,9 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-06-14
|
||||
*/
|
||||
package asys.zond.shell;
|
||||
|
||||
public interface CommandLineHandler {
|
||||
void handle(String commandLine);
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-06-14
|
||||
*/
|
||||
package asys.zond;
|
||||
package asys.zond.shell;
|
||||
|
||||
import jline.console.ConsoleReader;
|
||||
|
||||
67
zond/src/main/java/asys/zond/shell/Shell.java
Normal file
67
zond/src/main/java/asys/zond/shell/Shell.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-06-14
|
||||
*/
|
||||
package asys.zond.shell;
|
||||
|
||||
import jline.console.ConsoleReader;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class Shell {
|
||||
private static Shell instance = new Shell();
|
||||
private boolean active = false;
|
||||
private ConsoleReader console;
|
||||
private JlineProxySysOut proxySysOut;
|
||||
private Thread threadCommandHandler;
|
||||
|
||||
public static Shell getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Shell() {
|
||||
}
|
||||
|
||||
public void start(final InputStream inputStream, final CommandLineHandler commandLineHandler) throws IOException {
|
||||
proxySysOut = new JlineProxySysOut(System.out);
|
||||
console = new ConsoleReader(inputStream, proxySysOut);
|
||||
proxySysOut.setConsoleReader(console);
|
||||
console.setPrompt(ConsoleReader.RESET_LINE + ":");
|
||||
|
||||
threadCommandHandler = new Thread(() -> {
|
||||
Thread current = Thread.currentThread();
|
||||
|
||||
try {
|
||||
String line;
|
||||
while (!current.isInterrupted() && ((line = console.readLine()) != null)) {
|
||||
commandLineHandler.handle(line);
|
||||
}
|
||||
} catch (IOException ignore) {
|
||||
}
|
||||
|
||||
console.shutdown();
|
||||
}, "Shell: command handler");
|
||||
active = true;
|
||||
threadCommandHandler.start();
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
threadCommandHandler.interrupt();
|
||||
active = false;
|
||||
}
|
||||
|
||||
public JlineProxySysOut getOutput() {
|
||||
return proxySysOut;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
private void safeSleep() {
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user