Search in sources :

Example 6 with MessageConsumer

use of io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer in project zilla by aklivity.

the class WsServerFactory method newStream.

@Override
public MessageConsumer newStream(int msgTypeId, DirectBuffer buffer, int index, int length, MessageConsumer sender) {
    final BeginFW begin = beginRO.wrap(buffer, index, index + length);
    final long traceId = begin.traceId();
    final long routeId = begin.routeId();
    final long initialId = begin.streamId();
    final long authorization = begin.authorization();
    final long affinity = begin.affinity();
    final OctetsFW extension = begin.extension();
    // TODO: need lightweight approach (start)
    final HttpBeginExFW httpBeginEx = extension.get(httpBeginExRO::wrap);
    final Map<String, String> headers = new LinkedHashMap<>();
    httpBeginEx.headers().forEach(header -> {
        final String name = header.name().asString();
        final String value = header.value().asString();
        headers.merge(name, value, (v1, v2) -> String.format("%s, %s", v1, v2));
    });
    final String scheme = headers.get(":scheme");
    final String authority = headers.get(":authority");
    final String path = headers.get(":path");
    final String upgrade = headers.get("upgrade");
    final String version = headers.get("sec-websocket-version");
    final String key = headers.get("sec-websocket-key");
    final String[] protocols = parseProtocols(headers.get("sec-websocket-protocol"));
    // TODO: need lightweight approach (end)
    MessageConsumer newStream = null;
    if (upgrade == null) {
        final long newReplyId = supplyReplyId.applyAsLong(initialId);
        doHttpBegin(sender, routeId, newReplyId, 0L, 0L, 0, traceId, authorization, affinity, hs -> hs.item(h -> h.name(":status").value("400")).item(h -> h.name("connection").value("close")));
        doHttpEnd(sender, routeId, newReplyId, traceId);
        newStream = (t, b, o, l) -> {
        };
    } else if (key != null && WEBSOCKET_UPGRADE.equalsIgnoreCase(upgrade) && WEBSOCKET_VERSION_13.equals(version)) {
        WsBindingConfig binding = bindings.get(routeId);
        if (binding != null) {
            WsRouteConfig route = null;
            String protocol = null;
            for (int i = 0; i < (protocols != null ? protocols.length : 1); i++) {
                String newProtocol = protocols != null ? protocols[i] : null;
                WsRouteConfig newRoute = binding.resolve(authorization, newProtocol, scheme, authority, path);
                if (newRoute != null && (route == null || newRoute.order < route.order)) {
                    route = newRoute;
                    protocol = newProtocol;
                }
            }
            if (route != null) {
                newStream = new WsServer(sender, routeId, initialId, route.id, key, protocol, scheme, authority, path)::onNetMessage;
            }
        }
    }
    return newStream;
}
Also used : Encoder(java.util.Base64.Encoder) BindingHandler(io.aklivity.zilla.runtime.engine.binding.BindingHandler) MessageDigest(java.security.MessageDigest) MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) OctetsFW(io.aklivity.zilla.runtime.binding.ws.internal.types.OctetsFW) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) DataFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.DataFW) LinkedHashMap(java.util.LinkedHashMap) WsMaskUtil.xor(io.aklivity.zilla.runtime.binding.ws.internal.util.WsMaskUtil.xor) WsBinding(io.aklivity.zilla.runtime.binding.ws.internal.WsBinding) Long2ObjectHashMap(org.agrona.collections.Long2ObjectHashMap) WindowFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.WindowFW) HttpBeginExFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.HttpBeginExFW) FlushFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.FlushFW) Map(java.util.Map) HttpHeaderFW(io.aklivity.zilla.runtime.binding.ws.internal.types.HttpHeaderFW) LangUtil(org.agrona.LangUtil) Array32FW(io.aklivity.zilla.runtime.binding.ws.internal.types.Array32FW) EndFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.EndFW) STATUS_UNEXPECTED_CONDITION(io.aklivity.zilla.runtime.binding.ws.internal.types.codec.WsHeaderFW.STATUS_UNEXPECTED_CONDITION) Flyweight(io.aklivity.zilla.runtime.binding.ws.internal.types.Flyweight) STATUS_PROTOCOL_ERROR(io.aklivity.zilla.runtime.binding.ws.internal.types.codec.WsHeaderFW.STATUS_PROTOCOL_ERROR) WsRouteConfig(io.aklivity.zilla.runtime.binding.ws.internal.config.WsRouteConfig) BIG_ENDIAN(java.nio.ByteOrder.BIG_ENDIAN) EngineContext(io.aklivity.zilla.runtime.engine.EngineContext) WsDataExFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.WsDataExFW) WsEndExFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.WsEndExFW) UTF_8(java.nio.charset.StandardCharsets.UTF_8) WsBeginExFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.WsBeginExFW) ResetFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.ResetFW) ChallengeFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.ChallengeFW) STATUS_NORMAL_CLOSURE(io.aklivity.zilla.runtime.binding.ws.internal.types.codec.WsHeaderFW.STATUS_NORMAL_CLOSURE) AbortFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.AbortFW) BindingConfig(io.aklivity.zilla.runtime.engine.config.BindingConfig) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) Consumer(java.util.function.Consumer) ByteOrder.nativeOrder(java.nio.ByteOrder.nativeOrder) ByteOrder(java.nio.ByteOrder) Base64(java.util.Base64) LongUnaryOperator(java.util.function.LongUnaryOperator) BeginFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.BeginFW) MutableDirectBuffer(org.agrona.MutableDirectBuffer) WsConfiguration(io.aklivity.zilla.runtime.binding.ws.internal.WsConfiguration) WsBindingConfig(io.aklivity.zilla.runtime.binding.ws.internal.config.WsBindingConfig) WsHeaderFW(io.aklivity.zilla.runtime.binding.ws.internal.types.codec.WsHeaderFW) DirectBuffer(org.agrona.DirectBuffer) MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) OctetsFW(io.aklivity.zilla.runtime.binding.ws.internal.types.OctetsFW) BeginFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.BeginFW) WsBindingConfig(io.aklivity.zilla.runtime.binding.ws.internal.config.WsBindingConfig) LinkedHashMap(java.util.LinkedHashMap) HttpBeginExFW(io.aklivity.zilla.runtime.binding.ws.internal.types.stream.HttpBeginExFW) WsRouteConfig(io.aklivity.zilla.runtime.binding.ws.internal.config.WsRouteConfig)

