Archived
0

Zond: передача комманд в процесс

This commit is contained in:
2017-07-13 23:02:05 +03:00
parent 1f9bac5a0a
commit b82e9a6a99
4 changed files with 99 additions and 3 deletions

View File

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

View File

@@ -18,6 +18,7 @@ import java.util.stream.Collectors;
public class Main {
private Executor executor;
private CommandLine commandLine;
private PipeInputStream proxyStdIn;
public static void main(String[] args) throws IOException {
new Main().start(args);
@@ -31,7 +32,8 @@ public class Main {
System.exit(-2);
return;
}
ZondCommandHandler commandHandler = new ZondCommandHandler();
proxyStdIn = new PipeInputStream();
ZondCommandHandler commandHandler = new ZondCommandHandler(proxyStdIn);
startShell(commandHandler);
initExecCommand(args, Shell.getInstance().getOutput());
commandHandler.setExecutor(executor, commandLine);
@@ -51,7 +53,7 @@ public class Main {
String cmdLine = Arrays.stream(args).collect(Collectors.joining(" "));
commandLine = CommandLine.parse(cmdLine);
executor = new DefaultExecutor();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout);
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, stdout, proxyStdIn);
executor.setStreamHandler(pumpStreamHandler);
}
}

View File

@@ -0,0 +1,88 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-07-13
*/
package asys.zond;
import java.io.IOException;
import java.io.InputStream;
public class PipeInputStream 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;
}
}

View File

@@ -21,6 +21,11 @@ public class ZondCommandHandler implements CommandHandler {
private ExecuteWatchdog watchdog;
private Thread threadExec;
private Server server;
private PipeInputStream proxyStdIn;
ZondCommandHandler(PipeInputStream proxyStdIn) {
this.proxyStdIn = proxyStdIn;
}
@Override
public void handle(String commandLine) {
@@ -28,6 +33,7 @@ public class ZondCommandHandler implements CommandHandler {
internalCommand(commandLine.substring(1));
} else {
Shell.getInstance().getOutput().println(commandLine);
proxyStdIn.write(commandLine+"\n");
}
}