Skip to content

Commit

Permalink
Merge pull request #39288 from mkouba/issue-39153
Browse files Browse the repository at this point in the history
WebSockets Next: ping/pong and callbacks API refactoring
  • Loading branch information
mkouba committed Mar 8, 2024
2 parents 443cc92 + d11d802 commit 09ff3b6
Show file tree
Hide file tree
Showing 52 changed files with 828 additions and 383 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import org.jboss.jandex.DotName;

import io.quarkus.websockets.next.BinaryMessage;
import io.quarkus.websockets.next.OnBinaryMessage;
import io.quarkus.websockets.next.OnClose;
import io.quarkus.websockets.next.OnMessage;
import io.quarkus.websockets.next.OnOpen;
import io.quarkus.websockets.next.TextMessage;
import io.quarkus.websockets.next.OnPongMessage;
import io.quarkus.websockets.next.OnTextMessage;
import io.quarkus.websockets.next.WebSocket;
import io.quarkus.websockets.next.WebSocketConnection;
import io.smallrye.common.annotation.Blocking;
Expand All @@ -22,7 +22,9 @@ final class WebSocketDotNames {
static final DotName WEB_SOCKET = DotName.createSimple(WebSocket.class);
static final DotName WEB_SOCKET_CONNECTION = DotName.createSimple(WebSocketConnection.class);
static final DotName ON_OPEN = DotName.createSimple(OnOpen.class);
static final DotName ON_MESSAGE = DotName.createSimple(OnMessage.class);
static final DotName ON_TEXT_MESSAGE = DotName.createSimple(OnTextMessage.class);
static final DotName ON_BINARY_MESSAGE = DotName.createSimple(OnBinaryMessage.class);
static final DotName ON_PONG_MESSAGE = DotName.createSimple(OnPongMessage.class);
static final DotName ON_CLOSE = DotName.createSimple(OnClose.class);
static final DotName UNI = DotName.createSimple(Uni.class);
static final DotName MULTI = DotName.createSimple(Multi.class);
Expand All @@ -33,6 +35,4 @@ final class WebSocketDotNames {
static final DotName JSON_OBJECT = DotName.createSimple(JsonObject.class);
static final DotName JSON_ARRAY = DotName.createSimple(JsonArray.class);
static final DotName VOID = DotName.createSimple(Void.class);
static final DotName BINARY_MESSAGE = DotName.createSimple(BinaryMessage.class);
static final DotName TEXT_MESSAGE = DotName.createSimple(TextMessage.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.PrimitiveType;
import org.jboss.jandex.Type;
import org.jboss.jandex.Type.Kind;

import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.websockets.next.WebSocket;
import io.quarkus.websockets.next.runtime.WebSocketEndpoint;
import io.quarkus.websockets.next.runtime.WebSocketEndpoint.ExecutionModel;
import io.quarkus.websockets.next.runtime.WebSocketEndpoint.MessageType;

/**
* This build item represents a WebSocket endpoint class.
Expand All @@ -24,16 +21,20 @@ public final class WebSocketEndpointBuildItem extends MultiBuildItem {
public final String path;
public final WebSocket.ExecutionMode executionMode;
public final Callback onOpen;
public final Callback onMessage;
public final Callback onTextMessage;
public final Callback onBinaryMessage;
public final Callback onPongMessage;
public final Callback onClose;

public WebSocketEndpointBuildItem(BeanInfo bean, String path, WebSocket.ExecutionMode executionMode, Callback onOpen,
Callback onMessage, Callback onClose) {
Callback onTextMessage, Callback onBinaryMessage, Callback onPongMessage, Callback onClose) {
this.bean = bean;
this.path = path;
this.executionMode = executionMode;
this.onOpen = onOpen;
this.onMessage = onMessage;
this.onTextMessage = onTextMessage;
this.onBinaryMessage = onBinaryMessage;
this.onPongMessage = onPongMessage;
this.onClose = onClose;
}

Expand All @@ -42,15 +43,21 @@ public static class Callback {
public final AnnotationInstance annotation;
public final MethodInfo method;
public final ExecutionModel executionModel;
public final MessageType consumedMessageType;
public final MessageType producedMessageType;
public final MessageType messageType;

public Callback(AnnotationInstance annotation, MethodInfo method, ExecutionModel executionModel) {
this.method = method;
this.annotation = annotation;
this.executionModel = executionModel;
this.consumedMessageType = initMessageType(method.parameters().isEmpty() ? null : method.parameterType(0));
this.producedMessageType = initMessageType(method.returnType());
if (WebSocketDotNames.ON_BINARY_MESSAGE.equals(annotation.name())) {
this.messageType = MessageType.BINARY;
} else if (WebSocketDotNames.ON_TEXT_MESSAGE.equals(annotation.name())) {
this.messageType = MessageType.TEXT;
} else if (WebSocketDotNames.ON_PONG_MESSAGE.equals(annotation.name())) {
this.messageType = MessageType.PONG;
} else {
this.messageType = MessageType.NONE;
}
}

public Type returnType() {
Expand All @@ -74,23 +81,19 @@ public boolean isReturnTypeMulti() {
}

public boolean acceptsMessage() {
return consumedMessageType != MessageType.NONE;
return messageType != MessageType.NONE;
}

public boolean acceptsBinaryMessage() {
return consumedMessageType == MessageType.BINARY;
return messageType == MessageType.BINARY || messageType == MessageType.PONG;
}

public boolean acceptsMulti() {
return acceptsMessage() && method.parameterType(0).name().equals(WebSocketDotNames.MULTI);
}

public WebSocketEndpoint.MessageType consumedMessageType() {
return consumedMessageType;
}

public WebSocketEndpoint.MessageType producedMessageType() {
return producedMessageType;
public MessageType messageType() {
return messageType;
}

public boolean broadcast() {
Expand All @@ -99,7 +102,7 @@ public boolean broadcast() {
}

public DotName getInputCodec() {
return getCodec("inputCodec");
return getCodec("codec");
}

public DotName getOutputCodec() {
Expand All @@ -108,44 +111,18 @@ public DotName getOutputCodec() {
}

private DotName getCodec(String valueName) {
AnnotationInstance messageAnnotation = method.declaredAnnotation(WebSocketDotNames.BINARY_MESSAGE);
if (messageAnnotation == null) {
messageAnnotation = method.declaredAnnotation(WebSocketDotNames.TEXT_MESSAGE);
}
if (messageAnnotation != null) {
AnnotationValue codecValue = messageAnnotation.value(valueName);
if (codecValue != null) {
return codecValue.asClass().name();
}
AnnotationValue codecValue = annotation.value(valueName);
if (codecValue != null) {
return codecValue.asClass().name();
}
return null;
}

MessageType initMessageType(Type messageType) {
MessageType ret = MessageType.NONE;
if (messageType != null && !messageType.name().equals(WebSocketDotNames.VOID)) {
if (method.hasDeclaredAnnotation(WebSocketDotNames.BINARY_MESSAGE)) {
ret = MessageType.BINARY;
} else if (method.hasDeclaredAnnotation(WebSocketDotNames.TEXT_MESSAGE)) {
ret = MessageType.TEXT;
} else {
if (isByteArray(messageType) || WebSocketDotNames.BUFFER.equals(messageType.name())) {
ret = MessageType.BINARY;
} else {
ret = MessageType.TEXT;
}
}
}
return ret;
}

static boolean isByteArray(Type type) {
return type.kind() == Kind.ARRAY && PrimitiveType.BYTE.equals(type.asArrayType().constituent());
}

static boolean isUniVoid(Type type) {
return WebSocketDotNames.UNI.equals(type.name())
&& type.asParameterizedType().arguments().get(0).name().equals(WebSocketDotNames.VOID);
enum MessageType {
NONE,
PONG,
TEXT,
BINARY
}

}
Expand Down

0 comments on commit 09ff3b6

Please sign in to comment.