Archived
0

Zond: алгоритмы с командной строкой выделили в отдельный пакет

This commit is contained in:
2017-06-14 16:41:14 +03:00
parent 4f9c4790a3
commit 52c5a73942
6 changed files with 96 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
group = 'asys' group = 'asys'
version = '0.6.9-SNAPSHOT' version = '0.6.10-SNAPSHOT'
apply plugin: 'application' apply plugin: 'application'

View File

@@ -6,7 +6,7 @@
package asys.zond; package asys.zond;
import asys.zond.proxy.Connector; import asys.zond.proxy.Connector;
import jline.console.ConsoleReader; import asys.zond.shell.Shell;
import org.apache.commons.exec.*; import org.apache.commons.exec.*;
import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Color; import org.fusesource.jansi.Ansi.Color;
@@ -17,7 +17,7 @@ import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Main { public class Main {
private static JlineProxySysOut proxySysOut; private static ExecuteWatchdog watchdog;
private static void printLogo() { private static void printLogo() {
System.out.println( System.out.println(
@@ -31,8 +31,8 @@ public class Main {
String msg = Ansi.ansi().reset() String msg = Ansi.ansi().reset()
.bg(Color.BLUE).fg(Color.WHITE).a("[ASys Zond] ") .bg(Color.BLUE).fg(Color.WHITE).a("[ASys Zond] ")
.a(message).reset().toString(); .a(message).reset().toString();
if (proxySysOut != null) { if (Shell.getInstance().isActive()) {
proxySysOut.println(msg); Shell.getInstance().getOutput().println(msg);
} else { } else {
System.out.println(msg); System.out.println(msg);
} }
@@ -53,56 +53,32 @@ public class Main {
loadConfig(); loadConfig();
proxySysOut = new JlineProxySysOut(System.out); PipeInputStream pipeInputStream = new PipeInputStream();
JlineProxySysIn proxySysIn = new JlineProxySysIn(); Shell.getInstance().start(System.in, commandLine -> {
ConsoleReader console = new ConsoleReader(System.in, proxySysOut); if (commandLine.equalsIgnoreCase(":exit")) {
proxySysOut.setConsoleReader(console); Shell.getInstance().getOutput().println("force exit");
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(); watchdog.destroyProcess();
break; Shell.getInstance().shutdown();
}
proxySysIn.write(line+"\r\n");
}
}
} catch (Throwable ignore) {
} }
pipeInputStream.write(commandLine+"\r\n");
}); });
try {
thread.start();
} catch (Exception e) {
e.printStackTrace();//FIXME
}
Connector.getInstance().startReconnect(); Connector.getInstance().startReconnect();
int resultCode = executeProcess(args, proxySysOut, proxySysIn); int resultCode = executeProcess(args, Shell.getInstance().getOutput(), pipeInputStream);
Connector.getInstance().setNeedReconnect(false); Connector.getInstance().setNeedReconnect(false);
Connector.getInstance().stopReconnect(); Connector.getInstance().stopReconnect();
Connector.getInstance().disconnect(); Connector.getInstance().disconnect();
thread.interrupt(); Shell.getInstance().shutdown();
console.shutdown();
System.out.println( System.out.println(Ansi.ansi().reset().newline()
Ansi.ansi().reset().newline()
.fg(Color.GREEN).a("Process Finished. Code: ") .fg(Color.GREEN).a("Process Finished. Code: ")
.bold().fg(Color.WHITE).a(resultCode).reset() .bold().fg(Color.WHITE).a(resultCode).reset());
);
} }
private static ExecuteWatchdog watchdog;
private static int executeProcess(String[] args, PrintStream printStream, InputStream stdin) throws IOException { private static int executeProcess(String[] args, PrintStream printStream, InputStream stdin) throws IOException {
String cmdLine = Arrays.stream(args).collect(Collectors.joining(" ")); String cmdLine = Arrays.stream(args).collect(Collectors.joining(" "));
CommandLine commandLine = CommandLine.parse(cmdLine); CommandLine commandLine = CommandLine.parse(cmdLine);

View File

@@ -7,8 +7,8 @@ package asys.zond;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
public class JlineProxySysIn extends InputStream { public class PipeInputStream extends InputStream {
private byte[] buffer = new byte[15]; private byte[] buffer = new byte[1024];
private int lastWritePos = 0, private int lastWritePos = 0,
lastReadPos = 0, lastReadPos = 0,
wallPos = buffer.length-1; wallPos = buffer.length-1;

View File

@@ -0,0 +1,9 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-06-14
*/
package asys.zond.shell;
public interface CommandLineHandler {
void handle(String commandLine);
}

View File

@@ -2,7 +2,7 @@
* DmitriyMX <dimon550@gmail.com> * DmitriyMX <dimon550@gmail.com>
* 2017-06-14 * 2017-06-14
*/ */
package asys.zond; package asys.zond.shell;
import jline.console.ConsoleReader; import jline.console.ConsoleReader;

View 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) {
}
}
}