Fleshing out WebSocketPing impl

This commit is contained in:
Joakim Erdfelt 2012-07-06 08:43:45 -07:00
parent 3b2aa4c45e
commit 4f30fe2a40
3 changed files with 49 additions and 7 deletions

View File

@ -33,7 +33,7 @@ public class WebSocketBlockingConnection
} }
else else
{ {
throw new IllegalArgumentException("Unsupported implementation of WebSocketConnection"); throw new IllegalArgumentException("WebSocketConnection must implement internal RawConnection interface");
} }
this.bufferPool = this.conn.getBufferPool(); this.bufferPool = this.conn.getBufferPool();
this.policy = conn.getPolicy(); this.policy = conn.getPolicy();

View File

@ -1,19 +1,61 @@
package org.eclipse.jetty.websocket.api.io; package org.eclipse.jetty.websocket.api.io;
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.api.WebSocketConnection; import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.PingFrame;
import org.eclipse.jetty.websocket.generator.Generator;
import org.eclipse.jetty.websocket.io.RawConnection;
public class WebSocketPing public class WebSocketPing
{ {
private WebSocketConnection conn; private RawConnection conn;
private ByteBufferPool bufferPool;
private WebSocketPolicy policy;
private Generator generator;
public WebSocketPing(WebSocketConnection conn) public WebSocketPing(WebSocketConnection conn)
{ {
this.conn = conn; if (conn instanceof RawConnection)
{
this.conn = (RawConnection)conn;
}
else
{
throw new IllegalArgumentException("WebSocketConnection must implement internal RawConnection interface");
}
this.bufferPool = this.conn.getBufferPool();
this.policy = conn.getPolicy();
this.generator = new Generator(this.policy);
} }
public void sendPing(byte buf[], int offset, int len) public void sendPing(byte data[]) throws IOException
{ {
// TODO: implement PingFrame frame = new PingFrame(data);
// TODO: should this block and wait for a pong? (how?) ByteBuffer buf = bufferPool.acquire(policy.getBufferSize(),false);
try
{
generator.generate(buf,frame);
FutureCallback<Void> blocking = new FutureCallback<>();
this.conn.writeRaw(null,blocking,buf);
blocking.get(); // block till finished sending?
}
catch (InterruptedException e)
{
throw new IOException("Blocking write failed",e);
}
catch (ExecutionException e)
{
FutureCallback.rethrow(e);
}
finally
{
bufferPool.release(buf);
}
} }
} }

View File

@ -30,7 +30,7 @@ public class EchoBroadcastPingSocket extends EchoBroadcastSocket
System.err.println("Ping " + pinger); System.err.println("Ping " + pinger);
byte data[] = new byte[] byte data[] = new byte[]
{ (byte)1, (byte)2, (byte)3 }; { (byte)1, (byte)2, (byte)3 };
pinger.sendPing(data,0,3); pinger.sendPing(data);
} }
} }
catch (Exception e) catch (Exception e)