Skip to content

Commit

Permalink
Move the non-blocking input stream implementation to the Pty, #140
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jan 9, 2018
1 parent e6d5912 commit b15a992
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.fusesource.jansi.internal.CLibrary;
import org.jline.terminal.Attributes;
import org.jline.terminal.Size;
import org.jline.terminal.impl.AbstractPty;
import org.jline.terminal.spi.Pty;
import org.jline.utils.OSUtils;

Expand All @@ -27,7 +28,7 @@
import static org.jline.terminal.impl.jansi.JansiSupportImpl.JANSI_MINOR_VERSION;
import static org.jline.utils.ExecHelper.exec;

public abstract class JansiNativePty implements Pty {
public abstract class JansiNativePty extends AbstractPty implements Pty {

private final int master;
private final int slave;
Expand Down Expand Up @@ -117,7 +118,7 @@ public OutputStream getMasterOutput() {
return new FileOutputStream(getMasterFD());
}

public InputStream getSlaveInput() {
protected InputStream doGetSlaveInput() {
return new FileInputStream(getSlaveFD());
}

Expand All @@ -134,7 +135,7 @@ public Attributes getAttr() throws IOException {
}

@Override
public void setAttr(Attributes attr) throws IOException {
protected void doSetAttr(Attributes attr) throws IOException {
CLibrary.Termios tios = toTermios(attr);
CLibrary.tcsetattr(slave, TCSANOW, tios);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
import com.sun.jna.Platform;
import org.jline.terminal.Attributes;
import org.jline.terminal.Size;
import org.jline.terminal.impl.AbstractPty;
import org.jline.terminal.spi.Pty;
import org.jline.terminal.impl.jna.freebsd.FreeBsdNativePty;
import org.jline.terminal.impl.jna.linux.LinuxNativePty;
import org.jline.terminal.impl.jna.osx.OsXNativePty;
import org.jline.terminal.impl.jna.solaris.SolarisNativePty;

public abstract class JnaNativePty implements Pty {
public abstract class JnaNativePty extends AbstractPty implements Pty {

private final int master;
private final int slave;
Expand Down Expand Up @@ -123,7 +124,7 @@ public OutputStream getMasterOutput() {
return new FileOutputStream(getMasterFD());
}

public InputStream getSlaveInput() {
protected InputStream doGetSlaveInput() {
return new FileInputStream(getSlaveFD());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Attributes getAttr() throws IOException {
}

@Override
public void setAttr(Attributes attr) throws IOException {
protected void doSetAttr(Attributes attr) throws IOException {
termios termios = new termios(attr);
C_LIBRARY.tcsetattr(getSlave(), TCSANOW, termios);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Attributes getAttr() throws IOException {
}

@Override
public void setAttr(Attributes attr) throws IOException {
protected void doSetAttr(Attributes attr) throws IOException {
termios termios = new termios(attr);
termios org = new termios();
C_LIBRARY.tcgetattr(getSlave(), org);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Attributes getAttr() throws IOException {
}

@Override
public void setAttr(Attributes attr) throws IOException {
protected void doSetAttr(Attributes attr) throws IOException {
termios termios = new termios(attr);
C_LIBRARY.tcsetattr(getSlave(), TCSANOW, termios);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Attributes getAttr() throws IOException {
}

@Override
public void setAttr(Attributes attr) throws IOException {
protected void doSetAttr(Attributes attr) throws IOException {
termios termios = new termios(attr);
C_LIBRARY.tcsetattr(getSlave(), TCSANOW, termios);
}
Expand Down
95 changes: 95 additions & 0 deletions terminal/src/main/java/org/jline/terminal/impl/AbstractPty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.jline.terminal.impl;

import org.jline.terminal.Attributes;
import org.jline.terminal.spi.Pty;
import org.jline.utils.NonBlockingInputStream;

import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;

public abstract class AbstractPty implements Pty {

private Attributes current;

@Override
public void setAttr(Attributes attr) throws IOException {
current = new Attributes(attr);
doSetAttr(attr);
}

@Override
public InputStream getSlaveInput() throws IOException {
InputStream si = doGetSlaveInput();
if (Boolean.parseBoolean(System.getProperty("org.jline.terminal.pty.nonBlockingReads", "true"))) {
return new PtyInputStream(si);
} else {
return si;
}
}

protected abstract void doSetAttr(Attributes attr) throws IOException;

protected abstract InputStream doGetSlaveInput() throws IOException;

protected void checkInterrupted() throws InterruptedIOException {
if (Thread.interrupted()) {
throw new InterruptedIOException();
}
}

class PtyInputStream extends NonBlockingInputStream {
final InputStream in;
int c = 0;

PtyInputStream(InputStream in) {
this.in = in;
}

@Override
public int read(long timeout, boolean isPeek) throws IOException {
checkInterrupted();
if (c != 0) {
int r = c;
if (!isPeek) {
c = 0;
}
return r;
} else {
setNonBlocking();
long start = System.currentTimeMillis();
while (true) {
int r = in.read();
if (r >= 0) {
if (isPeek) {
c = r;
}
return r;
}
checkInterrupted();
long cur = System.currentTimeMillis();
if (timeout > 0 && cur - start > timeout) {
return NonBlockingInputStream.READ_EXPIRED;
}
}
}
}

private void setNonBlocking() {
if (current == null
|| current.getControlChar(Attributes.ControlChar.VMIN) != 0
|| current.getControlChar(Attributes.ControlChar.VTIME) != 1) {
try {
Attributes attr = getAttr();
attr.setControlChar(Attributes.ControlChar.VMIN, 0);
attr.setControlChar(Attributes.ControlChar.VTIME, 1);
setAttr(attr);
} catch (IOException e) {
throw new IOError(e);
}
}
}
}

}
6 changes: 3 additions & 3 deletions terminal/src/main/java/org/jline/terminal/impl/ExecPty.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import static org.jline.utils.ExecHelper.exec;

public class ExecPty implements Pty {
public class ExecPty extends AbstractPty implements Pty {

private final String name;
private final boolean system;
Expand Down Expand Up @@ -69,7 +69,7 @@ public OutputStream getMasterOutput() {
}

@Override
public InputStream getSlaveInput() throws IOException {
protected InputStream doGetSlaveInput() throws IOException {
return system
? new FileInputStream(FileDescriptor.in)
: new FileInputStream(getName());
Expand All @@ -89,7 +89,7 @@ public Attributes getAttr() throws IOException {
}

@Override
public void setAttr(Attributes attr) throws IOException {
protected void doSetAttr(Attributes attr) throws IOException {
List<String> commands = getFlagsToSet(attr, getAttr());
if (!commands.isEmpty()) {
commands.add(0, OSUtils.STTY_COMMAND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import org.jline.terminal.Attributes;
import org.jline.terminal.spi.Pty;
import org.jline.utils.ClosedException;
import org.jline.utils.NonBlocking;
Expand All @@ -37,7 +36,6 @@ public class PosixPtyTerminal extends AbstractPosixTerminal {
private Thread inputPumpThread;
private Thread outputPumpThread;
private AtomicBoolean paused = new AtomicBoolean(true);
private Attributes current;

public PosixPtyTerminal(String name, String type, Pty pty, InputStream in, OutputStream out, Charset encoding) throws IOException {
this(name, type, pty, in, out, encoding, SignalHandler.SIG_DFL);
Expand All @@ -49,7 +47,7 @@ public PosixPtyTerminal(String name, String type, Pty pty, InputStream in, Outpu
this.out = Objects.requireNonNull(out);
this.masterInput = pty.getMasterInput();
this.masterOutput = pty.getMasterOutput();
this.input = new InputStreamWrapper(new PosixInputStream(pty.getSlaveInput()));
this.input = new InputStreamWrapper(NonBlocking.nonBlocking(name, pty.getSlaveInput()));
this.output = pty.getSlaveOutput();
this.reader = NonBlocking.nonBlocking(name, input, encoding());
this.writer = new PrintWriter(new OutputStreamWriter(output, encoding()));
Expand Down Expand Up @@ -104,62 +102,6 @@ public boolean paused() {
return paused.get();
}

@Override
public void setAttributes(Attributes attr) {
super.setAttributes(attr);
current = new Attributes(attr);
}

class PosixInputStream extends NonBlockingInputStream {

final InputStream in;
int c = 0;

PosixInputStream(InputStream in) {
this.in = in;
}

@Override
public int read(long timeout, boolean isPeek) throws IOException {
checkInterrupted();
if (c != 0) {
int r = c;
if (!isPeek) {
c = 0;
}
return r;
} else {
setNonBlocking();
long start = System.currentTimeMillis();
while (true) {
int r = in.read();
if (r >= 0) {
if (isPeek) {
c = r;
}
return r;
}
checkInterrupted();
long cur = System.currentTimeMillis();
if (timeout > 0 && cur - start > timeout) {
return NonBlockingInputStream.READ_EXPIRED;
}
}
}
}

private void setNonBlocking() {
if (current == null
|| current.getControlChar(Attributes.ControlChar.VMIN) != 0
|| current.getControlChar(Attributes.ControlChar.VTIME) != 1) {
Attributes attr = getAttributes();
attr.setControlChar(Attributes.ControlChar.VMIN, 0);
attr.setControlChar(Attributes.ControlChar.VTIME, 1);
setAttributes(attr);
}
}
}

private class InputStreamWrapper extends NonBlockingInputStream {

private final NonBlockingInputStream in;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.HashMap;
import java.util.Map;

import org.jline.terminal.Attributes;
import org.jline.utils.NonBlocking;
import org.jline.terminal.spi.Pty;
import org.jline.utils.NonBlockingInputStream;
Expand All @@ -34,12 +33,11 @@ public class PosixSysTerminal extends AbstractPosixTerminal {
protected final PrintWriter writer;
protected final Map<Signal, Object> nativeHandlers = new HashMap<>();
protected final Task closer;
private Attributes current;

public PosixSysTerminal(String name, String type, Pty pty, Charset encoding,
boolean nativeSignals, SignalHandler signalHandler) throws IOException {
super(name, type, pty, encoding, signalHandler);
this.input = new PosixInputStream(pty.getSlaveInput());
this.input = NonBlocking.nonBlocking(getName(), pty.getSlaveInput());
this.output = pty.getSlaveOutput();
this.reader = NonBlocking.nonBlocking(getName(), input, encoding());
this.writer = new PrintWriter(new OutputStreamWriter(output, encoding()));
Expand Down Expand Up @@ -99,59 +97,4 @@ public void close() throws IOException {
reader.shutdown();
}

@Override
public void setAttributes(Attributes attr) {
super.setAttributes(attr);
current = new Attributes(attr);
}

class PosixInputStream extends NonBlockingInputStream {
final InputStream in;
int c = 0;

PosixInputStream(InputStream in) {
this.in = in;
}

@Override
public int read(long timeout, boolean isPeek) throws IOException {
checkInterrupted();
if (c != 0) {
int r = c;
if (!isPeek) {
c = 0;
}
return r;
} else {
setNonBlocking();
long start = System.currentTimeMillis();
while (true) {
int r = in.read();
if (r >= 0) {
if (isPeek) {
c = r;
}
return r;
}
checkInterrupted();
long cur = System.currentTimeMillis();
if (timeout > 0 && cur - start > timeout) {
return NonBlockingInputStream.READ_EXPIRED;
}
}
}
}

private void setNonBlocking() {
if (current == null
|| current.getControlChar(Attributes.ControlChar.VMIN) != 0
|| current.getControlChar(Attributes.ControlChar.VTIME) != 1) {
Attributes attr = getAttributes();
attr.setControlChar(Attributes.ControlChar.VMIN, 0);
attr.setControlChar(Attributes.ControlChar.VTIME, 1);
setAttributes(attr);
}
}
}

}

0 comments on commit b15a992

Please sign in to comment.