Start of refactor from dev community discussion
This commit is contained in:
parent
3df516779b
commit
1529baea92
|
@ -0,0 +1,28 @@
|
|||
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.
|
||||
* <p>
|
||||
* Acceptable method patterns.<br>
|
||||
* Note: <code>methodName</code> can be any name you want to use.
|
||||
* <ol>
|
||||
* <li><code>public void methodName(String text)</code></li>
|
||||
* <li><code>public void methodName({@link WebSocketConnection} conn, String text)</code></li>
|
||||
* </ol>
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(value =
|
||||
{ ElementType.METHOD })
|
||||
public @interface OnWebSocketMessage
|
||||
{
|
||||
/* no config */
|
||||
}
|
|
@ -5,7 +5,6 @@ import java.net.InetSocketAddress;
|
|||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jetty.util.Callback;
|
||||
import org.eclipse.jetty.websocket.frames.BaseFrame;
|
||||
|
||||
/**
|
||||
* Connection interface for WebSocket protocol <a href="https://tools.ietf.org/html/rfc6455">RFC-6455</a>.
|
||||
|
@ -67,37 +66,6 @@ public interface WebSocketConnection
|
|||
*/
|
||||
boolean isOpen();
|
||||
|
||||
/**
|
||||
* Send a frame.
|
||||
* <p>
|
||||
* Basic usage, results in a series of non-blocking async writes.
|
||||
*/
|
||||
void write(BaseFrame frame) throws IOException;
|
||||
|
||||
/**
|
||||
* Send a binary message.
|
||||
* <p>
|
||||
* Basic usage, results in an non-blocking async write.
|
||||
*/
|
||||
void write(byte[] data, int offset, int length) throws IOException;
|
||||
|
||||
/**
|
||||
* Send a series of binary messages.
|
||||
* <p>
|
||||
* Note: each buffer results in its own binary message frame.
|
||||
* <p>
|
||||
* Basic usage, results in a series of non-blocking async writes.
|
||||
*/
|
||||
void write(ByteBuffer... buffers) throws IOException;
|
||||
|
||||
/**
|
||||
* Send a series of frames.
|
||||
* <p>
|
||||
* Advanced usage, with callbacks, allows for concurrent NIO style results of the entire write operation. (Callback is only called once at the end of
|
||||
* processing all of the frames)
|
||||
*/
|
||||
<C> void write(C context, Callback<C> callback, BaseFrame ... frames) throws IOException;
|
||||
|
||||
/**
|
||||
* Send a series of binary messages.
|
||||
* <p>
|
||||
|
@ -117,11 +85,4 @@ public interface WebSocketConnection
|
|||
* processing all of the messages)
|
||||
*/
|
||||
<C> void write(C context, Callback<C> callback, String... messages) throws IOException;
|
||||
|
||||
/**
|
||||
* Send a text message.
|
||||
* <p>
|
||||
* Basic usage, results in an non-blocking async write.
|
||||
*/
|
||||
void write(String message) throws IOException;
|
||||
}
|
|
@ -14,17 +14,13 @@ import org.eclipse.jetty.io.ByteBufferPool;
|
|||
import org.eclipse.jetty.io.RuntimeIOException;
|
||||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.util.Callback;
|
||||
import org.eclipse.jetty.util.FutureCallback;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.websocket.api.CloseException;
|
||||
import org.eclipse.jetty.websocket.api.ExtensionConfig;
|
||||
import org.eclipse.jetty.websocket.api.ProtocolException;
|
||||
import org.eclipse.jetty.websocket.api.StatusCode;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketConnection;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||
import org.eclipse.jetty.websocket.callbacks.WebSocketCloseCallback;
|
||||
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.TextFrame;
|
||||
|
@ -188,7 +184,7 @@ public class WebSocketAsyncConnection extends AbstractAsyncConnection implements
|
|||
terminateConnection(StatusCode.PROTOCOL,null);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
parser.parse(buffer);
|
||||
}
|
||||
}
|
||||
|
@ -233,104 +229,6 @@ public class WebSocketAsyncConnection extends AbstractAsyncConnection implements
|
|||
return String.format("%s{g=%s,p=%s}",super.toString(),generator,parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void write(BaseFrame frame) throws IOException
|
||||
{
|
||||
if (frame == null)
|
||||
{
|
||||
// nothing to write
|
||||
return;
|
||||
}
|
||||
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
LOG.debug("write(BaseFrame->{})",frame);
|
||||
}
|
||||
|
||||
ByteBuffer raw = bufferPool.acquire(frame.getPayloadLength() + FrameGenerator.OVERHEAD,false);
|
||||
BufferUtil.clearToFill(raw);
|
||||
generator.generate(raw,frame);
|
||||
BufferUtil.flipToFlush(raw,0);
|
||||
Callback<Void> nop = new FutureCallback<>(); // TODO: add buffer release callback?
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
LOG.debug("Raw Buffer: {}",BufferUtil.toDetailString(raw));
|
||||
}
|
||||
getEndPoint().write(null,nop,raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void write(byte[] data, int offset, int length) throws IOException
|
||||
{
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
LOG.debug("write(byte[]->{})",data);
|
||||
}
|
||||
write(new BinaryFrame(data,offset,length));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void write(ByteBuffer... buffers) throws IOException
|
||||
{
|
||||
int len = buffers.length;
|
||||
if (len == 0)
|
||||
{
|
||||
// nothing to write
|
||||
return;
|
||||
}
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
LOG.debug("write(ByteBuffers->{})",buffers.length);
|
||||
}
|
||||
ByteBuffer raw[] = new ByteBuffer[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
raw[i] = bufferPool.acquire(buffers[i].remaining() + FrameGenerator.OVERHEAD,false);
|
||||
BufferUtil.clearToFill(raw[i]);
|
||||
BinaryFrame frame = new BinaryFrame(buffers[i]);
|
||||
generator.generate(raw[i],frame);
|
||||
BufferUtil.flipToFlush(raw[i],0);
|
||||
}
|
||||
Callback<Void> nop = new FutureCallback<>(); // TODO: add buffer release callback?
|
||||
getEndPoint().write(null,nop,raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <C> void write(C context, Callback<C> callback, BaseFrame... frames) throws IOException
|
||||
{
|
||||
int len = frames.length;
|
||||
if (len == 0)
|
||||
{
|
||||
// nothing to write
|
||||
return;
|
||||
}
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
LOG.debug("write(context,{},BaseFrames->{})",callback,frames.length);
|
||||
}
|
||||
ByteBuffer raw[] = new ByteBuffer[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
raw[i] = bufferPool.acquire(frames[i].getPayloadLength() + FrameGenerator.OVERHEAD,false);
|
||||
BufferUtil.clearToFill(raw[i]);
|
||||
generator.generate(raw[i],frames[i]);
|
||||
BufferUtil.flipToFlush(raw[i],0);
|
||||
}
|
||||
getEndPoint().write(context,callback,raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -380,25 +278,6 @@ public class WebSocketAsyncConnection extends AbstractAsyncConnection implements
|
|||
{
|
||||
frames[i] = new TextFrame(messages[i]);
|
||||
}
|
||||
write(context,callback,frames);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void write(String message) throws IOException
|
||||
{
|
||||
if (message == null)
|
||||
{
|
||||
// nothing to write
|
||||
return;
|
||||
}
|
||||
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
LOG.debug("write(String->{})",message);
|
||||
}
|
||||
write(new TextFrame(message));
|
||||
// TODO write(context,callback,frames);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
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.
|
||||
* <p>
|
||||
* Basic usage, results in a blocking write.
|
||||
*/
|
||||
public void write(byte[] data, int offset, int length) throws IOException
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a series of binary messages.
|
||||
* <p>
|
||||
* Note: each buffer results in its own binary message frame.
|
||||
* <p>
|
||||
* Basic usage, results in a series of blocking writes.
|
||||
*/
|
||||
public void write(ByteBuffer... buffers) throws IOException
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Send text messages.
|
||||
* <p>
|
||||
* Basic usage, results in a series of blocking writes.
|
||||
*/
|
||||
public void write(String... messages) throws IOException
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.eclipse.jetty.websocket.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class WebSocketOutputStream extends OutputStream
|
||||
{
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package org.eclipse.jetty.websocket.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
public class WebSocketWriter extends Writer
|
||||
{
|
||||
|
||||
@Override
|
||||
public void write(char[] cbuf, int off, int len) throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package org.eclipse.jetty.websocket.annotations;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Test of constructing a new WebSocket based on a base class
|
||||
|
@ -9,12 +8,12 @@ import java.nio.ByteBuffer;
|
|||
@WebSocket
|
||||
public class MyEchoBinarySocket extends MyEchoSocket
|
||||
{
|
||||
@OnWebSocketBinary
|
||||
public void echoBin(ByteBuffer payload)
|
||||
@OnWebSocketMessage
|
||||
public void echoBin(byte buf[], int offset, int length)
|
||||
{
|
||||
try
|
||||
{
|
||||
getConnection().write(payload);
|
||||
getConnection().write(buf,offset,length);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.eclipse.jetty.websocket.annotations;
|
|||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.WebSocketConnection;
|
||||
import org.eclipse.jetty.websocket.io.WebSocketBlockingConnection;
|
||||
|
||||
/**
|
||||
* The most common websocket implementation.
|
||||
|
@ -13,10 +14,11 @@ import org.eclipse.jetty.websocket.api.WebSocketConnection;
|
|||
public class MyEchoSocket
|
||||
{
|
||||
private WebSocketConnection conn;
|
||||
private WebSocketBlockingConnection blocking;
|
||||
|
||||
public WebSocketConnection getConnection()
|
||||
public WebSocketBlockingConnection getConnection()
|
||||
{
|
||||
return conn;
|
||||
return blocking;
|
||||
}
|
||||
|
||||
@OnWebSocketClose
|
||||
|
@ -29,6 +31,7 @@ public class MyEchoSocket
|
|||
public void onConnect(WebSocketConnection conn)
|
||||
{
|
||||
this.conn = conn;
|
||||
this.blocking = new WebSocketBlockingConnection(conn);
|
||||
}
|
||||
|
||||
@OnWebSocketText
|
||||
|
@ -43,7 +46,7 @@ public class MyEchoSocket
|
|||
|
||||
try
|
||||
{
|
||||
conn.write(message);
|
||||
blocking.write(message);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.eclipse.jetty.websocket.annotations;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.jetty.util.FutureCallback;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketConnection;
|
||||
|
||||
/**
|
||||
|
@ -20,7 +21,7 @@ public class MyStatelessEchoSocket
|
|||
{
|
||||
try
|
||||
{
|
||||
conn.write(text);
|
||||
conn.write(null,new FutureCallback<Void>(),text);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
|
|
|
@ -5,7 +5,6 @@ import java.net.InetSocketAddress;
|
|||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jetty.util.Callback;
|
||||
import org.eclipse.jetty.websocket.frames.BaseFrame;
|
||||
import org.junit.rules.TestName;
|
||||
|
||||
public class LocalWebSocketConnection implements WebSocketConnection
|
||||
|
@ -67,26 +66,6 @@ public class LocalWebSocketConnection implements WebSocketConnection
|
|||
return String.format("LocalWebSocketConnection[%s]",id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(BaseFrame frame) throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] data, int offset, int length) throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ByteBuffer... buffers) throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public <C> void write(C context, Callback<C> callback, BaseFrame... frames) throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public <C> void write(C context, Callback<C> callback, ByteBuffer... buffers) throws IOException
|
||||
{
|
||||
|
@ -96,9 +75,4 @@ public class LocalWebSocketConnection implements WebSocketConnection
|
|||
public <C> void write(C context, Callback<C> callback, String... messages) throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(String message) throws IOException
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package org.eclipse.jetty.websocket.api.samples;
|
||||
|
||||
import java.awt.Frame;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
|
||||
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.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 AnnotatedStreamingSocket
|
||||
{
|
||||
public EventCapture capture = new EventCapture();
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@OnWebSocketFrame
|
||||
public void onFrame(Frame frame)
|
||||
{
|
||||
}
|
||||
|
||||
// Binary
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(byte buf[], int offset, int length)
|
||||
{
|
||||
}
|
||||
|
||||
// Binary
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(InputStream stream)
|
||||
{
|
||||
}
|
||||
|
||||
// Text
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(Reader stream)
|
||||
{
|
||||
}
|
||||
|
||||
// Text
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(String message)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue