Zond: модифицируем ProxyStdIn
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
group = 'asys'
|
group = 'asys'
|
||||||
version = '0.6-SNAPSHOT'
|
version = '0.6.7-SNAPSHOT'
|
||||||
|
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
|||||||
88
zond/src/main/java/asys/zond/JlineProxySysIn.java
Normal file
88
zond/src/main/java/asys/zond/JlineProxySysIn.java
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2017-06-14
|
||||||
|
*/
|
||||||
|
package asys.zond;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public class JlineProxySysIn extends InputStream {
|
||||||
|
private byte[] buffer = new byte[15];
|
||||||
|
private int lastWritePos = 0,
|
||||||
|
lastReadPos = 0,
|
||||||
|
wallPos = buffer.length-1;
|
||||||
|
|
||||||
|
public synchronized void write(String s) {
|
||||||
|
byte[] strBytes = s.getBytes();
|
||||||
|
|
||||||
|
while ((lastWritePos < lastReadPos) && ((lastReadPos - lastWritePos) >= strBytes.length)) {
|
||||||
|
try {
|
||||||
|
wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((lastWritePos + strBytes.length) >= 1024) {
|
||||||
|
wallPos = lastWritePos;
|
||||||
|
lastWritePos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.arraycopy(strBytes, 0, this.buffer, lastWritePos, strBytes.length);
|
||||||
|
lastWritePos += strBytes.length;
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized int read() throws IOException {
|
||||||
|
if (lastReadPos == lastWritePos) {
|
||||||
|
try {
|
||||||
|
wait();
|
||||||
|
} catch (InterruptedException ignore) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastReadPos == wallPos) {
|
||||||
|
lastReadPos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.buffer[lastReadPos++];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read(byte[] b) throws IOException {
|
||||||
|
return this.read(b, 0, b.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized int read(byte[] buffOut, int off, int len) throws IOException {
|
||||||
|
if (lastReadPos == lastWritePos) {
|
||||||
|
try {
|
||||||
|
wait();
|
||||||
|
} catch (InterruptedException ignore) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int actualLen = len;
|
||||||
|
|
||||||
|
if (lastReadPos > lastWritePos) {
|
||||||
|
if ((lastReadPos + len) > wallPos) {
|
||||||
|
actualLen = (wallPos - lastReadPos);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ((lastReadPos + len) > lastWritePos) {
|
||||||
|
actualLen = (lastWritePos - lastReadPos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.arraycopy(this.buffer, lastReadPos, buffOut, off, actualLen);
|
||||||
|
lastReadPos += actualLen;
|
||||||
|
if (lastReadPos == wallPos) {
|
||||||
|
lastReadPos = 0;
|
||||||
|
}
|
||||||
|
notify();
|
||||||
|
return actualLen;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,10 +7,7 @@ package asys.zond;
|
|||||||
|
|
||||||
import asys.zond.proxy.Connector;
|
import asys.zond.proxy.Connector;
|
||||||
import jline.console.ConsoleReader;
|
import jline.console.ConsoleReader;
|
||||||
import org.apache.commons.exec.CommandLine;
|
import org.apache.commons.exec.*;
|
||||||
import org.apache.commons.exec.DefaultExecutor;
|
|
||||||
import org.apache.commons.exec.ExecuteException;
|
|
||||||
import org.apache.commons.exec.PumpStreamHandler;
|
|
||||||
import org.fusesource.jansi.Ansi;
|
import org.fusesource.jansi.Ansi;
|
||||||
import org.fusesource.jansi.Ansi.Color;
|
import org.fusesource.jansi.Ansi.Color;
|
||||||
import org.fusesource.jansi.AnsiConsole;
|
import org.fusesource.jansi.AnsiConsole;
|
||||||
@@ -52,9 +49,11 @@ public class Main {
|
|||||||
loadConfig();
|
loadConfig();
|
||||||
|
|
||||||
JlineProxySysOut proxySysOut = new JlineProxySysOut(System.out);
|
JlineProxySysOut proxySysOut = new JlineProxySysOut(System.out);
|
||||||
|
JlineProxySysIn proxySysIn = new JlineProxySysIn();
|
||||||
ConsoleReader console = new ConsoleReader(System.in, proxySysOut);
|
ConsoleReader console = new ConsoleReader(System.in, proxySysOut);
|
||||||
proxySysOut.setConsoleReader(console);
|
proxySysOut.setConsoleReader(console);
|
||||||
console.setPrompt(ConsoleReader.RESET_LINE + ":");
|
console.setPrompt(ConsoleReader.RESET_LINE + ":");
|
||||||
|
|
||||||
Thread thread = new Thread(() -> {
|
Thread thread = new Thread(() -> {
|
||||||
Thread current = Thread.currentThread();
|
Thread current = Thread.currentThread();
|
||||||
|
|
||||||
@@ -62,7 +61,12 @@ public class Main {
|
|||||||
while (!current.isInterrupted()) {
|
while (!current.isInterrupted()) {
|
||||||
String line;
|
String line;
|
||||||
if ((line = console.readLine()) != null) {
|
if ((line = console.readLine()) != null) {
|
||||||
proxySysOut.println(line);
|
if (line.equalsIgnoreCase(":exit")) {
|
||||||
|
proxySysOut.println("force exit");
|
||||||
|
watchdog.destroyProcess();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
proxySysIn.write(line+"\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -79,30 +83,34 @@ public class Main {
|
|||||||
|
|
||||||
// Connector.getInstance().startReconnect();
|
// Connector.getInstance().startReconnect();
|
||||||
|
|
||||||
// int resultCode = executeProcess(args);
|
int resultCode = executeProcess(args, proxySysOut, proxySysIn);
|
||||||
|
|
||||||
// Connector.getInstance().setNeedReconnect(false);
|
// Connector.getInstance().setNeedReconnect(false);
|
||||||
// Connector.getInstance().stopReconnect();
|
// Connector.getInstance().stopReconnect();
|
||||||
// Connector.getInstance().disconnect();
|
// Connector.getInstance().disconnect();
|
||||||
|
|
||||||
// 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 int executeProcess(String[] args) {
|
private static ExecuteWatchdog watchdog;
|
||||||
|
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);
|
||||||
DefaultExecutor executor = new DefaultExecutor();
|
DefaultExecutor executor = new DefaultExecutor();
|
||||||
|
|
||||||
PrintStream proxySysOut = new ProxySysOut(System.out);
|
// PrintStream proxySysOut = new ProxySysOut(System.out);
|
||||||
InputStream proxySysIn = new ProxySysIn();
|
// InputStream proxySysIn = new ProxySysIn();
|
||||||
|
|
||||||
PumpStreamHandler psh = new PumpStreamHandler(proxySysOut, proxySysOut, proxySysIn);
|
// PumpStreamHandler psh = new PumpStreamHandler(proxySysOut, proxySysOut, proxySysIn);
|
||||||
|
PumpStreamHandler psh = new PumpStreamHandler(printStream, printStream, stdin);
|
||||||
psh.setStopTimeout(-1999); //hack: по-умолчанию в Apache Exec добавляется еще 2000L милисекунд
|
psh.setStopTimeout(-1999); //hack: по-умолчанию в Apache Exec добавляется еще 2000L милисекунд
|
||||||
executor.setStreamHandler(psh);
|
executor.setStreamHandler(psh);
|
||||||
|
watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
|
||||||
|
executor.setWatchdog(watchdog);
|
||||||
|
|
||||||
int resultCode = 0;
|
int resultCode = 0;
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user