-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented JobOutputStreamForwarder which fixes #6
- Loading branch information
Showing
5 changed files
with
82 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ hs_err_pid* | |
|
||
__pycache__ | ||
*.pyc | ||
/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 59 additions & 12 deletions
71
src/main/java/nl/esciencecenter/xenon/grpc/jobs/JobOutputStreamsForwarder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,80 @@ | ||
package nl.esciencecenter.xenon.grpc.jobs; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import nl.esciencecenter.xenon.grpc.XenonProto; | ||
|
||
import com.google.protobuf.ByteString; | ||
|
||
import io.grpc.Status; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
class JobOutputStreamsForwarder { | ||
private final XenonProto.JobOutputStreams.Builder builder; | ||
|
||
private final XenonProto.JobOutputStreams.Builder builder; | ||
private final StreamObserver<XenonProto.JobOutputStreams> observer; | ||
|
||
private static final int BUFFER_SIZE = 1024; | ||
|
||
private int streamsDone = 0; | ||
|
||
class StreamForwarder extends Thread { | ||
|
||
private final byte[] buffer = new byte[BUFFER_SIZE]; | ||
|
||
private final InputStream in; | ||
|
||
private boolean stdout = false; | ||
|
||
public StreamForwarder(InputStream in, boolean stdout) { | ||
super("Stream forwarder " + (stdout ? "stdout" : "stderr")); | ||
this.in = in; | ||
this.stdout = stdout; | ||
setDaemon(true); | ||
} | ||
|
||
public void run() { | ||
try { | ||
while (true) { | ||
int read = in.read(buffer); | ||
|
||
if (read > 0) { | ||
XenonProto.JobOutputStreams response; | ||
|
||
if (stdout) { | ||
response = builder.clearStderr().setStdout(ByteString.copyFrom(buffer, 0, read)).build(); | ||
} else { | ||
response = builder.clearStdout().setStderr(ByteString.copyFrom(buffer, 0, read)).build(); | ||
} | ||
|
||
writeOut(response); | ||
|
||
} else if (read == -1) { | ||
close(); | ||
return; | ||
} } | ||
} catch (IOException e) { | ||
observer.onError(Status.INTERNAL.withDescription(e.getMessage()).withCause(e).asException()); | ||
} | ||
} | ||
} | ||
|
||
JobOutputStreamsForwarder(StreamObserver<XenonProto.JobOutputStreams> responseObserver, InputStream stderr, InputStream stdout) { | ||
this.observer = responseObserver; | ||
builder = XenonProto.JobOutputStreams.newBuilder(); | ||
// TODO setup forwarder to pipe stdout and stderr of streams to responseObserver | ||
} | ||
|
||
private void writeStdOut(byte[] value) { | ||
XenonProto.JobOutputStreams response = builder.clearStderr().setStdout(ByteString.copyFrom(value)).build(); | ||
observer.onNext(response); | ||
// We should fully read the in and output streams here (non blocking and in parallel) and forward the data to the responseObserver. | ||
new StreamForwarder(stdout, true).start(); | ||
new StreamForwarder(stderr, false).start(); | ||
} | ||
|
||
private void writeStdErr(byte[] value) { | ||
XenonProto.JobOutputStreams response = builder.clearStdout().setStderr(ByteString.copyFrom(value)).build(); | ||
observer.onNext(response); | ||
private synchronized void writeOut(XenonProto.JobOutputStreams response) { | ||
observer.onNext(response); | ||
} | ||
|
||
void close() { | ||
observer.onCompleted(); | ||
public synchronized void close() { | ||
if (++streamsDone == 2) { | ||
observer.onCompleted(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters