Revision: 7851
Author: [email protected]
Date: Thu Apr  1 16:53:00 2010
Log: Add support for breaking up large messages, isolated test for ShmFutexTransport.

http://code.google.com/p/google-web-toolkit/source/detail?r=7851

Added:
/changes/jat/csproto/dev/core/test/com/google/gwt/dev/shell/ShmFutexTransportTest.java
Modified:
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransport.java

=======================================
--- /dev/null
+++ /changes/jat/csproto/dev/core/test/com/google/gwt/dev/shell/ShmFutexTransportTest.java Thu Apr 1 16:53:00 2010
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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 com.google.gwt.dev.shell;
+
+import com.google.gwt.dev.shell.TransportFactory.TransportArgs;
+
+import junit.framework.TestCase;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * Tests {...@link ShmFutexTransport} stand-alone.
+ */
+public class ShmFutexTransportTest extends TestCase {
+
+  private static final int[] lengths = new int[] {
+    0,
+    1,
+    2,
+    4095,
+    4096,
+    4097,
+    8191,
+    8192,
+    8193,
+  };
+
+  public void testTransport() throws InterruptedException, IOException {
+    ShmFutexTransportFactory factory = new ShmFutexTransportFactory();
+    if (!factory.isAvailable()) {
+ System.out.println("Skipping ShmFutexTransport tests on this platform");
+      return;
+    }
+    TransportArgs args = new TransportArgs();
+    final Transport server = factory.createTransport(args);
+    assertNotNull(server);
+    Transport client = factory.connectTransport(args.transportArgs);
+    assertNotNull(client);
+    Thread serverThread = new Thread() {
+      @Override
+      public void run() {
+        try {
+          runServer(server);
+        } catch (IOException e) {
+          throw new RuntimeException(e);
+        }
+      }
+    };
+    serverThread.start();
+    Thread.sleep(500);
+    runClient(client);
+    client.closeChannel();
+    server.closeChannel();
+  }
+
+ private void readMessage(Transport transport, int len) throws IOException {
+    transport.beginReadMessage();
+    InputStream istr = transport.getInputStream();
+    while (len-- > 0) {
+      int val = istr.read();
+      assertFalse(val == -1);
+      assertEquals((byte) len, (byte) val);
+    }
+    transport.endReadMessage();
+  }
+
+  private void runClient(Transport client) throws IOException {
+    for (int length : lengths) {
+      writeMessage(client, length);
+      readMessage(client, length + 1);
+    }
+  }
+
+  private void runServer(Transport server) throws IOException {
+    for (int length : lengths) {
+      readMessage(server, length);
+      writeMessage(server, length + 1);
+    }
+  }
+
+ private void writeMessage(Transport transport, int len) throws IOException {
+    transport.beginWriteMessage();
+    OutputStream ostr = transport.getOutputStream();
+    while (len-- > 0) {
+      ostr.write((byte) len);
+    }
+    transport.endWriteMessage();
+  }
+}
=======================================
--- /changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransport.java Thu Apr 1 11:03:27 2010 +++ /changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransport.java Thu Apr 1 16:53:00 2010
@@ -72,6 +72,16 @@
     @Override
     public int read() {
       try {
+        if (serverToClientBuf.position() >= serverToClientBuf.limit()) {
+          // ran out of buffer, mark free and wait for full again
+          nativeEndClientRead(shmFutexAddr);
+          if (!nativeBeginClientRead(shmFutexAddr)) {
+            // no need to loop, other side shouldn't be doing anything but
+ // waiting for us to empty the buffer if the message is too large
+            return -1;
+          }
+          serverToClientBuf.position(0);
+        }
         return serverToClientBuf.get();
       } catch (BufferUnderflowException e) {
         return -1;
@@ -117,6 +127,16 @@
     @Override
     public void write(int b) throws IOException {
       try {
+        if (clientToServerBuf.position() >= clientToServerBuf.limit()) {
+          // ran out of buffer, mark free and wait for full again
+          nativeEndClientWrite(shmFutexAddr);
+          if (!nativeBeginClientWrite(shmFutexAddr)) {
+            // no need to loop, other side shouldn't be doing anything but
+ // waiting for us to refill the buffer if the message is too large
+            throw new IOException();
+          }
+          clientToServerBuf.position(0);
+        }
         clientToServerBuf.put((byte) b);
       } catch (BufferOverflowException e) {
         throw new IOException(e);
@@ -159,6 +179,16 @@
     @Override
     public int read() {
       try {
+        if (clientToServerBuf.position() >= clientToServerBuf.limit()) {
+          // ran out of buffer, mark free and wait for full again
+          nativeEndServerRead(shmFutexAddr);
+          if (!nativeBeginServerRead(shmFutexAddr)) {
+            // no need to loop, other side shouldn't be doing anything but
+ // waiting for us to empty the buffer if the message is too large
+            return -1;
+          }
+          clientToServerBuf.position(0);
+        }
         return clientToServerBuf.get();
       } catch (BufferUnderflowException e) {
         return -1;
@@ -204,6 +234,16 @@
     @Override
     public void write(int b) throws IOException {
       try {
+        if (serverToClientBuf.position() >= serverToClientBuf.limit()) {
+          // ran out of buffer, mark free and wait for full again
+          nativeEndClientWrite(shmFutexAddr);
+          if (!nativeBeginClientWrite(shmFutexAddr)) {
+            // no need to loop, other side shouldn't be doing anything but
+ // waiting for us to refill the buffer if the message is too large
+            throw new IOException();
+          }
+          serverToClientBuf.position(0);
+        }
         serverToClientBuf.put((byte) b);
       } catch (BufferOverflowException e) {
         throw new IOException(e);

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

To unsubscribe, reply using "remove me" as the subject.

Reply via email to