Question

In GWT in order to run the application in hosted mode "dev mode" you append get.codesvr parameter to the url as you can see below.

/?gwt.codesvr=127.0.0.1:9997

First question is I want to know how does GWT know when to start a JVM instance to serve .class files instead of compiled JavaScript files? I can't seem to find how GWT works in dev mode. I do find main( ) in com.google.gwt.devDevMode. How does this main( ) be called?

Second question is in the documentation it says that GWT devmode run with Jetty server, however I see actual JavaScript in browser. How does this jetty server outputs JavaScript from .class files of the client side code?

Thanks.

Was it helpful?

Solution

All the magic done through Sockets and the Browser Plugin.

Design: Out of Process Hosted Mode (OOPHM)

Here is the Essential part.

Consider the following GWT code:

public class MyEntryPoint implements EntryPoint {
    private static native int jsniMethod() /*-{
      return 1;
    }-*/;

    public void onModuleLoad() {
      jsniMethod();
    }
  }

JavaScript: the browser plugin sends a LoadModuleMessage with the module name.

Java: the hosted mode server receives the LoadModuleMessage, loads the module and invokes the onModuleLoad in the corresponding EntryPoints. In this case MyEntryPoint::onModuleLoad is called. When MyEntryPoint is compiled, a LoadJsniMessage is sent to create browser-side JavaScript functions for each JSNI method, then when onModuleLoad invokes jsniMethod an InvokeMessage is sent.

JavaScript: This is the key part of the example. The JavaScript engine is currently awaiting a return from the LoadModuleMessage it sent, but it must be in a position to invoke the call to MyEntryPoint::jsniMethod on the same thread. This is accomplished by having the thread enter a read-and-dispatch routine following every remote invocation. In this case, the thread receives the LoadJsniMessage and InvokeMessage messages, invokes jsniMethod and sends a ReturnMessage containing the value 1.

Java: The read-and-dispatch routine receives the ReturnMessage and knows to return from the call to jsniMethod. Having fully executed the onModuleLoad method it sends a ReturnMessage and falls back into a top level read-and-dispatch loop. (Since all calls originate from the browser's UI event dispatch, only the hosted mode server needs to remain in a read-and-dispatch routine during idle time. The browser simply returns control by exiting the JavaScript function that was originally called.)

OTHER TIPS

Adding the gwt.codesvr to the querystring is obviously not enough.

First, you launch the DevMode (com.google.gwt.dev.DevMode class), which launches an embedded Jetty server (unless you disabled it through the -noserver argument) and (more importantly) listens for your browser(s) (defaults to only the localhost network interface, on port 9997).

Then you open your app in your browser with the gwt.codesvr pointing to the address and port the DevMode listens on. When the *.nocache.js file loads, it detects the gwt.codesvr and loads devmode.html instead of your *.cache.html. That code will load the GWT Dev Plugin that you installed in your browser (or tell you to install it) and direct it to connect to the DevMode at the address and port specified in the gwt.codesvr query-string argument.

That way, your browser talks to the DevMode which loads your Java code and compiles and runs it in the JVM. How that dialog works is described in the GWT wiki as already pointed out by Suresh.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top