Example 7 with MessageConsumer

use of io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer in project zilla by aklivity.

the class TlsServerFactory method newStream.

private MessageConsumer newStream(MessageConsumer sender, long routeId, long streamId, long sequence, long acknowledge, int maximum, long traceId, long authorization, long affinity, Consumer<OctetsFW.Builder> extension) {
    final BeginFW begin = beginRW.wrap(writeBuffer, 0, writeBuffer.capacity()).routeId(routeId).streamId(streamId).sequence(sequence).acknowledge(acknowledge).maximum(maximum).traceId(traceId).authorization(authorization).affinity(affinity).extension(extension).build();
    MessageConsumer receiver = streamFactory.newStream(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof(), sender);
    receiver.accept(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof());
    return receiver;
}
Also used : MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) BeginFW(io.aklivity.zilla.runtime.binding.tls.internal.types.stream.BeginFW)

Example 8 with MessageConsumer

use of io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer in project zilla by aklivity.

the class TlsClientFactory method newStream.

private MessageConsumer newStream(MessageConsumer sender, long routeId, long streamId, long sequence, long acknowledge, int maximum, long traceId, long authorization, long affinity, Consumer<OctetsFW.Builder> extension) {
    final BeginFW begin = beginRW.wrap(writeBuffer, 0, writeBuffer.capacity()).routeId(routeId).streamId(streamId).sequence(sequence).acknowledge(acknowledge).maximum(maximum).traceId(traceId).authorization(authorization).affinity(affinity).extension(extension).build();
    MessageConsumer receiver = streamFactory.newStream(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof(), sender);
    receiver.accept(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof());
    return receiver;
}
Also used : MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) BeginFW(io.aklivity.zilla.runtime.binding.tls.internal.types.stream.BeginFW)

Example 9 with MessageConsumer

use of io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer in project zilla by aklivity.

the class TcpServerFactory method newStream.

