From c7fcb6e694bc05d7c81647bdcb4751da0312421e Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 5 Jul 2012 14:02:21 -0700 Subject: [PATCH] Cleaning up Annotations + Removing @OnWebSocketText + Removing @OnWebSocketBinary + Starting to rework EventMethods + WebSocketEventDriver for new @OnWebSocketMessage annotation + Updating various example/demo/test sockets for this new reality. --- .../annotations/OnWebSocketBinary.java | 31 --- .../annotations/OnWebSocketMessage.java | 17 +- .../annotations/OnWebSocketText.java | 28 --- .../jetty/websocket/api/EventMethod.java | 12 - .../jetty/websocket/api/EventMethods.java | 37 +-- .../websocket/api/EventMethodsCache.java | 213 +++++++++--------- .../eclipse/jetty/websocket/api/Frame.java | 25 ++ .../api/WebSocketBlockingConnection.java | 100 ++++++++ .../websocket/api/WebSocketEventDriver.java | 60 +++-- .../{io => api}/WebSocketOutputStream.java | 2 +- .../{io => api}/WebSocketWriter.java | 2 +- .../jetty/websocket/io/RawConnection.java | 20 ++ .../io/WebSocketAsyncConnection.java | 15 +- .../io/WebSocketBlockingConnection.java | 48 ---- .../annotations/BadBinarySignatureSocket.java | 9 +- .../annotations/BadDuplicateBinarySocket.java | 11 +- .../annotations/BadTextSignatureSocket.java | 5 +- .../websocket/annotations/MyEchoSocket.java | 4 +- .../annotations/MyStatelessEchoSocket.java | 2 +- .../websocket/api/EventMethodsCacheTest.java | 75 +++--- .../api/WebSocketEventDriverTest.java | 8 +- .../samples/AnnotatedBinaryArraySocket.java | 33 +++ .../samples/AnnotatedBinaryStreamSocket.java | 35 +++ ...aySocket.java => AnnotatedTextSocket.java} | 6 +- ...et.java => AnnotatedTextStreamSocket.java} | 17 +- 25 files changed, 451 insertions(+), 364 deletions(-) delete mode 100644 jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketBinary.java delete mode 100644 jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketText.java create mode 100644 jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/Frame.java create mode 100644 jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketBlockingConnection.java rename jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/{io => api}/WebSocketOutputStream.java (85%) rename jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/{io => api}/WebSocketWriter.java (92%) create mode 100644 jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/RawConnection.java delete mode 100644 jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketBlockingConnection.java create mode 100644 jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBinaryArraySocket.java create mode 100644 jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBinaryStreamSocket.java rename jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/{AnnotatedByteArraySocket.java => AnnotatedTextSocket.java} (87%) rename jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/{AnnotatedByteBufferSocket.java => AnnotatedTextStreamSocket.java} (74%) diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketBinary.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketBinary.java deleted file mode 100644 index fdc795af018..00000000000 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketBinary.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.eclipse.jetty.websocket.annotations; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -import java.nio.ByteBuffer; - -import org.eclipse.jetty.websocket.api.WebSocketConnection; - -/** - * Annotation for tagging methods to receive Binary message events. - *

- * Acceptable method patterns.
- * Note: methodName can be any name you want to use. - *

    - *
  1. public void methodName(byte payload[], int offset, int length)
  2. - *
  3. public void methodName({@link ByteBuffer} payload)
  4. - *
  5. public void methodName({@link WebSocketConnection} conn, byte payload[], int offset, int length)
  6. - *
  7. public void methodName({@link WebSocketConnection} conn, {@link ByteBuffer} payload)
  8. - *
- */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target(value = -{ ElementType.METHOD }) -public @interface OnWebSocketBinary -{ - /* no config */ -} diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketMessage.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketMessage.java index a63b4e9504b..e874ede4e58 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketMessage.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketMessage.java @@ -1,5 +1,6 @@ package org.eclipse.jetty.websocket.annotations; +import java.io.Reader; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -9,13 +10,27 @@ import java.lang.annotation.Target; import org.eclipse.jetty.websocket.api.WebSocketConnection; /** - * Annotation for tagging methods to receive Text message events. + * Annotation for tagging methods to receive Binary or Text Message events. *

* Acceptable method patterns.
* Note: methodName can be any name you want to use. + *

+ * Text Message Versions *

    *
  1. public void methodName(String text)
  2. *
  3. public void methodName({@link WebSocketConnection} conn, String text)
  4. + *
  5. public void methodName(Reader reader)
  6. + *
  7. public void methodName({@link WebSocketConnection} conn, Reader reader)
  8. + *
+ * Note: that the {@link Reader} in this case will always use UTF-8 encoding/charset (this is dictated by the RFC 6455 spec for Text Messages. If you need to + * use a non-UTF-8 encoding/charset, you are instructed to use the binary messaging techniques. + *

+ * Binary Message Versions + *

    + *
  1. public void methodName(byte buf[], int offset, int length)
  2. + *
  3. public void methodName({@link WebSocketConnection} conn, byte buf[], int offset, int length)
  4. + *
  5. public void methodName(InputStream stream)
  6. + *
  7. public void methodName({@link WebSocketConnection} conn, InputStream stream)
  8. *
*/ @Documented diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketText.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketText.java deleted file mode 100644 index bebeaa3171e..00000000000 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/annotations/OnWebSocketText.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.eclipse.jetty.websocket.annotations; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.eclipse.jetty.websocket.api.WebSocketConnection; - -/** - * Annotation for tagging methods to receive Text message events. - *

- * Acceptable method patterns.
- * Note: methodName can be any name you want to use. - *

    - *
  1. public void methodName(String text)
  2. - *
  3. public void methodName({@link WebSocketConnection} conn, String text)
  4. - *
- */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target(value = -{ ElementType.METHOD }) -public @interface OnWebSocketText -{ - /* no config */ -} diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethod.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethod.java index 2a1b1777ab1..84177f07197 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethod.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethod.java @@ -114,16 +114,4 @@ public class EventMethod { return this.paramTypes; } - - public boolean isParameterPresent(Class type) - { - for (Class param : paramTypes) - { - if (param.equals(type)) - { - return true; - } - } - return false; - } } \ No newline at end of file diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethods.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethods.java index 145d8278197..416b5d7b0b5 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethods.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethods.java @@ -1,9 +1,5 @@ package org.eclipse.jetty.websocket.api; -import java.util.HashMap; -import java.util.Map; - -import org.eclipse.jetty.websocket.frames.BaseFrame; /** * A representation of the methods available to call for a particular class. @@ -17,11 +13,11 @@ public class EventMethods public EventMethod onConnect = null; public EventMethod onClose = null; public EventMethod onBinary = null; + public EventMethod onBinaryStream = null; public EventMethod onText = null; + public EventMethod onTextStream = null; public EventMethod onException = null; - - // special case, multiple methods allowed - private Map, EventMethod> onFrames = new HashMap, EventMethod>(); + public EventMethod onFrame = null; public EventMethods(Class pojoClass, boolean annotated) { @@ -29,19 +25,6 @@ public class EventMethods this.isAnnotated = annotated; } - public void addOnFrame(EventMethod eventMethod) - { - Class paramTypes[] = eventMethod.getParamTypes(); - Class lastType = paramTypes[paramTypes.length - 1]; - if (!BaseFrame.class.isAssignableFrom(lastType)) - { - throw new InvalidWebSocketException("Unrecognized @OnWebSocketFrame frame type " + lastType); - } - @SuppressWarnings("unchecked") - Class frameType = (Class)lastType; - onFrames.put(frameType,eventMethod); - } - @Override public boolean equals(Object obj) { @@ -72,16 +55,6 @@ public class EventMethods return true; } - public EventMethod getOnFrame(Class frameType) - { - return onFrames.get(frameType); - } - - public Map, EventMethod> getOnFrames() - { - return onFrames; - } - public Class getPojoClass() { return pojoClass; @@ -119,8 +92,8 @@ public class EventMethods builder.append(onText); builder.append(", onException="); builder.append(onException); - builder.append(", onFrames="); - builder.append(onFrames); + builder.append(", onFrame="); + builder.append(onFrame); builder.append("]"); return builder.toString(); } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethodsCache.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethodsCache.java index 211e53f7ebf..d6d147eb0d2 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethodsCache.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethodsCache.java @@ -1,30 +1,70 @@ package org.eclipse.jetty.websocket.api; +import java.io.InputStream; +import java.io.Reader; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.concurrent.ConcurrentHashMap; import org.eclipse.jetty.util.StringUtil; -import org.eclipse.jetty.websocket.annotations.OnWebSocketBinary; import org.eclipse.jetty.websocket.annotations.OnWebSocketClose; import org.eclipse.jetty.websocket.annotations.OnWebSocketConnect; import org.eclipse.jetty.websocket.annotations.OnWebSocketFrame; -import org.eclipse.jetty.websocket.annotations.OnWebSocketText; +import org.eclipse.jetty.websocket.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.annotations.WebSocket; -import org.eclipse.jetty.websocket.frames.BaseFrame; -import org.eclipse.jetty.websocket.frames.BinaryFrame; -import org.eclipse.jetty.websocket.frames.CloseFrame; -import org.eclipse.jetty.websocket.frames.ControlFrame; -import org.eclipse.jetty.websocket.frames.DataFrame; -import org.eclipse.jetty.websocket.frames.PingFrame; -import org.eclipse.jetty.websocket.frames.PongFrame; -import org.eclipse.jetty.websocket.frames.TextFrame; public class EventMethodsCache { + @SuppressWarnings("serial") + public static class InvalidSignatureException extends InvalidWebSocketException + { + public static InvalidSignatureException build(Method method, Class annoClass, ParamList... paramlists) + { + // Build big detailed exception to help the developer + StringBuilder err = new StringBuilder(); + err.append("Invalid declaration of "); + err.append(method); + err.append(StringUtil.__LINE_SEPARATOR); + + err.append("Acceptable method declarations for @"); + err.append(annoClass.getSimpleName()); + err.append(" are:"); + for (ParamList validParams : paramlists) + { + for (Class[] params : validParams) + { + err.append(StringUtil.__LINE_SEPARATOR); + err.append("public void ").append(method.getName()); + err.append('('); + boolean delim = false; + for (Class type : params) + { + if (delim) + { + err.append(','); + } + err.append(' '); + err.append(type.getName()); + if (type.isArray()) + { + err.append("[]"); + } + delim = true; + } + err.append(')'); + } + } + return new InvalidSignatureException(err.toString()); + } + + public InvalidSignatureException(String message) + { + super(message); + } + } + @SuppressWarnings("serial") private static class ParamList extends ArrayList[]> { @@ -33,27 +73,30 @@ public class EventMethodsCache this.add(paramTypes); } } - /** - * Parameter list for @OnWebSocketBinary + * Parameter list for @OnWebSocketMessage (Binary mode) */ private static final ParamList validBinaryParams; /** * Parameter list for @OnWebSocketConnect */ private static final ParamList validConnectParams; + /** + * Parameter list for @OnWebSocketClose + */ private static final ParamList validCloseParams; + /** + * Parameter list for @OnWebSocketFrame + */ private static final ParamList validFrameParams; + + /** + * Parameter list for @OnWebSocketMessage (Text mode) + */ private static final ParamList validTextParams; static { - validBinaryParams = new ParamList(); - validBinaryParams.addParams(ByteBuffer.class); - validBinaryParams.addParams(byte[].class,int.class,int.class); - validBinaryParams.addParams(WebSocketConnection.class,ByteBuffer.class); - validBinaryParams.addParams(WebSocketConnection.class,byte[].class,int.class,int.class); - validConnectParams = new ParamList(); validConnectParams.addParams(WebSocketConnection.class); @@ -64,25 +107,18 @@ public class EventMethodsCache validTextParams = new ParamList(); validTextParams.addParams(String.class); validTextParams.addParams(WebSocketConnection.class,String.class); + validTextParams.addParams(Reader.class); + validTextParams.addParams(WebSocketConnection.class,Reader.class); + + validBinaryParams = new ParamList(); + validBinaryParams.addParams(byte[].class,int.class,int.class); + validBinaryParams.addParams(WebSocketConnection.class,byte[].class,int.class,int.class); + validBinaryParams.addParams(InputStream.class); + validBinaryParams.addParams(WebSocketConnection.class,InputStream.class); validFrameParams = new ParamList(); - validFrameParams.addParams(BaseFrame.class); - validFrameParams.addParams(BinaryFrame.class); - validFrameParams.addParams(CloseFrame.class); - validFrameParams.addParams(ControlFrame.class); - validFrameParams.addParams(DataFrame.class); - validFrameParams.addParams(PingFrame.class); - validFrameParams.addParams(PongFrame.class); - validFrameParams.addParams(TextFrame.class); - - validFrameParams.addParams(WebSocketConnection.class,BaseFrame.class); - validFrameParams.addParams(WebSocketConnection.class,BinaryFrame.class); - validFrameParams.addParams(WebSocketConnection.class,CloseFrame.class); - validFrameParams.addParams(WebSocketConnection.class,ControlFrame.class); - validFrameParams.addParams(WebSocketConnection.class,DataFrame.class); - validFrameParams.addParams(WebSocketConnection.class,PingFrame.class); - validFrameParams.addParams(WebSocketConnection.class,PongFrame.class); - validFrameParams.addParams(WebSocketConnection.class,TextFrame.class); + validFrameParams.addParams(Frame.class); + validFrameParams.addParams(WebSocketConnection.class,Frame.class); } private ConcurrentHashMap, EventMethods> cache; @@ -92,33 +128,6 @@ public class EventMethodsCache cache = new ConcurrentHashMap<>(); } - private void assertFrameUnset(EventMethods events, Method method) - { - Class paramTypes[] = method.getParameterTypes(); - Class lastType = paramTypes[paramTypes.length - 1]; - if (!BaseFrame.class.isAssignableFrom(lastType)) - { - throw new InvalidWebSocketException("Unrecognized @OnWebSocketFrame frame type " + lastType); - } - @SuppressWarnings("unchecked") - Class frameType = (Class)lastType; - - EventMethod dup = events.getOnFrame(frameType); - if (dup != null) - { - // Attempt to add duplicate frame type (a no-no) - StringBuilder err = new StringBuilder(); - err.append("Duplicate Frame Type declaration on "); - err.append(method); - err.append(StringUtil.__LINE_SEPARATOR); - - err.append("Type ").append(frameType.getSimpleName()).append(" previously declared at "); - err.append(dup.getMethod()); - - throw new InvalidWebSocketException(err.toString()); - } - } - private void assertIsPublicNonStatic(Method method) { int mods = method.getModifiers(); @@ -199,39 +208,7 @@ public class EventMethodsCache if (!valid) { - // Build big detailed exception to help the developer - StringBuilder err = new StringBuilder(); - err.append("Invalid declaration of "); - err.append(method); - err.append(StringUtil.__LINE_SEPARATOR); - - err.append("Acceptable method declarations for @"); - err.append(annoClass.getSimpleName()); - err.append(" are:"); - for (Class[] params : validParams) - { - err.append(StringUtil.__LINE_SEPARATOR); - err.append("public void ").append(method.getName()); - err.append('('); - boolean delim = false; - for (Class type : params) - { - if (delim) - { - err.append(','); - } - err.append(' '); - err.append(type.getName()); - if (type.isArray()) - { - err.append("[]"); - } - delim = true; - } - err.append(')'); - } - - throw new InvalidWebSocketException(err.toString()); + throw InvalidSignatureException.build(method,annoClass,validParams); } } @@ -297,6 +274,12 @@ public class EventMethodsCache return true; } + private boolean isSignatureMatch(Method method, ParamList validbinaryparams2) + { + // TODO Auto-generated method stub + return false; + } + /** * Register a pojo with the cache. * @@ -328,12 +311,28 @@ public class EventMethodsCache continue; } - if (method.getAnnotation(OnWebSocketBinary.class) != null) + if (method.getAnnotation(OnWebSocketMessage.class) != null) { - assertValidSignature(method,OnWebSocketBinary.class,validBinaryParams); - assertUnset(events.onBinary,OnWebSocketBinary.class,method); - events.onBinary = new EventMethod(pojo,method); - continue; + if (isSignatureMatch(method,validTextParams)) + { + // Text mode + // TODO + + assertUnset(events.onText,OnWebSocketMessage.class,method); + events.onText = new EventMethod(pojo,method); + continue; + } + + if (isSignatureMatch(method,validBinaryParams)) + { + // Binary Mode + // TODO + assertUnset(events.onBinary,OnWebSocketMessage.class,method); + events.onBinary = new EventMethod(pojo,method); + continue; + } + + throw InvalidSignatureException.build(method,OnWebSocketMessage.class,validTextParams,validBinaryParams); } if (method.getAnnotation(OnWebSocketClose.class) != null) @@ -344,19 +343,11 @@ public class EventMethodsCache continue; } - if (method.getAnnotation(OnWebSocketText.class) != null) - { - assertValidSignature(method,OnWebSocketText.class,validTextParams); - assertUnset(events.onText,OnWebSocketText.class,method); - events.onText = new EventMethod(pojo,method); - continue; - } - if (method.getAnnotation(OnWebSocketFrame.class) != null) { assertValidSignature(method,OnWebSocketFrame.class,validFrameParams); - assertFrameUnset(events,method); - events.addOnFrame(new EventMethod(pojo,method)); + assertUnset(events.onFrame,OnWebSocketFrame.class,method); + events.onFrame = new EventMethod(pojo,method); continue; } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/Frame.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/Frame.java new file mode 100644 index 00000000000..b5d2cf34c6a --- /dev/null +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/Frame.java @@ -0,0 +1,25 @@ +package org.eclipse.jetty.websocket.api; + +/** + * The immutable frame details. + */ +public interface Frame +{ + public byte[] getMask(); + + public int getOpCode(); + + public byte[] getPayload(); + + public long getPayloadLength(); + + public boolean isFin(); + + public boolean isMasked(); + + public boolean isRsv1(); + + public boolean isRsv2(); + + public boolean isRsv3(); +} diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketBlockingConnection.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketBlockingConnection.java new file mode 100644 index 00000000000..b1b9713897c --- /dev/null +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketBlockingConnection.java @@ -0,0 +1,100 @@ +package org.eclipse.jetty.websocket.api; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.concurrent.ExecutionException; + +import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.util.FutureCallback; +import org.eclipse.jetty.websocket.frames.BinaryFrame; +import org.eclipse.jetty.websocket.frames.TextFrame; +import org.eclipse.jetty.websocket.generator.Generator; +import org.eclipse.jetty.websocket.io.RawConnection; + +/** + * For working with the {@link WebSocketConnection} in a blocking technique. + *

+ * This is an end-user accessible class. + */ +public class WebSocketBlockingConnection +{ + private final RawConnection conn; + private final ByteBufferPool bufferPool; + private final WebSocketPolicy policy; + private final Generator generator; + + public WebSocketBlockingConnection(WebSocketConnection conn) + { + if (conn instanceof RawConnection) + { + this.conn = (RawConnection)conn; + } + else + { + throw new IllegalArgumentException("Unsupported implementation of WebSocketConnection"); + } + this.bufferPool = this.conn.getBufferPool(); + this.policy = conn.getPolicy(); + this.generator = new Generator(this.policy); + } + + /** + * Send a binary message. + *

+ * Basic usage, results in a blocking write. + */ + public void write(byte[] data, int offset, int length) throws IOException + { + BinaryFrame frame = new BinaryFrame(data,offset,length); + ByteBuffer buf = bufferPool.acquire(policy.getBufferSize(),false); + try + { + generator.generate(buf,frame); + FutureCallback blocking = new FutureCallback<>(); + this.conn.writeRaw(null,blocking,buf); + blocking.get(); // block till finished + } + catch (InterruptedException e) + { + throw new IOException("Blocking write failed",e); + } + catch (ExecutionException e) + { + FutureCallback.rethrow(e); + } + finally + { + bufferPool.release(buf); + } + } + + /** + * Send text message. + *

+ * Basic usage, results in a blocking write. + */ + public void write(String message) throws IOException + { + TextFrame frame = new TextFrame(message); + ByteBuffer buf = bufferPool.acquire(policy.getBufferSize(),false); + try + { + generator.generate(buf,frame); + FutureCallback blocking = new FutureCallback<>(); + this.conn.writeRaw(null,blocking,buf); + blocking.get(); // block till finished + } + catch (InterruptedException e) + { + throw new IOException("Blocking write failed",e); + } + catch (ExecutionException e) + { + FutureCallback.rethrow(e); + } + finally + { + bufferPool.release(buf); + } + } +} diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketEventDriver.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketEventDriver.java index f4a614cad72..476838900a7 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketEventDriver.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketEventDriver.java @@ -1,7 +1,6 @@ package org.eclipse.jetty.websocket.api; import java.io.IOException; -import java.nio.ByteBuffer; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; @@ -94,6 +93,13 @@ public class WebSocketEventDriver implements Parser.Listener LOG.debug("{}.onFrame({})",websocket.getClass().getSimpleName(),frame); } + // Generic Read-Only Frame version + if ((frame instanceof Frame) && (events.onFrame != null)) + { + events.onFrame.call(websocket,connection,frame); + // DO NOT return; - as this is just a read-only notification. + } + // Specified Close Case if ((frame instanceof CloseFrame) && (events.onClose != null)) { @@ -105,51 +111,41 @@ public class WebSocketEventDriver implements Parser.Listener try { // Specified Text Case - if ((frame instanceof TextFrame) && (events.onText != null)) + if (frame instanceof TextFrame) { - TextFrame text = (TextFrame)frame; - events.onText.call(websocket,connection,text.getPayloadUTF8()); + if (events.onText != null) + { + TextFrame text = (TextFrame)frame; + events.onText.call(websocket,connection,text.getPayloadUTF8()); + return; + } + + // TODO + // if (events.onTextStream != null) + // { + // } + return; } // Specified Binary Case - if ((frame instanceof BinaryFrame) && (events.onBinary != null)) + if (frame instanceof BinaryFrame) { - BinaryFrame bin = (BinaryFrame)frame; - - if (events.onBinary.isParameterPresent(ByteBuffer.class)) - { - // Byte buffer approach - events.onBinary.call(websocket,connection,bin.getPayload()); - } - else + if (events.onBinary != null) { + BinaryFrame bin = (BinaryFrame)frame; // Byte array approach byte buf[] = BufferUtil.toArray(bin.getPayload()); events.onBinary.call(websocket,connection,buf,0,buf.length); } + // TODO + // if (events.onBinaryStream != null) + // { + // } + return; } - - // Basic Hierarchy Case - Class frameType = frame.getClass(); - while (true) - { - EventMethod event = events.getOnFrame(frameType); - if (event != null) - { - event.call(websocket,connection,frame); - return; - } - - if (!BaseFrame.class.isAssignableFrom(frameType.getSuperclass())) - { - // not assignable - return; - } - frameType = (Class)frameType.getSuperclass(); - } } catch (Throwable t) { diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketOutputStream.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketOutputStream.java similarity index 85% rename from jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketOutputStream.java rename to jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketOutputStream.java index 1c34676334f..13c079cbe7f 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketOutputStream.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketOutputStream.java @@ -1,4 +1,4 @@ -package org.eclipse.jetty.websocket.io; +package org.eclipse.jetty.websocket.api; import java.io.IOException; import java.io.OutputStream; diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketWriter.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketWriter.java similarity index 92% rename from jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketWriter.java rename to jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketWriter.java index 3e44381fb4d..ef79ca6505e 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketWriter.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketWriter.java @@ -1,4 +1,4 @@ -package org.eclipse.jetty.websocket.io; +package org.eclipse.jetty.websocket.api; import java.io.IOException; import java.io.Writer; diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/RawConnection.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/RawConnection.java new file mode 100644 index 00000000000..801f3c3abe3 --- /dev/null +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/RawConnection.java @@ -0,0 +1,20 @@ +package org.eclipse.jetty.websocket.io; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.concurrent.Executor; + +import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.util.Callback; + +/** + * For advanced usage of connections. + */ +public interface RawConnection +{ + ByteBufferPool getBufferPool(); + + Executor getExecutor(); + + void writeRaw(C context, Callback callback, ByteBuffer... buf) throws IOException; +} diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketAsyncConnection.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketAsyncConnection.java index 315f687cfb2..3e157054e0d 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketAsyncConnection.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketAsyncConnection.java @@ -31,7 +31,7 @@ import org.eclipse.jetty.websocket.parser.Parser; /** * Provides the implementation of {@link WebSocketConnection} within the framework of the new {@link AsyncConnection} framework of jetty-io */ -public class WebSocketAsyncConnection extends AbstractAsyncConnection implements WebSocketConnection +public class WebSocketAsyncConnection extends AbstractAsyncConnection implements RawConnection, WebSocketConnection { private static final Logger LOG = Log.getLogger(WebSocketAsyncConnection.class); private static final ThreadLocal CURRENT_CONNECTION = new ThreadLocal(); @@ -90,11 +90,18 @@ public class WebSocketAsyncConnection extends AbstractAsyncConnection implements } } + @Override public ByteBufferPool getBufferPool() { return bufferPool; } + @Override + public Executor getExecutor() + { + return getExecutor(); + } + /** * Get the list of extensions in use. *

@@ -281,4 +288,10 @@ public class WebSocketAsyncConnection extends AbstractAsyncConnection implements } // TODO write(context,callback,frames); } + + @Override + public void writeRaw(C context, Callback callback, ByteBuffer... buf) throws IOException + { + getEndPoint().write(context,callback,buf); + } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketBlockingConnection.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketBlockingConnection.java deleted file mode 100644 index 06c4b498ad0..00000000000 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/WebSocketBlockingConnection.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.eclipse.jetty.websocket.io; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import org.eclipse.jetty.websocket.api.WebSocketConnection; - -public class WebSocketBlockingConnection -{ - private WebSocketConnection conn; - - public WebSocketBlockingConnection(WebSocketConnection conn) - { - this.conn = conn; - } - - /** - * Send a binary message. - *

- * Basic usage, results in a blocking write. - */ - public void write(byte[] data, int offset, int length) throws IOException - { - - } - - /** - * Send a series of binary messages. - *

- * Note: each buffer results in its own binary message frame. - *

- * Basic usage, results in a series of blocking writes. - */ - public void write(ByteBuffer... buffers) throws IOException - { - - } - - /** - * Send text messages. - *

- * Basic usage, results in a series of blocking writes. - */ - public void write(String... messages) throws IOException - { - - } -} diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadBinarySignatureSocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadBinarySignatureSocket.java index b79ca87f5ee..430981b3e07 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadBinarySignatureSocket.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadBinarySignatureSocket.java @@ -1,17 +1,18 @@ package org.eclipse.jetty.websocket.annotations; -import java.nio.ByteBuffer; - import org.eclipse.jetty.websocket.api.WebSocketConnection; +/** + * Invalid Socket: Annotate a message interest on a method with a return type. + */ @WebSocket public class BadBinarySignatureSocket { /** * Declaring a non-void return type */ - @OnWebSocketBinary - public boolean onBinary(WebSocketConnection conn, ByteBuffer payload) + @OnWebSocketMessage + public boolean onBinary(WebSocketConnection conn, byte buf[], int offset, int len) { return false; } diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadDuplicateBinarySocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadDuplicateBinarySocket.java index 2f7e89aecb4..6a7cfcc0096 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadDuplicateBinarySocket.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadDuplicateBinarySocket.java @@ -1,14 +1,17 @@ package org.eclipse.jetty.websocket.annotations; -import java.nio.ByteBuffer; +import java.io.InputStream; +/** + * Invalid Socket: Annotate 2 methods with interest in Binary Messages. + */ @WebSocket public class BadDuplicateBinarySocket { /** * First method */ - @OnWebSocketBinary + @OnWebSocketMessage public void binMe(byte[] payload, int offset, int len) { /* ignore */ @@ -17,8 +20,8 @@ public class BadDuplicateBinarySocket /** * Second method */ - @OnWebSocketBinary - public void binMe(ByteBuffer payload) + @OnWebSocketMessage + public void streamMe(InputStream stream) { /* ignore */ } diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadTextSignatureSocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadTextSignatureSocket.java index 3cd1a3c5d06..bf74f42948f 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadTextSignatureSocket.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/BadTextSignatureSocket.java @@ -2,13 +2,16 @@ package org.eclipse.jetty.websocket.annotations; import org.eclipse.jetty.websocket.api.WebSocketConnection; +/** + * Invalid Socket: Annotate a message interest on a static method + */ @WebSocket public class BadTextSignatureSocket { /** * Declaring a static method */ - @OnWebSocketText + @OnWebSocketMessage public static void onText(WebSocketConnection conn, String text) { /* do nothing */ diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/MyEchoSocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/MyEchoSocket.java index c9c4d90c093..45fb4bcf98f 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/MyEchoSocket.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/MyEchoSocket.java @@ -2,8 +2,8 @@ package org.eclipse.jetty.websocket.annotations; import java.io.IOException; +import org.eclipse.jetty.websocket.api.WebSocketBlockingConnection; import org.eclipse.jetty.websocket.api.WebSocketConnection; -import org.eclipse.jetty.websocket.io.WebSocketBlockingConnection; /** * The most common websocket implementation. @@ -34,7 +34,7 @@ public class MyEchoSocket this.blocking = new WebSocketBlockingConnection(conn); } - @OnWebSocketText + @OnWebSocketMessage public void onText(String message) { if (conn == null) diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/MyStatelessEchoSocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/MyStatelessEchoSocket.java index 411334633a3..ec619c74860 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/MyStatelessEchoSocket.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/annotations/MyStatelessEchoSocket.java @@ -16,7 +16,7 @@ import org.eclipse.jetty.websocket.api.WebSocketConnection; @WebSocket public class MyStatelessEchoSocket { - @OnWebSocketText + @OnWebSocketMessage public void onText(WebSocketConnection conn, String text) { try diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/EventMethodsCacheTest.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/EventMethodsCacheTest.java index 41d168ed63f..6eea6a28442 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/EventMethodsCacheTest.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/EventMethodsCacheTest.java @@ -15,8 +15,6 @@ import org.eclipse.jetty.websocket.annotations.NotASocket; import org.eclipse.jetty.websocket.annotations.WebSocket; import org.eclipse.jetty.websocket.api.samples.AdapterConnectCloseSocket; import org.eclipse.jetty.websocket.api.samples.ListenerBasicSocket; -import org.eclipse.jetty.websocket.frames.BaseFrame; -import org.eclipse.jetty.websocket.frames.TextFrame; import org.junit.Assert; import org.junit.Test; @@ -57,7 +55,8 @@ public class EventMethodsCacheTest assertHasEventMethod(classId + ".onException",methods.onException); assertHasEventMethod(classId + ".onText",methods.onText); - Assert.assertThat(".getOnFrames()",methods.getOnFrames().size(),is(0)); + // Advanced, only available from @OnWebSocketFrame annotation + assertNoEventMethod(classId + ".onFrame",methods.onFrame); } /** @@ -158,8 +157,7 @@ public class EventMethodsCacheTest assertHasEventMethod(classId + ".onConnect",methods.onConnect); assertNoEventMethod(classId + ".onException",methods.onException); assertHasEventMethod(classId + ".onText",methods.onText); - - Assert.assertThat(".getOnFrames()",methods.getOnFrames().size(),is(0)); + assertNoEventMethod(classId + ".onFrame",methods.onFrame); } /** @@ -171,15 +169,16 @@ public class EventMethodsCacheTest EventMethodsCache cache = new EventMethodsCache(); EventMethods methods = cache.getMethods(MyEchoSocket.class); - Assert.assertThat("EventMethods for MyEchoSocket",methods,notNullValue()); + String classId = MyEchoSocket.class.getSimpleName(); - assertNoEventMethod("MyEchoSocket.onBinary",methods.onBinary); - assertHasEventMethod("MyEchoSocket.onClose",methods.onClose); - assertHasEventMethod("MyEchoSocket.onConnect",methods.onConnect); - assertNoEventMethod("MyEchoSocket.onException",methods.onException); - assertHasEventMethod("MyEchoSocket.onText",methods.onText); + Assert.assertThat("EventMethods for " + classId,methods,notNullValue()); - Assert.assertThat("MyEchoSocket.getOnFrames()",methods.getOnFrames().size(),is(0)); + assertNoEventMethod(classId + ".onBinary",methods.onBinary); + assertHasEventMethod(classId + ".onClose",methods.onClose); + assertHasEventMethod(classId + ".onConnect",methods.onConnect); + assertNoEventMethod(classId + ".onException",methods.onException); + assertHasEventMethod(classId + ".onText",methods.onText); + assertNoEventMethod(classId + ".onFrame",methods.onFrame); } /** @@ -191,15 +190,16 @@ public class EventMethodsCacheTest EventMethodsCache cache = new EventMethodsCache(); EventMethods methods = cache.getMethods(MyStatelessEchoSocket.class); - Assert.assertThat("EventMethods for MyStatelessEchoSocket",methods,notNullValue()); + String classId = MyStatelessEchoSocket.class.getSimpleName(); - assertNoEventMethod("MyStatelessEchoSocket.onBinary",methods.onBinary); - assertNoEventMethod("MyStatelessEchoSocket.onClose",methods.onClose); - assertNoEventMethod("MyStatelessEchoSocket.onConnect",methods.onConnect); - assertNoEventMethod("MyStatelessEchoSocket.onException",methods.onException); - assertHasEventMethod("MyStatelessEchoSocket.onText",methods.onText); + Assert.assertThat("EventMethods for " + classId,methods,notNullValue()); - Assert.assertThat("MyEchoSocket.getOnFrames()",methods.getOnFrames().size(),is(0)); + assertNoEventMethod(classId + ".onBinary",methods.onBinary); + assertNoEventMethod(classId + ".onClose",methods.onClose); + assertNoEventMethod(classId + ".onConnect",methods.onConnect); + assertNoEventMethod(classId + ".onException",methods.onException); + assertHasEventMethod(classId + ".onText",methods.onText); + assertNoEventMethod(classId + ".onFrame",methods.onFrame); } /** @@ -211,19 +211,20 @@ public class EventMethodsCacheTest EventMethodsCache cache = new EventMethodsCache(); EventMethods methods = cache.getMethods(NoopSocket.class); - Assert.assertThat("Methods for NoopSocket",methods,notNullValue()); + String classId = NoopSocket.class.getSimpleName(); - assertNoEventMethod("NoopSocket.onBinary",methods.onBinary); - assertNoEventMethod("NoopSocket.onClose",methods.onClose); - assertNoEventMethod("NoopSocket.onConnect",methods.onConnect); - assertNoEventMethod("NoopSocket.onException",methods.onException); - assertNoEventMethod("NoopSocket.onText",methods.onText); + Assert.assertThat("Methods for " + classId,methods,notNullValue()); - Assert.assertThat("MyEchoSocket.getOnFrames()",methods.getOnFrames().size(),is(0)); + assertNoEventMethod(classId + ".onBinary",methods.onBinary); + assertNoEventMethod(classId + ".onClose",methods.onClose); + assertNoEventMethod(classId + ".onConnect",methods.onConnect); + assertNoEventMethod(classId + ".onException",methods.onException); + assertNoEventMethod(classId + ".onText",methods.onText); + assertNoEventMethod(classId + ".onFrame",methods.onFrame); } /** - * Test Case for no exceptions and 3 methods + * Test Case for no exceptions and 1 methods */ @Test public void testAnnotatedOnFrame() @@ -231,17 +232,16 @@ public class EventMethodsCacheTest EventMethodsCache cache = new EventMethodsCache(); EventMethods methods = cache.getMethods(FrameSocket.class); - Assert.assertThat("EventMethods for MyEchoSocket",methods,notNullValue()); + String classId = FrameSocket.class.getSimpleName(); - assertNoEventMethod("MyEchoSocket.onBinary",methods.onBinary); - assertNoEventMethod("MyEchoSocket.onClose",methods.onClose); - assertNoEventMethod("MyEchoSocket.onConnect",methods.onConnect); - assertNoEventMethod("MyEchoSocket.onException",methods.onException); - assertNoEventMethod("MyEchoSocket.onText",methods.onText); + Assert.assertThat("EventMethods for " + classId,methods,notNullValue()); - Assert.assertThat("MyEchoSocket.getOnFrames()",methods.getOnFrames().size(),is(2)); - assertHasEventMethod("MyEchoSocket.onFrame(BaseFrame)",methods.getOnFrame(BaseFrame.class)); - assertHasEventMethod("MyEchoSocket.onFrame(BaseFrame)",methods.getOnFrame(TextFrame.class)); + assertNoEventMethod(classId + ".onBinary",methods.onBinary); + assertNoEventMethod(classId + ".onClose",methods.onClose); + assertNoEventMethod(classId + ".onConnect",methods.onConnect); + assertNoEventMethod(classId + ".onException",methods.onException); + assertNoEventMethod(classId + ".onText",methods.onText); + assertHasEventMethod(classId + ".onFrame",methods.onFrame); } /** @@ -281,7 +281,6 @@ public class EventMethodsCacheTest assertHasEventMethod(classId + ".onBinary",methods.onBinary); assertHasEventMethod(classId + ".onException",methods.onException); assertHasEventMethod(classId + ".onText",methods.onText); - - Assert.assertThat(".getOnFrames()",methods.getOnFrames().size(),is(0)); + assertNoEventMethod(classId + ".onFrame",methods.onFrame); } } diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/WebSocketEventDriverTest.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/WebSocketEventDriverTest.java index 72dc5421dc0..1992c31bceb 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/WebSocketEventDriverTest.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/WebSocketEventDriverTest.java @@ -2,8 +2,8 @@ package org.eclipse.jetty.websocket.api; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.websocket.api.samples.AdapterConnectCloseSocket; -import org.eclipse.jetty.websocket.api.samples.AnnotatedByteArraySocket; -import org.eclipse.jetty.websocket.api.samples.AnnotatedByteBufferSocket; +import org.eclipse.jetty.websocket.api.samples.AnnotatedBinaryArraySocket; +import org.eclipse.jetty.websocket.api.samples.AnnotatedBinaryStreamSocket; import org.eclipse.jetty.websocket.api.samples.AnnotatedFramesSocket; import org.eclipse.jetty.websocket.api.samples.ListenerBasicSocket; import org.eclipse.jetty.websocket.frames.BinaryFrame; @@ -46,7 +46,7 @@ public class WebSocketEventDriverTest @Test public void testAnnotated_ByteArray() { - AnnotatedByteArraySocket socket = new AnnotatedByteArraySocket(); + AnnotatedBinaryArraySocket socket = new AnnotatedBinaryArraySocket(); WebSocketEventDriver driver = newDriver(socket); LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); @@ -64,7 +64,7 @@ public class WebSocketEventDriverTest @Test public void testAnnotated_ByteBuffer() { - AnnotatedByteBufferSocket socket = new AnnotatedByteBufferSocket(); + AnnotatedBinaryStreamSocket socket = new AnnotatedBinaryStreamSocket(); WebSocketEventDriver driver = newDriver(socket); LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBinaryArraySocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBinaryArraySocket.java new file mode 100644 index 00000000000..b507c7d9026 --- /dev/null +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBinaryArraySocket.java @@ -0,0 +1,33 @@ +package org.eclipse.jetty.websocket.api.samples; + +import org.eclipse.jetty.websocket.annotations.OnWebSocketClose; +import org.eclipse.jetty.websocket.annotations.OnWebSocketConnect; +import org.eclipse.jetty.websocket.annotations.OnWebSocketMessage; +import org.eclipse.jetty.websocket.annotations.WebSocket; +import org.eclipse.jetty.websocket.api.EventCapture; +import org.eclipse.jetty.websocket.api.WebSocketConnection; + +@WebSocket +public class AnnotatedBinaryArraySocket +{ + public EventCapture capture = new EventCapture(); + + @OnWebSocketMessage + public void onBinary(byte payload[], int offset, int length) + { + capture.add("onBinary([%d],%d,%d)",payload.length,offset,length); + } + + @OnWebSocketClose + public void onClose(int statusCode, String reason) + { + capture.add("onClose(%d, %s)",statusCode,capture.q(reason)); + } + + @OnWebSocketConnect + public void onConnect(WebSocketConnection conn) + { + capture.add("onConnect(%s)", conn); + } + +} diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBinaryStreamSocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBinaryStreamSocket.java new file mode 100644 index 00000000000..9deae2e0e83 --- /dev/null +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBinaryStreamSocket.java @@ -0,0 +1,35 @@ +package org.eclipse.jetty.websocket.api.samples; + +import java.io.InputStream; + +import org.eclipse.jetty.websocket.annotations.OnWebSocketClose; +import org.eclipse.jetty.websocket.annotations.OnWebSocketConnect; +import org.eclipse.jetty.websocket.annotations.OnWebSocketMessage; +import org.eclipse.jetty.websocket.annotations.WebSocket; +import org.eclipse.jetty.websocket.api.EventCapture; +import org.eclipse.jetty.websocket.api.WebSocketConnection; + +@WebSocket +public class AnnotatedBinaryStreamSocket +{ + public EventCapture capture = new EventCapture(); + + @OnWebSocketMessage + public void onBinary(InputStream stream) + { + capture.add("onBinary(%s)",stream); + } + + @OnWebSocketClose + public void onClose(int statusCode, String reason) + { + capture.add("onClose(%d, %s)",statusCode,capture.q(reason)); + } + + @OnWebSocketConnect + public void onConnect(WebSocketConnection conn) + { + capture.add("onConnect(%s)", conn); + } + +} diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedByteArraySocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedTextSocket.java similarity index 87% rename from jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedByteArraySocket.java rename to jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedTextSocket.java index c112dd784e4..4de0a4185af 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedByteArraySocket.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedTextSocket.java @@ -1,18 +1,18 @@ package org.eclipse.jetty.websocket.api.samples; -import org.eclipse.jetty.websocket.annotations.OnWebSocketBinary; import org.eclipse.jetty.websocket.annotations.OnWebSocketClose; import org.eclipse.jetty.websocket.annotations.OnWebSocketConnect; +import org.eclipse.jetty.websocket.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.annotations.WebSocket; import org.eclipse.jetty.websocket.api.EventCapture; import org.eclipse.jetty.websocket.api.WebSocketConnection; @WebSocket -public class AnnotatedByteArraySocket +public class AnnotatedTextSocket { public EventCapture capture = new EventCapture(); - @OnWebSocketBinary + @OnWebSocketMessage public void onBinary(byte payload[], int offset, int length) { capture.add("onBinary([%d],%d,%d)",payload.length,offset,length); diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedByteBufferSocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedTextStreamSocket.java similarity index 74% rename from jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedByteBufferSocket.java rename to jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedTextStreamSocket.java index 1a4f0c80b74..a62a5a25182 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedByteBufferSocket.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedTextStreamSocket.java @@ -1,25 +1,19 @@ package org.eclipse.jetty.websocket.api.samples; -import java.nio.ByteBuffer; +import java.io.Reader; -import org.eclipse.jetty.websocket.annotations.OnWebSocketBinary; import org.eclipse.jetty.websocket.annotations.OnWebSocketClose; import org.eclipse.jetty.websocket.annotations.OnWebSocketConnect; +import org.eclipse.jetty.websocket.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.annotations.WebSocket; import org.eclipse.jetty.websocket.api.EventCapture; import org.eclipse.jetty.websocket.api.WebSocketConnection; @WebSocket -public class AnnotatedByteBufferSocket +public class AnnotatedTextStreamSocket { public EventCapture capture = new EventCapture(); - @OnWebSocketBinary - public void onBinary(ByteBuffer payload) - { - capture.add("onBinary(%s)",payload); - } - @OnWebSocketClose public void onClose(int statusCode, String reason) { @@ -32,4 +26,9 @@ public class AnnotatedByteBufferSocket capture.add("onConnect(%s)", conn); } + @OnWebSocketMessage + public void onText(Reader reader) + { + capture.add("onText(%s)",reader); + } }