You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/01/21 10:46:19 UTC
svn commit: r1436239 - in /tomcat/trunk: java/org/apache/tomcat/websocket/
java/org/apache/tomcat/websocket/server/ test/org/apache/tomcat/websocket/
Author: markt
Date: Mon Jan 21 09:46:18 2013
New Revision: 1436239
URL: http://svn.apache.org/viewvc?rev=1436239&view=rev
Log:
Remove remaining reference to Servlet classes to server web socket package
Added:
tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java
- copied, changed from r1436225, tomcat/trunk/java/org/apache/tomcat/websocket/WsFrame.java
tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java (with props)
Removed:
tomcat/trunk/java/org/apache/tomcat/websocket/WsFrame.java
Modified:
tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java
tomcat/trunk/test/org/apache/tomcat/websocket/TestWsFrame.java
Copied: tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java (from r1436225, tomcat/trunk/java/org/apache/tomcat/websocket/WsFrame.java)
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java?p2=tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java&p1=tomcat/trunk/java/org/apache/tomcat/websocket/WsFrame.java&r1=1436225&r2=1436239&rev=1436239&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsFrame.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java Mon Jan 21 09:46:18 2013
@@ -24,7 +24,6 @@ import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
-import javax.servlet.ServletInputStream;
import javax.websocket.CloseReason;
import javax.websocket.CloseReason.CloseCodes;
import javax.websocket.MessageHandler;
@@ -37,13 +36,13 @@ import org.apache.tomcat.util.res.String
* extracts the messages. WebSocket Pings received will be responded to
* automatically without any action required by the application.
*/
-public class WsFrame {
+public abstract class WsFrameBase {
private static final StringManager sm =
StringManager.getManager(Constants.PACKAGE_NAME);
// Connection level attributes
- private final ServletInputStream sis;
+ private final Object connectionReadLock = new Object();
private final WsSession wsSession;
private final byte[] inputBuffer;
@@ -79,8 +78,7 @@ public class WsFrame {
private int readPos = 0;
private int writePos = 0;
- public WsFrame(ServletInputStream sis, WsSession wsSession) {
- this.sis = sis;
+ public WsFrameBase(WsSession wsSession) {
this.wsSession = wsSession;
// TODO This needs to work for client and server side code
@@ -101,11 +99,10 @@ public class WsFrame {
* Called when there is data in the ServletInputStream to process.
*/
public void onDataAvailable() throws IOException {
- synchronized (sis) {
- while (sis.isReady()) {
+ synchronized (connectionReadLock) {
+ while (isDataAvailable()) {
// Fill up the input buffer with as much data as we can
- int read = sis.read(inputBuffer, writePos,
- inputBuffer.length - writePos);
+ int read = fillInputBuffer(inputBuffer, writePos);
if (read == 0) {
return;
}
@@ -136,6 +133,27 @@ public class WsFrame {
/**
+ * Allows sub-classes to control whether the read loop in
+ * {@link #onDataAvailable()} should continue or terminate.
+ *
+ * @return <code>true</code> if the data source is ready to be read
+ */
+ protected abstract boolean isDataAvailable();
+
+
+ /**
+ * Fill as much of the input buffer as possible (i.e. to the end of the
+ * supplied buffer).
+ *
+ * @param inputBuffer The input buffer
+ * @param start The start point
+ * @return The number of bytes (possibly zero) added to the buffer
+ */
+ protected abstract int fillInputBuffer(byte[] inputBuffer, int start)
+ throws IOException;
+
+
+ /**
* @return <code>true</code> if sufficient data was present to process all
* of the initial header
*/
Added: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java?rev=1436239&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java Mon Jan 21 09:46:18 2013
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.websocket.server;
+
+import java.io.IOException;
+
+import javax.servlet.ServletInputStream;
+
+import org.apache.tomcat.websocket.WsFrameBase;
+import org.apache.tomcat.websocket.WsSession;
+
+public class WsFrameServer extends WsFrameBase {
+
+ private final ServletInputStream sis;
+
+ public WsFrameServer(ServletInputStream sis, WsSession wsSession) {
+ super(wsSession);
+ this.sis = sis;
+ }
+
+ @Override
+ protected boolean isDataAvailable() {
+ return sis.isReady();
+ }
+
+ @Override
+ protected int fillInputBuffer(byte[] inputBuffer, int start)
+ throws IOException {
+ return sis.read(inputBuffer, start,
+ inputBuffer.length - start);
+ }
+}
Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java?rev=1436239&r1=1436238&r2=1436239&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java Mon Jan 21 09:46:18 2013
@@ -30,7 +30,7 @@ import javax.websocket.CloseReason.Close
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfiguration;
-import org.apache.tomcat.websocket.WsFrame;
+import org.apache.tomcat.websocket.WsFrameBase;
import org.apache.tomcat.websocket.WsIOException;
import org.apache.tomcat.websocket.WsSession;
@@ -71,7 +71,7 @@ public class WsProtocolHandler implement
ClassLoader cl = t.getContextClassLoader();
t.setContextClassLoader(applicationClassLoader);
try {
- WsFrame wsFrame = new WsFrame(sis, wsSession);
+ WsFrameBase wsFrame = new WsFrameServer(sis, wsSession);
sis.setReadListener(new WsReadListener(this, wsFrame, wsSession));
WsRemoteEndpointServer wsRemoteEndpointServer =
new WsRemoteEndpointServer(wsSession, sos);
@@ -100,12 +100,12 @@ public class WsProtocolHandler implement
private static class WsReadListener implements ReadListener {
private final WsProtocolHandler wsProtocolHandler;
- private final WsFrame wsFrame;
+ private final WsFrameBase wsFrame;
private final WsSession wsSession;
private WsReadListener(WsProtocolHandler wsProtocolHandler,
- WsFrame wsFrame, WsSession wsSession) {
+ WsFrameBase wsFrame, WsSession wsSession) {
this.wsProtocolHandler = wsProtocolHandler;
this.wsFrame = wsFrame;
this.wsSession = wsSession;
Modified: tomcat/trunk/test/org/apache/tomcat/websocket/TestWsFrame.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/TestWsFrame.java?rev=1436239&r1=1436238&r2=1436239&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/websocket/TestWsFrame.java (original)
+++ tomcat/trunk/test/org/apache/tomcat/websocket/TestWsFrame.java Mon Jan 21 09:46:18 2013
@@ -25,25 +25,25 @@ public class TestWsFrame {
@Test
public void testByteArrayToLong() throws IOException {
- Assert.assertEquals(0L, WsFrame.byteArrayToLong(new byte[] { 0 }, 0, 1));
- Assert.assertEquals(1L, WsFrame.byteArrayToLong(new byte[] { 1 }, 0, 1));
- Assert.assertEquals(0xFF, WsFrame.byteArrayToLong(new byte[] { -1 }, 0, 1));
+ Assert.assertEquals(0L, WsFrameBase.byteArrayToLong(new byte[] { 0 }, 0, 1));
+ Assert.assertEquals(1L, WsFrameBase.byteArrayToLong(new byte[] { 1 }, 0, 1));
+ Assert.assertEquals(0xFF, WsFrameBase.byteArrayToLong(new byte[] { -1 }, 0, 1));
Assert.assertEquals(0xFFFF,
- WsFrame.byteArrayToLong(new byte[] { -1, -1 }, 0, 2));
+ WsFrameBase.byteArrayToLong(new byte[] { -1, -1 }, 0, 2));
Assert.assertEquals(0xFFFFFF,
- WsFrame.byteArrayToLong(new byte[] { -1, -1, -1 }, 0, 3));
+ WsFrameBase.byteArrayToLong(new byte[] { -1, -1, -1 }, 0, 3));
}
@Test
public void testByteArrayToLongOffset() throws IOException {
- Assert.assertEquals(0L, WsFrame.byteArrayToLong(new byte[] { 20, 0 }, 1, 1));
- Assert.assertEquals(1L, WsFrame.byteArrayToLong(new byte[] { 20, 1 }, 1, 1));
- Assert.assertEquals(0xFF, WsFrame.byteArrayToLong(new byte[] { 20, -1 }, 1, 1));
+ Assert.assertEquals(0L, WsFrameBase.byteArrayToLong(new byte[] { 20, 0 }, 1, 1));
+ Assert.assertEquals(1L, WsFrameBase.byteArrayToLong(new byte[] { 20, 1 }, 1, 1));
+ Assert.assertEquals(0xFF, WsFrameBase.byteArrayToLong(new byte[] { 20, -1 }, 1, 1));
Assert.assertEquals(0xFFFF,
- WsFrame.byteArrayToLong(new byte[] { 20, -1, -1 }, 1, 2));
+ WsFrameBase.byteArrayToLong(new byte[] { 20, -1, -1 }, 1, 2));
Assert.assertEquals(0xFFFFFF,
- WsFrame.byteArrayToLong(new byte[] { 20, -1, -1, -1 }, 1, 3));
+ WsFrameBase.byteArrayToLong(new byte[] { 20, -1, -1, -1 }, 1, 3));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org