package com.oracle.truffle.tools.dap.test;
import java.io.PrintWriter;
import com.oracle.truffle.api.instrumentation.TruffleInstrument;
import com.oracle.truffle.tools.dap.server.DebugProtocolServerImpl;
import com.oracle.truffle.tools.dap.server.ExecutionContext;
import com.oracle.truffle.tools.dap.types.DebugProtocolServer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.concurrent.Executors;
@TruffleInstrument.Registration(id = DAPTestInstrument.ID, services = DAPSessionHandlerProvider.class)
public class DAPTestInstrument extends TruffleInstrument {
public static final String ID = "DAPTestInstrument";
@Override
protected void onCreate(final Env env) {
env.registerService(new DAPSessionHandlerProvider() {
@Override
public DAPSessionHandler getSessionHandler(final boolean suspend, final boolean inspectInternal, final boolean inspectInitialization) throws IOException {
return new DAPSessionHandler() {
private PipedOutputStream out;
private InputStream in;
private ExecutionContext context;
DAPSessionHandler init() throws IOException {
this.context = new ExecutionContext(env, new PrintWriter(env.out(), true), new PrintWriter(env.err(), true), inspectInternal, inspectInitialization);
this.out = new PipedOutputStream();
PipedOutputStream pos = new PipedOutputStream();
in = new PipedInputStream(pos, 2048);
DebugProtocolServer.Session.connect(DebugProtocolServerImpl.create(context, suspend, inspectInternal, inspectInitialization), new PipedInputStream(this.out, 2048), pos,
Executors.newSingleThreadExecutor());
return this;
}
@Override
public OutputStream getOutputStream() {
return out;
}
@Override
public InputStream getInputStream() {
return in;
}
}.init();
}
});
}
}
interface DAPSessionHandlerProvider {
DAPSessionHandler getSessionHandler(boolean suspend, boolean inspectInternal, boolean inspectInitialization) throws IOException;
}
interface DAPSessionHandler {
OutputStream getOutputStream();
InputStream getInputStream();
}