no frame byte

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1225 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-02-01 02:59:43 +00:00
parent 8733b438db
commit 9a02a4e57a
4 changed files with 9 additions and 207 deletions

View File

@ -13,6 +13,7 @@ public interface WebSocket
public interface Outbound
{
void sendMessage(String data) throws IOException;
void sendMessage(byte frame,String data) throws IOException;
void sendMessage(byte frame,byte[] data) throws IOException;
void sendMessage(byte frame,byte[] data, int offset, int length) throws IOException;

View File

@ -142,6 +142,13 @@ public class WebSocketConnection implements Connection, WebSocket.Outbound
return _timestamp;
}
public void sendMessage(String content) throws IOException
{
_generator.addFrame(WebSocket.SENTINEL_FRAME,content,_maxIdleTimeMs);
_generator.flush();
_idle.access(_endp);
}
public void sendMessage(byte frame, String content) throws IOException
{
_generator.addFrame(frame,content,_maxIdleTimeMs);

View File

@ -114,7 +114,7 @@ public class WebSocketTest extends TestCase
_outbound=outbound;
try
{
_outbound.sendMessage(SENTINEL_FRAME,"Roger That");
_outbound.sendMessage("Roger That");
}
catch (IOException e)
{

View File

@ -1,206 +0,0 @@
package org.eclipse.jetty.websocket;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ConnectedEndPoint;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
import org.eclipse.jetty.io.nio.SelectChannelEndPoint;
import org.eclipse.jetty.io.nio.SelectorManager;
import org.eclipse.jetty.io.nio.SelectorManager.SelectSet;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;
public class WebSocketTestClient extends AbstractLifeCycle
{
final ThreadPool _threadpool = new QueuedThreadPool();
final WebSocketBuffers _buffers = new WebSocketBuffers(4*4096);
SelectorManager _manager = new SelectorManager()
{
@Override
protected SocketChannel acceptChannel(SelectionKey key) throws IOException
{
throw new IllegalStateException();
}
@Override
public boolean dispatch(Runnable task)
{
return _threadpool.dispatch(task);
}
@Override
protected void endPointClosed(SelectChannelEndPoint endpoint)
{
}
@Override
protected void endPointOpened(SelectChannelEndPoint endpoint)
{
}
@Override
protected void endPointUpgraded(ConnectedEndPoint endpoint, Connection oldConnection)
{
}
@Override
protected Connection newConnection(SocketChannel channel, SelectChannelEndPoint endpoint)
{
WebSocket ws=(WebSocket)endpoint.getSelectionKey().attachment();
WebSocketConnection connection = new WebSocketConnection(ws,endpoint,_buffers,System.currentTimeMillis(), 30000);
// TODO Blocking upgrade code. Should be async
ByteArrayBuffer upgrade=new ByteArrayBuffer(
"GET / HTTP/1.1\r\n"+
"Host: localhost:8080\r\n" +
"Upgrade: WebSocket\r\n" +
"Connection: Upgrade\r\n" +
"\r\n");
try
{
while (upgrade.length()>0 && endpoint.isOpen())
{
int l = endpoint.flush(upgrade);
if (l>0)
upgrade.skip(l);
Thread.sleep(10);
}
IndirectNIOBuffer upgraded = new IndirectNIOBuffer(2048);
String up;
do
{
endpoint.fill(upgraded);
up=upgraded.toString();
}
while(endpoint.isOpen() && !up.contains("\r\n\r\n"));
}
catch(Exception e)
{
Log.warn(e);
}
ws.onConnect(connection);
return connection;
}
@Override
protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key) throws IOException
{
SelectChannelEndPoint ep=new SelectChannelEndPoint(channel,selectSet,key);
return ep;
}
};
protected void doStart()
throws Exception
{
((LifeCycle)_threadpool).start();
_manager.start();
_threadpool.dispatch(new Runnable()
{
public void run()
{
while (isRunning())
{
try
{
_manager.doSelect(0);
}
catch (Exception e)
{
Log.warn(e.toString());
Log.debug(e);
Thread.yield();
}
}
}
});
}
public void open(SocketAddress addr, WebSocket websocket)
throws IOException
{
SocketChannel channel = SocketChannel.open();
channel.configureBlocking( false );
channel.connect(addr);
_manager.register( channel, websocket);
}
public static void main(String[] args)
throws Exception
{
WebSocketTestClient client = new WebSocketTestClient();
client.start();
TestWebSocket[] ws=new TestWebSocket[100];
for (int i=0;i<ws.length;i++)
ws[i]=new TestWebSocket(i);
for (TestWebSocket w : ws)
client.open(new InetSocketAddress("localhost",8080),w);
Thread.sleep(5000);
for (TestWebSocket w : ws)
if (w._out!=null)
w._out.sendMessage(WebSocket.SENTINEL_FRAME,"hello world from "+w._id);
client._threadpool.join();
}
static class TestWebSocket implements WebSocket
{
int _id;
Outbound _out;
TestWebSocket(int i)
{
_id=i;
}
public void onConnect(Outbound outbound)
{
_out=outbound;
System.err.println("onConnect");
}
public void onDisconnect()
{
System.err.println("onDisconnect");
}
public void onMessage(byte frame, String data)
{
System.err.println("onMessage "+data);
}
public void onMessage(byte frame, byte[] data, int offset, int length)
{
}
}
}