private MessageConsumer newStream(MessageConsumer sender, long routeId, long streamId, long sequence, long acknowledge, int maximum, long traceId, InetSocketAddress localAddress, InetSocketAddress remoteAddress) {
    BeginFW begin = beginRW.wrap(writeBuffer, 0, writeBuffer.capacity()).routeId(routeId).streamId(streamId).sequence(sequence).acknowledge(acknowledge).maximum(maximum).traceId(traceId).affinity(streamId).extension(b -> b.set(proxyBeginEx(remoteAddress, localAddress))).build();
    MessageConsumer receiver = streamFactory.newStream(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof(), sender);
    assert receiver != null;
    receiver.accept(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof());
    return receiver;
}
Also used : LongSupplier(java.util.function.LongSupplier) BindingHandler(io.aklivity.zilla.runtime.engine.binding.BindingHandler) MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) OP_WRITE(java.nio.channels.SelectionKey.OP_WRITE) TcpOptionsConfig(io.aklivity.zilla.runtime.binding.tcp.internal.config.TcpOptionsConfig) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) OctetsFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.OctetsFW) SocketChannel(java.nio.channels.SocketChannel) TcpRouteConfig(io.aklivity.zilla.runtime.binding.tcp.internal.config.TcpRouteConfig) Buffer(java.nio.Buffer) LangUtil(org.agrona.LangUtil) ResetFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.ResetFW) WindowFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.WindowFW) AbortFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.AbortFW) DataFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.DataFW) IpUtil.proxyAddress(io.aklivity.zilla.runtime.binding.tcp.internal.util.IpUtil.proxyAddress) CloseHelper(org.agrona.CloseHelper) StandardSocketOptions(java.net.StandardSocketOptions) EngineContext(io.aklivity.zilla.runtime.engine.EngineContext) SO_KEEPALIVE(java.net.StandardSocketOptions.SO_KEEPALIVE) TcpBindingConfig(io.aklivity.zilla.runtime.binding.tcp.internal.config.TcpBindingConfig) EndFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.EndFW) BufferPool(io.aklivity.zilla.runtime.engine.buffer.BufferPool) LongFunction(java.util.function.LongFunction) TcpServerBindingConfig(io.aklivity.zilla.runtime.binding.tcp.internal.config.TcpServerBindingConfig) BeginFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.BeginFW) PollerKey(io.aklivity.zilla.runtime.engine.poller.PollerKey) IOException(java.io.IOException) ProxyBeginExFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.ProxyBeginExFW) InetSocketAddress(java.net.InetSocketAddress) BindingConfig(io.aklivity.zilla.runtime.engine.config.BindingConfig) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteOrder.nativeOrder(java.nio.ByteOrder.nativeOrder) SelectableChannel(java.nio.channels.SelectableChannel) LongUnaryOperator(java.util.function.LongUnaryOperator) NO_SLOT(io.aklivity.zilla.runtime.engine.buffer.BufferPool.NO_SLOT) TcpConfiguration(io.aklivity.zilla.runtime.binding.tcp.internal.TcpConfiguration) OP_READ(java.nio.channels.SelectionKey.OP_READ) MutableDirectBuffer(org.agrona.MutableDirectBuffer) Flyweight(io.aklivity.zilla.runtime.binding.tcp.internal.types.Flyweight) TCP_NODELAY(java.net.StandardSocketOptions.TCP_NODELAY) WRITE_SPIN_COUNT(io.aklivity.zilla.runtime.binding.tcp.internal.TcpBinding.WRITE_SPIN_COUNT) DirectBuffer(org.agrona.DirectBuffer) MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) BeginFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.BeginFW)

Example 10 with MessageConsumer

use of io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer in project zilla by aklivity.

the class TcpClientFactory method newStream.

@Override
public MessageConsumer newStream(int msgTypeId, DirectBuffer buffer, int index, int length, MessageConsumer application) {
    final BeginFW begin = beginRO.wrap(buffer, index, index + length);
    final long routeId = begin.routeId();
    final long authorization = begin.authorization();
    final OctetsFW extension = begin.extension();
    final ProxyBeginExFW beginEx = extension.get(beginExRO::tryWrap);
    InetSocketAddress route = null;
    TcpBindingConfig binding = router.lookup(routeId);
    if (binding != null) {
        route = router.resolve(binding, authorization, beginEx);
    }
    MessageConsumer newStream = null;
    if (route != null) {
        final long initialId = begin.streamId();
        final SocketChannel channel = newSocketChannel();
        final TcpClient client = new TcpClient(application, routeId, initialId, channel);
        client.doNetConnect(route, binding.options);
        newStream = client::onAppMessage;
    }
    return newStream;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) InetSocketAddress(java.net.InetSocketAddress) TcpBindingConfig(io.aklivity.zilla.runtime.binding.tcp.internal.config.TcpBindingConfig) OctetsFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.OctetsFW) BeginFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.BeginFW) ProxyBeginExFW(io.aklivity.zilla.runtime.binding.tcp.internal.types.stream.ProxyBeginExFW)

Aggregations

MessageConsumer (io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer)77 BeginFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.BeginFW)31 OctetsFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW)17 ExtensionFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.ExtensionFW)17 KafkaBeginExFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaBeginExFW)17 KafkaBindingConfig (io.aklivity.zilla.runtime.binding.kafka.internal.config.KafkaBindingConfig)14 String16FW (io.aklivity.zilla.runtime.binding.kafka.internal.types.String16FW)14 BindingHandler (io.aklivity.zilla.runtime.engine.binding.BindingHandler)13 KafkaRouteConfig (io.aklivity.zilla.runtime.binding.kafka.internal.config.KafkaRouteConfig)11 BindingConfig (io.aklivity.zilla.runtime.engine.config.BindingConfig)10 MutableDirectBuffer (org.agrona.MutableDirectBuffer)10 EngineContext (io.aklivity.zilla.runtime.engine.EngineContext)9 LongUnaryOperator (java.util.function.LongUnaryOperator)9 DirectBuffer (org.agrona.DirectBuffer)9 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)9 KafkaCache (io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCache)6 KafkaCacheTopic (io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCacheTopic)6 KafkaCachePartition (io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCachePartition)4 KafkaOffsetFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.KafkaOffsetFW)4 BeginFW (io.aklivity.zilla.runtime.binding.tls.internal.types.stream.BeginFW)4