From 4f30fe2a40e1ba345fd61218a5c507dde5e952d2 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 6 Jul 2012 08:43:45 -0700 Subject: [PATCH] Fleshing out WebSocketPing impl --- .../api/io/WebSocketBlockingConnection.java | 2 +- .../jetty/websocket/api/io/WebSocketPing.java | 52 +++++++++++++++++-- .../echo/EchoBroadcastPingSocket.java | 2 +- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketBlockingConnection.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketBlockingConnection.java index ec43c6071a6..66dc8a433a9 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketBlockingConnection.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketBlockingConnection.java @@ -33,7 +33,7 @@ public class WebSocketBlockingConnection } else { - throw new IllegalArgumentException("Unsupported implementation of WebSocketConnection"); + throw new IllegalArgumentException("WebSocketConnection must implement internal RawConnection interface"); } this.bufferPool = this.conn.getBufferPool(); this.policy = conn.getPolicy(); diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketPing.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketPing.java index b85ffaecad0..96af3b1ed96 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketPing.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketPing.java @@ -1,19 +1,61 @@ 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.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 { - private WebSocketConnection conn; + private RawConnection conn; + private ByteBufferPool bufferPool; + private WebSocketPolicy policy; + private Generator generator; 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 - // TODO: should this block and wait for a pong? (how?) + PingFrame frame = new PingFrame(data); + 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 sending? + } + 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-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastPingSocket.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastPingSocket.java index 231f518935f..bf0afcd18f0 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastPingSocket.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastPingSocket.java @@ -30,7 +30,7 @@ public class EchoBroadcastPingSocket extends EchoBroadcastSocket System.err.println("Ping " + pinger); byte data[] = new byte[] { (byte)1, (byte)2, (byte)3 }; - pinger.sendPing(data,0,3); + pinger.sendPing(data); } } catch (Exception e)