Top Description Inners Methods
org.graalvm.polyglot.io

public Interface MessageTransport

Imports
java.io.IOException, java.net.URI

Allows to take over transport of message communication initiated by an instrument. Implement this interface to provide a transport of message communication. When an instrument is about to create a server endpoint, it calls the 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();
Since
19.0
See Also
org.graalvm.polyglot.Engine.Builder#serverTransport(MessageTransport), org.graalvm.polyglot.Context.Builder#serverTransport(MessageTransport)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
MessageTransport.VetoException

Thrown when a transport connection is vetoed.

Method Summary

Modifier and TypeMethod and Description
public MessageEndpoint

Returns:

an implementation of MessageEndpoint call back, or null.
open
(URI
the connection URI
uri
,
MessageEndpoint
the peer endpoint representation
peerEndpoint
)

Called when a connection to an URI is to be established.

Method Detail

openback 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 null is returned.

This method can be called concurrently from multiple threads. However, the MessageEndpoint ought to be called on one thread at a time, unless you're sure that the particular implementation can handle concurrent calls. The same holds true for the returned endpoint, it's called synchronously.

Parameters
uri:URI

the connection URI

peerEndpoint:MessageEndpoint

the peer endpoint representation

Returns:MessageEndpoint

an implementation of MessageEndpoint call back, or null.

Exceptions
VetoException:
to veto connection to the URL.
Since
19.0
org.graalvm.polyglot.io back to summary

public final Class MessageTransport.VetoException

extends Exception
Class Inheritance

Thrown when a transport connection is vetoed. The initiator of the connection is obliged to abandon it when this exception is thrown.
Since
19.0

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
public
VetoException(String message)

Create a new VetoException.

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Exception.serialVersionUID.

Constructor Detail

VetoExceptionback to summary
public VetoException(String message)

Create a new VetoException.

Since
19.0