MessageTransport#open(URI, MessageEndpoint) method with
the server URI. Usage example:
class RoutedServer implements MessageEndpoint {
private final MessageEndpoint remoteEndpoint;
private final OutputStream routerOut = getRouterOutputStream();
private final WritableByteChannel routerOutChannel;
RoutedServer(MessageEndpoint remoteEndpoint) {
this.remoteEndpoint = remoteEndpoint;
this.routerOutChannel = Channels.newChannel(routerOut);
new Thread(() -> {
try {
runInputLoop();
} catch (IOException ex) {
}
}).start();
}
@Override
public void sendText(String text) throws IOException {
routerOut.write(text.getBytes());
routerOut.flush();
}
@Override
public void sendBinary(ByteBuffer data) throws IOException {
routerOutChannel.write(data);
routerOut.flush();
}
@Override
public void sendPing(ByteBuffer data) throws IOException {
remoteEndpoint.sendPong(data);
}
@Override
public void sendPong(ByteBuffer data) throws IOException {
// Did we send ping?
}
@Override
public void sendClose() throws IOException {
routerOut.close();
}
private void runInputLoop() throws IOException {
try (InputStream routerIn = getRouterInputStream()) {
byte[] buf = new byte[1024];
ByteBuffer bb = ByteBuffer.wrap(buf);
int l;
while ((l = routerIn.read(buf)) > 0) {
bb.limit(l);
remoteEndpoint.sendBinary(bb);
bb.rewind();
}
} finally {
remoteEndpoint.sendClose();
}
}
}
Engine.newBuilder().serverTransport(
(uri, peerEndpoint) -> {
if (denyHost.equals(uri.getHost())) {
throw new MessageTransport.VetoException("Denied access.");
} else if (routedURI.equals(uri)) {
return new RoutedServer(peerEndpoint);
} else {
// Permit instruments to setup the servers themselves
return null;
}
}).build();
org.graalvm.polyglot.Engine.Builder#serverTransport(MessageTransport) , org.graalvm.polyglot.Context.Builder#serverTransport(MessageTransport)
| Modifier and Type | Class and Description |
|---|---|
| public static class | MessageTransport.
Thrown when a transport connection is vetoed. |
| Modifier and Type | Method and Description |
|---|---|
| public MessageEndpoint | open(URI
the connection URI uri, MessageEndpoint the peer endpoint representation peerEndpoint)Called when a connection to an URI is to be established. |
| open | back to summary |
|---|---|
| public MessageEndpoint open(URI uri, MessageEndpoint peerEndpoint) throws IOException, VetoException Called when a connection to an URI is to be established. The virtualized connection is either
opened and an endpoint call back is returned, or the connection is not virtualized in which
case
This method can be called concurrently from multiple threads. However, the
| |
| Modifier and Type | Field and Description |
|---|---|
| private static final long |
| Access | Constructor and Description |
|---|---|
| public |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| VetoException | back to summary |
|---|---|
| public VetoException(String message) Create a new VetoException.
| |