javagwtgwt-hosted-modegwt-dev-mode

How does codeserver parameter in GWT work?


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.


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.)