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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations