Revision: 7841
Author: [email protected]
Date: Thu Apr 1 09:19:11 2010
Log: Checkstyle fixes, add Shm+Futex transport factory and hooks into
BrowserChannelServer.
http://code.google.com/p/google-web-toolkit/source/detail?r=7841
Added:
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransportFactory.java
/changes/jat/csproto/dev/core/src/com/google/gwt/util/tools/NativeLoader.java
Modified:
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/BrowserChannelServer.java
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransport.java
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransportTestServer.java
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/Transport.java
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/TransportFactory.java
=======================================
--- /dev/null
+++
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransportFactory.java
Thu Apr 1 09:19:11 2010
@@ -0,0 +1,40 @@
+/*
+ * 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.util.tools.NativeLoader;
+
+/**
+ * A factory for creating ShmFutexTransport instances.
+ */
+public class ShmFutexTransportFactory implements TransportFactory {
+
+ public synchronized Transport connectTransport(String args) {
+ return ShmFutexTransport.connectToChannel(args);
+ }
+
+ public synchronized Transport createTransport(TransportArgs args) {
+ return ShmFutexTransport.createNewChannel(args);
+ }
+
+ public String getName() {
+ return "shmfutex";
+ }
+
+ public boolean isAvailable() {
+ return NativeLoader.getInstance().loadPlatformLibrary("ShmFutex");
+ }
+}
=======================================
--- /dev/null
+++
/changes/jat/csproto/dev/core/src/com/google/gwt/util/tools/NativeLoader.java
Thu Apr 1 09:19:11 2010
@@ -0,0 +1,107 @@
+/*
+ * 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.util.tools;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A utility for loading native libraries.
+ */
+public class NativeLoader {
+
+ private static NativeLoader INSTANCE = new NativeLoader();
+
+ public static NativeLoader getInstance() {
+ return INSTANCE;
+ }
+
+ private Map<String, String> loaded = new HashMap<String, String>();
+
+ /**
+ * Canonicalized OS name.
+ */
+ private String os;
+
+ /**
+ * Canonicalized architecture.
+ */
+ private String arch;
+
+ protected NativeLoader() {
+ os = System.getProperty("os.name");
+ arch = System.getProperty("os.arch");
+ if (arch.matches("i?\\d86")) {
+ arch = "x86";
+ } else if ("amd64".equals(arch)) {
+ arch = "x86_64";
+ }
+ // TODO: more canonicalization?
+ }
+
+ /**
+ * Load a native library which is provided on all platforms.
+ *
+ * @param libraryName the base name of the library, not including any
+ * platform-specific prefixes or suffixes
+ * @return true if the library was loaded
+ */
+ public boolean loadLibrary(String libraryName) {
+ return loadLibraryFile(libraryName + "_" + arch);
+ }
+
+ /**
+ * Load a native library which is provided on all platforms.
+ *
+ * @param libraryName the base name of the library, not including any
+ * platform-specific prefixes or suffixes
+ * @return true if the library was loaded
+ */
+ public boolean loadPlatformLibrary(String libraryName) {
+ return loadLibraryFile(libraryName + "_" + os + "_" + arch);
+ }
+
+ /**
+ * Load a native library after any platform/architecture suffixes are
+ * applied.
+ *
+ * @param libraryName the base name of the library, not including any
+ * platform-specific prefixes or suffixes
+ * @return true if the library was loaded
+ */
+ protected synchronized boolean loadLibraryFile(String libraryName) {
+ if (loaded.containsKey(libraryName)) {
+ return loaded.get(libraryName) != null;
+ }
+ // add platform-specific prefix/suffix
+ String mappedLibraryName = System.mapLibraryName(libraryName);
+ File lib = new File(Utility.getInstallPath(), mappedLibraryName);
+ String canonicalPath = null;
+ if (lib.exists()) {
+ try {
+ String path = lib.getCanonicalPath();
+ System.load(path);
+ canonicalPath = path;
+ } catch (IOException e) {
+ } catch (UnsatisfiedLinkError e) {
+ }
+ }
+ loaded.put(libraryName, canonicalPath);
+ return canonicalPath != null;
+ }
+}
=======================================
---
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/BrowserChannelServer.java
Wed Mar 31 20:30:31 2010
+++
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/BrowserChannelServer.java
Thu Apr 1 09:19:11 2010
@@ -108,6 +108,18 @@
private static final Object cacheLock = new Object();
+ /**
+ * List of TransportFactory classes which can potentially provide a
transport
+ * for use on this platform.
+ */
+ private static Class<?>[] transports = new Class[] {
+ ShmFutexTransportFactory.class,
+ };
+
+ private static Map<String, TransportFactory> transportFactories = null;
+
+ private static final Object transportLock = new Object();
+
private final SessionHandlerServer handler;
private final boolean ignoreRemoteDeath;
@@ -675,6 +687,29 @@
thread.setName("Code server (initializing)");
thread.start();
}
+
+ /**
+ * Create transport factories for those which are available on this
platform.
+ *
+ * <p>MUST ONLY BE CALLED WHILE HOLDING {...@link #transportLock}.
+ */
+ private void populateTransportFactories() {
+ transportFactories = new HashMap<String, TransportFactory>();
+ for (Class<?> clazz : transports) {
+ Class<? extends TransportFactory> factoryClass = clazz.asSubclass(
+ TransportFactory.class);
+ try {
+ TransportFactory factory = factoryClass.newInstance();
+ if (factory.isAvailable()) {
+ transportFactories.put(factory.getName(), factory);
+ }
+ } catch (InstantiationException e) {
+ // Ignore classes we can't instantiate
+ } catch (IllegalAccessException e) {
+ // Ignore classes we can't access
+ }
+ }
+ }
/**
* Select a transport from those provided by the client.
@@ -683,7 +718,17 @@
* @return null to continue in-band, or a transport type
*/
private TransportFactory selectTransport(String[] transports) {
- // TODO(jat): add support for shared memory, others
- return null;
+ synchronized (transportLock) {
+ if (transportFactories == null) {
+ populateTransportFactories();
+ }
+ for (String transport : transports) {
+ TransportFactory factory = transportFactories.get(transport);
+ if (factory != null) {
+ return factory;
+ }
+ }
+ return null;
+ }
}
}
=======================================
---
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransport.java
Wed Mar 31 20:30:31 2010
+++
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransport.java
Thu Apr 1 09:19:11 2010
@@ -15,6 +15,8 @@
*/
package com.google.gwt.dev.shell;
+import com.google.gwt.dev.shell.TransportFactory.TransportArgs;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -136,17 +138,15 @@
* {...@link #connectToChannel} call
* @return a ShmFutexTransport instance, or null on an error
*/
- public static ShmFutexTransport createNewChannel(String[] outputKey) {
- if (outputKey.length != 1) {
- throw new IllegalArgumentException(
- "Expected 1-element array for returned argument value");
- }
- long shmFutexAddr = nativeCreate(outputKey);
+ public static ShmFutexTransport createNewChannel(TransportArgs
outputKey) {
+ String[] keyArray = new String[1];
+ long shmFutexAddr = nativeCreate(keyArray);
if (shmFutexAddr == 0) {
return null;
}
- ShmFutexTransport shm = new ShmFutexTransport(shmFutexAddr,
outputKey[0]);
+ ShmFutexTransport shm = new ShmFutexTransport(shmFutexAddr,
keyArray[0]);
shm.ownsShm = true;
+ outputKey.transportArgs = keyArray[0];
return shm;
}
=======================================
---
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransportTestServer.java
Wed Mar 31 20:30:31 2010
+++
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/ShmFutexTransportTestServer.java
Thu Apr 1 09:19:11 2010
@@ -15,10 +15,15 @@
*/
package com.google.gwt.dev.shell;
+import com.google.gwt.dev.shell.TransportFactory.TransportArgs;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+/**
+ * Server-side test of Java wrapper for Shm+Futex transport.
+ */
public class ShmFutexTransportTestServer {
private static final int NUM_PACKETS = 800000;
@@ -40,18 +45,33 @@
String platformLibrary = System.mapLibraryName(library);
System.out.println("Mapped name = " + platformLibrary);
System.load("/tmp/" + platformLibrary);
- String[] outputKey = new String[1];
+ TransportArgs outputKey = new TransportArgs();
ShmFutexTransport shm = ShmFutexTransport.createNewChannel(outputKey);
if (shm == null) {
System.err.println("Unable to create shared memory segment");
}
- System.out.println("Connect to key " + outputKey[0]);
+ System.out.println("Connect to key " + outputKey.transportArgs);
try {
runServer(shm);
} finally {
shm.closeChannel();
}
}
+
+ private static void readPacket(InputStream istr, int len) {
+ while (len-- > 0) {
+ try {
+ int b = istr.read();
+ if ((byte) b != (byte) len) {
+ throw new IllegalStateException("Expected " + (byte) len + ",
got "
+ + (byte) b);
+ }
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
private static void runServer(Transport shm) {
InputStream istr = shm.getInputStream();
@@ -76,19 +96,4 @@
}
}
}
-
- private static void readPacket(InputStream istr, int len) {
- while (len-- > 0) {
- try {
- int b = istr.read();
- if ((byte) b != (byte) len) {
- throw new IllegalStateException("Expected " + (byte) len + ",
got "
- + (byte) b);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-}
+}
=======================================
---
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/Transport.java
Wed Mar 31 20:30:31 2010
+++
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/Transport.java
Thu Apr 1 09:19:11 2010
@@ -23,23 +23,6 @@
*/
public interface Transport {
- /**
- * @return the InputStream for this transport (only valid after
createChannel
- * has been called).
- */
- InputStream getInputStream();
-
- /**
- * @return the OutputStream for this transport (only valid after
createChannel
- * has been called).
- */
- OutputStream getOutputStream();
-
- /**
- * Close this transport channel. Must be called to free up held
resources.
- */
- void closeChannel();
-
/**
* Begin reading a message. Must be called before reading any bytes from
the
* input stream for a message.
@@ -56,6 +39,11 @@
*/
boolean beginWriteMessage();
+ /**
+ * Close this transport channel. Must be called to free up held
resources.
+ */
+ void closeChannel();
+
/**
* Finishes reading a message. Must be called after the last byte of a
* message is read and before the next call to {...@link
#beginReadMessage()}.
@@ -68,4 +56,16 @@
* {...@link #beginWriteMessage()}.
*/
void endWriteMessage();
-}
+
+ /**
+ * @return the InputStream for this transport (only valid after
createChannel
+ * has been called).
+ */
+ InputStream getInputStream();
+
+ /**
+ * @return the OutputStream for this transport (only valid after
createChannel
+ * has been called).
+ */
+ OutputStream getOutputStream();
+}
=======================================
---
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/TransportFactory.java
Wed Mar 31 20:30:31 2010
+++
/changes/jat/csproto/dev/core/src/com/google/gwt/dev/shell/TransportFactory.java
Thu Apr 1 09:19:11 2010
@@ -15,9 +15,19 @@
*/
package com.google.gwt.dev.shell;
+/**
+ * Factory for creating Transport instances.
+ */
public interface TransportFactory {
+ /**
+ * Holds the secondary return value from creating a transport.
+ */
public class TransportArgs {
+
+ /**
+ * The arguments necessary to connect to the created transport.
+ */
public String transportArgs;
}
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors